/
main.go
69 lines (57 loc) · 1.66 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
package main
import (
"log"
"net/http"
"os"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
"github.com/unrolled/render"
)
var (
commandsFilename = "commands.json"
authFilename = "auth.json"
logger = log.New(os.Stdout, "[ducking-ninja] ", 0)
)
func main() {
commands := readCommands(commandsFilename)
r := render.New(render.Options{
IndentJSON: true,
Layout: "layout",
Extensions: []string{".html"},
})
router := mux.NewRouter()
router.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, "/web/", http.StatusMovedPermanently)
})
router.HandleFunc("/web/", WebIndex(r))
router.HandleFunc("/web/logs", WebShowLogs(r))
router.HandleFunc("/api/logs", ApiShowLogs(r)).Methods("GET")
loggedRouter := mux.NewRouter()
loggedRouter.HandleFunc("/web/commands", WebListCommands(r, commands))
loggedRouter.HandleFunc("/web/do/{command}", WebExecuteCommand(r, commands))
loggedRouter.HandleFunc("/api/commands", ApiListCommands(r, commands)).Methods("GET")
loggedRouter.HandleFunc("/api/do/{command}", ApiExecuteCommand(r, commands)).Methods("GET")
// run logging middleware only for loggedRouter
router.PathPrefix("/").Handler(negroni.New(
logging(),
negroni.Wrap(loggedRouter),
))
n := negroni.New(
negroni.NewRecovery(),
&negroni.Logger{logger},
&negroni.Static{
Dir: http.Dir("assets"),
Prefix: "/web",
IndexFile: "index.html",
},
)
n.Use(authenticate(r, readAuth(authFilename)))
n.UseHandler(router)
port := os.Getenv("PORT")
if port == "" {
port = "3333"
}
addr := ":" + port
logger.Printf("listening on %s", addr)
logger.Fatal(http.ListenAndServe(addr, n))
}