-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (48 loc) · 1.41 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
package main
import (
"github.com/barbibrussa/bookmanager/pkg/models"
"github.com/barbibrussa/bookmanager/pkg/server"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"log"
"net/http"
"os"
)
func main() {
db, err := gorm.Open(mysql.Open(server.NewDSN()), &gorm.Config{})
if err != nil {
log.Fatal("Error while connecting to the database: ", err)
}
err = db.AutoMigrate(&models.Book{}, &models.Checkout{}, &models.Review{})
if err != nil {
log.Fatal("Error while auto-migrating models: ", err)
}
r := chi.NewRouter()
s := server.NewServer(db)
user := os.Getenv("APP_USER")
password := os.Getenv("APP_PASSWORD")
if len(user) == 0 || len(password) == 0 {
log.Fatalf("Error: empty user and password credentials")
return
}
r.Use(middleware.BasicAuth("librarians", map[string]string{
user: password,
}))
r.Get("/books", s.ListBooks)
r.Post("/books", s.CreateBook)
r.Delete("/books/{id}", s.DeleteBook)
r.Get("/books/{id}", s.GetBook)
r.Post("/books/{id}/borrow", s.BorrowBook)
r.Put("/books/{id}/return", s.ReturnBook)
r.Get("/checkouts", s.ListCheckouts)
r.Get("/books/available", s.ListBooksAvailable)
r.Post("/books/{id}/review", s.CreateReview)
r.Get("/books/{id}/reviews", s.ListBookReviews)
r.Delete("/review/{id}", s.DeleteReview)
err = http.ListenAndServe(":3030", r)
if err != nil {
log.Fatal("Error serving HTPP on port :3030 ", err)
}
}