-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (51 loc) · 1.46 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
58
59
60
61
62
package main
import (
"context"
"encoding/json"
"log"
"net/http"
"os"
"github.com/Qalifah/movieReviewApi/db"
"github.com/Qalifah/movieReviewApi/handler"
"github.com/Qalifah/movieReviewApi/models"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
func main() {
gormHandler, err := gorm.Open("postgres", os.Getenv("DB_ADDRESS"))
if err != nil {
log.Fatalln(err)
}
requestHandler := handler.NewRequestHandler(
db.NewMovieRepository(redis.NewClient(&redis.Options{Addr: "localhost:6379"})),
db.NewCommentRepository(gormHandler),
)
router := gin.Default()
router.GET("/movies", requestHandler.AllMovies)
router.POST("/movies/:movie_id/comment", requestHandler.AddComment)
router.GET("/movies/:movie_id/comment", requestHandler.ListComments)
router.GET("/movies/:movie_id/characters", requestHandler.ListCharacters)
log.Fatal(router.Run(":8080"))
}
func PopulateRedis(ctx context.Context, data models.Movies, client *redis.Client) (*redis.Client, error) {
for _, item := range data {
err := client.Set(ctx, item.Title, item, 0).Err()
if err != nil {
return nil, err
}
}
return client, nil
}
func GetDataFromAPI() (models.Movies, error) {
resp, err := http.Get("https://swapi.dev/api/films/")
if err != nil {
return nil, err
}
var movies models.Movies
if err = json.NewDecoder(resp.Body).Decode(&movies); err != nil {
return nil, err
}
return movies, nil
}