forked from XiovV/Golty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (97 loc) · 3.13 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
package main
import (
"flag"
"io"
"net/http"
"os"
"time"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
var (
port string
dlRoot string
)
func initLogFile() {
logFile, err := os.OpenFile("log.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Error("Error reading log file: ", err)
}
mw := io.MultiWriter(os.Stdout, logFile)
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(mw)
}
func initDatabase() {
log.Info("Initiating db")
databases := [3]string{"./config/channels.json", "./config/playlists.json", "./config/videos.json"}
for _, database := range databases {
createDir(database)
}
}
func init() {
flag.StringVar(&port, "port", "8080", "port to listen")
flag.StringVar(&dlRoot, "dl", "./downloads", "root download folder")
}
func uploadCheckerChannels() {
interval, err := GetCheckingInterval(TypeChannel)
if err != nil {
log.Errorf("uploadCheckerChannels: %v", err)
}
if interval == 0 {
time.Sleep(5 * time.Minute)
uploadCheckerChannels()
} else if interval != 0 {
for {
if interval != 0 {
time.Sleep(time.Duration(interval) * time.Minute)
go CheckAll("channels")
log.Infof("upload Checker for channels running every %v minutes", interval)
}
}
}
}
func uploadCheckerPlaylists() {
interval, err := GetCheckingInterval(TypePlaylist)
if err != nil {
log.Errorf("uploadCheckerPlaylists: %v", err)
}
if interval == 0 {
time.Sleep(5 * time.Minute)
uploadCheckerPlaylists()
} else if interval != 0 {
for {
if interval != 0 {
<-time.After(time.Duration(interval) * time.Minute)
go CheckAll("playlists")
log.Infof("upload Checker for playlists running every %v minutes", interval)
}
}
}
}
func main() {
flag.Parse()
log.Info("server running on port " + port)
go uploadCheckerChannels()
go uploadCheckerPlaylists()
r := mux.NewRouter()
headers := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"})
methods := handlers.AllowedMethods([]string{"GET", "POST"})
origins := handlers.AllowedOrigins([]string{"*"})
r.HandleFunc("/", HandleIndex).Methods("GET")
r.HandleFunc("/logs", HandleLogs).Methods("GET")
r.HandleFunc("/playlists", HandlePlaylists).Methods("GET")
r.HandleFunc("/videos", HandleVideos).Methods("GET")
r.HandleFunc("/api/add", HandleAddTarget).Methods("POST")
r.HandleFunc("/api/check", HandleCheckTarget).Methods("POST")
r.HandleFunc("/api/delete", HandleDeleteTarget).Methods("POST")
r.HandleFunc("/api/get", HandleGetTargets).Methods("POST")
r.HandleFunc("/api/check-all", HandleCheckAllTargets).Methods("POST")
r.HandleFunc("/api/update-checking-interval", HandleUpdateCheckingInterval).Methods("POST")
r.HandleFunc("/api/get-videos", HandleGetVideos).Methods("GET")
r.HandleFunc("/api/download-video", HandleDownloadVideo).Methods("POST")
r.HandleFunc("/api/version", HandleGetVersion).Methods("GET")
r.HandleFunc("/favicon.ico", ServeFavicon).Methods("GET")
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
log.Fatal(http.ListenAndServe(":"+port, handlers.CORS(headers, methods, origins)(r)))
}