Compare commits

...

5 Commits

9 changed files with 74 additions and 13 deletions

20
Dockerfile Normal file
View File

@@ -0,0 +1,20 @@
FROM golang:1.25.3-alpine AS builder
WORKDIR /app
COPY . .
# Build
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux go build -a -o main ./cmd/server
###########
FROM alpine:3.22.2
RUN apk --no-cache add ca-certificates
WORKDIR /root/
# Copy the pre-built binary
COPY --from=builder /app/main .
EXPOSE 8080
CMD ["./main"]

View File

@@ -7,6 +7,7 @@ import (
_ "gitea.gabilandia.com/gabdlr/agenda-web-go/docs" _ "gitea.gabilandia.com/gabdlr/agenda-web-go/docs"
"gitea.gabilandia.com/gabdlr/agenda-web-go/internal/database" "gitea.gabilandia.com/gabdlr/agenda-web-go/internal/database"
"gitea.gabilandia.com/gabdlr/agenda-web-go/internal/handler" "gitea.gabilandia.com/gabdlr/agenda-web-go/internal/handler"
"gitea.gabilandia.com/gabdlr/agenda-web-go/internal/middleware"
"gitea.gabilandia.com/gabdlr/agenda-web-go/internal/repository" "gitea.gabilandia.com/gabdlr/agenda-web-go/internal/repository"
httpSwagger "github.com/swaggo/http-swagger" httpSwagger "github.com/swaggo/http-swagger"
) )
@@ -17,12 +18,12 @@ import (
// @termsOfService http://swagger.io/terms/ // @termsOfService http://swagger.io/terms/
// @contact.name API Support // @contact.name API Support
// @contact.email support@yourapp.com // @contact.email gabriel.delosrios@tutamail.com
// @license.name MIT // @license.name MIT
// @license.url https://opensource.org/licenses/MIT // @license.url https://opensource.org/licenses/MIT
// @host localhost:8080 // @host agenda-web-go.gabilandia.com
// @BasePath / // @BasePath /
func main() { func main() {
err := database.InitDB() err := database.InitDB()
@@ -36,5 +37,5 @@ func main() {
handler.HandleContacts(mux, contactRepo) handler.HandleContacts(mux, contactRepo)
handler.HandlHealthChecks(mux) handler.HandlHealthChecks(mux)
mux.HandleFunc("/swagger/", httpSwagger.WrapHandler) mux.HandleFunc("/swagger/", httpSwagger.WrapHandler)
log.Fatal(http.ListenAndServe(":8080", mux)) log.Fatal(http.ListenAndServe(":8080", middleware.CorsMiddleware(mux)))
} }

23
docker-compose.yml Normal file
View File

@@ -0,0 +1,23 @@
services:
go-app:
build: .
container_name: agenda-web-go
environment:
- DB_NAME=${DB_NAME}
- DB_USER=${DB_USER}
- DB_PASSWORD=${DB_PASSWORD}
- DB_HOST=${DB_HOST}
- DB_PORT=${DB_PORT}
- ALLOWED_ORIGIN=${ALLOWED_ORIGIN}
networks:
- db-network
volumes:
#- ./cache:/root/.cache
- ./:/app
restart: unless-stopped
ports:
- 3005:8080
networks:
db-network:
external: true

View File

@@ -12,7 +12,7 @@ const docTemplate = `{
"termsOfService": "http://swagger.io/terms/", "termsOfService": "http://swagger.io/terms/",
"contact": { "contact": {
"name": "API Support", "name": "API Support",
"email": "support@yourapp.com" "email": "gabriel.delosrios@tutamail.com"
}, },
"license": { "license": {
"name": "MIT", "name": "MIT",
@@ -364,7 +364,7 @@ const docTemplate = `{
// SwaggerInfo holds exported Swagger Info so clients can modify it // SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{ var SwaggerInfo = &swag.Spec{
Version: "1.0", Version: "1.0",
Host: "localhost:8080", Host: "agenda-web-go.gabilandia.com",
BasePath: "/", BasePath: "/",
Schemes: []string{}, Schemes: []string{},
Title: "Contacts API", Title: "Contacts API",

View File

@@ -6,7 +6,7 @@
"termsOfService": "http://swagger.io/terms/", "termsOfService": "http://swagger.io/terms/",
"contact": { "contact": {
"name": "API Support", "name": "API Support",
"email": "support@yourapp.com" "email": "gabriel.delosrios@tutamail.com"
}, },
"license": { "license": {
"name": "MIT", "name": "MIT",
@@ -14,7 +14,7 @@
}, },
"version": "1.0" "version": "1.0"
}, },
"host": "localhost:8080", "host": "agenda-web-go.gabilandia.com",
"basePath": "/", "basePath": "/",
"paths": { "paths": {
"/contacts": { "/contacts": {

View File

@@ -47,10 +47,10 @@ definitions:
description: Phone number in international format description: Phone number in international format
type: string type: string
type: object type: object
host: localhost:8080 host: agenda-web-go.gabilandia.com
info: info:
contact: contact:
email: support@yourapp.com email: gabriel.delosrios@tutamail.com
name: API Support name: API Support
description: A simple Contacts CRUD API description: A simple Contacts CRUD API
license: license:

View File

@@ -87,9 +87,7 @@ func ensureTablesExist() error {
id INT AUTO_INCREMENT PRIMARY KEY, id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(120) NOT NULL, name VARCHAR(120) NOT NULL,
company VARCHAR(120) NOT NULL, company VARCHAR(120) NOT NULL,
phone VARCHAR(15) NOT NULL, phone VARCHAR(15) NOT NULL
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; ) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
`) `)

View File

@@ -9,7 +9,7 @@ type APIResponse struct {
// Indicates if the request was successful // Indicates if the request was successful
Success bool `json:"success"` Success bool `json:"success"`
// The response data // The response data
Data any `json:"data,omitempty"` Data any `json:"data"`
// List of errors if any occurred // List of errors if any occurred
Errors []APIError `json:"errors,omitempty"` Errors []APIError `json:"errors,omitempty"`
// Optional message // Optional message

View File

@@ -0,0 +1,19 @@
package middleware
import (
"net/http"
"os"
)
func CorsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", os.Getenv("ALLOWED_ORIGIN"))
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}