-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (61 loc) · 1.81 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
package main
import (
"encoding/json"
"flag"
"html/template"
"log"
"net/http"
"github.com/gorilla/mux"
)
type jResponse struct {
Page int64 `json:"page,omitempty"`
Data *Data `json:"data,omitempty"`
}
// Data ...
type Data struct {
First int64 `json:"first,omitempty"`
Second int64 `json:"second,omitempty"`
Third int64 `json:"third,omitempty"`
}
var tpl *template.Template
var responseStruct []jResponse
func init() {
tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
}
func index(w http.ResponseWriter, req *http.Request) {
tpl.ExecuteTemplate(w, "index.gohtml", nil)
log.Println("INDEX...")
}
func about(w http.ResponseWriter, req *http.Request) {
tpl.ExecuteTemplate(w, "about.gohtml", nil)
log.Println("ABOUT...")
}
func chart(w http.ResponseWriter, req *http.Request) {
tpl.ExecuteTemplate(w, "chart.gohtml", nil)
log.Println("CHART...")
}
func updateChartData(w http.ResponseWriter, req *http.Request) {
data := append(responseStruct, jResponse{Page: 2, Data: &Data{First: 23, Second: 82, Third: 22}})
jData, err := json.Marshal(data)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(jData)
log.Println("API CALL...")
}
func main() {
port := flag.String("p", "8080", "port")
dir := flag.String("d", "./templates/", "dir")
flag.Parse()
router := mux.NewRouter()
// Serving static files, JS, CSS, Images
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
router.HandleFunc("/", index)
router.HandleFunc("/about", about)
router.HandleFunc("/chart", chart)
apiRoutes := router.PathPrefix("/api").Subrouter()
apiRoutes.Path("/update-chart-data").Methods("GET").HandlerFunc(updateChartData)
log.Printf("Serving %s on port %s\n", *dir, *port)
log.Fatal(http.ListenAndServe(":"+*port, router))
}