-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
86 lines (68 loc) · 1.9 KB
/
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
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"github.com/gorilla/mux"
"gopkg.in/redis.v5"
)
type App struct {
Router *mux.Router
RedisClient *redis.Client
}
func (a *App) Initialize(port string, password string, dbStr string) {
if len(port) == 0 {
port = "6379"
}
if len(dbStr) == 0 {
dbStr = "0"
}
db, err := strconv.Atoi(dbStr)
if err != nil {
log.Fatal(err)
}
a.RedisClient = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("localhost:%s", port),
Password: password,
DB: db,
})
a.Router = mux.NewRouter()
a.initializeRoutes()
}
func (a *App) Run(addr string) {
log.Printf("Listening at http://localhost%s\n", addr)
log.Fatal(http.ListenAndServe(addr, a.Router))
}
func (a *App) initializeRoutes() {
a.Router.HandleFunc("/calendar/{building:[a-zA-Z]+}/{room:[0-9a-zA-Z]+}", a.getRoom).Methods("GET")
a.Router.HandleFunc("/calendar/{building:[a-zA-Z]+}", a.getBuilding).Methods("GET")
}
func (a *App) getBuilding(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
building, err := getBuilding(a.RedisClient, strings.ToUpper(vars["building"]))
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJSON(w, http.StatusOK, building)
}
func (a *App) getRoom(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
room, err := getRoom(a.RedisClient, strings.ToUpper(vars["building"]), vars["room"])
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJSON(w, http.StatusOK, room)
}
func respondWithError(w http.ResponseWriter, code int, message string) {
respondWithJSON(w, code, map[string]string{"error": message})
}
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
json.NewEncoder(w).Encode(payload)
}