-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
47 lines (39 loc) · 1.29 KB
/
handler.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
// a custom type that we can use for handling errors and formatting responses
type handler func(w http.ResponseWriter, r *http.Request) (interface{}, *errorHandler)
// attach the standard ServeHTTP method to our handler so the http library can call it
func (fn handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// call the actual handler
response, err := fn(w, r)
// check for errors
if err != nil {
log.Printf("ERROR: %v\n", err.Error)
http.Error(w, fmt.Sprintf(`{"error":"%s"}`, err.Message), err.Code)
return
}
if response == nil {
log.Printf("ERROR: response from method is nil\n")
http.Error(w, "Internal server error. Check the logs.", http.StatusInternalServerError)
return
}
// turn the response into JSON
bytes, e := json.Marshal(response)
if e != nil {
http.Error(w, "Error marshalling JSON", http.StatusInternalServerError)
return
}
// send the response and log
w.Header().Set("Content-Type", "application/json")
// Allow cors
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Method", "GET,PUT,POST,DELETE")
w.Header().Set("Access-Control-Allow-Headers", "X-Requested-With")
w.Write(bytes)
log.Printf("%s %s %s %d", r.RemoteAddr, r.Method, r.URL, 200)
}