Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
89 lines (70 loc) · 2.7 KB
/
Copy pathmain.go
File metadata and controls
89 lines (70 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"log"
"os"
"github.com/Dijkstra-Edu/Archivist/src/controllers"
"github.com/Dijkstra-Edu/Archivist/src/repositories"
"github.com/Dijkstra-Edu/Archivist/src/services"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
// Swagger imports
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
_ "github.com/Dijkstra-Edu/Archivist/docs" // Import generated docs
)
// @title Archivist API
// @version 1.0
// @description Blog API service with MongoDB backend
// @termsOfService https://dijkstra-edu.com/terms/
// @contact.name API Support
// @contact.email support@dijkstra-edu.com
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
// @BasePath /
func main() {
// Load environment variables from .env file
if err := godotenv.Load(); err != nil {
log.Println("No .env file found, using default values")
}
// Get configuration from environment
connectionString := getEnv("MONGODB_CONNECTION_STRING", "mongodb://localhost:27017/archivist")
serverPort := getEnv("SERVER_PORT", "8080")
// Initialize MongoDB repository (connection established lazily)
blogRepo, err := repositories.NewBlogRepository(connectionString)
if err != nil {
log.Fatalf("Failed to initialize MongoDB repository: %v", err)
}
log.Println("MongoDB repository initialized (connection will be tested on demand)")
// Initialize services
healthService := services.NewHealthService(blogRepo)
blogService := services.NewBlogService(blogRepo)
// Initialize controllers
healthController := controllers.NewHealthController(healthService)
blogController := controllers.NewBlogController(blogService)
// Setup Gin router
router := gin.Default()
// Health check endpoint
router.GET("/health", healthController.GetHealth)
// Blog endpoints
router.POST("/blogs", blogController.CreateBlog)
router.GET("/blogs/search", blogController.SearchBlogs) // Must come before /:id route
router.GET("/blogs/:id", blogController.GetBlog)
router.PUT("/blogs/:id", blogController.UpdateBlog)
router.DELETE("/blogs/:id", blogController.DeleteBlog)
router.GET("/blogs", blogController.GetAllBlogs) // Must come last to avoid conflicts
// Swagger documentation endpoint
router.GET("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
// Start server
log.Printf("Server starting on port %s", serverPort)
if err := router.Run(":" + serverPort); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}
// getEnv retrieves environment variable or returns a default value
func getEnv(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}