-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (71 loc) · 2.49 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
73
74
75
76
77
78
79
80
81
82
package main
import (
"database/sql"
"time"
"github.com/go-playground/validator/v10"
_ "github.com/go-sql-driver/mysql"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/sirupsen/logrus"
"github.com/zulfikarrosadi/go-blog-api/article"
"github.com/zulfikarrosadi/go-blog-api/auth"
"github.com/zulfikarrosadi/go-blog-api/lib"
)
func main() {
e := echo.New()
validator := validator.New()
db := GetDBConnection()
e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
LogStatus: true,
LogRemoteIP: true,
LogURIPath: true,
LogMethod: true,
LogUserAgent: true,
LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
lib.Logrus.WithFields(logrus.Fields{
"uri_path": v.URIPath,
"status": v.Status,
"remote_ip": v.RemoteIP,
"method": v.Method,
"user_agent": v.UserAgent,
}).Info("request")
return nil
},
}))
articleRepository := article.NewArticleRepository(GetDBConnection())
articleService := article.NewArticleService(articleRepository, validator)
articleHandler := article.NewArticleApi(articleService)
authRepository := auth.NewAuthRepository(db)
authService := auth.NewAuthService(authRepository, validator)
authHandler := auth.NewAuthHandler(authService)
authMiddleware := auth.NewAuthMiddleware()
e.POST("/api/signin", authHandler.SignInHandler)
e.POST("/api/#", authHandler.#Handler)
e.POST("/api/refresh", authHandler.RefreshTokenHandler)
protectedRouteGroup := e.Group("/api/auth")
protectedRouteGroup.Use(authMiddleware.DeserializeUser)
protectedRouteGroup.Use(authMiddleware.AuthenticationRequired)
e.GET("/api/articles", articleHandler.GetArticles)
e.GET("/api/articles/:slug", articleHandler.GetArticleById)
protectedRouteGroup.POST("/articles", articleHandler.CreateArticle)
protectedRouteGroup.DELETE("/articles/:id", articleHandler.DeleteArticle)
protectedRouteGroup.PUT("/articles/:id", articleHandler.UpdateArticle)
protectedRouteGroup.POST("/files", lib.FileUploadHandler)
e.Logger.Fatal(e.Start("localhost:3000"))
}
func GetDBConnection() *sql.DB {
dsn := "root:@tcp(localhost:3306)/golang_article?parseTime=true"
d, err := sql.Open("mysql", dsn)
if err != nil {
lib.Logrus.WithFields(logrus.Fields{
"timestamp": time.Now(),
"details": err.Error(),
"context": map[string]any{
"action": "get_db_connection",
},
}).Error("Failed to open connection to database")
}
d.SetMaxOpenConns(6)
d.SetMaxIdleConns(2)
return d
}