-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
76 lines (61 loc) · 1.89 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"log"
"net"
"net/http"
"github.com/codegangsta/negroni"
"github.com/jessevdk/go-flags"
"github.com/julienschmidt/httprouter"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/thomasdesr/go-shorten/handlers"
"github.com/thomasdesr/go-shorten/storage"
)
var opts Options
func main() {
if _, err := flags.Parse(&opts); err != nil {
return
}
store, err := createStorageFromOption(&opts)
if err != nil {
log.Fatal(err)
}
log.Println("Storage successfully created")
n := negroni.New(
negroni.NewRecovery(),
negroni.NewLogger(),
negroni.NewStatic(http.Dir("static")),
)
r := httprouter.New()
r.Handler("GET", "/healthcheck", handlers.Healthcheck(store, "/healthcheck"))
// Serve the index
indexPage, err := handlers.NewIndex("static/templates/index.tmpl")
if err != nil {
log.Fatal("Failed to create index Page", err)
}
// If we don't have any matches, serve the respective go link
r.HandleMethodNotAllowed = false
r.NotFound = handlers.GetShort(store, indexPage)
// Go Endpoints
r.Handler("GET", "/go", handlers.ServeGoDashboard())
// API handlers
r.Handler("POST", "/", handlers.SetShort(store)) // TODO(@thomas): move this to a stable API endpoint
if ss, ok := store.(storage.SearchableStorage); ok {
r.Handler("GET", "/_api/v1/search", handlers.Search(ss))
}
if tns, ok := store.(storage.TopN); ok {
r.Handler("GET", "/_api/v1/top_n", handlers.TopN(tns))
}
n.UseHandler(r)
go func() {
log.Printf("Starting prometheus HTTP Listner on %s", net.JoinHostPort(opts.BindHost, "8081"))
err := http.ListenAndServe(net.JoinHostPort(opts.BindHost, "8081"), promhttp.Handler())
if err != nil {
log.Println(err)
}
}()
log.Printf("Starting HTTP Listener on: %s", net.JoinHostPort(opts.BindHost, opts.BindPort))
err = http.ListenAndServe(net.JoinHostPort(opts.BindHost, opts.BindPort), n)
if err != nil {
log.Fatal(err)
}
}