-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (58 loc) · 2.29 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
// package main
// import (
// "jwt-auth/handler"
// "log"
// "net/http"
// )
// func main() {
// http.HandleFunc("/login", handler.Login)
// http.HandleFunc("/signup", handler.Signup)
// http.HandleFunc("/home", handler.Home) //Protected
// http.HandleFunc("/refresh", handler.Refresh) //Protected
// log.Fatal(http.ListenAndServe(":8080", nil))
// }
package main
import (
"log"
"net/http"
"jwt-auth/db"
"jwt-auth/handler" // Replace with your actual module name
"jwt-auth/utils"
"github.com/rs/cors"
)
func main() {
mux := http.NewServeMux()
pgdb := db.NewMydb()
// defer pgdb
// Define your route handlers
mux.HandleFunc("/login", handler.Login(pgdb))
mux.HandleFunc("/signup", handler.Signup(pgdb))
mux.HandleFunc("/home", utils.AuthMiddleware((handler.Home)))
// Protected
mux.HandleFunc("/refresh", handler.Refresh) // Protected
mux.HandleFunc("/check-auth", utils.AuthMiddleware(handler.CheckAuth))
mux.HandleFunc("/getuser/{id}", utils.AuthMiddleware(handler.GetUserById(pgdb)))
mux.HandleFunc("/getuser/{id}/workoutplan", utils.AuthMiddleware(handler.GetUserWorkOutPlan(pgdb)))
mux.HandleFunc("/getuser/{id}/cardioplan", utils.AuthMiddleware(handler.GetUserCardioPlan(pgdb)))
mux.HandleFunc("/getuser/{id}/dietplan", utils.AuthMiddleware(handler.GetUserDietPlan(pgdb)))
mux.HandleFunc("/userinput/{id}", utils.AuthMiddleware(handler.GetUserInput(pgdb)))
mux.HandleFunc("/userdailydata/{id}", utils.AuthMiddleware(handler.GetUserDailyData(pgdb)))
mux.HandleFunc("/userdailydata/{id}/{date}", utils.AuthMiddleware(handler.GetUserDailyDatabyDate(pgdb)))
mux.HandleFunc("/userdailydatatarget/{id}", utils.AuthMiddleware(handler.GetUserDailyDataTargetData(pgdb)))
mux.HandleFunc("/userdailydatatarget/{id}/{date}", utils.AuthMiddleware(handler.GetUserDailyDataTargetDatabyDate(pgdb)))
// Set up CORS
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"}, // Replace with your Flutter app's URL
AllowedMethods: []string{"GET", "POST", "OPTIONS"},
AllowedHeaders: []string{"*"},
ExposedHeaders: []string{"*"},
AllowPrivateNetwork: true,
AllowCredentials: true,
// OptionsPassthrough: true,
Debug: true,
})
// Wrap your router with the CORS handler
handler := c.Handler(mux)
// Start the server
log.Fatal(http.ListenAndServe(":8080", handler))
}