This repository has been archived by the owner on Oct 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
161 lines (137 loc) · 4.13 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
package main
import (
"flag"
"fmt"
"github.com/gin-gonic/gin"
// gin/logger.go might report undefined: isatty.IsCygwinTerminal
// Fix: go get -u github.com/mattn/go-isatty
"github.com/elazarl/go-bindata-assetfs"
"github.com/gin-contrib/multitemplate"
"database/sql"
"github.com/gchaincl/dotsql"
_ "github.com/mattn/go-sqlite3"
"html/template"
"net/http"
"os"
)
var (
// Set the following uppercase three with -ldflags "-X main.VERSION=v1.2.3 [...]"
VERSION string = "unknown"
BUILD_COMMIT string = "unknown"
BUILD_DATE string = "unknown"
versionFlag bool
address string
port int
)
type server struct {
dotAlter *dotsql.DotSql
dotSelect *dotsql.DotSql
db *sql.DB
router *gin.Engine
}
func (s server) internalServerError(c *gin.Context, err string) {
// TODO: Better error logging
fmt.Println(err)
c.Status(http.StatusInternalServerError)
}
func main() {
flagSet := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flagSet.BoolVar(&versionFlag, "version", false, "Displays the version and license information.")
flagSet.StringVar(&address, "address", "127.0.0.1", "The address to listen on.")
flagSet.IntVar(&port, "port", 65431, "The port to listen on.")
flagSet.Parse(os.Args[1:])
switch {
case versionFlag:
fmt.Println("plexStats")
fmt.Println("https://github.com/hashworks/plexStats")
fmt.Println("Version: " + VERSION)
fmt.Println("Commit: " + BUILD_COMMIT)
fmt.Println("Build date: " + BUILD_DATE)
fmt.Println()
fmt.Println("Published under the GNU General Public License v3.0.")
default:
run()
}
}
func run() {
var s server
var err error
// Load init commands
dotInit, err := dotsql.LoadFromString(string(MustAsset("sql/init.sql")))
if err != nil {
panic(err)
}
// Load alter commands
s.dotAlter, err = dotsql.LoadFromString(string(MustAsset("sql/alter.sql")))
if err != nil {
panic(err)
}
// Load select commands
s.dotSelect, err = dotsql.LoadFromString(string(MustAsset("sql/select.sql")))
if err != nil {
panic(err)
}
// Open database file
s.db, err = sql.Open("sqlite3", "plex.db")
defer s.db.Close()
if err != nil {
fmt.Printf("Failed to open the database: %s\n", err.Error())
os.Exit(1)
}
// Init database
for _, command := range []string{
"create-table-event",
"create-trigger-rating",
"create-table-media",
"create-table-filter",
"create-table-server",
"create-table-account",
"create-table-client",
"create-table-address",
"create-table-hasDirector",
"create-table-hasProducer",
"create-table-isSimilarWith",
"create-table-hasWriter",
"create-table-hasRole",
"create-table-hasGenre",
"create-table-isFromCountry",
"create-table-isInCollection",
} {
_, err := dotInit.Exec(s.db, command)
if err != nil {
fmt.Printf("Failed to init database: %s failed with %s\n", command, err.Error())
os.Exit(1)
}
}
//gin.SetMode(gin.ReleaseMode)
s.router = gin.Default()
//s.router.SetFuncMap(templateFunctionMap())
// Load template file names from Asset
templateNames, err := AssetDir("templates")
if err != nil {
panic(err)
}
// Create a base template where we add the template functions
tmpl := template.New("")
tmpl.Funcs(templateFunctionMap())
// Iterate trough template files, load them into multitemplate
multiT := multitemplate.New()
for _, templateName := range templateNames {
basename := templateName[:len(templateName)-5]
tmpl := tmpl.New(basename)
tmpl, err := tmpl.Parse(string(MustAsset("templates/" + templateName)))
if err != nil {
panic(err)
}
multiT.Add(basename, tmpl)
fmt.Printf("Loaded templates/%s as %s\n", templateName, templateName[:len(templateName)-5])
}
// multitemplate is our new HTML renderer
s.router.HTMLRender = multiT
s.router.StaticFS("/scripts/", &assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: "node_modules/chart.js/dist"})
s.router.StaticFS("/css/", &assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: "css"})
s.router.GET("/", s.indexHandler)
s.router.GET("/playsByTime/*usernames", s.playsByTimeHandler)
s.router.POST("/webhook", s.backendHandler)
s.router.Run(fmt.Sprintf("%s:%d", address, port))
}