2017-08-03 12:13:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2019-05-10 21:00:08 +00:00
|
|
|
"strings"
|
2017-08-03 12:13:53 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
type apiServer struct {
|
|
|
|
store storage
|
|
|
|
}
|
|
|
|
|
2021-09-07 10:45:27 +00:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2017-08-03 12:13:53 +00:00
|
|
|
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) {
|
2019-05-10 21:00:08 +00:00
|
|
|
var secret string
|
|
|
|
|
|
|
|
if strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
|
2021-09-07 10:45:27 +00:00
|
|
|
tmp := apiRequest{}
|
2019-05-10 21:00:08 +00:00
|
|
|
if err := json.NewDecoder(r.Body).Decode(&tmp); err != nil {
|
2021-09-07 10:45:27 +00:00
|
|
|
a.errorResponse(res, http.StatusBadRequest, err.Error())
|
2019-05-10 21:00:08 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-07 10:45:27 +00:00
|
|
|
secret = tmp.Secret
|
2019-05-10 21:00:08 +00:00
|
|
|
} else {
|
|
|
|
secret = r.FormValue("secret")
|
|
|
|
}
|
|
|
|
|
2017-08-03 12:13:53 +00:00
|
|
|
if secret == "" {
|
2021-09-07 10:45:27 +00:00
|
|
|
a.errorResponse(res, http.StatusBadRequest, "Secret missing")
|
2017-08-03 12:13:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := a.store.Create(secret)
|
|
|
|
if err != nil {
|
2021-09-07 10:45:27 +00:00
|
|
|
a.errorResponse(res, http.StatusInternalServerError, err.Error())
|
2017-08-03 12:13:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-07 10:45:27 +00:00
|
|
|
a.jsonResponse(res, http.StatusCreated, apiResponse{
|
|
|
|
Success: true,
|
|
|
|
SecretId: id,
|
2017-08-03 12:13:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a apiServer) handleRead(res http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
id := vars["id"]
|
|
|
|
if id == "" {
|
2021-09-07 10:45:27 +00:00
|
|
|
a.errorResponse(res, http.StatusBadRequest, "ID missing")
|
2017-08-03 12:13:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := a.store.ReadAndDestroy(id)
|
|
|
|
if err != nil {
|
2017-08-03 16:18:05 +00:00
|
|
|
status := http.StatusInternalServerError
|
|
|
|
if err == errSecretNotFound {
|
|
|
|
status = http.StatusNotFound
|
|
|
|
}
|
2021-09-07 10:45:27 +00:00
|
|
|
a.errorResponse(res, status, err.Error())
|
2017-08-03 12:13:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-07 10:45:27 +00:00
|
|
|
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,
|
2017-08-03 12:13:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-07 10:45:27 +00:00
|
|
|
func (a apiServer) jsonResponse(res http.ResponseWriter, status int, response apiResponse) {
|
2017-08-03 12:13:53 +00:00
|
|
|
res.Header().Set("Content-Type", "application/json")
|
2021-08-09 15:37:48 +00:00
|
|
|
res.Header().Set("Cache-Control", "no-store, max-age=0")
|
2017-08-03 12:13:53 +00:00
|
|
|
res.WriteHeader(status)
|
|
|
|
|
|
|
|
json.NewEncoder(res).Encode(response)
|
|
|
|
}
|