forked from Luzifer/ots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
104 lines (86 loc) · 2.2 KB
/
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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"encoding/json"
"net/http"
"strings"
"github.com/gorilla/mux"
)
type apiServer struct {
store storage
}
type apiResponse struct {
Success bool `json:"success"`
Error string `json:"error,omitempty"`
Secret string `json:"secret,omitempty"`
SecretId string `json:"secret_id,omitempty"`
}
type apiRequest struct {
Secret string `json:"secret"`
}
func newAPI(s storage) *apiServer {
return &apiServer{
store: s,
}
}
func (a apiServer) Register(r *mux.Router) {
r.HandleFunc("/create", a.handleCreate)
r.HandleFunc("/get/{id}", a.handleRead)
}
func (a apiServer) handleCreate(res http.ResponseWriter, r *http.Request) {
var secret string
if strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
tmp := apiRequest{}
if err := json.NewDecoder(r.Body).Decode(&tmp); err != nil {
a.errorResponse(res, http.StatusBadRequest, err.Error())
return
}
secret = tmp.Secret
} else {
secret = r.FormValue("secret")
}
if secret == "" {
a.errorResponse(res, http.StatusBadRequest, "Secret missing")
return
}
id, err := a.store.Create(secret)
if err != nil {
a.errorResponse(res, http.StatusInternalServerError, err.Error())
return
}
a.jsonResponse(res, http.StatusCreated, apiResponse{
Success: true,
SecretId: id,
})
}
func (a apiServer) handleRead(res http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
if id == "" {
a.errorResponse(res, http.StatusBadRequest, "ID missing")
return
}
secret, err := a.store.ReadAndDestroy(id)
if err != nil {
status := http.StatusInternalServerError
if err == errSecretNotFound {
status = http.StatusNotFound
}
a.errorResponse(res, status, err.Error())
return
}
a.jsonResponse(res, http.StatusOK, apiResponse{
Success: true,
Secret: secret,
})
}
func (a apiServer) errorResponse(res http.ResponseWriter, status int, msg string) {
a.jsonResponse(res, status, apiResponse{
Error: msg,
})
}
func (a apiServer) jsonResponse(res http.ResponseWriter, status int, response apiResponse) {
res.Header().Set("Content-Type", "application/json")
res.Header().Set("Cache-Control", "no-store, max-age=0")
res.WriteHeader(status)
json.NewEncoder(res).Encode(response)
}