-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (66 loc) · 2.2 KB
/
main.go
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
package main
import (
"fmt"
"log"
"os"
"github.com/go-redis/redis/v8"
"github.com/ilhamtubagus/newsTags/app"
"github.com/ilhamtubagus/newsTags/infrastructure/cache"
"github.com/ilhamtubagus/newsTags/infrastructure/persistence"
"github.com/ilhamtubagus/newsTags/interface/handler"
"github.com/ilhamtubagus/newsTags/utils"
"github.com/joho/godotenv"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
)
var dbClient *gorm.DB
var rdbClient *redis.Client
func init() {
err := godotenv.Load(".env")
if err != nil {
log.Fatal(err)
}
// Postgres initialization
dbClient = utils.GetDatabaseClient()
// Redis initialization
rdbClient = utils.GetRedisClient()
}
func main() {
// Instantiate postgres repositories
databaseServices := persistence.NewDatabaseRepositories(dbClient)
err := databaseServices.AutoMigrate()
if err != nil {
log.Fatal(err)
}
// Instantiate redis cacher
redis := cache.NewRedisCacher(rdbClient)
// Instantiate apps
tagApp := app.TagAppImpl{TagRepo: databaseServices.TagRepository}
topicApp := app.TopicAppImpl{TopicRepo: databaseServices.TopicRepository}
newsApp := app.NewsAppImpl{NewsRepo: databaseServices.NewsRepository, TagRepo: databaseServices.TagRepository, TopicRepo: databaseServices.TopicRepository}
// Glue apps with handlers
tagHandler := handler.NewTagHandler(&tagApp, redis)
topicHandler := handler.NewTopicHandler(&topicApp, redis)
newsHandler := handler.NewNewsHandler(&newsApp, redis)
// Define routes
e := echo.New()
// Tag routes
e.POST("/tag", tagHandler.SaveTag)
e.PUT("/tag/:id", tagHandler.UpdateTag)
e.GET("/tag/:id", tagHandler.GetTagById)
e.DELETE("/tag/:id", tagHandler.DeleteTag)
e.GET("/tag", tagHandler.GetAllTag)
// Topic routes
e.POST("/topic", topicHandler.SaveTopic)
e.PUT("/topic/:id", topicHandler.UpdateTopic)
e.GET("/topic/:id", topicHandler.GetTopicById)
e.DELETE("/topic/:id", topicHandler.DeleteTopic)
e.GET("/topic", topicHandler.GetAllTopic)
// News Routes
e.POST("/news", newsHandler.SaveNews)
e.GET("/news/:id", newsHandler.GetNewsById)
e.GET("/news", newsHandler.GetAllNews)
e.PUT("/news/:id", newsHandler.UpdateNews)
e.DELETE("/news/:id", newsHandler.DeleteNews)
e.Logger.Fatal(e.Start(fmt.Sprintf(":%s", os.Getenv("APP_PORT"))))
}