-
Notifications
You must be signed in to change notification settings - Fork 33
/
main.go
52 lines (42 loc) · 1021 Bytes
/
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
_ "embed"
"github.com/PacktPublishing/Test-Driven-Development-in-Go/chapter04/db"
"github.com/PacktPublishing/Test-Driven-Development-in-Go/chapter04/handlers"
)
//go:embed books.json
var booksFile []byte
//go:embed users.json
var usersFile []byte
func main() {
port, ok := os.LookupEnv("BOOKSWAP_PORT")
if !ok {
log.Fatal("$BOOKSWAP_PORT not found")
}
books, users := importInitial()
ps := db.NewPostingService()
b := db.NewBookService(books, ps)
u := db.NewUserService(users, b)
h := handlers.NewHandler(b, u)
router := handlers.ConfigureServer(h)
log.Printf("Listening on :%s...\n", port)
log.Fatal(http.ListenAndServe(fmt.Sprint(":", port), router))
}
func importInitial() ([]db.Book, []db.User) {
var books []db.Book
var users []db.User
err := json.Unmarshal(booksFile, &books)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(usersFile, &users)
if err != nil {
log.Fatal(err)
}
return books, users
}