-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaddevshandler.go
72 lines (62 loc) · 1.42 KB
/
baddevshandler.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
package main
import (
"github.com/gorilla/mux"
"net/http"
)
//API related types
type APIFunc func(w http.ResponseWriter, r *http.Request)
type APIEndPoint struct {
path string
apiFunc APIFunc
}
var apiCalls []APIEndPoint
var apiMap map[string]APIFunc
//Domain related types
type Domain struct {
name string
ip string
}
func init() {
//intialize api calls, add your api calls here
apiCalls = []APIEndPoint{
APIEndPoint{
path: "/domains",
apiFunc: badDevsAPI,
},
APIEndPoint{
path: "/domains/{id}",
apiFunc: badDevsAPI,
},
APIEndPoint{
path: "/domains/{id}/delete",
apiFunc: badDevsAPI,
},
APIEndPoint{
path: "/domains/add",
apiFunc: badDevsAPI,
},
}
//setup api calls map for fast lookup
apiMap = make(map[string]APIFunc)
for _, call := range apiCalls {
apiMap[call.path] = call.apiFunc
}
}
func badDevsIndex(w http.ResponseWriter, req *http.Request) {
//fmt.Printf("%+v\n", req)
http.ServeFile(w, req, "client/dist/index.html")
}
func badDevsAPI(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
http.ServeFile(w, req, "index.html")
}
func badDevsHandler(r *mux.Router, s string) {
//setup reachable urls
badDevsInfo("Setting up routes for %v\n", s)
r = r.Host("baddevs.io").Subrouter()
r.HandleFunc("/", badDevsIndex)
//setup api functions
for _, call := range apiCalls {
r.HandleFunc(call.path, badDevsAPI)
}
}