-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
37 lines (31 loc) · 908 Bytes
/
main.go
File metadata and controls
37 lines (31 loc) · 908 Bytes
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
package main
import (
"api-server/authHandler"
"api-server/bookHandler"
"fmt"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"net/http"
)
func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello from book-server")
})
r.Post("/login", authHandler.LoginHandler)
r.Post("/logout", authHandler.LogoutHandler)
r.Route("/books", func(r chi.Router) {
r.Use(authHandler.JWTAuthMiddleware) // Apply middleware to all routes in this group
r.Get("/", bookHandler.ListBooks)
r.Post("/", bookHandler.CreateBook)
r.Get("/{id}", bookHandler.GetBooks)
r.Put("/{id}", bookHandler.UpdateBook)
r.Delete("/{id}", bookHandler.DeleteBook)
})
fmt.Printf("Starting server on port: 8080\n")
err := http.ListenAndServe(":8080", r)
if err != nil {
fmt.Println("Could not start server", err)
}
}