Add API skeleton

This commit is contained in:
Ian Gulliver
2022-03-16 04:12:17 +00:00
parent b61359cd73
commit 745773de00
4 changed files with 50 additions and 3 deletions

43
api.go Normal file
View File

@@ -0,0 +1,43 @@
package main
import "log"
import "net/http"
import "github.com/gorilla/mux"
type API struct {
router *mux.Router
}
func NewAPI() *API {
api := &API{
router: mux.NewRouter(),
}
api.router.HandleFunc("/template", api.listTemplates).Methods("GET")
api.router.HandleFunc("/template", api.createTemplate).Methods("POST")
api.router.HandleFunc("/template/{id}", api.getTemplate).Methods("GET")
api.router.HandleFunc("/template/{id}", api.updateTemplate).Methods("PATCH")
return api
}
func (api *API) ServeHTTP(w http.ResponseWriter, r *http.Request) {
api.router.ServeHTTP(w, r)
}
func (api *API) listTemplates(w http.ResponseWriter, r *http.Request) {
log.Printf("listTemplates")
}
func (api *API) createTemplate(w http.ResponseWriter, r *http.Request) {
log.Printf("createTemplate")
}
func (api *API) getTemplate(w http.ResponseWriter, r *http.Request) {
log.Printf("getTemplate %s", mux.Vars(r))
}
func (api *API) updateTemplate(w http.ResponseWriter, r *http.Request) {
log.Printf("updateTemplate %s", mux.Vars(r))
}

2
go.mod
View File

@@ -2,4 +2,4 @@ module github.com/firestuff/checky
go 1.16
require github.com/google/uuid v1.3.0
require github.com/gorilla/mux v1.8.0

4
go.sum
View File

@@ -1,2 +1,2 @@
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=

View File

@@ -13,6 +13,10 @@ func main() {
flag.Parse()
mux := http.NewServeMux()
api := NewAPI()
mux.Handle("/api/", http.StripPrefix("/api", api))
srv := &http.Server{
Addr: *bindFlag,
Handler: mux,