-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
162 lines (123 loc) · 5.83 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"embed"
"fmt"
"log"
"net/http"
"sync"
"github.com/snykk/kanban-app/client"
"github.com/snykk/kanban-app/config"
"github.com/snykk/kanban-app/handler/api"
"github.com/snykk/kanban-app/handler/web"
"github.com/snykk/kanban-app/middleware"
"github.com/snykk/kanban-app/repository"
"github.com/snykk/kanban-app/service"
_ "github.com/lib/pq"
"gorm.io/gorm"
)
type APIHandler struct {
UserAPIHandler api.UserAPI
TaskAPIHandler api.TaskAPI
CategoryAPIHandler api.CategoryAPI
}
type ClientHandler struct {
AuthWeb web.AuthWeb
DashboardWeb web.DashboardWeb
ModifyWeb web.ModifyWeb
HomeWeb web.HomeWeb
}
//go:embed views/*
var Resources embed.FS
func init() {
if err := config.InitializeAppConfig(); err != nil {
log.Fatalln(err)
}
}
func main() {
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
mux := http.NewServeMux()
err := repository.ConnectDB()
if err != nil {
panic(err)
}
db := repository.GetDBConnection()
mux = RunServer(db, mux)
mux = RunClient(mux, Resources)
fmt.Printf("Server is running on port %d", config.AppConfig.Port)
err = http.ListenAndServe(fmt.Sprintf(":%d", config.AppConfig.Port), mux)
if err != nil {
panic(err)
}
}()
wg.Wait()
}
func RunServer(db *gorm.DB, mux *http.ServeMux) *http.ServeMux {
userRepo := repository.NewUserRepository(db)
taskRepo := repository.NewTaskRepository(db)
categoryRepo := repository.NewCategoryRepository(db)
userService := service.NewUserService(userRepo, categoryRepo)
taskService := service.NewTaskService(taskRepo, categoryRepo)
categoryService := service.NewCategoryService(categoryRepo, taskRepo)
userAPIHandler := api.NewUserAPI(userService)
taskAPIHandler := api.NewTaskAPI(taskService)
categoryAPIHandler := api.NewCategoryAPI(categoryService)
apiHandler := APIHandler{
UserAPIHandler: userAPIHandler,
TaskAPIHandler: taskAPIHandler,
CategoryAPIHandler: categoryAPIHandler,
}
MuxRoute(mux, "POST", "/api/v1/users/login", middleware.Post(http.HandlerFunc(apiHandler.UserAPIHandler.Login)))
MuxRoute(mux, "POST", "/api/v1/users/register", middleware.Post(http.HandlerFunc(apiHandler.UserAPIHandler.Register)))
MuxRoute(mux, "POST", "/api/v1/users/logout", middleware.Post(http.HandlerFunc(apiHandler.UserAPIHandler.Logout)))
MuxRoute(mux, "GET", "/api/v1/users/get", middleware.Get(middleware.Auth(http.HandlerFunc(apiHandler.UserAPIHandler.GetUserById))), "?user_id=")
MuxRoute(mux, "DELETE", "/api/v1/users/delete", middleware.Delete(http.HandlerFunc(apiHandler.UserAPIHandler.Delete)), "?user_id=")
MuxRoute(mux, "GET", "/api/v1/tasks/get", middleware.Get(middleware.Auth(http.HandlerFunc(apiHandler.TaskAPIHandler.GetTask))), "?task_id=")
MuxRoute(mux, "POST", "/api/v1/tasks/create", middleware.Post(middleware.Auth(http.HandlerFunc(apiHandler.TaskAPIHandler.CreateNewTask))))
MuxRoute(mux, "PUT", "/api/v1/tasks/update", middleware.Put(middleware.Auth(http.HandlerFunc(apiHandler.TaskAPIHandler.UpdateTask))), "?task_id=")
MuxRoute(mux, "PUT", "/api/v1/tasks/update/category", middleware.Put(middleware.Auth(http.HandlerFunc(apiHandler.TaskAPIHandler.UpdateTaskCategory))), "?task_id=")
MuxRoute(mux, "DELETE", "/api/v1/tasks/delete", middleware.Delete(middleware.Auth(http.HandlerFunc(apiHandler.TaskAPIHandler.DeleteTask))), "?task_id=")
MuxRoute(mux, "GET", "/api/v1/categories/get", middleware.Get(middleware.Auth(http.HandlerFunc(apiHandler.CategoryAPIHandler.GetCategory))))
MuxRoute(mux, "GET", "/api/v1/categories/dashboard", middleware.Get(middleware.Auth(http.HandlerFunc(apiHandler.CategoryAPIHandler.GetCategoryWithTasks))))
MuxRoute(mux, "POST", "/api/v1/categories/create", middleware.Post(middleware.Auth(http.HandlerFunc(apiHandler.CategoryAPIHandler.CreateNewCategory))))
MuxRoute(mux, "DELETE", "/api/v1/categories/delete", middleware.Delete(middleware.Auth(http.HandlerFunc(apiHandler.CategoryAPIHandler.DeleteCategory))), "?category_id=")
return mux
}
func RunClient(mux *http.ServeMux, embed embed.FS) *http.ServeMux {
userClient := client.NewUserClient()
categoryClient := client.NewCategoryClient()
taskClient := client.NewTaskClient()
authWeb := web.NewAuthWeb(userClient, embed)
dashboardWeb := web.NewDashboardWeb(categoryClient, userClient, embed)
modifyWeb := web.NewModifyWeb(taskClient, categoryClient, embed)
homeWeb := web.NewHomeWeb(embed)
client := ClientHandler{
authWeb, dashboardWeb, modifyWeb, homeWeb,
}
mux.HandleFunc("/login", client.AuthWeb.Login)
mux.HandleFunc("/login/process", client.AuthWeb.LoginProcess)
mux.HandleFunc("/register", client.AuthWeb.Register)
mux.HandleFunc("/register/process", client.AuthWeb.RegisterProcess)
mux.HandleFunc("/logout", client.AuthWeb.Logout)
mux.Handle("/dashboard", middleware.Auth(http.HandlerFunc(client.DashboardWeb.Dashboard)))
mux.Handle("/category/add", middleware.Auth(http.HandlerFunc(client.ModifyWeb.AddCategory)))
mux.Handle("/category/create", middleware.Auth(http.HandlerFunc(client.ModifyWeb.AddCategoryProcess)))
mux.Handle("/task/add", middleware.Auth(http.HandlerFunc(client.ModifyWeb.AddTask)))
mux.Handle("/task/create", middleware.Auth(http.HandlerFunc(client.ModifyWeb.AddTaskProcess)))
mux.Handle("/task/update", middleware.Auth(http.HandlerFunc(client.ModifyWeb.UpdateTask)))
mux.Handle("/task/update/process", middleware.Auth(http.HandlerFunc(client.ModifyWeb.UpdateTaskProcess)))
mux.Handle("/task/delete", middleware.Auth(http.HandlerFunc(client.ModifyWeb.DeleteTask)))
mux.Handle("/category/delete", middleware.Auth(http.HandlerFunc(client.ModifyWeb.DeleteCategory)))
mux.HandleFunc("/", client.HomeWeb.Index)
return mux
}
func MuxRoute(mux *http.ServeMux, method string, path string, handler http.Handler, opt ...string) {
if len(opt) > 0 {
fmt.Printf("[%s]: %s %v \n", method, path, opt)
} else {
fmt.Printf("[%s]: %s \n", method, path)
}
mux.Handle(path, handler)
}