From 2a308ee03cc9f5ca3a2bf3c5ac1ab3b4e6bc3555 Mon Sep 17 00:00:00 2001 From: Tom Wiesing Date: Tue, 13 Dec 2022 10:10:51 +0100 Subject: [PATCH] internal/component: Check for provisionable --- go.mod | 1 + go.sum | 2 ++ internal/dis/component/control/info/info.go | 23 ++++++++----------- internal/dis/component/meta/meta.go | 4 ++++ internal/dis/component/sql/sql.go | 7 +++--- .../dis/component/triplestore/triplestore.go | 7 +++--- 6 files changed, 24 insertions(+), 20 deletions(-) diff --git a/go.mod b/go.mod index 5481291..ebe48ba 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/go-sql-driver/mysql v1.6.0 github.com/gorilla/mux v1.8.0 github.com/gorilla/websocket v1.5.0 + github.com/julienschmidt/httprouter v1.3.0 github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.28.0 github.com/tkw1536/goprogram v0.2.4 diff --git a/go.sum b/go.sum index 3a00e9f..c5c0110 100644 --- a/go.sum +++ b/go.sum @@ -27,6 +27,8 @@ github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkr github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= +github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= diff --git a/internal/dis/component/control/info/info.go b/internal/dis/component/control/info/info.go index 2888d7c..07eeb2f 100644 --- a/internal/dis/component/control/info/info.go +++ b/internal/dis/component/control/info/info.go @@ -7,7 +7,7 @@ import ( "github.com/FAU-CDI/wisski-distillery/internal/dis/component" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter/logger" - "github.com/gorilla/mux" + "github.com/julienschmidt/httprouter" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances" "github.com/FAU-CDI/wisski-distillery/pkg/httpx" @@ -33,7 +33,7 @@ var ( func (*Info) Routes() []string { return []string{"/dis/"} } func (info *Info) Handler(ctx context.Context, route string) (handler http.Handler, err error) { - router := mux.NewRouter() + router := httprouter.New() { socket := &httpx.WebSocket{ Context: ctx, @@ -46,40 +46,35 @@ func (info *Info) Handler(ctx context.Context, route string) (handler http.Handl } // handle everything - router.Path(route).HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, route+"/index", http.StatusTemporaryRedirect) + router.HandlerFunc(http.MethodGet, route, func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "index", http.StatusTemporaryRedirect) }) // add a handler for the index page - router.Path(route + "index").Handler(httpx.HTMLHandler[indexContext]{ + router.Handler(http.MethodGet, "index", httpx.HTMLHandler[indexContext]{ Handler: info.index, Template: indexTemplate, }) // add a handler for the component page - router.Path(route + "components").Handler(httpx.HTMLHandler[componentContext]{ + router.Handler(http.MethodGet, "components", httpx.HTMLHandler[componentContext]{ Handler: info.components, Template: componentsTemplate, }) // add a handler for the component page - router.Path(route + "ingredients/{slug}").Handler(httpx.HTMLHandler[ingredientsContext]{ + router.Handler(http.MethodGet, "ingredients/{slug}", httpx.HTMLHandler[ingredientsContext]{ Handler: info.ingredients, Template: ingredientsTemplate, }) // add a handler for the instance page - router.Path(route + "instance/{slug}").Handler(httpx.HTMLHandler[instanceContext]{ + router.Handler(http.MethodGet, "instance/{slug}", httpx.HTMLHandler[instanceContext]{ Handler: info.instance, Template: instanceTemplate, }) - router.Path(route + "api/login").Handler(httpx.RedirectHandler(func(r *http.Request) (string, int, error) { - // enforce POST - if r.Method != http.MethodPost { - return "", 0, httpx.ErrMethodNotAllowed - } - + router.Handler(http.MethodPost, "api/login", httpx.RedirectHandler(func(r *http.Request) (string, int, error) { // parse the form if err := r.ParseForm(); err != nil { return "", 0, err diff --git a/internal/dis/component/meta/meta.go b/internal/dis/component/meta/meta.go index 615f82e..949d677 100644 --- a/internal/dis/component/meta/meta.go +++ b/internal/dis/component/meta/meta.go @@ -17,6 +17,10 @@ type Meta struct { sc map[string]*Storage } +var ( + _ component.Provisionable = (*Meta)(nil) +) + // Storage returns a Storage for the instance with the given slug. // When slug is nil, returns a global storage. func (meta *Meta) Storage(slug string) *Storage { diff --git a/internal/dis/component/sql/sql.go b/internal/dis/component/sql/sql.go index ad772e9..1b152d4 100644 --- a/internal/dis/component/sql/sql.go +++ b/internal/dis/component/sql/sql.go @@ -21,9 +21,10 @@ type SQL struct { } var ( - _ component.Backupable = (*SQL)(nil) - _ component.Snapshotable = (*SQL)(nil) - _ component.Installable = (*SQL)(nil) + _ component.Backupable = (*SQL)(nil) + _ component.Snapshotable = (*SQL)(nil) + _ component.Installable = (*SQL)(nil) + _ component.Provisionable = (*SQL)(nil) ) func (sql *SQL) Path() string { diff --git a/internal/dis/component/triplestore/triplestore.go b/internal/dis/component/triplestore/triplestore.go index 78f1d74..5a572ea 100644 --- a/internal/dis/component/triplestore/triplestore.go +++ b/internal/dis/component/triplestore/triplestore.go @@ -18,9 +18,10 @@ type Triplestore struct { } var ( - _ component.Backupable = (*Triplestore)(nil) - _ component.Snapshotable = (*Triplestore)(nil) - _ component.Installable = (*Triplestore)(nil) + _ component.Backupable = (*Triplestore)(nil) + _ component.Snapshotable = (*Triplestore)(nil) + _ component.Installable = (*Triplestore)(nil) + _ component.Provisionable = (*Triplestore)(nil) ) func (ts *Triplestore) Path() string {