forked from thomasdesr/go-shorten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (51 loc) · 1.46 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
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/thomaso-mirodin/go-shorten/handlers"
)
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()
// Serve the index
indexPage, err := handlers.NewIndex("static/templates/index.tmpl")
if err != nil {
log.Fatal("Failed to create index Page", err)
}
r.Handler("GET", "/healthcheck", handlers.Healthcheck(store, "/healthcheck"))
// Serve the "API"
r.HandleMethodNotAllowed = false
r.NotFound = handlers.GetShort(store, indexPage)
r.Handler("POST", "/", handlers.SetShort(store))
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)
}
}