-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (131 loc) · 4.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
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
"github.com/a-h/templ"
"github.com/go-chi/chi/v5"
"github.com/go-chi/cors"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
"github.com/sumer312/Health-App-Backend/controllers"
"github.com/sumer312/Health-App-Backend/internal/database"
"github.com/sumer312/Health-App-Backend/views/pages"
"github.com/sumer312/Health-App-Backend/views/partials"
)
func main() {
godotenv.Load()
port := "5000"
router := chi.NewRouter()
viewRouter := chi.NewRouter()
serverRouter := chi.NewRouter()
dbConnString := os.Getenv("DB_URL")
conn, connerr := sql.Open("postgres",
dbConnString,
)
if connerr != nil {
log.Fatalln("error connecting", connerr)
}
db := database.New(conn)
apiCfg := controllers.Api{DB: db}
router.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"https://*", "http://*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"*"},
AllowCredentials: true,
MaxAge: 3600,
}))
viewRouter.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
if partials.DrawerAuthFlag {
pages.Random("Already Logged In").Render(r.Context(), w)
} else {
pages.Login().Render(r.Context(), w)
}
})
viewRouter.HandleFunc("/signup", func(w http.ResponseWriter, r *http.Request) {
if partials.DrawerAuthFlag {
pages.Random("Already Logged In").Render(r.Context(), w)
} else {
pages.Signup().Render(r.Context(), w)
}
})
viewRouter.HandleFunc("/user-input/fatloss", func(w http.ResponseWriter, r *http.Request) {
if partials.DrawerAuthFlag {
pages.UserInputFatloss().Render(r.Context(), w)
} else {
pages.Login().Render(r.Context(), w)
}
})
viewRouter.HandleFunc("/user-input/muscle", func(w http.ResponseWriter, r *http.Request) {
if partials.DrawerAuthFlag {
pages.UserInputMuscle().Render(r.Context(), w)
} else {
pages.Login().Render(r.Context(), w)
}
})
viewRouter.HandleFunc("/user-input/maintain", func(w http.ResponseWriter, r *http.Request) {
if partials.DrawerAuthFlag {
pages.UserInputMaintain().Render(r.Context(), w)
} else {
pages.Login().Render(r.Context(), w)
}
})
viewRouter.HandleFunc(
"/logs",
viewRenderInControllerMiddleware(func(w http.ResponseWriter, r *http.Request) {
apiCfg.LogsRender(w, r)
}),
)
viewRouter.HandleFunc(
"/daily-input",
viewRenderInControllerMiddleware(func(w http.ResponseWriter, r *http.Request) {
apiCfg.DailyNutritionRender(w, r)
}),
)
viewRouter.HandleFunc(
"/profile",
viewRenderInControllerMiddleware(func(w http.ResponseWriter, r *http.Request) {
apiCfg.ProfileRender(w, r)
}),
)
viewRouter.HandleFunc(
"/kcal-calc",
viewRenderInControllerMiddleware(func(w http.ResponseWriter, r *http.Request) {
pages.KcalCalc().Render(r.Context(), w)
}),
)
serverRouter.Post("/login", apiCfg.LoginHandler)
serverRouter.Post("/signup", apiCfg.SignupHandler)
serverRouter.Post("/logout", apiCfg.LogoutHandler)
serverRouter.Post("/user-input", controllerMiddleware(apiCfg.InputHandler))
serverRouter.Post("/nutrition-api-request", controllerMiddleware(apiCfg.ApiRequest))
serverRouter.Post("/daily-input", controllerMiddleware(apiCfg.DailyNutritionInputHandler))
serverRouter.Post(
"/daily-input-delete",
controllerMiddleware(apiCfg.DailyNutritionDeleteRowById),
)
serverRouter.Delete("/change-program", controllerMiddleware(apiCfg.ChangeProgram))
serverRouter.Delete("/delete-user", controllerMiddleware(apiCfg.DeleteUser))
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if partials.DrawerAuthFlag {
apiCfg.HomeRender(w, r)
} else {
pages.Programs().Render(r.Context(), w)
}
})
viewRouter.Handle("/*", templ.Handler(pages.Random("404 not found")))
router.Mount("/view", viewRouter)
router.Mount("/server", serverRouter)
router.Handle("/*", templ.Handler(pages.Random("404 not found")))
srv := &http.Server{
Handler: router,
Addr: ":" + port,
}
fmt.Printf("Using chi \nServer starting on port %s\n", port)
err := srv.ListenAndServe()
if err != nil {
log.Fatalln("OOPs something went wrong")
}
}