first commit

This commit is contained in:
2025-08-12 21:51:41 +07:00
commit f1efac98a0
27 changed files with 2170 additions and 0 deletions

236
cmd/api/docs/docs.go Normal file
View File

@@ -0,0 +1,236 @@
// Code generated by swaggo/swag. DO NOT EDIT.
package docs
import "github.com/swaggo/swag"
const docTemplate = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{escape .Description}}",
"title": "{{.Title}}",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "API Support",
"url": "http://www.swagger.io/support",
"email": "support@swagger.io"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/": {
"get": {
"description": "Returns a hello world message",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"root"
],
"summary": "Hello World endpoint",
"responses": {
"200": {
"description": "Hello world message",
"schema": {
"$ref": "#/definitions/models.HelloWorldResponse"
}
}
}
}
},
"/api/v1/example": {
"get": {
"description": "Returns a simple message for GET request",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"example"
],
"summary": "Example GET service",
"responses": {
"200": {
"description": "Example GET response",
"schema": {
"$ref": "#/definitions/models.ExampleGetResponse"
}
}
}
},
"post": {
"description": "Accepts a JSON payload and returns a response with an ID",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"example"
],
"summary": "Example POST service",
"parameters": [
{
"description": "Example POST request",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/models.ExamplePostRequest"
}
}
],
"responses": {
"200": {
"description": "Example POST response",
"schema": {
"$ref": "#/definitions/models.ExamplePostResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"$ref": "#/definitions/models.ErrorResponse"
}
}
}
}
},
"/health": {
"get": {
"description": "Returns the health status of the API service",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"health"
],
"summary": "Health check endpoint",
"responses": {
"200": {
"description": "Health status",
"schema": {
"$ref": "#/definitions/models.HealthResponse"
}
},
"500": {
"description": "Internal server error",
"schema": {
"$ref": "#/definitions/models.ErrorResponse"
}
}
}
}
}
},
"definitions": {
"models.ErrorResponse": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"error": {
"type": "string"
},
"message": {
"type": "string"
}
}
},
"models.ExampleGetResponse": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
},
"models.ExamplePostRequest": {
"type": "object",
"required": [
"age",
"name"
],
"properties": {
"age": {
"type": "integer"
},
"name": {
"type": "string"
}
}
},
"models.ExamplePostResponse": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"message": {
"type": "string"
}
}
},
"models.HealthResponse": {
"type": "object",
"properties": {
"details": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"status": {
"type": "string"
},
"timestamp": {
"type": "string"
}
}
},
"models.HelloWorldResponse": {
"type": "object",
"properties": {
"message": {
"type": "string"
},
"version": {
"type": "string"
}
}
}
}
}`
// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "1.0.0",
Host: "localhost:8080",
BasePath: "/api/v1",
Schemes: []string{"http", "https"},
Title: "API Service",
Description: "A comprehensive Go API service with Swagger documentation",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
}
func init() {
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
}

217
cmd/api/docs/swagger.json Normal file
View File

@@ -0,0 +1,217 @@
{
"schemes": [
"http",
"https"
],
"swagger": "2.0",
"info": {
"description": "A comprehensive Go API service with Swagger documentation",
"title": "API Service",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "API Support",
"url": "http://www.swagger.io/support",
"email": "support@swagger.io"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "1.0.0"
},
"host": "localhost:8080",
"basePath": "/api/v1",
"paths": {
"/": {
"get": {
"description": "Returns a hello world message",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"root"
],
"summary": "Hello World endpoint",
"responses": {
"200": {
"description": "Hello world message",
"schema": {
"$ref": "#/definitions/models.HelloWorldResponse"
}
}
}
}
},
"/api/v1/example": {
"get": {
"description": "Returns a simple message for GET request",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"example"
],
"summary": "Example GET service",
"responses": {
"200": {
"description": "Example GET response",
"schema": {
"$ref": "#/definitions/models.ExampleGetResponse"
}
}
}
},
"post": {
"description": "Accepts a JSON payload and returns a response with an ID",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"example"
],
"summary": "Example POST service",
"parameters": [
{
"description": "Example POST request",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/models.ExamplePostRequest"
}
}
],
"responses": {
"200": {
"description": "Example POST response",
"schema": {
"$ref": "#/definitions/models.ExamplePostResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"$ref": "#/definitions/models.ErrorResponse"
}
}
}
}
},
"/health": {
"get": {
"description": "Returns the health status of the API service",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"health"
],
"summary": "Health check endpoint",
"responses": {
"200": {
"description": "Health status",
"schema": {
"$ref": "#/definitions/models.HealthResponse"
}
},
"500": {
"description": "Internal server error",
"schema": {
"$ref": "#/definitions/models.ErrorResponse"
}
}
}
}
}
},
"definitions": {
"models.ErrorResponse": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"error": {
"type": "string"
},
"message": {
"type": "string"
}
}
},
"models.ExampleGetResponse": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
},
"models.ExamplePostRequest": {
"type": "object",
"required": [
"age",
"name"
],
"properties": {
"age": {
"type": "integer"
},
"name": {
"type": "string"
}
}
},
"models.ExamplePostResponse": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"message": {
"type": "string"
}
}
},
"models.HealthResponse": {
"type": "object",
"properties": {
"details": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"status": {
"type": "string"
},
"timestamp": {
"type": "string"
}
}
},
"models.HelloWorldResponse": {
"type": "object",
"properties": {
"message": {
"type": "string"
},
"version": {
"type": "string"
}
}
}
}
}

143
cmd/api/docs/swagger.yaml Normal file
View File

@@ -0,0 +1,143 @@
basePath: /api/v1
definitions:
models.ErrorResponse:
properties:
code:
type: integer
error:
type: string
message:
type: string
type: object
models.ExampleGetResponse:
properties:
message:
type: string
type: object
models.ExamplePostRequest:
properties:
age:
type: integer
name:
type: string
required:
- age
- name
type: object
models.ExamplePostResponse:
properties:
id:
type: string
message:
type: string
type: object
models.HealthResponse:
properties:
details:
additionalProperties:
type: string
type: object
status:
type: string
timestamp:
type: string
type: object
models.HelloWorldResponse:
properties:
message:
type: string
version:
type: string
type: object
host: localhost:8080
info:
contact:
email: support@swagger.io
name: API Support
url: http://www.swagger.io/support
description: A comprehensive Go API service with Swagger documentation
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
termsOfService: http://swagger.io/terms/
title: API Service
version: 1.0.0
paths:
/:
get:
consumes:
- application/json
description: Returns a hello world message
produces:
- application/json
responses:
"200":
description: Hello world message
schema:
$ref: '#/definitions/models.HelloWorldResponse'
summary: Hello World endpoint
tags:
- root
/api/v1/example:
get:
consumes:
- application/json
description: Returns a simple message for GET request
produces:
- application/json
responses:
"200":
description: Example GET response
schema:
$ref: '#/definitions/models.ExampleGetResponse'
summary: Example GET service
tags:
- example
post:
consumes:
- application/json
description: Accepts a JSON payload and returns a response with an ID
parameters:
- description: Example POST request
in: body
name: request
required: true
schema:
$ref: '#/definitions/models.ExamplePostRequest'
produces:
- application/json
responses:
"200":
description: Example POST response
schema:
$ref: '#/definitions/models.ExamplePostResponse'
"400":
description: Bad request
schema:
$ref: '#/definitions/models.ErrorResponse'
summary: Example POST service
tags:
- example
/health:
get:
consumes:
- application/json
description: Returns the health status of the API service
produces:
- application/json
responses:
"200":
description: Health status
schema:
$ref: '#/definitions/models.HealthResponse'
"500":
description: Internal server error
schema:
$ref: '#/definitions/models.ErrorResponse'
summary: Health check endpoint
tags:
- health
schemes:
- http
- https
swagger: "2.0"

78
cmd/api/main.go Normal file
View File

@@ -0,0 +1,78 @@
package main
import (
"context"
"fmt"
"log"
"net/http"
"os/signal"
"syscall"
"time"
"api-service/internal/server"
_ "api-service/cmd/api/docs"
)
// @title API Service
// @version 1.0.0
// @description A comprehensive Go API service with Swagger documentation
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
// @BasePath /api/v1
// @schemes http https
func gracefulShutdown(apiServer *http.Server, done chan bool) {
// Create context that listens for the interrupt signal from the OS.
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
// Listen for the interrupt signal.
<-ctx.Done()
log.Println("shutting down gracefully, press Ctrl+C again to force")
stop() // Allow Ctrl+C to force shutdown
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := apiServer.Shutdown(ctx); err != nil {
log.Printf("Server forced to shutdown with error: %v", err)
}
log.Println("Server exiting")
// Notify the main goroutine that the shutdown is complete
done <- true
}
func main() {
log.Println("Starting API Service...")
server := server.NewServer()
// Create a done channel to signal when the shutdown is complete
done := make(chan bool, 1)
// Run graceful shutdown in a separate goroutine
go gracefulShutdown(server, done)
log.Printf("Server starting on port %s", server.Addr)
err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
panic(fmt.Sprintf("http server error: %s", err))
}
// Wait for the graceful shutdown to complete
<-done
log.Println("Graceful shutdown complete.")
}