forked from GitbookIO/micro-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
62 lines (49 loc) · 1.36 KB
/
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
package main
import (
"net/http"
"os"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/oschwald/maxminddb-golang"
"github.com/GitbookIO/micro-analytics/database"
"github.com/GitbookIO/micro-analytics/web"
)
type ServerOpts struct {
Port string
Version string
DriverOpts database.DriverOpts
Geolite2Reader *maxminddb.Reader
Auth *web.BasicAuth
}
// Build a http.Server based on the options
func NewServer(opts ServerOpts) (*http.Server, error) {
// Create base router
r := mux.NewRouter()
// Hello world
r.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"message":"Welcome to micro-analytics!", "version":"` + opts.Version + `"}`))
})
// Define private routes handler
routerOpts := web.RouterOpts{
DriverOpts: opts.DriverOpts,
Geolite2Reader: opts.Geolite2Reader,
Version: opts.Version,
}
handler, err := web.NewRouter(routerOpts)
if err != nil {
return nil, err
}
// Use authentication if username provided
if len(opts.Auth.Name) > 0 {
handler = web.BasicAuthMiddleware(opts.Auth, handler)
}
// Attach to main router
r.PathPrefix("/").Handler(handler)
// Use logging
handler = handlers.LoggingHandler(os.Stderr, r)
return &http.Server{
Addr: opts.Port,
Handler: handler,
}, nil
}