-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (74 loc) · 2.22 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/go-chi/chi"
)
func main() {
conf := apiConfig{fileserverHits: 0}
// create new mux (chi router)
r := chi.NewRouter()
// second router mux for api
r_api := chi.NewRouter()
// admin namespace router
r_admin := chi.NewRouter()
// mount api and admin router mux behind main router r
r.Mount("/api/", r_api)
r.Mount("/admin/", r_admin)
// add middleware to mux
corsRouter := middlewareCors(r)
// create fileserve http handler at root /
fs := http.FileServer(http.Dir("./"))
// Middle ware wrapping done extensive. Can be made much more compact.
// StripPrefix is a built in middleware. It takes the handler, does something, returns the handler.
fs_wrapped_one := http.StripPrefix("/app", fs)
// takes the handler, does something, returns the handler
fs_wrapped_two := conf.middlewareMetricsInc(fs_wrapped_one)
// You can either use Handle or HandleFunc. The result is basically the same.
// It all depends if you got a http.handler or a func that matches the
// implementation of a handlefunc
//register handler with different mux routers
r.Handle("/app", fs_wrapped_two)
r.Handle("/app/*", fs_wrapped_two)
r.HandleFunc("/reset", conf.resetMetrics)
r_api.Get("/healthz", healthz)
r_api.Get("/chirps", API_GetChirps)
r_api.Post("/chirps", API_PostChirp)
r_api.Get("/chirps/{id}", API_Get_Single_Chirp)
r_api.Post("/users", API_Create_User)
r_api.Post("/login", API_User_Login)
r_admin.Get("/metrics", conf.displayMetrics)
serv := &http.Server{
Handler: corsRouter,
Addr: "0.0.0.0:8080",
}
log.Print("Starting server on :8080")
err := serv.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
dat, err := json.Marshal(payload)
if err != nil {
log.Printf("Error marshalling JSON: %s", err)
w.WriteHeader(500)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(dat)
}
func respondWithError(w http.ResponseWriter, code int, msg string) {
if code >= 500 {
fmt.Printf("Server side error: %d", code)
}
type err_resp struct {
Err_msg string `json:"error"`
}
respondWithJSON(w, code, err_resp{
Err_msg: msg,
})
}