-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (51 loc) · 1.24 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
package main
import (
"fmt"
"github.com/bhill77/web/app/api"
"github.com/bhill77/web/app/api/handler"
"github.com/bhill77/web/module/cache"
newsModule "github.com/bhill77/web/module/news"
"github.com/bhill77/web/service/news"
"github.com/go-redis/redis"
"github.com/labstack/echo/v4"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"time"
)
func main() {
e := echo.New()
db := dbConnect()
articleRepo := newsModule.NewRepository(db)
redisClient := redisConnect()
redisCache := cache.NewRedisCacheRepository(redisClient)
newsService := news.NewService(articleRepo, redisCache)
articleHandler := handler.NewArticleHandler(newsService)
api.RegisterPath(e, articleHandler)
e.Logger.Fatal(e.Start(":8000"))
}
func dbConnect() *gorm.DB {
dsn := fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local",
"root",
"root",
"db",
3306,
"web")
connection, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic(err)
}
db, _ := connection.DB()
db.SetMaxIdleConns(10)
db.SetMaxOpenConns(100)
db.SetConnMaxLifetime(10 * time.Minute)
return connection
}
func redisConnect() *redis.Client {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
return client
}