-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
33 lines (26 loc) · 715 Bytes
/
Copy pathhandler.go
File metadata and controls
33 lines (26 loc) · 715 Bytes
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
package main
import (
"encoding/json"
"net/http"
"github.com/horri1520/hori-api/util"
)
type AppHandler struct {
h func(http.ResponseWriter, *http.Request) (int, interface{}, error)
}
func (a AppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
status, res, err := a.h(w, r)
// w.Header().Add("Access-Control-Allow-Headers", "*")
// w.Header().Add("Access-Control-Allow-Origin", "*")
// w.Header().Add("Access-Control-Allow-Methods", "GET")
w.Header().Add("Content-Type", "application/json")
if err != nil {
he := &util.HttpError{
Message: err.Error(),
}
w.WriteHeader(status)
json.NewEncoder(w).Encode(he)
return
}
w.WriteHeader(status)
json.NewEncoder(w).Encode(res)
}