forked from aunefyren/wrapperr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (105 loc) · 3.77 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
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
package main
import (
"aunefyren/wrapperr/files"
"aunefyren/wrapperr/routes"
"aunefyren/wrapperr/utilities"
"flag"
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/gorilla/mux"
_ "time/tzdata"
)
func main() {
utilities.PrintASCII()
// Create and define file for logging
file, err := os.OpenFile("config/wrapperr.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Println("Failed to load configuration file. Error: ")
log.Println(err)
fmt.Println("Failed to load configuration file. Error: ")
fmt.Println(err)
os.Exit(1)
}
config, err := files.GetConfig()
if err != nil {
log.Println("Failed to load configuration file. Error: ")
log.Println(err)
fmt.Println("Failed to load configuration file. Error: ")
fmt.Println(err)
os.Exit(1)
}
// Set time zone from config if it is not empty
if config.Timezone != "" {
loc, err := time.LoadLocation(config.Timezone)
if err != nil {
fmt.Println("Failed to set time zone from config. Error: ")
fmt.Println(err)
fmt.Println("Removing value...")
log.Println("Failed to set time zone from config. Error: ")
log.Println(err)
log.Println("Removing value...")
config.Timezone = ""
err = files.SaveConfig(config)
if err != nil {
log.Println("Failed to set new time zone in the config. Error: ")
log.Println(err)
log.Println("Exiting...")
os.Exit(1)
}
} else {
time.Local = loc
}
}
// Set log file is logging is enabled
if config.UseLogs {
log.SetOutput(file)
}
// Define port variable with the port from the config file as default
var port int
flag.IntVar(&port, "port", config.WrapperrPort, "The port Wrapperr is listening on.")
// Parse the flags from input
flag.Parse()
// Alert what port is in use
log.Println("Starting Wrapperr on port: " + strconv.Itoa(port) + ".")
fmt.Println("Starting Wrapperr on port: " + strconv.Itoa(port) + ".")
// Assign routes
router := mux.NewRouter().StrictSlash(true)
var root string
if config.WrapperrRoot != "" {
root = "/" + config.WrapperrRoot
} else {
root = ""
}
// Admin auth routes
router.HandleFunc(root+"/api/validate/admin", routes.ApiValidateAdmin)
router.HandleFunc(root+"/api/get/config", routes.ApiGetConfig)
router.HandleFunc(root+"/api/get/log", routes.ApiGetLog)
router.HandleFunc(root+"/api/set/config", routes.ApiSetConfig)
router.HandleFunc(root+"/api/update/admin", routes.ApiUpdateAdmin)
// No-auth routes
router.HandleFunc(root+"/api/get/config-state", routes.ApiWrapperrConfigured)
router.HandleFunc(root+"/api/login/admin", routes.ApiLogInAdmin)
router.HandleFunc(root+"/api/get/wrapperr-version", routes.ApiGetWrapperrVersion)
router.HandleFunc(root+"/api/get/admin-state", routes.ApiGetAdminState)
router.HandleFunc(root+"/api/get/functions", routes.ApiGetFunctions)
router.HandleFunc(root+"/api/create/admin", routes.ApiCreateAdmin)
router.HandleFunc(root+"/api/get/tautulli-connection", routes.ApiGetTautulliConncection)
router.HandleFunc(root+"/api/get/share-link", routes.ApiGetShareLink)
// User auth routes
router.HandleFunc(root+"/api/get/login-url", routes.ApiGetLoginURL)
router.HandleFunc(root+"/api/login/plex-auth", routes.ApiLoginPlexAuth)
router.HandleFunc(root+"/api/validate/plex-auth", routes.ApiValidatePlexAuth)
router.HandleFunc(root+"/api/create/share-link", routes.ApiCreateShareLink)
router.HandleFunc(root+"/api/get/user-share-link", routes.ApiGetUserShareLink)
router.HandleFunc(root+"/api/delete/user-share-link", routes.ApiDeleteUserShareLink)
// Get stats route
router.HandleFunc(root+"/api/get/statistics", routes.ApiWrapperGetStatistics)
// Static routes
router.PathPrefix(root).Handler(http.StripPrefix(root, http.FileServer(http.Dir("./web/"))))
// Start web-server
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(port), router))
}