server/api.go (raw)
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 |
package server import ( "hacklab.nilfm.cc/quartzgun/renderer" "hacklab.nilfm.cc/quartzgun/router" "html/template" "net/http" ) func apiConfigureAdapters(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { w.WriteHeader(201) next.ServeHTTP(w, req) }) } func apiGetAdapters(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { w.WriteHeader(201) next.ServeHTTP(w, req) }) } func apiAdapterSubscribe(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { w.WriteHeader(201) next.ServeHTTP(w, req) }) } func ProtectWithSubscriberKey(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { w.WriteHeader(201) next.ServeHTTP(w, req) }) } func apiMux() http.Handler { errTemplate, err := template.New("err").Parse("{{ $params := (.Context).Value \"params\" }}<html><body><h1>ERROR {{ $params.ErrorCode }}</h1><p class='error'>{{ $params.ErrorMessage }}</p></body></html>") if err != nil { panic("error template was malformed") } rtr := &router.Router{ Fallback: *errTemplate, } // adapters (POST & GET) rtr.Post("/adapters", ProtectWithSubscriberKey(apiConfigureAdapters(renderer.JSON("data")))) rtr.Get("/adapters", ProtectWithSubscriberKey(apiGetAdapters(renderer.JSON("data")))) // adapters/:name/subscribe rtr.Post(`/adapters/(?P<id>\S+)/subscribe`, ProtectWithSubscriberKey(apiAdapterSubscribe(renderer.JSON("data")))) return http.HandlerFunc(rtr.ServeHTTP) } |