-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
136 lines (111 loc) · 4.66 KB
/
handler.go
File metadata and controls
136 lines (111 loc) · 4.66 KB
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
package handler
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"github.com/goclient-vanmango/apiclient"
"github.com/goclient-vanmango/routes"
"github.com/goclient-vanmango/service"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
)
type FormData struct {
Username string
Password string
}
type VanCreationFormData struct {
Name string
Brand string
Description string
Category string
FuelType string
Price string
Image string
VanCode string
}
func Handler(w http.ResponseWriter, r *http.Request) {
apiclient := apiclient.NewAPIClient()
apiService := service.NewAPIService(apiclient)
apiHandler := routes.NewAPIHandler(apiService)
router := mux.NewRouter()
// Serve static files under /static
router.Handle("/static/{file:.*}", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
// Serve assets files under /assets
router.Handle("/assets/{file:.*}", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/"))))
// Serve static files
// fs := http.FileServer(http.Dir("./static"))
// http.Handle("/static/", http.StripPrefix("/static/", fs))
// // Serve asset files
// fsAssets := http.FileServer(http.Dir("./assets"))
// http.Handle("/assets/", http.StripPrefix("/assets/", fsAssets))
// Handle routes
router.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Add("Accept-Content", "application/json")
json.NewEncoder(w).Encode(struct {
Code int
Message string
}{
Code: http.StatusOK,
Message: "Client server is functioning",
})
log.Println("Client server is functioning")
})
router.HandleFunc("/", apiHandler.PageHomeHandler).Methods(http.MethodGet)
router.HandleFunc("/sign-in", apiHandler.PageSignInHandler).Methods(http.MethodGet)
router.HandleFunc("/logout", apiHandler.LogoutHandler).Methods(http.MethodGet)
router.HandleFunc("/authorize", apiHandler.AuthorizeUserHandler).Methods(http.MethodPost)
router.HandleFunc("/van-details", apiHandler.PageVanDetailsHandler).Methods(http.MethodGet)
router.HandleFunc("/create-van", apiHandler.PageCreateVanHandler).Methods(http.MethodPost)
router.HandleFunc("/create-new-van", apiHandler.CreateNewVanHandler).Methods(http.MethodPost)
router.HandleFunc("/van-details/update-van", apiHandler.PageUpdateVanHandler).Methods(http.MethodGet)
router.HandleFunc("/update-van/{vanID}", apiHandler.UpdateVanHandler).Methods(http.MethodPost)
router.HandleFunc("/van-details/delete-van", apiHandler.DeleteVanHandler).Methods(http.MethodGet)
_ = godotenv.Load()
port := os.Getenv("PORT")
// Start server
fmt.Println("Client server is listening on PORT ", port)
router.ServeHTTP(w, r)
}
// func main() {
// apiclient := apiclient.NewAPIClient()
// apiService := service.NewAPIService(apiclient)
// apiHandler := routes.NewAPIHandler(apiService)
// // Serve static files
// fs := http.FileServer(http.Dir("./static"))
// http.Handle("/static/", http.StripPrefix("/static/", fs))
// // Serve asset files
// fsAssets := http.FileServer(http.Dir("./assets"))
// http.Handle("/assets/", http.StripPrefix("/assets/", fsAssets))
// // Handle routes
// router := mux.NewRouter()
// router.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
// w.WriteHeader(http.StatusOK)
// w.Header().Add("Accept-Content", "application/json")
// json.NewEncoder(w).Encode(struct {
// Code int
// Message string
// }{
// Code: http.StatusOK,
// Message: "Client server is functioning",
// })
// log.Println("Client server is functioning")
// })
// router.HandleFunc("/", apiHandler.PageHomeHandler).Methods(http.MethodGet)
// router.HandleFunc("/sign-in", apiHandler.PageSignInHandler).Methods(http.MethodGet)
// router.HandleFunc("/logout", apiHandler.LogoutHandler).Methods(http.MethodGet)
// router.HandleFunc("/authorize", apiHandler.AuthorizeUserHandler).Methods(http.MethodGet)
// router.HandleFunc("/van-details", apiHandler.PageVanDetailsHandler).Methods(http.MethodGet)
// router.HandleFunc("/create-van", apiHandler.PageCreateVanHandler).Methods(http.MethodGet)
// router.HandleFunc("/create-new-van", apiHandler.CreateNewVanHandler).Methods(http.MethodGet)
// router.HandleFunc("/van-details/update-van", apiHandler.PageUpdateVanHandler).Methods(http.MethodGet)
// router.HandleFunc("/update-van/{vanID}", apiHandler.UpdateVanHandler).Methods(http.MethodGet)
// router.HandleFunc("/van-details/delete-van", apiHandler.DeleteVanHandler).Methods(http.MethodGet)
// _ = godotenv.Load()
// port := os.Getenv("PORT")
// // Start server
// fmt.Println("Client server is listening on PORT ", port)
// log.Fatal(http.ListenAndServe(":"+port, router))
// }