From 71ea9cac18aa399c2354f4d0112921878b5bd818 Mon Sep 17 00:00:00 2001 From: Florian Beisel Date: Wed, 24 Jan 2024 11:04:43 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20add=20helper=20functions=20and?= =?UTF-8?q?=20general=20cleanup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...p2.sql => 2_create_hostnames_table.up.sql} | 2 +- go.mod | 1 + go.sum | 2 + router/router.go | 1 - rules/interface.go | 9 +++++ rules/notebook.go | 40 +++++++++---------- rules/server.go | 40 +++++++++---------- 7 files changed, 53 insertions(+), 42 deletions(-) rename db/migrations/{2_create_hostnames_table.up2.sql => 2_create_hostnames_table.up.sql} (79%) diff --git a/db/migrations/2_create_hostnames_table.up2.sql b/db/migrations/2_create_hostnames_table.up.sql similarity index 79% rename from db/migrations/2_create_hostnames_table.up2.sql rename to db/migrations/2_create_hostnames_table.up.sql index e530f3a..9709a96 100644 --- a/db/migrations/2_create_hostnames_table.up2.sql +++ b/db/migrations/2_create_hostnames_table.up.sql @@ -1,4 +1,4 @@ -CREATE TABLE hostnames ( +CREATE TABLE IF NOT EXISTS hostnames ( id INTEGER PRIMARY KEY AUTOINCREMENT, category TEXT NOT NULL, hostname TEXT NOT NULL, diff --git a/go.mod b/go.mod index 02ccdd6..c02188c 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,7 @@ require ( github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/gabriel-vasile/mimetype v1.4.3 // indirect + github.com/gin-contrib/cors v1.5.0 github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect diff --git a/go.sum b/go.sum index 6298a8c..fa2890d 100644 --- a/go.sum +++ b/go.sum @@ -22,6 +22,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/gin-contrib/cors v1.5.0 h1:DgGKV7DDoOn36DFkNtbHrjoRiT5ExCe+PC9/xp7aKvk= +github.com/gin-contrib/cors v1.5.0/go.mod h1:TvU7MAZ3EwrPLI2ztzTt3tqgvBCq+wn8WpZmfADjupI= github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4= github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= diff --git a/router/router.go b/router/router.go index 1942e8f..5a19e97 100644 --- a/router/router.go +++ b/router/router.go @@ -44,7 +44,6 @@ func New() *gin.Engine { { // public routes v1.POST("/login", auth.LoginHandler) - v1.GET("/login", api.Helloworld) // Protected Routes authenticated := v1.Group("/").Use(middleware.Authenticate()) diff --git a/rules/interface.go b/rules/interface.go index 0ec904a..c80bdf8 100644 --- a/rules/interface.go +++ b/rules/interface.go @@ -1,6 +1,7 @@ package rules import ( + "encoding/json" "fmt" "log" @@ -59,3 +60,11 @@ func (br *BaseRule) baseUpdate(rule HostnameRule, category string, oldhostname s return newHostname, nil } + +func StructToJSON(v interface{}) (string, error) { + bytes, err := json.Marshal(v) + if err != nil { + return "", err + } + return string(bytes), nil +} diff --git a/rules/notebook.go b/rules/notebook.go index 939e5be..4872362 100644 --- a/rules/notebook.go +++ b/rules/notebook.go @@ -66,32 +66,32 @@ func (nr *NotebookRule) Generate(params map[string]interface{}) (string, []byte, return hostname, paramsJSON, nil } -// @Summary Generate hostname for category "notebook" -// @Description Generates a hostname for a notebook based on dynamic rules. -// @ID insert-notebook-hostname -// @Accept json -// @Produce json -// @Tags Generating Hostnames -// @Param body body NotebookRuleInput true "Input data to generate hostname" -// @Success 200 {object} models.SimpleHostnameResponse "Hostname" -// @Router /api/notebook [post] -// @Security Bearer +// @Summary Generate hostname for category "notebook" +// @Description Generates a hostname for a notebook based on dynamic rules. +// @ID insert-notebook-hostname +// @Accept json +// @Produce json +// @Tags Generating Hostnames +// @Param body body NotebookRuleInput true "Input data to generate hostname" +// @Success 200 {object} models.SimpleHostnameResponse "Hostname" +// @Router /api/notebook [post] +// @Security Bearer func (nr *NotebookRule) Insert(category string, params map[string]interface{}) (string, error) { // Generate the hostname return nr.baseInsert(nr, category, params) } -// @Summary Update hostname for category "notebook" -// @Description Generates a new hostname for a notebook based on dynamic rules. -// @ID update-notebook-hostname -// @Accept json -// @Produce json -// @Tags Manipulate existing Hostnames -// @Param body body NotebookRuleInput true "Input data to generate hostname" -// @Success 200 {object} models.SimpleHostnameResponse "Hostname" -// @Router /api/notebook [put] -// @Security Bearer +// @Summary Update hostname for category "notebook" +// @Description Generates a new hostname for a notebook based on dynamic rules. +// @ID update-notebook-hostname +// @Accept json +// @Produce json +// @Tags Manipulate existing Hostnames +// @Param body body NotebookRuleInput true "Input data to generate hostname" +// @Success 200 {object} models.SimpleHostnameResponse "Hostname" +// @Router /api/notebook [put] +// @Security Bearer func (nr *NotebookRule) Update(category string, oldhostname string, params map[string]interface{}) (string, error) { return nr.baseUpdate(nr, category, oldhostname, params) } diff --git a/rules/server.go b/rules/server.go index dc616a1..885c37c 100644 --- a/rules/server.go +++ b/rules/server.go @@ -108,32 +108,32 @@ func (sr *ServerRule) Generate(params map[string]interface{}) (string, []byte, e } -// @Summary Generate hostname for category "notebook" -// @Description Generates a hostname for a notebook based on dynamic rules. -// @ID insert-server-hostname -// @Accept json -// @Produce json -// @Tags Generating Hostnames -// @Param body body ServerRuleInput true "Input data to generate hostname" -// @Success 200 {object} models.SimpleHostnameResponse "Hostname" -// @Router /api/server [post] -// @Security Bearer +// @Summary Generate hostname for category "notebook" +// @Description Generates a hostname for a notebook based on dynamic rules. +// @ID insert-server-hostname +// @Accept json +// @Produce json +// @Tags Generating Hostnames +// @Param body body ServerRuleInput true "Input data to generate hostname" +// @Success 200 {object} models.SimpleHostnameResponse "Hostname" +// @Router /api/server [post] +// @Security Bearer func (nr *ServerRule) Insert(category string, params map[string]interface{}) (string, error) { // Generate the hostname return nr.baseInsert(nr, category, params) } -// @Summary Update hostname for category "notebook" -// @Description Generates a new hostname for a notebook based on dynamic rules. -// @ID update-server-hostname -// @Accept json -// @Produce json -// @Tags Manipulate existing Hostnames -// @Param body body ServerRuleInput true "Input data to generate hostname" -// @Success 200 {object} models.SimpleHostnameResponse "Hostname" -// @Router /api/server [put] -// @Security Bearer +// @Summary Update hostname for category "notebook" +// @Description Generates a new hostname for a notebook based on dynamic rules. +// @ID update-server-hostname +// @Accept json +// @Produce json +// @Tags Manipulate existing Hostnames +// @Param body body ServerRuleInput true "Input data to generate hostname" +// @Success 200 {object} models.SimpleHostnameResponse "Hostname" +// @Router /api/server [put] +// @Security Bearer func (nr *ServerRule) Update(category string, oldhostname string, params map[string]interface{}) (string, error) { return nr.baseUpdate(nr, category, oldhostname, params) }