This commit is contained in:
2024-05-09 17:57:32 -05:00
parent a69d2c7a40
commit 5043720f95
10 changed files with 121 additions and 45 deletions

View File

@@ -1,5 +1,31 @@
include: stages:
- local: .variables.yml - deploy
- project: 'developerdurp/yml'
ref: main variables:
file: 'pipeline.yml' GO_VERSION: "1.22"
GOLANGCI_LINT_VERISON: "v1.58.0"
UPLOAD_PACKAGE: "false"
build_go:
stage: deploy
allow_failure: false
trigger:
include:
- project: 'developerdurp/yml'
ref: 'main'
file:
- 'pipelines/go-build.yml'
strategy: depend
rules:
- exists:
- "go.mod"
deploy_staging:
stage: deploy
needs:
- job: build_go
script:
- echo "Deploy to staging server"
environment:
name: staging

View File

@@ -1,4 +1,4 @@
start: start:
docker run --name postgres-db -e POSTGRES_PASSWORD=docker -p 5432:5432 -d postgres sudo docker run --name postgres-db -e POSTGRES_PASSWORD=docker -p 5432:5432 -d postgres
stop: stop:
docker rm postgres-db -f sudo docker rm postgres-db -f

View File

@@ -6,6 +6,8 @@ import (
"gitlab.com/DeveloperDurp/DurpAPI/model" "gitlab.com/DeveloperDurp/DurpAPI/model"
"gitlab.com/DeveloperDurp/DurpAPI/service" "gitlab.com/DeveloperDurp/DurpAPI/service"
"gitlab.com/developerdurp/logger"
"gitlab.com/developerdurp/stdmodels"
) )
// GetDadJoke godoc // GetDadJoke godoc
@@ -16,16 +18,18 @@ import (
// @Accept json // @Accept json
// @Produce application/json // @Produce application/json
// @Success 200 {object} model.Message "response" // @Success 200 {object} model.Message "response"
// @failure 500 {object} model.Message "error" // @failure 500 {object} stdmodels.StandardError"error"
// //
// @Security Authorization // @Security Authorization
// //
// @Router /jokes/dadjoke [get] // @Router /jokes/dadjoke [get]
func (c *Controller) GetDadJoke(w http.ResponseWriter, r *http.Request) { func (c *Controller) GetDadJoke(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
joke, err := service.GetRandomDadJoke(c.Db.DB) joke, err := service.GetRandomDadJoke(c.Db.DB)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) stdmodels.FailureReponse("Failed to get Joke", w, http.StatusInternalServerError, []string{err.Error()})
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return return
} }
@@ -33,7 +37,6 @@ func (c *Controller) GetDadJoke(w http.ResponseWriter, r *http.Request) {
Message: joke, Message: joke,
} }
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(message) json.NewEncoder(w).Encode(message)
} }
@@ -46,7 +49,7 @@ func (c *Controller) GetDadJoke(w http.ResponseWriter, r *http.Request) {
// @Produce application/json // @Produce application/json
// @Param joke query string true "Dad Joke you wish to enter into database" // @Param joke query string true "Dad Joke you wish to enter into database"
// @Success 200 {object} model.Message "response" // @Success 200 {object} model.Message "response"
// @failure 500 {object} model.Message "error" // @failure 500 {object} stdmodels.StandardError"error"
// //
// @Security Authorization // @Security Authorization
// //
@@ -58,8 +61,7 @@ func (c *Controller) PostDadJoke(w http.ResponseWriter, r *http.Request) {
if contentType == "application/json" { if contentType == "application/json" {
err := json.NewDecoder(r.Body).Decode(&req) err := json.NewDecoder(r.Body).Decode(&req)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) logger.LogError("Failed to decode json file")
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return return
} }
} else { } else {
@@ -69,17 +71,11 @@ func (c *Controller) PostDadJoke(w http.ResponseWriter, r *http.Request) {
err := service.PostDadJoke(c.Db.DB, req) err := service.PostDadJoke(c.Db.DB, req)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) stdmodels.FailureReponse("Failed to add joke", w, http.StatusInternalServerError, []string{err.Error()})
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return return
} }
message := model.Message{ stdmodels.SuccessResponse("OK", w, http.StatusOK)
Message: "OK",
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(message)
} }
// DeleteDadJoke godoc // DeleteDadJoke godoc
@@ -91,7 +87,7 @@ func (c *Controller) PostDadJoke(w http.ResponseWriter, r *http.Request) {
// @Produce application/json // @Produce application/json
// @Param joke query string true "Dad joke you wish to delete from the database" // @Param joke query string true "Dad joke you wish to delete from the database"
// @Success 200 {object} model.Message "response" // @Success 200 {object} model.Message "response"
// @failure 500 {object} model.Message "error" // @failure 500 {object} stdmodels.StandardError"error"
// //
// @Security Authorization // @Security Authorization
// //

View File

@@ -1,10 +1,10 @@
package controller package controller
import ( import (
"encoding/json"
"net/http" "net/http"
"gitlab.com/DeveloperDurp/DurpAPI/model" "gitlab.com/developerdurp/logger"
"gitlab.com/developerdurp/stdmodels"
) )
// getHealth godoc // getHealth godoc
@@ -21,10 +21,6 @@ import (
// //
// @Router /health/gethealth [get] // @Router /health/gethealth [get]
func (c *Controller) GetHealth(w http.ResponseWriter, r *http.Request) { func (c *Controller) GetHealth(w http.ResponseWriter, r *http.Request) {
message := model.Message{ logger.LogInfo("Health Check")
Message: "OK", stdmodels.SuccessResponse("OK", w, http.StatusOK)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(message)
} }

View File

@@ -86,7 +86,7 @@ const docTemplate = `{
"500": { "500": {
"description": "error", "description": "error",
"schema": { "schema": {
"$ref": "#/definitions/model.Message" "$ref": "#/definitions/stdmodels.StandardError"
} }
} }
} }
@@ -127,7 +127,7 @@ const docTemplate = `{
"500": { "500": {
"description": "error", "description": "error",
"schema": { "schema": {
"$ref": "#/definitions/model.Message" "$ref": "#/definitions/stdmodels.StandardError"
} }
} }
} }
@@ -168,7 +168,7 @@ const docTemplate = `{
"500": { "500": {
"description": "error", "description": "error",
"schema": { "schema": {
"$ref": "#/definitions/model.Message" "$ref": "#/definitions/stdmodels.StandardError"
} }
} }
} }
@@ -270,6 +270,23 @@ const docTemplate = `{
"example": "message" "example": "message"
} }
} }
},
"stdmodels.StandardError": {
"type": "object",
"properties": {
"description": {
"type": "array",
"items": {
"type": "string"
}
},
"message": {
"type": "string"
},
"status": {
"type": "integer"
}
}
} }
}, },
"securityDefinitions": { "securityDefinitions": {
@@ -285,7 +302,7 @@ const docTemplate = `{
var SwaggerInfo = &swag.Spec{ var SwaggerInfo = &swag.Spec{
Version: "", Version: "",
Host: "", Host: "",
BasePath: "/api", BasePath: "/",
Schemes: []string{}, Schemes: []string{},
Title: "DurpAPI", Title: "DurpAPI",
Description: "API for Durp's needs", Description: "API for Durp's needs",

View File

@@ -14,7 +14,7 @@
"url": "http://www.apache.org/licenses/LICENSE-2.0.html" "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
} }
}, },
"basePath": "/api", "basePath": "/",
"paths": { "paths": {
"/health/gethealth": { "/health/gethealth": {
"get": { "get": {
@@ -78,7 +78,7 @@
"500": { "500": {
"description": "error", "description": "error",
"schema": { "schema": {
"$ref": "#/definitions/model.Message" "$ref": "#/definitions/stdmodels.StandardError"
} }
} }
} }
@@ -119,7 +119,7 @@
"500": { "500": {
"description": "error", "description": "error",
"schema": { "schema": {
"$ref": "#/definitions/model.Message" "$ref": "#/definitions/stdmodels.StandardError"
} }
} }
} }
@@ -160,7 +160,7 @@
"500": { "500": {
"description": "error", "description": "error",
"schema": { "schema": {
"$ref": "#/definitions/model.Message" "$ref": "#/definitions/stdmodels.StandardError"
} }
} }
} }
@@ -262,6 +262,23 @@
"example": "message" "example": "message"
} }
} }
},
"stdmodels.StandardError": {
"type": "object",
"properties": {
"description": {
"type": "array",
"items": {
"type": "string"
}
},
"message": {
"type": "string"
},
"status": {
"type": "integer"
}
}
} }
}, },
"securityDefinitions": { "securityDefinitions": {

View File

@@ -1,4 +1,4 @@
basePath: /api basePath: /
definitions: definitions:
model.Message: model.Message:
properties: properties:
@@ -6,6 +6,17 @@ definitions:
example: message example: message
type: string type: string
type: object type: object
stdmodels.StandardError:
properties:
description:
items:
type: string
type: array
message:
type: string
status:
type: integer
type: object
info: info:
contact: contact:
email: developerdurp@durp.info email: developerdurp@durp.info
@@ -60,7 +71,7 @@ paths:
"500": "500":
description: error description: error
schema: schema:
$ref: '#/definitions/model.Message' $ref: '#/definitions/stdmodels.StandardError'
security: security:
- Authorization: [] - Authorization: []
summary: Generate dadjoke summary: Generate dadjoke
@@ -80,7 +91,7 @@ paths:
"500": "500":
description: error description: error
schema: schema:
$ref: '#/definitions/model.Message' $ref: '#/definitions/stdmodels.StandardError'
security: security:
- Authorization: [] - Authorization: []
summary: Get dadjoke summary: Get dadjoke
@@ -106,7 +117,7 @@ paths:
"500": "500":
description: error description: error
schema: schema:
$ref: '#/definitions/model.Message' $ref: '#/definitions/stdmodels.StandardError'
security: security:
- Authorization: [] - Authorization: []
summary: Generate dadjoke summary: Generate dadjoke

2
go.mod
View File

@@ -26,6 +26,8 @@ require (
github.com/josharian/intern v1.0.0 // indirect github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect github.com/mailru/easyjson v0.7.7 // indirect
github.com/swaggo/files v1.0.1 // indirect github.com/swaggo/files v1.0.1 // indirect
gitlab.com/developerdurp/logger v1.0.0 // indirect
gitlab.com/developerdurp/stdmodels v1.0.0 // indirect
golang.org/x/crypto v0.21.0 // indirect golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.22.0 // indirect golang.org/x/net v0.22.0 // indirect
golang.org/x/sync v0.6.0 // indirect golang.org/x/sync v0.6.0 // indirect

4
go.sum
View File

@@ -51,6 +51,10 @@ github.com/swaggo/http-swagger v1.3.4/go.mod h1:9dAh0unqMBAlbp1uE2Uc2mQTxNMU/ha4
github.com/swaggo/swag v1.16.3 h1:PnCYjPCah8FK4I26l2F/KQ4yz3sILcVUN3cTlBFA9Pg= 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/swaggo/swag v1.16.3/go.mod h1:DImHIuOFXKpMFAQjcC7FG4m3Dg4+QuUgUzJmKjI/gRk=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
gitlab.com/developerdurp/logger v1.0.0 h1:wozbKR26RVoFVaUgJV2x8WsZHLWOJBqaSCSTpK7crfk=
gitlab.com/developerdurp/logger v1.0.0/go.mod h1:x6gZvBeEq8oQUXeQoLY2m78mYJjvb5KE+7Vb5AcS8oo=
gitlab.com/developerdurp/stdmodels v1.0.0 h1:LsFyEPGVUUOhiXxN/CyHLnV/cPpvLtbf0zxeFACYbTk=
gitlab.com/developerdurp/stdmodels v1.0.0/go.mod h1:RRLS9Wek0YYMy3Wz0pWhfYyYcu52vr06KPFVxQ6g0OU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 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.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 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=

View File

@@ -1,7 +1,7 @@
package middleware package middleware
import ( import (
"log" "log/slog"
"net/http" "net/http"
"time" "time"
) )
@@ -27,6 +27,13 @@ func Logging(next http.Handler) http.Handler {
next.ServeHTTP(wrapped, r) next.ServeHTTP(wrapped, r)
log.Println(wrapped.statusCode, r.Method, r.URL.Path, time.Since(start)) slog.Info(
"Health Check",
slog.Int("Method", wrapped.statusCode),
r.Method,
r.URL.Path,
slog.String("time", time.Since(start).String()),
)
// log.Println("INFO", wrapped.statusCode, r.Method, r.URL.Path, time.Since(start))
}) })
} }