-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
86 lines (71 loc) · 2.19 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"time"
"github.com/tardisx/gropple/config"
"github.com/tardisx/gropple/download"
"github.com/tardisx/gropple/version"
"github.com/tardisx/gropple/web"
)
func main() {
versionInfo := &version.Manager{
VersionInfo: version.Info{CurrentVersion: "v1.1.3"},
}
log.Printf("Starting gropple %s - https://github.com/tardisx/gropple", versionInfo.GetInfo().CurrentVersion)
var configPath string
flag.StringVar(&configPath, "config-path", "", "path to config file")
flag.Parse()
configService := &config.ConfigService{}
if configPath != "" {
configService.ConfigPath = configPath
} else {
configService.DetermineConfigDir()
}
exists, err := configService.ConfigFileExists()
if err != nil {
log.Fatal(err)
}
if !exists {
log.Print("No config file - creating default config")
configService.LoadDefaultConfig()
configService.WriteConfig()
log.Printf("Configuration written to %s", configService.ConfigPath)
} else {
err := configService.LoadConfig()
if err != nil {
log.Fatal(err)
}
log.Printf("Configuration loaded from %s", configService.ConfigPath)
}
// create the download manager
downloadManager := &download.Manager{MaxPerDomain: configService.Config.Server.MaximumActiveDownloads}
// create the web handlers
r := web.CreateRoutes(configService, downloadManager, versionInfo)
srv := &http.Server{
Handler: r,
Addr: fmt.Sprintf(":%d", configService.Config.Server.Port),
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 5 * time.Second,
ReadTimeout: 5 * time.Second,
}
// check for a new version every 4 hours
go func() {
for {
err := versionInfo.UpdateGitHubVersion()
if err != nil {
log.Printf("could not get version info: %s", err)
}
time.Sleep(time.Hour * 4)
}
}()
// start downloading queued downloads when slots available, and clean up
// old entries
go downloadManager.ManageQueue()
// add testdata if compiled with the '-tags testdata' flag
downloadManager.AddStressTestData(configService)
log.Printf("Visit %s for details on installing the bookmarklet and to check status", configService.Config.Server.Address)
log.Fatal(srv.ListenAndServe())
}