-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
219 lines (199 loc) · 7.03 KB
/
server.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// oerc, alias oer-collector
// Copyright (C) 2021-2024 emschu[aet]mailbox.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public
// License along with this program.
// If not, see <https://www.gnu.org/licenses/>.
package main
import (
"fmt"
rice "github.com/GeertJohan/go.rice"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"log"
"net"
"net/http"
"strings"
"time"
)
// StartServer method to start the built-in gin web server to serve the JSON Api
func StartServer() {
if isDebug() {
gin.SetMode("debug")
} else {
gin.SetMode("release")
}
go setupMaterializedView()
r := initRouter()
log.Printf("Starting API server...\n")
ip := net.ParseIP(GetAppConf().ServerHost)
var err error
if ip.To4() == nil {
// ipv6
err = r.Run(fmt.Sprintf("[%s]:%d", GetAppConf().ServerHost, GetAppConf().ServerPort))
} else {
err = r.Run(fmt.Sprintf("%s:%d", GetAppConf().ServerHost, GetAppConf().ServerPort))
}
if err != nil {
log.Printf("Problem starting server %v\n", err)
return
}
}
func setupMaterializedView() {
db := getDb()
db.Exec(`drop materialized view status_info`)
db.Exec(fmt.Sprintf(`create materialized view status_info as %s`, materializedStatusView))
if isDebug() {
log.Printf("Materialized status view")
}
}
// StatusInfoModel a gorm model for the materialized view
type StatusInfoModel struct {
ChannelFamilyCount uint64
ChannelCount uint64
ProgramEntryCount uint64
TvShowCount uint64
ImageLinkCount uint64
LogCount uint64
RecommendationCount uint64
DataStartTime string
DataEndTime string
}
// TableName of the materialized view prefixed by db schema
func (s *StatusInfoModel) TableName() string {
return fmt.Sprintf("%s.status_info", appConf.DbSchema)
}
// StatusResponse api object
type StatusResponse struct {
ChannelFamilyCount uint64 `json:"channel_family_count"`
ChannelCount uint64 `json:"channel_count"`
ProgramEntryCount uint64 `json:"program_entry_count"`
TvShowCount uint64 `json:"tv_show_count"`
ImageLinksCount uint64 `json:"image_links_count"`
LogCount uint64 `json:"log_count"`
RecommendationCount uint64 `json:"recommendation_count"`
Version string `json:"version"`
ServerDateTime string `json:"server_date_time"`
DataStartTime string `json:"data_start_time"`
DataEndTime string `json:"data_end_time"`
TvChannels *[]Channel `json:"tv_channels"`
TvChannelFamilies *[]ChannelFamily `json:"tv_channel_families"`
}
// ProgramResponse program response object
type ProgramResponse struct {
From *time.Time `json:"from"`
To *time.Time `json:"to"`
ChannelID int64 `json:"channel_id"`
Size int `json:"size"`
ProgramEntryList *[]ProgramEntry `json:"program_list"`
}
// ChannelResponse channel response object
type ChannelResponse struct {
Data *[]Channel `json:"data"`
Size int `json:"size"`
}
// LogEntriesResponse response object
type LogEntriesResponse struct {
Elements *[]LogEntry `json:"elements"`
Size int64 `json:"size"`
Page int64 `json:"page"`
PageCount int64 `json:"page_count"`
EntryCount int64 `json:"entry_count"`
}
// Error api return object
type Error struct {
Status string `json:"status"`
Message string `json:"message"`
}
// initRouter initialize routing information
func initRouter() *gin.Engine {
r := gin.New()
r.RedirectTrailingSlash = true
r.Use(gin.Logger())
r.Use(gzip.Gzip(gzip.DefaultCompression))
if appConf.ProxyURL != "" {
err := r.SetTrustedProxies([]string{appConf.ProxyURL})
if err != nil {
log.Fatal(fmt.Sprintf("Problem to trust proxy url '%s'", appConf.ProxyURL))
return nil
}
} else {
// trust no proxies - except the user specified one
err := r.SetTrustedProxies(nil)
if err != nil {
log.Fatal(fmt.Sprint("Problem to trust nil proxy url"))
return nil
}
}
r.Use(gin.Recovery())
box := rice.MustFindBox("spec")
if box == nil {
log.Fatal("Error retrieving specs rice box")
}
r.StaticFS("/spec", box.HTTPBox())
// define group
apiPrefix := "/api/v2"
apiV2 := r.Group(apiPrefix)
apiV2.Use(func(context *gin.Context) {
if len(GetAppConf().AccessControlAllowOrigin) > 0 {
context.Header("Access-Control-Allow-Origin", fmt.Sprintf("%s", GetAppConf().AccessControlAllowOrigin))
} else {
log.Println("Warning! Using insecure default value '*' for 'Access-Control-Allow-Origin' (CORS) header. Please specify a value 'AccessControlAllowOrigin' in .oerc.yaml.")
context.Header("Access-Control-Allow-Origin", "*")
}
// enable 10 h caching for /program, /log and /channel endpoints for browsers by default
if strings.HasPrefix(context.FullPath(), apiPrefix+"/program") ||
context.FullPath() == apiPrefix+"/log" ||
strings.HasPrefix(context.FullPath(), apiPrefix+"/channel") {
context.Header("Cache-Control", "public, max-age=36000")
}
})
// default redirect to client app and ping redirect
r.GET("/", func(context *gin.Context) {
context.Redirect(301, "/client")
})
r.GET("/ping", func(context *gin.Context) {
context.Redirect(301, "/api/v2/ping")
})
// ping
apiV2.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"data": "Pong", "date": time.Now()})
})
apiV2.GET("/status", getStatusHandler)
// channel data
apiV2.GET("/channels", getChannelsHandler)
apiV2.GET("/channel/:channel_id", getSingleChannelHandler)
// program data
apiV2.GET("/program/yesterday", getProgramYesterdayHandler)
apiV2.GET("/program/daily", getProgramTodayHandler)
apiV2.GET("/program/tomorrow", getProgramTomorrowHandler)
apiV2.GET("/program/daily/:channel_id", getDailyProgramWithChannelHandler)
apiV2.GET("/program/tomorrow/:channel_id", getTomorrowProgramWithChannelHandler)
apiV2.GET("/program/yesterday/:channel_id", getYesterdayProgramWithChannelHandler)
apiV2.GET("/program", getProgramHandler)
apiV2.GET("/program/entry/:id", getSingleProgramEntryHandler)
// log data
apiV2.GET("/log", getLogEntriesHandler)
apiV2.GET("/log/entry/:id", getSingleLogEntriesHandler)
apiV2.DELETE("/log/entry/:id", deleteSingleLogEntriesHandler)
apiV2.DELETE("/log/clear", clearAllLogEntriesHandler)
// recommendations
apiV2.GET("/recommendations", getRecommendationsHandler)
// search
apiV2.GET("/search", getSearchHandler)
if GetAppConf().ClientEnabled {
clientBox := rice.MustFindBox("client/dist/client").HTTPBox()
r.StaticFS("/client", clientBox)
}
return r
}