-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.go
More file actions
45 lines (35 loc) · 959 Bytes
/
server.go
File metadata and controls
45 lines (35 loc) · 959 Bytes
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
package main
import (
"net/http"
"os"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
"github.com/unrolled/render"
)
var webRoot string
// NewServer configures and returns a Server.
func NewServer() *negroni.Negroni {
formatter := render.New(render.Options{
IndentJSON: true,
})
n := negroni.Classic()
mx := mux.NewRouter()
initRoutes(mx, formatter)
n.UseHandler(mx)
return n
}
func initRoutes(mx *mux.Router, formatter *render.Render) {
webRoot = os.Getenv("WEBROOT")
if len(webRoot) == 0 {
root, err := os.Getwd()
if err != nil {
panic("Could not retrieve working directory")
} else {
webRoot = root
}
}
mx.HandleFunc("/api/test", testHandler(formatter)).Methods("GET")
mx.HandleFunc("/cookies/write", cookieWriteHandler(formatter)).Methods("GET")
mx.HandleFunc("/cookies/read", cookieReadHandler(formatter)).Methods("GET")
mx.PathPrefix("/").Handler(http.FileServer(http.Dir(webRoot + "/assets/")))
}