diff --git a/controller/dadjoke.go b/controller/dadjoke.go index 7cc9e8c..c2bf1a8 100644 --- a/controller/dadjoke.go +++ b/controller/dadjoke.go @@ -1,10 +1,9 @@ package controller import ( + "encoding/json" "net/http" - "github.com/gin-gonic/gin" - "gitlab.com/DeveloperDurp/DurpAPI/model" "gitlab.com/DeveloperDurp/DurpAPI/service" ) @@ -19,14 +18,20 @@ import ( // @Success 200 {object} model.Message "response" // @failure 500 {object} model.Message "error" // @Router /jokes/dadjoke [get] -func (c *Controller) GetDadJoke(ctx *gin.Context) { +func (c *Controller) GetDadJoke(w http.ResponseWriter, r *http.Request) { joke, err := service.GetRandomDadJoke(c.Db.DB) if err != nil { - ctx.JSON(http.StatusInternalServerError, gin.H{"message": err}) + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(http.StatusText(http.StatusInternalServerError))) return } - ctx.JSON(http.StatusOK, gin.H{"message": joke}) + message := model.Message{ + Message: joke, + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(message) } // PostDadJoke godoc @@ -40,19 +45,35 @@ func (c *Controller) GetDadJoke(ctx *gin.Context) { // @Success 200 {object} model.Message "response" // @failure 500 {object} model.Message "error" // @Router /jokes/dadjoke [post] -func (c *Controller) PostDadJoke(ctx *gin.Context) { +func (c *Controller) PostDadJoke(w http.ResponseWriter, r *http.Request) { + contentType := r.Header.Get("Content-Type") var req model.DadJoke - if err := ctx.ShouldBindJSON(&req); err != nil { - req.JOKE = ctx.Query("joke") + if contentType == "application/json" { + err := json.NewDecoder(r.Body).Decode(&req) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(http.StatusText(http.StatusInternalServerError))) + return + } + } else { + queryParams := r.URL.Query() + req.JOKE = queryParams.Get("joke") } err := service.PostDadJoke(c.Db.DB, req) if err != nil { - ctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()}) + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(http.StatusText(http.StatusInternalServerError))) return } - ctx.JSON(http.StatusOK, gin.H{"message": "OK"}) + + message := model.Message{ + Message: "OK", + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(message) } // DeleteDadJoke godoc @@ -66,17 +87,33 @@ func (c *Controller) PostDadJoke(ctx *gin.Context) { // @Success 200 {object} model.Message "response" // @failure 500 {object} model.Message "error" // @Router /jokes/dadjoke [delete] -func (c *Controller) DeleteDadJoke(ctx *gin.Context) { +func (c *Controller) DeleteDadJoke(w http.ResponseWriter, r *http.Request) { + contentType := r.Header.Get("Content-Type") var req model.DadJoke - if err := ctx.ShouldBindJSON(&req); err != nil { - req.JOKE = ctx.Query("joke") + if contentType == "application/json" { + err := json.NewDecoder(r.Body).Decode(&req) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(http.StatusText(http.StatusInternalServerError))) + return + } + } else { + queryParams := r.URL.Query() + req.JOKE = queryParams.Get("joke") } err := service.DeleteDadJoke(c.Db.DB, req) if err != nil { - ctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()}) + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(http.StatusText(http.StatusInternalServerError))) return } - ctx.JSON(http.StatusOK, gin.H{"message": "OK"}) + + message := model.Message{ + Message: "OK", + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(message) } diff --git a/controller/health.go b/controller/health.go index 1adf84a..92fcb3e 100644 --- a/controller/health.go +++ b/controller/health.go @@ -1,9 +1,10 @@ package controller import ( + "encoding/json" "net/http" - "github.com/gin-gonic/gin" + "gitlab.com/DeveloperDurp/DurpAPI/model" ) // getHealth godoc @@ -14,8 +15,16 @@ import ( // @Accept json // @Produce application/json // @Success 200 {object} model.Message "response" -// @failure 400 {object} model.Message "error" -// @Router /health/getHealth [get] -func (c *Controller) GetHealth(ctx *gin.Context) { - ctx.JSON(http.StatusOK, gin.H{"message": "OK"}) +// @failure 500 {object} model.Message "error" +// +// @Security Authorization +// +// @Router /health/gethealth [get] +func (c *Controller) GetHealth(w http.ResponseWriter, r *http.Request) { + message := model.Message{ + Message: "OK", + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(message) } diff --git a/controller/openai.go b/controller/openai.go index 1817ba2..73fc815 100644 --- a/controller/openai.go +++ b/controller/openai.go @@ -7,7 +7,7 @@ import ( "io" "net/http" - "github.com/gin-gonic/gin" + "gitlab.com/DeveloperDurp/DurpAPI/model" ) type ChatRequest struct { @@ -32,22 +32,35 @@ type Response struct { // @failure 400 {object} model.Message "error" // // @Router /openai/general [get] -func (c *Controller) GeneralOpenAI(ctx *gin.Context) { +func (c *Controller) GeneralOpenAI(w http.ResponseWriter, r *http.Request) { + contentType := r.Header.Get("Content-Type") var req ChatRequest - if err := ctx.ShouldBindJSON(&req); err != nil { - req.Message = ctx.Query("message") + if contentType == "application/json" { + err := json.NewDecoder(r.Body).Decode(&req) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(http.StatusText(http.StatusInternalServerError))) + return + } + } else { + queryParams := r.URL.Query() + req.Message = queryParams.Get("message") } result, err := c.createChatCompletion(req.Message, "mistral:instruct") if err != nil { - err := ctx.AbortWithError(http.StatusInternalServerError, err) - if err != nil { - fmt.Println("Failed to send message") - } + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(http.StatusText(http.StatusInternalServerError))) + return } - ctx.JSON(http.StatusOK, gin.H{"message": result}) + message := model.Message{ + Message: result, + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(message) } // TravelAgentOpenAI godoc @@ -61,23 +74,37 @@ func (c *Controller) GeneralOpenAI(ctx *gin.Context) { // @Success 200 {object} model.Message "response" // @failure 400 {object} model.Message "error" // @Router /openai/travelagent [get] -func (c *Controller) TravelAgentOpenAI(ctx *gin.Context) { +func (c *Controller) TravelAgentOpenAI(w http.ResponseWriter, r *http.Request) { + contentType := r.Header.Get("Content-Type") var req ChatRequest - if err := ctx.ShouldBindJSON(&req); err != nil { - req.Message = ctx.Query("message") + + if contentType == "application/json" { + err := json.NewDecoder(r.Body).Decode(&req) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(http.StatusText(http.StatusInternalServerError))) + return + } + } else { + queryParams := r.URL.Query() + req.Message = queryParams.Get("message") } req.Message = "I want you to act as a travel guide. I will give you my location and you will give me suggestions. " + req.Message - result, err := c.createChatCompletion(req.Message, "openchat") + result, err := c.createChatCompletion(req.Message, "mistral:instruct") if err != nil { - err := ctx.AbortWithError(http.StatusInternalServerError, err) - if err != nil { - fmt.Println("Failed to send message") - } + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(http.StatusText(http.StatusInternalServerError))) + return } - ctx.JSON(http.StatusOK, gin.H{"message": result}) + message := model.Message{ + Message: result, + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(message) } func (c *Controller) createChatCompletion(message string, model string) (string, error) { diff --git a/docs/docs.go b/docs/docs.go index 02a74a8..116c99b 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -1,5 +1,4 @@ -// Code generated by swaggo/swag. DO NOT EDIT. - +// Package docs Code generated by swaggo/swag. DO NOT EDIT package docs import "github.com/swaggo/swag" @@ -25,8 +24,13 @@ const docTemplate = `{ "host": "{{.Host}}", "basePath": "{{.BasePath}}", "paths": { - "/health/getHealth": { + "/health/gethealth": { "get": { + "security": [ + { + "Authorization": [] + } + ], "description": "Get the health of the API", "consumes": [ "application/json" @@ -45,7 +49,7 @@ const docTemplate = `{ "$ref": "#/definitions/model.Message" } }, - "400": { + "500": { "description": "error", "schema": { "$ref": "#/definitions/model.Message" @@ -230,35 +234,6 @@ const docTemplate = `{ } } } - }, - "/unraid/powerusage": { - "get": { - "description": "Gets the PSU Data from unraid", - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "unraid" - ], - "summary": "Unraid PSU Stats", - "responses": { - "200": { - "description": "response", - "schema": { - "$ref": "#/definitions/model.PowerSupply" - } - }, - "412": { - "description": "error", - "schema": { - "$ref": "#/definitions/model.Message" - } - } - } - } } }, "definitions": { @@ -270,72 +245,10 @@ const docTemplate = `{ "example": "message" } } - }, - "model.PowerSupply": { - "type": "object", - "properties": { - "12v_load": { - "type": "integer" - }, - "12v_watts": { - "type": "integer" - }, - "3v_load": { - "type": "integer" - }, - "3v_watts": { - "type": "integer" - }, - "5v_load": { - "type": "integer" - }, - "5v_watts": { - "type": "integer" - }, - "capacity": { - "type": "string" - }, - "efficiency": { - "type": "integer" - }, - "fan_rpm": { - "type": "integer" - }, - "load": { - "type": "integer" - }, - "poweredon": { - "type": "string" - }, - "poweredon_raw": { - "type": "string" - }, - "product": { - "type": "string" - }, - "temp1": { - "type": "integer" - }, - "temp2": { - "type": "integer" - }, - "uptime": { - "type": "string" - }, - "uptime_raw": { - "type": "string" - }, - "vendor": { - "type": "string" - }, - "watts": { - "type": "integer" - } - } } }, "securityDefinitions": { - "ApiKeyAuth": { + "Authorization": { "type": "apiKey", "name": "Authorization", "in": "header" diff --git a/docs/swagger.json b/docs/swagger.json index 46a4a9c..51e7c4c 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -16,8 +16,13 @@ }, "basePath": "/api", "paths": { - "/health/getHealth": { + "/health/gethealth": { "get": { + "security": [ + { + "Authorization": [] + } + ], "description": "Get the health of the API", "consumes": [ "application/json" @@ -36,7 +41,7 @@ "$ref": "#/definitions/model.Message" } }, - "400": { + "500": { "description": "error", "schema": { "$ref": "#/definitions/model.Message" @@ -221,35 +226,6 @@ } } } - }, - "/unraid/powerusage": { - "get": { - "description": "Gets the PSU Data from unraid", - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "unraid" - ], - "summary": "Unraid PSU Stats", - "responses": { - "200": { - "description": "response", - "schema": { - "$ref": "#/definitions/model.PowerSupply" - } - }, - "412": { - "description": "error", - "schema": { - "$ref": "#/definitions/model.Message" - } - } - } - } } }, "definitions": { @@ -261,72 +237,10 @@ "example": "message" } } - }, - "model.PowerSupply": { - "type": "object", - "properties": { - "12v_load": { - "type": "integer" - }, - "12v_watts": { - "type": "integer" - }, - "3v_load": { - "type": "integer" - }, - "3v_watts": { - "type": "integer" - }, - "5v_load": { - "type": "integer" - }, - "5v_watts": { - "type": "integer" - }, - "capacity": { - "type": "string" - }, - "efficiency": { - "type": "integer" - }, - "fan_rpm": { - "type": "integer" - }, - "load": { - "type": "integer" - }, - "poweredon": { - "type": "string" - }, - "poweredon_raw": { - "type": "string" - }, - "product": { - "type": "string" - }, - "temp1": { - "type": "integer" - }, - "temp2": { - "type": "integer" - }, - "uptime": { - "type": "string" - }, - "uptime_raw": { - "type": "string" - }, - "vendor": { - "type": "string" - }, - "watts": { - "type": "integer" - } - } } }, "securityDefinitions": { - "ApiKeyAuth": { + "Authorization": { "type": "apiKey", "name": "Authorization", "in": "header" diff --git a/docs/swagger.yaml b/docs/swagger.yaml index a768253..2fa0e94 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -6,47 +6,6 @@ definitions: example: message type: string type: object - model.PowerSupply: - properties: - 3v_load: - type: integer - 3v_watts: - type: integer - 5v_load: - type: integer - 5v_watts: - type: integer - 12v_load: - type: integer - 12v_watts: - type: integer - capacity: - type: string - efficiency: - type: integer - fan_rpm: - type: integer - load: - type: integer - poweredon: - type: string - poweredon_raw: - type: string - product: - type: string - temp1: - type: integer - temp2: - type: integer - uptime: - type: string - uptime_raw: - type: string - vendor: - type: string - watts: - type: integer - type: object info: contact: email: developerdurp@durp.info @@ -59,7 +18,7 @@ info: termsOfService: http://swagger.io/terms/ title: DurpAPI paths: - /health/getHealth: + /health/gethealth: get: consumes: - application/json @@ -71,10 +30,12 @@ paths: description: response schema: $ref: '#/definitions/model.Message' - "400": + "500": description: error schema: $ref: '#/definitions/model.Message' + security: + - Authorization: [] summary: Generate Health status tags: - health @@ -195,27 +156,8 @@ paths: summary: Travel Agent ChatGPT tags: - openai - /unraid/powerusage: - get: - consumes: - - application/json - description: Gets the PSU Data from unraid - produces: - - application/json - responses: - "200": - description: response - schema: - $ref: '#/definitions/model.PowerSupply' - "412": - description: error - schema: - $ref: '#/definitions/model.Message' - summary: Unraid PSU Stats - tags: - - unraid securityDefinitions: - ApiKeyAuth: + Authorization: in: header name: Authorization type: apiKey diff --git a/go.mod b/go.mod index 5c6944f..88fc8d7 100644 --- a/go.mod +++ b/go.mod @@ -1,61 +1,35 @@ module gitlab.com/DeveloperDurp/DurpAPI -go 1.19 +go 1.22.0 require ( - github.com/gin-gonic/gin v1.9.1 + github.com/caarlos0/env/v6 v6.10.1 github.com/joho/godotenv v1.5.1 - github.com/swaggo/files v1.0.1 - github.com/swaggo/gin-swagger v1.6.0 + github.com/swaggo/http-swagger v1.3.4 github.com/swaggo/swag v1.16.3 gorm.io/driver/postgres v1.5.7 - gorm.io/gorm v1.25.8 -) - -require ( - github.com/chenzhuoyu/iasm v0.9.1 // indirect - github.com/gabriel-vasile/mimetype v1.4.3 // indirect - github.com/google/go-cmp v0.5.8 // indirect - github.com/jackc/puddle/v2 v2.2.1 // indirect - golang.org/x/sync v0.6.0 // indirect + gorm.io/gorm v1.25.9 ) require ( github.com/KyleBanks/depth v1.2.1 // indirect - github.com/bytedance/sonic v1.11.3 // indirect - github.com/caarlos0/env/v6 v6.10.1 - github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect - github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-openapi/jsonpointer v0.21.0 // indirect github.com/go-openapi/jsonreference v0.21.0 // indirect github.com/go-openapi/spec v0.21.0 // indirect github.com/go-openapi/swag v0.23.0 // indirect - github.com/go-playground/locales v0.14.1 // indirect - github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/go-playground/validator/v10 v10.19.0 // indirect - github.com/goccy/go-json v0.10.2 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect github.com/jackc/pgx/v5 v5.5.5 // indirect + github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/josharian/intern v1.0.0 // indirect - github.com/json-iterator/go v1.1.12 // indirect - github.com/klauspost/cpuid/v2 v2.2.7 // indirect - github.com/leodido/go-urn v1.4.0 // indirect github.com/mailru/easyjson v0.7.7 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect - github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect - github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/pelletier/go-toml/v2 v2.2.0 // indirect - github.com/twitchyliquid64/golang-asm v0.15.1 // indirect - github.com/ugorji/go/codec v1.2.12 // indirect - golang.org/x/arch v0.7.0 // indirect + github.com/swaggo/files v1.0.1 // indirect golang.org/x/crypto v0.21.0 // indirect golang.org/x/net v0.22.0 // indirect - golang.org/x/sys v0.18.0 // indirect + golang.org/x/sync v0.6.0 // indirect golang.org/x/text v0.14.0 // indirect golang.org/x/tools v0.19.0 // indirect - google.golang.org/protobuf v1.33.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 402d2f6..96e33e2 100644 --- a/go.sum +++ b/go.sum @@ -1,28 +1,10 @@ github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= -github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= -github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= -github.com/bytedance/sonic v1.11.3 h1:jRN+yEjakWh8aK5FzrciUHG8OFXK+4/KrAX/ysEtHAA= -github.com/bytedance/sonic v1.11.3/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= github.com/caarlos0/env/v6 v6.10.1 h1:t1mPSxNpei6M5yAeu1qtRdPAK29Nbcf/n3G7x+b3/II= github.com/caarlos0/env/v6 v6.10.1/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc= -github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= -github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= -github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0= -github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA= -github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= -github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0= -github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -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/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4= -github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= -github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= -github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= -github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ= github.com/go-openapi/jsonpointer v0.21.0/go.mod h1:IUyH9l/+uyhIYQ/PXVA41Rexl+kOkAPDdXEYns6fzUY= github.com/go-openapi/jsonreference v0.21.0 h1:Rs+Y7hSXT83Jacb7kFyjn4ijOuVGSvOdF2+tg1TRrwQ= @@ -31,18 +13,6 @@ github.com/go-openapi/spec v0.21.0 h1:LTVzPc3p/RzRnkQqLRndbAzjY0d0BCL72A6j3CdL9Z github.com/go-openapi/spec v0.21.0/go.mod h1:78u6VdPw81XU44qEWGhtr982gJ5BWg2c0I5XwVMotYk= github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE= github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ= -github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= -github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= -github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= -github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= -github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= -github.com/go-playground/validator/v10 v10.19.0 h1:ol+5Fu+cSq9JD7SoSqe04GMI92cbn0+wvQ3bZ8b/AU4= -github.com/go-playground/validator/v10 v10.19.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= -github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= -github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= -github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= -github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA= @@ -59,62 +29,35 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= -github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= -github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= -github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= -github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= -github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= -github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= -github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= -github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= -github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= -github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= -github.com/pelletier/go-toml/v2 v2.2.0 h1:QLgLl2yMN7N+ruc31VynXs1vhMZa7CeHHejIeBAsoHo= -github.com/pelletier/go-toml/v2 v2.2.0/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/swaggo/files v1.0.1 h1:J1bVJ4XHZNq0I46UU90611i9/YzdrF7x92oX1ig5IdE= github.com/swaggo/files v1.0.1/go.mod h1:0qXmMNH6sXNf+73t65aKeB+ApmgxdnkQzVTAj2uaMUg= -github.com/swaggo/gin-swagger v1.6.0 h1:y8sxvQ3E20/RCyrXeFfg60r6H0Z+SwpTjMYsMm+zy8M= -github.com/swaggo/gin-swagger v1.6.0/go.mod h1:BG00cCEy294xtVpyIAHG6+e2Qzj/xKlRdOqDkvq0uzo= +github.com/swaggo/http-swagger v1.3.4 h1:q7t/XLx0n15H1Q9/tk3Y9L4n210XzJF5WtnDX64a5ww= +github.com/swaggo/http-swagger v1.3.4/go.mod h1:9dAh0unqMBAlbp1uE2Uc2mQTxNMU/ha4UbucIg1MFkQ= github.com/swaggo/swag v1.16.3 h1:PnCYjPCah8FK4I26l2F/KQ4yz3sILcVUN3cTlBFA9Pg= github.com/swaggo/swag v1.16.3/go.mod h1:DImHIuOFXKpMFAQjcC7FG4m3Dg4+QuUgUzJmKjI/gRk= -github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= -github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= -github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= -github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= -golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc= -golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic= +golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= @@ -131,9 +74,6 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= -golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= @@ -149,16 +89,13 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw= golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= -google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/driver/postgres v1.5.7 h1:8ptbNJTDbEmhdr62uReG5BGkdQyeasu/FZHxI0IMGnM= gorm.io/driver/postgres v1.5.7/go.mod h1:3e019WlBaYI5o5LIdNV+LyxCMNtLOQETBXL2h4chKpA= -gorm.io/gorm v1.25.8 h1:WAGEZ/aEcznN4D03laj8DKnehe1e9gYQAjW8xyPRdeo= -gorm.io/gorm v1.25.8/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= -nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= -rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= +gorm.io/gorm v1.25.9 h1:wct0gxZIELDk8+ZqF/MVnHLkA1rvYlBWUMv2EdsK1g8= +gorm.io/gorm v1.25.9/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= diff --git a/main.go b/main.go index b159119..4743472 100644 --- a/main.go +++ b/main.go @@ -2,13 +2,13 @@ package main import ( "fmt" + "net/http" - "github.com/gin-gonic/gin" - swaggerFiles "github.com/swaggo/files" - ginSwagger "github.com/swaggo/gin-swagger" + "github.com/swaggo/http-swagger" "gitlab.com/DeveloperDurp/DurpAPI/controller" "gitlab.com/DeveloperDurp/DurpAPI/docs" + "gitlab.com/DeveloperDurp/DurpAPI/middleware" ) // @title DurpAPI @@ -23,39 +23,37 @@ import ( // @license.url http://www.apache.org/licenses/LICENSE-2.0.html // @BasePath /api -// @securityDefinitions.apikey ApiKeyAuth +// @securityDefinitions.apikey Authorization // @in header // @name Authorization func main() { - r := gin.Default() c := controller.NewController() docs.SwaggerInfo.Host = c.Cfg.Host docs.SwaggerInfo.Version = c.Cfg.Version - v1 := r.Group("/api") - { - health := v1.Group("/health") - { - health.GET("getHealth", c.GetHealth) - } - jokes := v1.Group("/jokes") - { - jokes.GET("dadjoke", c.GetDadJoke) - jokes.POST("dadjoke", c.PostDadJoke) - jokes.DELETE("dadjoke", c.DeleteDadJoke) - } - openai := v1.Group("/openai") - { - openai.GET("general", c.GeneralOpenAI) - openai.GET("travelagent", c.TravelAgentOpenAI) - } - } - r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) + router := http.NewServeMux() + router.HandleFunc("/swagger/*", httpSwagger.Handler()) + router.HandleFunc("GET /api/health/gethealth", c.GetHealth) + router.HandleFunc("GET /api/jokes/dadjoke", c.GetDadJoke) + router.HandleFunc("POST /api/jokes/dadjoke", c.PostDadJoke) + router.HandleFunc("DELETE /api/jokes/dadjoke", c.DeleteDadJoke) + router.HandleFunc("GET /api/openai/general", c.GeneralOpenAI) + router.HandleFunc("GET /api/openai/travelagent", c.TravelAgentOpenAI) + // adminRouter := http.NewServeMux() - err := r.Run(":8080") - if err != nil { - fmt.Println("Failed to start server") + // router.Handle("/", middleware.EnsureAdmin(adminRouter)) + + stack := middleware.CreateStack( + middleware.Logging, + ) + + server := http.Server{ + Addr: ":8080", + Handler: stack(router), } + + fmt.Println("Server listening on port :8080") + server.ListenAndServe() } diff --git a/middleware/auth.go b/middleware/auth.go new file mode 100644 index 0000000..67f9687 --- /dev/null +++ b/middleware/auth.go @@ -0,0 +1,17 @@ +package middleware + +import ( + "net/http" + "strings" +) + +func EnsureAdmin(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !strings.Contains(r.Header.Get("Authorization"), "Admin") { + w.WriteHeader(http.StatusUnauthorized) + w.Write([]byte(http.StatusText(http.StatusUnauthorized))) + return + } + next.ServeHTTP(w, r) + }) +} diff --git a/middleware/logging.go b/middleware/logging.go new file mode 100644 index 0000000..4441935 --- /dev/null +++ b/middleware/logging.go @@ -0,0 +1,32 @@ +package middleware + +import ( + "log" + "net/http" + "time" +) + +type wrappedWriter struct { + http.ResponseWriter + statusCode int +} + +func (w *wrappedWriter) WriteHeader(statusCode int) { + w.ResponseWriter.WriteHeader(statusCode) + w.statusCode = statusCode +} + +func Logging(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + start := time.Now() + + wrapped := &wrappedWriter{ + ResponseWriter: w, + statusCode: http.StatusOK, + } + + next.ServeHTTP(wrapped, r) + + log.Println(wrapped.statusCode, r.Method, r.URL.Path, time.Since(start)) + }) +} diff --git a/middleware/middleware.go b/middleware/middleware.go new file mode 100644 index 0000000..387301b --- /dev/null +++ b/middleware/middleware.go @@ -0,0 +1,16 @@ +package middleware + +import "net/http" + +type Middleware func(http.Handler) http.Handler + +func CreateStack(xs ...Middleware) Middleware { + return func(next http.Handler) http.Handler { + for i := len(xs) - 1; i >= 0; i-- { + x := xs[i] + next = x(next) + } + + return next + } +} diff --git a/model/unraid.go b/model/unraid.go deleted file mode 100644 index 973b4e6..0000000 --- a/model/unraid.go +++ /dev/null @@ -1,23 +0,0 @@ -package model - -type PowerSupply struct { - TwelveVoltLoad int `json:"12v_load"` - TwelveVoltWatts int `json:"12v_watts"` - ThreeVoltLoad int `json:"3v_load"` - ThreeVoltWatts int `json:"3v_watts"` - FiveVoltLoad int `json:"5v_load"` - FiveVoltWatts int `json:"5v_watts"` - Capacity string `json:"capacity"` - Efficiency int `json:"efficiency"` - FanRPM int `json:"fan_rpm"` - Load int `json:"load"` - PoweredOn string `json:"poweredon"` - PoweredOnRaw string `json:"poweredon_raw"` - Product string `json:"product"` - Temp1 int `json:"temp1"` - Temp2 int `json:"temp2"` - Uptime string `json:"uptime"` - UptimeRaw string `json:"uptime_raw"` - Vendor string `json:"vendor"` - Watts int `json:"watts"` -}