Skip to content

Commit

Permalink
feat(docs): add image to README and enhance environment variable sect…
Browse files Browse the repository at this point in the history
…ion for clarity

feat(docker): update docker-compose.yml to include additional environment variables for better configuration
fix(api): load environment variables without production check to ensure proper configuration
refactor(handlers): improve API response structure and add detailed endpoint descriptions for better documentation
  • Loading branch information
Abdullah Alqahtani committed Dec 28, 2024
1 parent b339d33 commit d8976e5
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 22 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -438,7 +440,11 @@ graph LR
api --> |port| port1[3000:3000]
mongo --> |port| port2[27017:27017]
env1[Environment:<br/>MONGODB_URI<br/>PORT] --> api
subgraph Environment
env[Environment Variables:<br/>API_VERSION<br/>ENVIRONMENT<br/>PORT<br/>APP_NAME<br/>APP_DESCRIPTION<br/>MONGO_URI<br/>MONGO_DATABASE]
end
env --> api
end
```

Expand Down
Binary file added assets/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 2 additions & 5 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
################################################################################
# Docker Compose file for running the API and MongoDB
################################################################################

services:
# API service
api:
build:
context: .
Expand All @@ -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:
Expand All @@ -23,9 +32,11 @@ services:
networks:
- naqa-network

# Networks
networks:
naqa-network:
driver: bridge

# Volumes
volumes:
mongodb_data:
18 changes: 9 additions & 9 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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", ""),
Expand Down
42 changes: 36 additions & 6 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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",
})
}

0 comments on commit d8976e5

Please sign in to comment.