123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package api
- import (
- "log"
- "net/http"
- "time"
- "github.com/go-chi/chi"
- "github.com/go-chi/render"
- "github.com/willie68/schematic-service-go/dao"
- )
- const TenantHeader = "X-es-tenant"
- const timeout = 1 * time.Minute
- var APIKey string
- var SystemID string
- type ConfigDescription struct {
- StoreID string `json:"storeid"`
- TenantID string `json:"tenantID"`
- Size int `json:"size"`
- }
- func ConfigRoutes() *chi.Mux {
- router := chi.NewRouter()
- router.Post("/", PostConfigEndpoint)
- router.Get("/", GetConfigEndpoint)
- router.Delete("/", DeleteConfigEndpoint)
- router.Get("/size", GetConfigSizeEndpoint)
- router.Delete("/dropall", DropAllEndpoint)
- return router
- }
- func GetConfigEndpoint(response http.ResponseWriter, req *http.Request) {
- tenant := getTenant(req)
- if tenant == "" {
- Msg(response, http.StatusBadRequest, "tenant not set")
- return
- }
- c := ConfigDescription{
- StoreID: "myNewStore",
- TenantID: tenant,
- Size: 1234567,
- }
- render.JSON(response, req, c)
- }
- func PostConfigEndpoint(response http.ResponseWriter, req *http.Request) {
- tenant := getTenant(req)
- if tenant == "" {
- Msg(response, http.StatusBadRequest, "tenant not set")
- return
- }
- log.Printf("create store for tenant %s", tenant)
- render.Status(req, http.StatusCreated)
- render.JSON(response, req, tenant)
- }
- func DeleteConfigEndpoint(response http.ResponseWriter, req *http.Request) {
- tenant := getTenant(req)
- if tenant == "" {
- Msg(response, http.StatusBadRequest, "tenant not set")
- return
- }
- render.JSON(response, req, tenant)
- }
- func GetConfigSizeEndpoint(response http.ResponseWriter, req *http.Request) {
- tenant := getTenant(req)
- if tenant == "" {
- Msg(response, http.StatusBadRequest, "tenant not set")
- return
- }
- render.JSON(response, req, tenant)
- }
- func getTenant(req *http.Request) string {
- return req.Header.Get(TenantHeader)
- }
- func DropAllEndpoint(response http.ResponseWriter, req *http.Request) {
- dao.DropAll()
- render.JSON(response, req, "")
- }
|