Refactor health check and user management endpoints for improved structure and clarity

- Replaced the existing health check endpoint with a new handler at `/admin/v1/health` to align with the updated API structure.
- Introduced a dedicated `health.go` file to encapsulate health check logic and response formatting.
- Updated Swagger documentation to reflect the new health check endpoint and its response structure.
- Refined user management routes to enhance clarity and maintainability, including changes to login, logout, and profile management endpoints.
- Ensured all user-related endpoints are now prefixed appropriately and documented in Swagger for better API usability.
This commit is contained in:
n.nakhostin
2025-08-10 19:06:20 +03:30
parent dbcc2d1d4a
commit 2119f49b9b
7 changed files with 1345 additions and 1190 deletions
+1 -7
View File
@@ -116,13 +116,7 @@ func initHTTPServer(conf infra.Config, log logger.Logger) *echo.Echo {
})
// Add health check endpoint
e.GET("/health", func(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]interface{}{
"status": "healthy",
"time": time.Now().Unix(),
"version": "1.0.0",
})
})
e.GET("/admin/v1/health", healthHandler)
// Add Swagger documentation endpoint
e.GET("/swagger/*", echoSwagger.WrapHandler)
+484 -425
View File
File diff suppressed because it is too large Load Diff
+484 -425
View File
File diff suppressed because it is too large Load Diff
+305 -270
View File
@@ -1,5 +1,14 @@
basePath: /api/v1
basePath: /admin/v1
definitions:
main.HealthResponse:
properties:
status:
type: string
time:
type: integer
version:
type: string
type: object
response.APIError:
properties:
code:
@@ -191,7 +200,265 @@ info:
title: Tender Management API
version: 1.0.0
paths:
/users/admin:
/change-password:
put:
consumes:
- application/json
description: Change current user password
parameters:
- description: Password change data
in: body
name: password
required: true
schema:
$ref: '#/definitions/user.ChangePasswordForm'
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/response.APIResponse'
"400":
description: Bad Request
schema:
$ref: '#/definitions/response.APIResponse'
"401":
description: Unauthorized
schema:
$ref: '#/definitions/response.APIResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/response.APIResponse'
security:
- BearerAuth: []
summary: Change password
tags:
- Users
/health:
get:
consumes:
- application/json
description: Get server health status
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/main.HealthResponse'
summary: Health check
tags:
- Health
/login:
post:
consumes:
- application/json
description: Authenticate user with username and password
parameters:
- description: Login credentials
in: body
name: login
required: true
schema:
$ref: '#/definitions/user.LoginForm'
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/response.APIResponse'
- properties:
data:
$ref: '#/definitions/user.AuthResponse'
type: object
"400":
description: Bad Request
schema:
$ref: '#/definitions/response.APIResponse'
"401":
description: Unauthorized
schema:
$ref: '#/definitions/response.APIResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/response.APIResponse'
summary: Login user
tags:
- Authorization
/logout:
delete:
consumes:
- application/json
description: Logout current user and invalidate tokens
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/response.APIResponse'
"401":
description: Unauthorized
schema:
$ref: '#/definitions/response.APIResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/response.APIResponse'
security:
- BearerAuth: []
summary: Logout user
tags:
- Users
/profile:
get:
consumes:
- application/json
description: Get current user profile information
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/response.APIResponse'
- properties:
data:
$ref: '#/definitions/user.UserResponse'
type: object
"401":
description: Unauthorized
schema:
$ref: '#/definitions/response.APIResponse'
"404":
description: Not Found
schema:
$ref: '#/definitions/response.APIResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/response.APIResponse'
security:
- BearerAuth: []
summary: Get user profile
tags:
- Users
put:
consumes:
- application/json
description: Update current user profile information
parameters:
- description: Profile update data
in: body
name: profile
required: true
schema:
$ref: '#/definitions/user.UpdateUserForm'
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/response.APIResponse'
- properties:
data:
$ref: '#/definitions/user.UserResponse'
type: object
"400":
description: Bad Request
schema:
$ref: '#/definitions/response.APIResponse'
"401":
description: Unauthorized
schema:
$ref: '#/definitions/response.APIResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/response.APIResponse'
security:
- BearerAuth: []
summary: Update user profile
tags:
- Users
/refresh-token:
post:
consumes:
- application/json
description: Refresh access token using refresh token
parameters:
- description: Refresh token
in: body
name: refresh
required: true
schema:
$ref: '#/definitions/user.RefreshTokenForm'
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/response.APIResponse'
- properties:
data:
$ref: '#/definitions/user.AuthResponse'
type: object
"400":
description: Bad Request
schema:
$ref: '#/definitions/response.APIResponse'
"401":
description: Unauthorized
schema:
$ref: '#/definitions/response.APIResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/response.APIResponse'
summary: Refresh access token
tags:
- Authorization
/reset-password:
post:
consumes:
- application/json
description: Send password reset email to user
parameters:
- description: Password reset request
in: body
name: reset
required: true
schema:
$ref: '#/definitions/user.ResetPasswordForm'
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/response.APIResponse'
"400":
description: Bad Request
schema:
$ref: '#/definitions/response.APIResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/response.APIResponse'
summary: Reset password
tags:
- Authorization
/users:
get:
consumes:
- application/json
@@ -261,7 +528,7 @@ paths:
- BearerAuth: []
summary: List users
tags:
- users
- Users
post:
consumes:
- application/json
@@ -305,8 +572,8 @@ paths:
- BearerAuth: []
summary: Create user
tags:
- users
/users/admin/{id}:
- Users
/users/{id}:
delete:
consumes:
- application/json
@@ -348,7 +615,7 @@ paths:
- BearerAuth: []
summary: Delete user
tags:
- users
- Users
get:
consumes:
- application/json
@@ -395,7 +662,7 @@ paths:
- BearerAuth: []
summary: Get user by ID
tags:
- users
- Users
put:
consumes:
- application/json
@@ -448,8 +715,8 @@ paths:
- BearerAuth: []
summary: Update user
tags:
- users
/users/admin/{id}/role:
- Users
/users/{id}/role:
put:
consumes:
- application/json
@@ -497,8 +764,8 @@ paths:
- BearerAuth: []
summary: Update user role
tags:
- users
/users/admin/{id}/status:
- Users
/users/{id}/status:
put:
consumes:
- application/json
@@ -546,8 +813,8 @@ paths:
- BearerAuth: []
summary: Update user status
tags:
- users
/users/admin/company/{company_id}:
- Users
/users/company/{company_id}:
get:
consumes:
- application/json
@@ -570,9 +837,15 @@ paths:
- application/json
responses:
"200":
description: OK
description: Users with pagination metadata
schema:
$ref: '#/definitions/response.APIResponse'
allOf:
- $ref: '#/definitions/response.APIResponse'
- properties:
data:
additionalProperties: true
type: object
type: object
"400":
description: Bad Request
schema:
@@ -593,8 +866,8 @@ paths:
- BearerAuth: []
summary: Get users by company ID
tags:
- users
/users/admin/role/{role}:
- Users
/users/role/{role}:
get:
consumes:
- application/json
@@ -617,9 +890,15 @@ paths:
- application/json
responses:
"200":
description: OK
description: Users with pagination metadata
schema:
$ref: '#/definitions/response.APIResponse'
allOf:
- $ref: '#/definitions/response.APIResponse'
- properties:
data:
additionalProperties: true
type: object
type: object
"400":
description: Bad Request
schema:
@@ -640,250 +919,7 @@ paths:
- BearerAuth: []
summary: Get users by role
tags:
- users
/users/change-password:
put:
consumes:
- application/json
description: Change current user password
parameters:
- description: Password change data
in: body
name: password
required: true
schema:
$ref: '#/definitions/user.ChangePasswordForm'
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/response.APIResponse'
"400":
description: Bad Request
schema:
$ref: '#/definitions/response.APIResponse'
"401":
description: Unauthorized
schema:
$ref: '#/definitions/response.APIResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/response.APIResponse'
security:
- BearerAuth: []
summary: Change password
tags:
- users
/users/login:
post:
consumes:
- application/json
description: Authenticate user with email/username and password
parameters:
- description: Login credentials
in: body
name: login
required: true
schema:
$ref: '#/definitions/user.LoginForm'
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/response.APIResponse'
- properties:
data:
$ref: '#/definitions/user.AuthResponse'
type: object
"400":
description: Bad Request
schema:
$ref: '#/definitions/response.APIResponse'
"401":
description: Unauthorized
schema:
$ref: '#/definitions/response.APIResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/response.APIResponse'
summary: Login user
tags:
- users
/users/logout:
post:
consumes:
- application/json
description: Logout current user and invalidate tokens
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/response.APIResponse'
"401":
description: Unauthorized
schema:
$ref: '#/definitions/response.APIResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/response.APIResponse'
security:
- BearerAuth: []
summary: Logout user
tags:
- users
/users/profile:
get:
consumes:
- application/json
description: Get current user profile information
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/response.APIResponse'
- properties:
data:
$ref: '#/definitions/user.UserResponse'
type: object
"401":
description: Unauthorized
schema:
$ref: '#/definitions/response.APIResponse'
"404":
description: Not Found
schema:
$ref: '#/definitions/response.APIResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/response.APIResponse'
security:
- BearerAuth: []
summary: Get user profile
tags:
- users
put:
consumes:
- application/json
description: Update current user profile information
parameters:
- description: Profile update data
in: body
name: profile
required: true
schema:
$ref: '#/definitions/user.UpdateUserForm'
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/response.APIResponse'
- properties:
data:
$ref: '#/definitions/user.UserResponse'
type: object
"400":
description: Bad Request
schema:
$ref: '#/definitions/response.APIResponse'
"401":
description: Unauthorized
schema:
$ref: '#/definitions/response.APIResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/response.APIResponse'
security:
- BearerAuth: []
summary: Update user profile
tags:
- users
/users/refresh-token:
post:
consumes:
- application/json
description: Refresh access token using refresh token
parameters:
- description: Refresh token
in: body
name: refresh
required: true
schema:
$ref: '#/definitions/user.RefreshTokenForm'
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/response.APIResponse'
- properties:
data:
$ref: '#/definitions/user.AuthResponse'
type: object
"400":
description: Bad Request
schema:
$ref: '#/definitions/response.APIResponse'
"401":
description: Unauthorized
schema:
$ref: '#/definitions/response.APIResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/response.APIResponse'
summary: Refresh access token
tags:
- users
/users/reset-password:
post:
consumes:
- application/json
description: Send password reset email to user
parameters:
- description: Password reset request
in: body
name: reset
required: true
schema:
$ref: '#/definitions/user.ResetPasswordForm'
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/response.APIResponse'
"400":
description: Bad Request
schema:
$ref: '#/definitions/response.APIResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/response.APIResponse'
summary: Reset password
tags:
- users
- Users
securityDefinitions:
BearerAuth:
description: Type "Bearer" followed by a space and JWT token.
@@ -892,11 +928,10 @@ securityDefinitions:
type: apiKey
swagger: "2.0"
tags:
- description: Customer management operations
name: customers
- description: User management operations
name: users
- description: User management operations including authentication, profile management,
and admin operations
name: Users
- description: Authentication operations including login, logout, and token management
name: Authorization
- description: Health check operations
name: health
- description: Authentication operations
name: auth
name: Health
+30
View File
@@ -0,0 +1,30 @@
package main
import (
"net/http"
"time"
"github.com/labstack/echo/v4"
)
// @Summary Health check
// @Description Get server health status
// @Tags Health
// @Accept json
// @Produce json
// @Success 200 {object} HealthResponse
// @Router /health [get]
func healthHandler(c echo.Context) error {
return c.JSON(http.StatusOK, HealthResponse{
Status: "healthy",
Time: time.Now().Unix(),
Version: "1.0.0",
})
}
// HealthResponse represents the health check response
type HealthResponse struct {
Status string `json:"status"`
Time int64 `json:"time"`
Version string `json:"version"`
}
+6 -28
View File
@@ -1,22 +1,3 @@
// Package main Tender Management API
//
// This is the API documentation for the Tender Management System.
//
// Schemes: http, https
// Host: localhost:8081
// BasePath: /api/v1
// Version: 1.0.0
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Security:
// - bearer
//
// swagger:meta
package main
// @title Tender Management API
@@ -32,25 +13,22 @@ package main
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8081
// @BasePath /api/v1
// @BasePath /admin/v1
// @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization
// @description Type "Bearer" followed by a space and JWT token.
// @tag.name customers
// @tag.description Customer management operations
// @tag.name Users
// @tag.description User management operations including authentication, profile management, and admin operations
// @tag.name users
// @tag.description User management operations
// @tag.name Authorization
// @tag.description Authentication operations including login, logout, and token management
// @tag.name health
// @tag.name Health
// @tag.description Health check operations
// @tag.name auth
// @tag.description Authentication operations
import (
"context"
"fmt"