-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
206 lines (164 loc) · 5.08 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"context"
"fmt"
"log"
"net/http"
"user_management/handlers"
"user_management/middleware"
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"
"github.com/redis/go-redis/v9"
"github.com/rs/cors"
)
const (
configPath = "config.toml"
)
var PORT = ":8080"
var BASE_URL = "/api/v1/users"
var CONFIG_FILE = koanf.New(".")
func init() {
if err := CONFIG_FILE.Load(file.Provider(configPath), toml.Parser()); err != nil {
log.Fatalf("Error loading config file: %s", err)
}
if CONFIG_FILE.String("BASE_URL") == "" {
log.Fatal("Base URL is required in config file.")
}
if CONFIG_FILE.String("FILE_PATH") == "" {
log.Fatal("File path is required in config file.")
}
if CONFIG_FILE.String("SECRET") == "" {
log.Fatal("Secret is required in config file.")
}
if CONFIG_FILE.String("redis.HOST") == "" {
log.Fatal("Redis host is required in config file.")
}
if CONFIG_FILE.String("redis.PORT") == "" {
log.Fatal("Redis port is required in config file.")
}
if CONFIG_FILE.String("smtp.HOST") == "" {
log.Fatal("SMTP host is required in config file.")
}
if CONFIG_FILE.String("smtp.PORT") == "" {
log.Fatal("SMTP port is required in config file.")
}
if CONFIG_FILE.String("smtp.USERNAME") == "" {
log.Fatal("SMTP username is required in config file.")
}
if CONFIG_FILE.String("smtp.PASSWORD") == "" {
log.Fatal("SMTP password is required in config file.")
}
}
func main() {
filePath := CONFIG_FILE.String("FILE_PATH")
redisClient := redis.NewClient(&redis.Options{
Addr: CONFIG_FILE.String("redis.HOST") + ":" + CONFIG_FILE.String("redis.PORT"),
Password: "",
DB: 0,
})
ctx := context.Background()
mux := http.NewServeMux()
// Static route
mux.Handle("/", http.FileServer(http.Dir("./static")))
// Set up the routes
mux.HandleFunc(BASE_URL+"/user", func(w http.ResponseWriter, r *http.Request) {
// GET /v1/users/user?username={username}
if r.Method == http.MethodGet {
handlers.GetUserHandler(w, r, filePath)
return
}
// POST /v1/users/user
if r.Method == http.MethodPost {
secret := CONFIG_FILE.String("SECRET")
handlers.AddUserHandler(w, r, filePath, secret, redisClient, ctx)
return
}
// PUT /v1/users/user?username={username}
if r.Method == http.MethodPut {
handlers.UpdateUserHandler(w, r, filePath)
return
}
// DELETE /v1/users/user?username={username}
if r.Method == http.MethodDelete {
handlers.DeleteUserHandler(w, r, filePath)
return
}
if r.Method == http.MethodHead {
return
}
// Return 405 for all other methods
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
})
mux.HandleFunc(BASE_URL+"/all", func(w http.ResponseWriter, r *http.Request) {
// GET /v1/users/all
if r.Method == http.MethodGet {
handlers.GetAllUsersHandler(w, r, filePath)
return
}
if r.Method == http.MethodHead {
return
}
// Return 405 for all other methods
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
})
mux.HandleFunc(BASE_URL+"/user/enable", func(w http.ResponseWriter, r *http.Request) {
// POST /v1/users/user/enable?username={username}
if r.Method == http.MethodPost {
handlers.EnableUserRequestHandler(w, r, filePath)
return
}
// Return 405 for all other methods
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
})
mux.HandleFunc(BASE_URL+"/user/disable", func(w http.ResponseWriter, r *http.Request) {
// POST /v1/users/user/disable?username={username}
if r.Method == http.MethodPost {
handlers.DisableUserHandler(w, r, filePath)
return
}
// Return 405 for all other methods
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
})
mux.HandleFunc(BASE_URL+"/register", func(w http.ResponseWriter, r *http.Request) {
// GET /v1/users/register?displayname={displayName}?token={token}
if r.Method == http.MethodGet {
handlers.RegisterPageHandler(w, r)
return
}
// POST /v1/users/register
if r.Method == http.MethodPost {
handlers.RegisterUserHandler(w, r, CONFIG_FILE, redisClient, ctx)
return
}
// Return 405 for all other methods
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
})
mux.HandleFunc(BASE_URL+"/reset_password", func(w http.ResponseWriter, r *http.Request) {
// PUT /v1/users/reset_password
if r.Method == http.MethodPut {
handlers.ResetPasswordHandler(w, r, filePath)
return
}
// Return 405 for all other methods
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
})
// Health check
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "Server is Healthy")
})
corsOpt := cors.Options{
AllowedOrigins: []string{"https://admin.sitblueprint.com"},
AllowCredentials: true,
}
// Start the HTTP server
handler := cors.New(corsOpt).Handler(
middleware.LoggingMiddleware(mux),
)
// Ignore first char of PORT
fmt.Printf("Server is running on port %s\n", PORT[1:])
if err := http.ListenAndServe(PORT, handler); err != nil {
log.Fatalf("Error starting server: %s\n", err)
}
}