-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.go
44 lines (34 loc) · 860 Bytes
/
api.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 stdapi
import (
"fmt"
"net/http"
"github.com/convox/logger"
"github.com/gorilla/mux"
)
func New(ns, hostname string) *Server {
logger := logger.New(fmt.Sprintf("ns=%s", ns))
server := &Server{
Hostname: hostname,
Logger: logger,
}
server.Router = &Router{
Parent: nil,
Router: mux.NewRouter(),
Server: server,
}
server.Router.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
id, _ := generateId(12)
logger.Logf("id=%s route=unknown code=404 method=%q path=%q", id, r.Method, r.URL.Path)
})
server.Router.HandleFunc("/check", server.check)
return server
}
func (s *Server) check(w http.ResponseWriter, r *http.Request) {
if s.Check != nil {
if err := s.Check(NewContext(w, r)); err != nil {
http.Error(w, err.Error(), 500)
}
}
fmt.Fprintf(w, "ok")
}