-
Notifications
You must be signed in to change notification settings - Fork 8
/
bandit-server.go
64 lines (51 loc) · 1.55 KB
/
bandit-server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"flag"
"fmt"
"github.com/peleteiro/bandit-server/assets"
"github.com/peleteiro/bandit-server/handlers"
"github.com/peleteiro/bandit-server/repository"
"github.com/peleteiro/bandit-server/strategies"
"log"
"math/rand"
"net/http"
"os"
"time"
)
var port int = 3000
var memcached string = "no"
var logger = log.New(os.Stdout, "bandit-server: ", log.Ldate|log.Ltime)
func init() {
flag.IntVar(&port, "port", 3000, "http port")
flag.StringVar(&memcached, "use-memcached", "no", "Memcached host:port. Usualy you don't need memcache (read README.md).")
}
func main() {
flag.Parse()
rand.Seed(time.Now().UnixNano())
var repo repository.Repository
if memcached == "no" {
repo = repository.NewMemory()
} else {
repo = repository.NewMemcached(memcached)
}
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
var favicon, _ = assets.Asset("favicon.ico")
w.Header().Set("Content-Type", "image/x-icon")
w.Write(favicon)
})
http.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
var robots, _ = assets.Asset("robots.txt")
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Write(robots)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintf(w, "ok")
})
http.HandleFunc("/ucb1", handlers.NewHttpHandler(strategies.NewUCB1(), repo))
logger.Print("Listening on port ", port)
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
if err != nil {
logger.Fatal(err)
}
}