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

View File

@@ -1,10 +1,10 @@
package controller
import (
"encoding/json"
"net/http"
"gitlab.com/DeveloperDurp/DurpAPI/model"
"gitlab.com/developerdurp/logger"
"gitlab.com/developerdurp/stdmodels"
)
// getHealth godoc
@@ -21,10 +21,6 @@ import (
//
// @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)
logger.LogInfo("Health Check")
stdmodels.SuccessResponse("OK", w, http.StatusOK)
}

View File

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

View File

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

View File

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

2
go.mod
View File

@@ -26,6 +26,8 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // 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/net v0.22.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/go.mod h1:DImHIuOFXKpMFAQjcC7FG4m3Dg4+QuUgUzJmKjI/gRk=
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-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=

View File

@@ -1,7 +1,7 @@
package middleware
import (
"log"
"log/slog"
"net/http"
"time"
)
@@ -27,6 +27,13 @@ func Logging(next http.Handler) http.Handler {
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))
})
}