/
api.go
46 lines (34 loc) · 840 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
45
46
package keygen
import (
"encoding/json"
"net/http"
"github.com/thanhbinhdoan1993/practice-kuard/pkg/apiutils"
"github.com/julienschmidt/httprouter"
)
type KeyGenStatus struct {
Config Config `json:"config"`
History []History `json:"history"`
}
type History struct {
ID int `json:"id"`
Data string `json:"data"`
}
func (kg *KeyGen) APIPut(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
c := Config{}
err := json.NewDecoder(r.Body).Decode(&c)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
kg.LoadConfig(c)
kg.APIGet(w, r, params)
}
func (kg *KeyGen) APIGet(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
kg.mu.Lock()
defer kg.mu.Unlock()
s := &KeyGenStatus{
Config: kg.config,
History: kg.history,
}
apiutils.ServeJSON(w, s)
}