add godotenv, regenerate swagger docs

This commit is contained in:
zardzul
2026-03-13 22:23:42 +01:00
parent 82e1b51d38
commit 873cd6a57d
12 changed files with 71 additions and 48 deletions
+2 -2
View File
@@ -3,9 +3,9 @@
Music collection software for indexing bought (physical) albums. Build with Golang and Postgres Music collection software for indexing bought (physical) albums. Build with Golang and Postgres
## Swagger ## Swagger
The API documentation is available at `/swagger/index.html` after running the application. It provides details about the available endpoints, request/response formats, and other relevant information for developers to interact with the API effectively. The API documentation is available at [/api/v1/swagger/index.html](http://localhost:8080/api/v1/swagger/index.html) after running the application. It provides details about the available endpoints, request/response formats, and other relevant information for developers to interact with the API effectively.
Regenerate the docs with the following command in the /api/ folder: Regenerate the docs with the following command in the /api/ folder:
``` ```
$ go run github.com/swaggo/swag/cmd/swag@v1.16.4 init -g main.go -o docs go run github.com/swaggo/swag/cmd/swag@v1.16.4 init -g main.go -o docs
``` ```
+1 -4
View File
@@ -1,5 +1,2 @@
# Postgres # Postgres
POSTGRES_HOST= DATABASE_URL=postgresql://user:password@host:5432/database
POSTGRES_USER=
POSTGRES_PASSWORD=
POSTGRES_DB=
+17
View File
@@ -0,0 +1,17 @@
FROM golang:1.26.1-alpine3.23 AS builder
WORKDIR /build
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o music-index
# Production stage
FROM alpine:3.21.3
WORKDIR /app
COPY --from=builder /build/music-index /app/music-index
EXPOSE 8080
ENTRYPOINT ["/app/music-index"]
+1 -1
View File
@@ -17,7 +17,7 @@ func Connect() (*pgxpool.Pool, *db.Queries, error) {
databaseURL := os.Getenv("DATABASE_URL") databaseURL := os.Getenv("DATABASE_URL")
if databaseURL == "" { if databaseURL == "" {
databaseURL = "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable" panic("DATABASE_URL environment variable not set")
} }
pool, err := pgxpool.New(ctx, databaseURL) pool, err := pgxpool.New(ctx, databaseURL)
+4 -4
View File
@@ -15,7 +15,7 @@ const docTemplate = `{
"host": "{{.Host}}", "host": "{{.Host}}",
"basePath": "{{.BasePath}}", "basePath": "{{.BasePath}}",
"paths": { "paths": {
"/artist/getAll": { "/artists": {
"get": { "get": {
"produces": [ "produces": [
"application/json" "application/json"
@@ -44,7 +44,7 @@ const docTemplate = `{
} }
} }
}, },
"/user/create": { "/users/create": {
"post": { "post": {
"consumes": [ "consumes": [
"application/json" "application/json"
@@ -89,7 +89,7 @@ const docTemplate = `{
} }
} }
}, },
"/user/getUserNameByID/{id}": { "/users/{id}": {
"get": { "get": {
"produces": [ "produces": [
"application/json" "application/json"
@@ -185,7 +185,7 @@ const docTemplate = `{
var SwaggerInfo = &swag.Spec{ var SwaggerInfo = &swag.Spec{
Version: "1.0", Version: "1.0",
Host: "", Host: "",
BasePath: "/", BasePath: "/api/v1/",
Schemes: []string{}, Schemes: []string{},
Title: "Music Index API", Title: "Music Index API",
Description: "API for managing users and artists in Music Index.", Description: "API for managing users and artists in Music Index.",
+4 -4
View File
@@ -6,9 +6,9 @@
"contact": {}, "contact": {},
"version": "1.0" "version": "1.0"
}, },
"basePath": "/", "basePath": "/api/v1/",
"paths": { "paths": {
"/artist/getAll": { "/artists": {
"get": { "get": {
"produces": [ "produces": [
"application/json" "application/json"
@@ -37,7 +37,7 @@
} }
} }
}, },
"/user/create": { "/users/create": {
"post": { "post": {
"consumes": [ "consumes": [
"application/json" "application/json"
@@ -82,7 +82,7 @@
} }
} }
}, },
"/user/getUserNameByID/{id}": { "/users/{id}": {
"get": { "get": {
"produces": [ "produces": [
"application/json" "application/json"
+29 -29
View File
@@ -1,4 +1,4 @@
basePath: / basePath: /api/v1/
definitions: definitions:
handlers.CreateUserRequest: handlers.CreateUserRequest:
properties: properties:
@@ -37,7 +37,7 @@ info:
title: Music Index API title: Music Index API
version: "1.0" version: "1.0"
paths: paths:
/artist/getAll: /artists:
get: get:
produces: produces:
- application/json - application/json
@@ -56,7 +56,33 @@ paths:
summary: List all artists summary: List all artists
tags: tags:
- artist - artist
/user/create: /users/{id}:
get:
parameters:
- description: User ID (UUID)
in: path
name: id
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/handlers.UsernameResponse'
"400":
description: Bad Request
schema:
$ref: '#/definitions/handlers.ErrorResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/handlers.ErrorResponse'
summary: Get username by user ID
tags:
- user
/users/create:
post: post:
consumes: consumes:
- application/json - application/json
@@ -85,30 +111,4 @@ paths:
summary: Create a user summary: Create a user
tags: tags:
- user - user
/user/getUserNameByID/{id}:
get:
parameters:
- description: User ID (UUID)
in: path
name: id
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/handlers.UsernameResponse'
"400":
description: Bad Request
schema:
$ref: '#/definitions/handlers.ErrorResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/handlers.ErrorResponse'
summary: Get username by user ID
tags:
- user
swagger: "2.0" swagger: "2.0"
+1
View File
@@ -33,6 +33,7 @@ require (
github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/josharian/intern v1.0.0 // indirect github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.3.0 // indirect github.com/klauspost/cpuid/v2 v2.3.0 // indirect
+2
View File
@@ -59,6 +59,8 @@ github.com/jackc/pgx/v5 v5.8.0 h1:TYPDoleBBme0xGSAX3/+NujXXtpZn9HBONkQC7IEZSo=
github.com/jackc/pgx/v5 v5.8.0/go.mod h1:QVeDInX2m9VyzvNeiCJVjCkNFqzsNb43204HshNSZKw= github.com/jackc/pgx/v5 v5.8.0/go.mod h1:QVeDInX2m9VyzvNeiCJVjCkNFqzsNb43204HshNSZKw=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
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 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= 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 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
+1 -1
View File
@@ -21,7 +21,7 @@ func NewArtistHandler(repo repository.ArtistRepository) *ArtistHandler {
// @Produce json // @Produce json
// @Success 200 {array} map[string]interface{} // @Success 200 {array} map[string]interface{}
// @Failure 500 {object} ErrorResponse // @Failure 500 {object} ErrorResponse
// @Router /api/v1/artists [get] // @Router /artists [get]
func (handler *ArtistHandler) GetAll(c *gin.Context) { func (handler *ArtistHandler) GetAll(c *gin.Context) {
artists, err := handler.repo.GetAll(c.Request.Context()) artists, err := handler.repo.GetAll(c.Request.Context())
if err != nil { if err != nil {
+2 -2
View File
@@ -51,7 +51,7 @@ type ErrorResponse struct {
// @Success 201 {object} CreateUserResponse // @Success 201 {object} CreateUserResponse
// @Failure 400 {object} ErrorResponse // @Failure 400 {object} ErrorResponse
// @Failure 500 {object} ErrorResponse // @Failure 500 {object} ErrorResponse
// @Router /api/v1/users/create [post] // @Router /users/create [post]
func (h *UserHandler) CreateUser(c *gin.Context) { func (h *UserHandler) CreateUser(c *gin.Context) {
var req CreateUserRequest var req CreateUserRequest
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBindJSON(&req); err != nil {
@@ -90,7 +90,7 @@ func (h *UserHandler) CreateUser(c *gin.Context) {
// @Success 200 {object} UsernameResponse // @Success 200 {object} UsernameResponse
// @Failure 400 {object} ErrorResponse // @Failure 400 {object} ErrorResponse
// @Failure 500 {object} ErrorResponse // @Failure 500 {object} ErrorResponse
// @Router /api/v1/users/{id} [get] // @Router /users/{id} [get]
func (h *UserHandler) GetUsernameByID(c *gin.Context) { func (h *UserHandler) GetUsernameByID(c *gin.Context) {
idParam := c.Param("id") idParam := c.Param("id")
var pgID pgtype.UUID var pgID pgtype.UUID
+7 -1
View File
@@ -8,14 +8,20 @@ import (
"zardzul/music-index/routes" "zardzul/music-index/routes"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/joho/godotenv"
) )
// @title Music Index API // @title Music Index API
// @version 1.0 // @version 1.0
// @description API for managing users and artists in Music Index. // @description API for managing users and artists in Music Index.
// @BasePath / // @BasePath /api/v1/
func main() { func main() {
err := godotenv.Load(".env")
if err != nil {
panic("Error loading .env file")
}
pool, queries, databaseError := database.Connect() pool, queries, databaseError := database.Connect()
if databaseError != nil { if databaseError != nil {
panic(databaseError) panic(databaseError)