-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
105 lines (83 loc) · 2.48 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* @Author: Bartuccio Antoine
* @Date: 2018-09-15 22:17:31
* @Last Modified by: Bartuccio Antoine
* @Last Modified time: 2018-11-24 23:23:18
*/
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/memstore"
"github.com/gin-gonic/gin"
"gitlab.com/GT-RIMi/RIMo/goproviders"
"gitlab.com/GT-RIMi/RIMo/query"
"gitlab.com/GT-RIMi/RIMo/settings"
)
func main() {
// Loads settings
settings.InitSettings("settings.json", "settings_custom.json")
// Register Providers
if err := query.RegisterProvider(&goproviders.TmdbProvider{}, "tmdb"); err != nil {
log.Printf("Could not connect backend tmdb %s\n", err)
}
// Configure Gin
router := gin.Default()
router.Delims("{[{", "}]}")
router.LoadHTMLGlob("templates/*")
router.StaticFS("/static", http.Dir("./static"))
// Configure sessions
store := memstore.NewStore([]byte(settings.SettingsValue("session_secret").(string)))
router.Use(sessions.Sessions("memory", store))
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.gohtml", gin.H{
"settings": settings.SettingsValue("debug").(bool),
})
})
router.POST("/discover", func(c *gin.Context) {
var q query.QueryDiscoverStruct
if err := c.ShouldBindJSON(&q); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, query.Discover(&q))
})
router.POST("/search", func(c *gin.Context) {
var search query.QueryStruct
var lastSearch *query.QueryStore
if err := c.ShouldBindJSON(&search); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
session := sessions.Default(c)
sessionKey := fmt.Sprintf("last_search_%d", search.MediaType)
lastSearchRow := session.Get(sessionKey)
if lastSearchRow != nil {
if err := json.Unmarshal(lastSearchRow.([]byte), &lastSearch); err != nil {
log.Printf("could not retrieve lastSearch %s\n", err)
lastSearch = nil
}
}
response := query.QueryStore{
Query: search,
Response: query.Search(&search, lastSearch),
}
c.JSON(http.StatusOK, response.Response)
// Save response in session
exportedSearch, err := json.Marshal(response)
if err != nil {
log.Printf("error marshaling response for session %s\n", err)
return
}
session.Set(sessionKey, exportedSearch)
if err := session.Save(); err != nil {
log.Printf("error saving session %s\n", err)
}
})
if err := router.Run(); err != nil {
log.Fatal(err)
}
}