forked from wizelineacademy/2022Q2GO-Bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghibli.go
61 lines (48 loc) · 1.24 KB
/
ghibli.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
package main
import (
"log"
"net/http"
"os"
"github.com/hochata/2022Q2GO-Bootcamp/adapters/controllers"
"github.com/hochata/2022Q2GO-Bootcamp/adapters/csv"
"github.com/hochata/2022Q2GO-Bootcamp/adapters/external"
"github.com/hochata/2022Q2GO-Bootcamp/entities"
)
const (
filmsPath = "data/ghibli.csv"
speciesPath = "data/species.csv"
speciesLimit = 5
)
func main() {
data, err := os.Open(filmsPath)
if err != nil {
log.Fatal(err)
}
defer data.Close()
router := http.NewServeMux()
csvRepo, err := csv.NewRepo(data, csv.NewFilmCsvCaster())
if err != nil {
log.Fatal(err)
}
filmCotroller := controllers.NewBasicController[entities.Film](
csvRepo,
)
speciesRepo := external.NewExternalRepository[entities.Species](
"https://ghibliapi.herokuapp.com/species", speciesLimit)
cacheFile, err := os.Create(speciesPath)
if err != nil {
log.Fatal(err)
}
defer cacheFile.Close()
cachedRepo := csv.NewCachedRepo(
csv.NewSpeciesCsvCaster(),
cacheFile,
)
cachedRepo.Hook(speciesRepo)
speciesController := controllers.NewBasicController[entities.Species](
cachedRepo,
)
router.HandleFunc("/films", filmCotroller.Serve)
router.HandleFunc("/species", speciesController.Serve)
log.Fatal(http.ListenAndServe(":8009", router))
}