Add API skeleton
This commit is contained in:
43
api.go
Normal file
43
api.go
Normal 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
2
go.mod
@@ -2,4 +2,4 @@ module github.com/firestuff/checky
|
|||||||
|
|
||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
require github.com/google/uuid v1.3.0
|
require github.com/gorilla/mux v1.8.0
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -1,2 +1,2 @@
|
|||||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
||||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
||||||
|
|||||||
4
main.go
4
main.go
@@ -13,6 +13,10 @@ func main() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
|
api := NewAPI()
|
||||||
|
mux.Handle("/api/", http.StripPrefix("/api", api))
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: *bindFlag,
|
Addr: *bindFlag,
|
||||||
Handler: mux,
|
Handler: mux,
|
||||||
|
|||||||
Reference in New Issue
Block a user