-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (42 loc) · 1.63 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
package main
import (
"awesomeProject/domain"
"awesomeProject/rest"
"awesomeProject/utils"
"fmt"
"log"
"net/http"
)
func main() {
templates := domain.NewTemplates()
page := domain.NewPageData()
mux := http.NewServeMux()
mux.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("./css"))))
mux.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("./js"))))
mux.HandleFunc("GET /router/stats", rest.RouterStatsHandler(templates, page))
mux.HandleFunc("POST /dpi/switch", rest.DpiSwitchHandler(templates, page))
mux.HandleFunc("POST /domains", rest.AddDomainHandler(templates, page))
mux.HandleFunc("PUT /domains", rest.UpdateDomainHandler(templates, page))
mux.HandleFunc("DELETE /domains", rest.DeleteDomainHandler(templates, page))
mux.HandleFunc("/", indexHandler(templates, page))
log.Println("Server started on :8082")
if err := http.ListenAndServe(":8082", logRequest(mux)); err != nil {
log.Fatalf("Server failed to start: %v", err)
}
}
func logRequest(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
})
}
func indexHandler(templates *domain.Templates, page *domain.PageData) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
page.DpiProp.Status = utils.GetDpiProtectionStatus()
page.Router.Stats = utils.GetRouterStats(false)
page.Domains = utils.GetDomains()
if err := templates.Render(w, "index", page); err != nil {
http.Error(w, fmt.Sprintf("error rendering template: %v", err), http.StatusInternalServerError)
}
}
}