/
app.go
44 lines (37 loc) · 875 Bytes
/
app.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
package main
import (
"encoding/json"
"github.com/gorilla/mux"
"io"
"log"
"net/http"
)
type Data struct {
ID int
Name string
GallonPerCalorie float64
}
func main() {
mux := mux.NewRouter()
mux.HandleFunc("/", homeHandler)
mux.HandleFunc("/data/{food}", dataHandler)
mux.HandleFunc("/static/{folder}/{file}", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, r.URL.Path[1:])
})
port := ":3000"
log.Println("Listening on " + port)
http.ListenAndServe(port, mux)
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/main.html")
}
func dataHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
data := Data{
ID: 1,
Name: vars["food"],
GallonPerCalorie: 1.34,
}
b, _ := json.Marshal(data)
io.WriteString(w, string(b))
}