-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.go
executable file
·53 lines (43 loc) · 1.49 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
package main
import (
"context"
"flag"
"github.com/elcct/taillachat/api"
"github.com/elcct/taillachat/system"
"github.com/golang/glog"
"github.com/gorilla/mux"
"gopkg.in/igm/sockjs-go.v2/sockjs"
"net/http"
)
// use is a middleware chainer
func use(h http.Handler, middleware ...func(http.Handler) http.Handler) http.Handler {
for _, m := range middleware {
h = m(h)
}
return h
}
func main() {
flag.Parse()
defer glog.Flush()
err := system.Init()
if err != nil {
glog.Fatal(err)
}
api.Template = system.CurrentApplication.Template
api.MediaContent = system.CurrentApplication.Configuration.UploadPath
router := mux.NewRouter()
// Setup static files
router.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/", http.FileServer(http.Dir(system.CurrentApplication.Configuration.PublicPath))))
router.Path("/chat/{any:.*}").Handler(sockjs.NewHandler("/chat", sockjs.DefaultOptions, api.Chat))
router.Handle("/", use(http.HandlerFunc(api.Index), templates))
router.Handle("/terms", use(http.HandlerFunc(api.Terms), templates))
router.Handle("/privacy", use(http.HandlerFunc(api.Privacy), templates))
glog.Error(http.ListenAndServe(system.CurrentApplication.Configuration.Bind, router))
}
// Templates adds templates to the context
func templates(inner http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), "template", system.CurrentApplication.Template)
inner.ServeHTTP(w, r.WithContext(ctx))
})
}