diff --git a/README.md b/README.md index 794c1e0..9762813 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,9 @@ Music collection software for indexing bought (physical) albums. Build with Golang and Postgres ## 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: ``` -$ 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 ``` \ No newline at end of file diff --git a/api/.env.example b/api/.env.example index 51fd3ed..0e8ac2b 100644 --- a/api/.env.example +++ b/api/.env.example @@ -1,5 +1,2 @@ # Postgres -POSTGRES_HOST= -POSTGRES_USER= -POSTGRES_PASSWORD= -POSTGRES_DB= \ No newline at end of file +DATABASE_URL=postgresql://user:password@host:5432/database \ No newline at end of file diff --git a/api/Dockerfile b/api/Dockerfile new file mode 100644 index 0000000..2f5ca09 --- /dev/null +++ b/api/Dockerfile @@ -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"] \ No newline at end of file diff --git a/api/database/database.go b/api/database/database.go index 82c518d..3dd025a 100644 --- a/api/database/database.go +++ b/api/database/database.go @@ -17,7 +17,7 @@ func Connect() (*pgxpool.Pool, *db.Queries, error) { databaseURL := os.Getenv("DATABASE_URL") if databaseURL == "" { - databaseURL = "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable" + panic("DATABASE_URL environment variable not set") } pool, err := pgxpool.New(ctx, databaseURL) diff --git a/api/docs/docs.go b/api/docs/docs.go index 1c78c5e..d4d8870 100644 --- a/api/docs/docs.go +++ b/api/docs/docs.go @@ -15,7 +15,7 @@ const docTemplate = `{ "host": "{{.Host}}", "basePath": "{{.BasePath}}", "paths": { - "/artist/getAll": { + "/artists": { "get": { "produces": [ "application/json" @@ -44,7 +44,7 @@ const docTemplate = `{ } } }, - "/user/create": { + "/users/create": { "post": { "consumes": [ "application/json" @@ -89,7 +89,7 @@ const docTemplate = `{ } } }, - "/user/getUserNameByID/{id}": { + "/users/{id}": { "get": { "produces": [ "application/json" @@ -185,7 +185,7 @@ const docTemplate = `{ var SwaggerInfo = &swag.Spec{ Version: "1.0", Host: "", - BasePath: "/", + BasePath: "/api/v1/", Schemes: []string{}, Title: "Music Index API", Description: "API for managing users and artists in Music Index.", diff --git a/api/docs/swagger.json b/api/docs/swagger.json index a8d3350..0989513 100644 --- a/api/docs/swagger.json +++ b/api/docs/swagger.json @@ -6,9 +6,9 @@ "contact": {}, "version": "1.0" }, - "basePath": "/", + "basePath": "/api/v1/", "paths": { - "/artist/getAll": { + "/artists": { "get": { "produces": [ "application/json" @@ -37,7 +37,7 @@ } } }, - "/user/create": { + "/users/create": { "post": { "consumes": [ "application/json" @@ -82,7 +82,7 @@ } } }, - "/user/getUserNameByID/{id}": { + "/users/{id}": { "get": { "produces": [ "application/json" diff --git a/api/docs/swagger.yaml b/api/docs/swagger.yaml index 1d94d3b..7383ac0 100644 --- a/api/docs/swagger.yaml +++ b/api/docs/swagger.yaml @@ -1,4 +1,4 @@ -basePath: / +basePath: /api/v1/ definitions: handlers.CreateUserRequest: properties: @@ -37,7 +37,7 @@ info: title: Music Index API version: "1.0" paths: - /artist/getAll: + /artists: get: produces: - application/json @@ -56,7 +56,33 @@ paths: summary: List all artists tags: - 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: consumes: - application/json @@ -85,30 +111,4 @@ paths: summary: Create a user tags: - 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" diff --git a/api/go.mod b/api/go.mod index e3b8965..9f6929d 100644 --- a/api/go.mod +++ b/api/go.mod @@ -33,6 +33,7 @@ require ( github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // 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/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.3.0 // indirect diff --git a/api/go.sum b/api/go.sum index 33fa530..f74cb14 100644 --- a/api/go.sum +++ b/api/go.sum @@ -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/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/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= diff --git a/api/handlers/artist_handler.go b/api/handlers/artist_handler.go index aabba97..576ab9d 100644 --- a/api/handlers/artist_handler.go +++ b/api/handlers/artist_handler.go @@ -21,7 +21,7 @@ func NewArtistHandler(repo repository.ArtistRepository) *ArtistHandler { // @Produce json // @Success 200 {array} map[string]interface{} // @Failure 500 {object} ErrorResponse -// @Router /api/v1/artists [get] +// @Router /artists [get] func (handler *ArtistHandler) GetAll(c *gin.Context) { artists, err := handler.repo.GetAll(c.Request.Context()) if err != nil { diff --git a/api/handlers/user_handler.go b/api/handlers/user_handler.go index cc6400b..c9bfff6 100644 --- a/api/handlers/user_handler.go +++ b/api/handlers/user_handler.go @@ -51,7 +51,7 @@ type ErrorResponse struct { // @Success 201 {object} CreateUserResponse // @Failure 400 {object} ErrorResponse // @Failure 500 {object} ErrorResponse -// @Router /api/v1/users/create [post] +// @Router /users/create [post] func (h *UserHandler) CreateUser(c *gin.Context) { var req CreateUserRequest if err := c.ShouldBindJSON(&req); err != nil { @@ -90,7 +90,7 @@ func (h *UserHandler) CreateUser(c *gin.Context) { // @Success 200 {object} UsernameResponse // @Failure 400 {object} ErrorResponse // @Failure 500 {object} ErrorResponse -// @Router /api/v1/users/{id} [get] +// @Router /users/{id} [get] func (h *UserHandler) GetUsernameByID(c *gin.Context) { idParam := c.Param("id") var pgID pgtype.UUID diff --git a/api/main.go b/api/main.go index b7adfd7..2ab94df 100644 --- a/api/main.go +++ b/api/main.go @@ -8,14 +8,20 @@ import ( "zardzul/music-index/routes" "github.com/gin-gonic/gin" + "github.com/joho/godotenv" ) // @title Music Index API // @version 1.0 // @description API for managing users and artists in Music Index. -// @BasePath / +// @BasePath /api/v1/ func main() { + err := godotenv.Load(".env") + if err != nil { + panic("Error loading .env file") + } + pool, queries, databaseError := database.Connect() if databaseError != nil { panic(databaseError)