mirror of
https://github.com/status-im/fathom.git
synced 2025-03-01 11:30:28 +00:00
add api routes + handlers for managing sites
This commit is contained in:
parent
f721b75f43
commit
dc78bb45d9
@ -12,6 +12,11 @@ func (api *API) Routes() *mux.Router {
|
||||
r := mux.NewRouter()
|
||||
r.Handle("/collect", NewCollector(api.database)).Methods(http.MethodGet)
|
||||
|
||||
r.Handle("/api/sites", HandlerFunc(api.GetSitesHandler)).Methods(http.MethodGet)
|
||||
r.Handle("/api/sites", HandlerFunc(api.SaveSiteHandler)).Methods(http.MethodPost)
|
||||
r.Handle("/api/sites/{id:[0-9]+}", HandlerFunc(api.SaveSiteHandler)).Methods(http.MethodPost)
|
||||
r.Handle("/api/sites/{id:[0-9]+}", HandlerFunc(api.DeleteSiteHandler)).Methods(http.MethodDelete)
|
||||
|
||||
r.Handle("/api/session", HandlerFunc(api.GetSession)).Methods(http.MethodGet)
|
||||
r.Handle("/api/session", HandlerFunc(api.CreateSession)).Methods(http.MethodPost)
|
||||
r.Handle("/api/session", HandlerFunc(api.DeleteSession)).Methods(http.MethodDelete)
|
||||
|
50
pkg/api/sites.go
Normal file
50
pkg/api/sites.go
Normal file
@ -0,0 +1,50 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/usefathom/fathom/pkg/models"
|
||||
)
|
||||
|
||||
// GET /api/sites
|
||||
func (api *API) GetSitesHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
result, err := api.database.GetSites()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return respond(w, http.StatusOK, envelope{Data: result})
|
||||
}
|
||||
|
||||
// POST /api/sites
|
||||
// POST /api/sites/{id}
|
||||
func (api *API) SaveSiteHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
s := &models.Site{}
|
||||
err := json.NewDecoder(r.Body).Decode(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := api.database.SaveSite(s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return respond(w, http.StatusOK, envelope{Data: s})
|
||||
}
|
||||
|
||||
// DELETE /api/sites/{id}
|
||||
func (api *API) DeleteSiteHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
vars := mux.Vars(r)
|
||||
id, err := strconv.ParseInt(vars["id"], 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := api.database.DeleteSite(&models.Site{ID: id}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return respond(w, http.StatusOK, envelope{Data: true})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user