diff --git a/README.md b/README.md index 891cd16..0053419 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,8 @@ Set the following environment variables or edit the .env file: http://localhost:3000/api/v1 ``` +![dev make - air](./assets/1.png) + ## Environment Variables Before running the application, make sure to set up your environment variables: @@ -438,7 +440,11 @@ graph LR api --> |port| port1[3000:3000] mongo --> |port| port2[27017:27017] - env1[Environment:
MONGODB_URI
PORT] --> api + subgraph Environment + env[Environment Variables:
API_VERSION
ENVIRONMENT
PORT
APP_NAME
APP_DESCRIPTION
MONGO_URI
MONGO_DATABASE] + end + + env --> api end ``` diff --git a/assets/1.png b/assets/1.png new file mode 100644 index 0000000..0c1115d Binary files /dev/null and b/assets/1.png differ diff --git a/cmd/api/main.go b/cmd/api/main.go index 867ea48..c6a8c26 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -15,12 +15,9 @@ func main() { // ############################################################################### // Environment and Configuration Setup // ############################################################################### - if os.Getenv("ENV") != "production" { - if err := godotenv.Load(); err != nil { - log.Printf("Warning: Error loading .env file: %v", err) - } + if err := godotenv.Load(); err != nil { + log.Printf("Warning: Error loading .env file: %v", err) } - mongoConfig := config.NewMongoConfig() db, err := config.ConnectDB(mongoConfig) if err != nil { diff --git a/docker-compose.yml b/docker-compose.yml index ad49653..2906cf5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,9 @@ +################################################################################ +# Docker Compose file for running the API and MongoDB +################################################################################ + services: + # API service api: build: context: . @@ -7,13 +12,17 @@ services: - "${PORT:-3000}:${PORT:-3000}" restart: unless-stopped environment: + - API_VERSION=${API_VERSION} + - ENVIRONMENT=${ENVIRONMENT} + - APP_NAME=${APP_NAME} + - APP_DESCRIPTION=${APP_DESCRIPTION} - MONGODB_URI=${MONGO_URI}/${MONGO_DATABASE} - PORT=${PORT} depends_on: - mongodb networks: - naqa-network - + # MongoDB service mongodb: image: mongo:latest ports: @@ -23,9 +32,11 @@ services: networks: - naqa-network +# Networks networks: naqa-network: driver: bridge +# Volumes volumes: mongodb_data: \ No newline at end of file diff --git a/internal/config/config.go b/internal/config/config.go index 8088cad..1df1810 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -7,13 +7,13 @@ import ( ) type Config struct { - AppName string - Version string - Port string - Environment string - Description string - MongoURI string - MongoDatabase string + AppName string + Version string + Port string + Environment string + Description string + MongoURI string + MongoDatabase string } func LoadConfig() (*Config, error) { @@ -22,8 +22,8 @@ func LoadConfig() (*Config, error) { } return &Config{ - AppName: getEnv("APP_NAME", "Naqa API"), - Version: getEnv("API_VERSION", "1.0.0"), + AppName: getEnv("APP_NAME", "Naqa API"), + Version: getEnv("API_VERSION", "1.0.0"), Port: getEnv("PORT", "3000"), Environment: getEnv("ENVIRONMENT", "development"), Description: getEnv("APP_DESCRIPTION", ""), diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 620bbbe..4084083 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -43,7 +43,7 @@ func (h *Handler) ApiV1Handler(c *fiber.Ctx) error { "status": "active", "message": "Welcome to NAQA API v1", "version": "1.0.0", - "env": os.Getenv("APP_ENV"), + "env": os.Getenv("ENVIRONMENT"), "server_time": time.Now().Format(time.RFC3339), "request_id": c.Get("X-Request-ID", uuid.New().String()), "endpoints": []string{ @@ -62,15 +62,45 @@ func (h *Handler) HealthCheckHandler(c *fiber.Ctx) error { } func (h *Handler) GetStocksBaseHandler(c *fiber.Ctx) error { + latestYear := constants.AvailableYears[len(constants.AvailableYears)-1] return c.JSON(fiber.Map{ "status": "success", "message": "Welcome to Stocks API", "available_years": constants.AvailableYears, - "endpoints": map[string]string{ - "get_stocks": "/api/v1/stocks/year/{year}", - "search": "/api/v1/stocks/year/{year}/search", - "calculate": "/api/v1/stocks/year/{year}/calculate-purification", + "endpoints": []map[string]interface{}{ + { + "name": "Get Stocks", + "path": "/api/v1/stocks/year/{year}", + "method": "GET", + "description": "Get all stocks for a specific year", + "example": fmt.Sprintf("/api/v1/stocks/year/%s", latestYear), + }, + { + "name": "Search Stocks", + "path": "/api/v1/stocks/year/{year}/search", + "method": "GET", + "description": "Search stocks with filters", + "example": fmt.Sprintf("/api/v1/stocks/year/%s/search?sector=الطاقة&sharia_opinion=نقية", latestYear), + "parameters": []string{ + "name", "code", "sector", "sharia_opinion", + }, + }, + { + "name": "Calculate Purification", + "path": "/api/v1/stocks/year/{year}/calculate-purification", + "method": "POST", + "description": "Calculate stock purification amount", + "example": map[string]interface{}{ + "url": fmt.Sprintf("/api/v1/stocks/year/%s/calculate-purification", latestYear), + "body": map[string]interface{}{ + "start_date": "2023-01-01", + "end_date": "2023-12-31", + "number_of_stocks": 100, + "stock_code": "1111", + }, + }, + }, }, - "example": fmt.Sprintf("/api/v1/stocks/year/%s", constants.AvailableYears[len(constants.AvailableYears)-1]), + "documentation": "https://github.com/anqorithm/naqa-api", }) }