gropple/main.go

92 lines
2.3 KiB
Go
Raw Permalink Normal View History

2021-09-21 08:33:24 +09:30
package main
import (
2022-05-14 16:38:48 +09:30
"flag"
2021-09-21 08:33:24 +09:30
"fmt"
"log"
"net/http"
"time"
2021-09-28 21:17:54 +09:30
"github.com/tardisx/gropple/config"
"github.com/tardisx/gropple/download"
2025-04-25 16:48:36 +09:30
v "github.com/tardisx/gropple/version"
2023-11-20 07:38:16 +10:30
"github.com/tardisx/gropple/web"
2021-09-21 08:33:24 +09:30
)
2025-04-25 16:48:36 +09:30
var (
version = "dev"
)
2021-09-21 08:33:24 +09:30
func main() {
2025-04-25 16:48:36 +09:30
versionInfo := &v.Manager{
2025-04-25 17:10:59 +09:30
// version from goreleaser has no leading 'v', even if the tag does
VersionInfo: v.Info{CurrentVersion: "v" + version},
2023-11-20 07:38:16 +10:30
}
2022-04-18 13:01:07 +09:30
log.Printf("Starting gropple %s - https://github.com/tardisx/gropple", versionInfo.GetInfo().CurrentVersion)
2022-05-14 16:38:48 +09:30
var configPath string
flag.StringVar(&configPath, "config-path", "", "path to config file")
2022-05-14 16:38:48 +09:30
flag.Parse()
2023-11-20 07:38:16 +10:30
configService := &config.ConfigService{}
2022-05-14 16:38:48 +09:30
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
2023-11-20 07:38:16 +10:30
downloadManager := &download.Manager{MaxPerDomain: configService.Config.Server.MaximumActiveDownloads}
2021-09-21 08:33:24 +09:30
2023-11-20 07:38:16 +10:30
// create the web handlers
r := web.CreateRoutes(configService, downloadManager, versionInfo)
2021-09-21 08:33:24 +09:30
srv := &http.Server{
Handler: r,
Addr: fmt.Sprintf(":%d", configService.Config.Server.Port),
2021-09-21 08:33:24 +09:30
// Good practice: enforce timeouts for servers you create!
2021-09-21 17:59:30 +09:30
WriteTimeout: 5 * time.Second,
ReadTimeout: 5 * time.Second,
2021-09-21 08:33:24 +09:30
}
2021-09-26 21:13:33 +09:30
// check for a new version every 4 hours
go func() {
for {
2023-03-15 04:45:10 +10:30
err := versionInfo.UpdateGitHubVersion()
if err != nil {
log.Printf("could not get version info: %s", err)
}
2021-09-26 21:13:33 +09:30
time.Sleep(time.Hour * 4)
}
}()
// start downloading queued downloads when slots available, and clean up
// old entries
2023-11-20 07:38:16 +10:30
go downloadManager.ManageQueue()
// add testdata if compiled with the '-tags testdata' flag
2023-11-20 07:38:16 +10:30
downloadManager.AddStressTestData(configService)
log.Printf("Visit %s for details on installing the bookmarklet and to check status", configService.Config.Server.Address)
2021-09-21 08:33:24 +09:30
log.Fatal(srv.ListenAndServe())
2021-09-26 21:13:33 +09:30
}