From 745773de006bde89527eafaf89c4d52d6c9bd64d Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Wed, 16 Mar 2022 04:12:17 +0000 Subject: [PATCH] Add API skeleton --- api.go | 43 +++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 4 ++-- main.go | 4 ++++ 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 api.go diff --git a/api.go b/api.go new file mode 100644 index 0000000..7457624 --- /dev/null +++ b/api.go @@ -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)) +} diff --git a/go.mod b/go.mod index 7ee7405..eefa1ae 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 3dfe1c9..5350288 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index e91379e..7f10bb5 100644 --- a/main.go +++ b/main.go @@ -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,