-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
60 lines (48 loc) · 1.62 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
package main
import (
"log"
"net/http"
"os"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
func main() {
router := ConfigureRouter()
log.Fatal(http.ListenAndServe(":3001", handlers.LoggingHandler(os.Stdout, router)))
}
//ConfigureRouter setup the router
func ConfigureRouter() *mux.Router {
router := mux.NewRouter()
router.PathPrefix("/static").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
router.HandleFunc("/", homeHandler)
router.HandleFunc("/metacortex", metacortexHandler)
router.HandleFunc("/agents/{name}", agentsHandler)
router.HandleFunc("/authenticate", authenticate)
router.Handle("/api/megacity", authMiddleware(megacityHandler))
router.Handle("/api/levrai", authMiddleware(levraiHandler))
return router
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Welcome to the Matrix!"))
}
func metacortexHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Mr Anderson's not so secure workplace!"))
}
func agentsHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
w.WriteHeader(http.StatusOK)
w.Write([]byte("My name is agent " + vars["name"]))
}
var megacityHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Welcome to the Megacity!"))
})
var levraiHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
name := r.Header.Get("name")
if name != "neo" {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Only Neo can enter the Merovingian's restaurant!"))
return
}
w.Write([]byte("Welcome to the LeVrai!"))
})