46 lines
868 B
Go
46 lines
868 B
Go
package main
|
|
|
|
import (
|
|
// Standard
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
type Request struct {
|
|
ID string
|
|
}
|
|
|
|
func responseError(w http.ResponseWriter, err error) {
|
|
res := map[string]interface{}{
|
|
"ok": false,
|
|
"error": err.Error(),
|
|
}
|
|
resJSON, _ := json.Marshal(res)
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
w.Write(resJSON)
|
|
}
|
|
|
|
func responseData(w http.ResponseWriter, data interface{}) {
|
|
resJSON, _ := json.Marshal(data)
|
|
w.Header().Add("Content-Type", "application/json")
|
|
w.Write(resJSON)
|
|
}
|
|
|
|
func request(r *http.Request, data interface{}) (err error) {
|
|
body, _ := io.ReadAll(r.Body)
|
|
defer r.Body.Close()
|
|
err = json.Unmarshal(body, data)
|
|
return
|
|
}
|
|
|
|
func sessionUUID(r *http.Request) (string, error) {
|
|
headers := r.Header["X-Session-Id"]
|
|
if len(headers) > 0 {
|
|
return headers[0], nil
|
|
}
|
|
return "", errors.New("Invalid session")
|
|
}
|