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"
|
2021-09-30 23:48:56 +09:30
|
|
|
"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-04-07 21:46:39 +09:30
|
|
|
|
2022-05-14 16:38:48 +09:30
|
|
|
var configPath string
|
|
|
|
flag.StringVar(&configPath, "config-path", "", "path to config file")
|
2023-11-11 13:07:26 +10:30
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-04-07 21:46:39 +09:30
|
|
|
exists, err := configService.ConfigFileExists()
|
2022-04-07 20:39:14 +09:30
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if !exists {
|
2021-09-30 12:27:59 +09:30
|
|
|
log.Print("No config file - creating default config")
|
2022-04-07 21:46:39 +09:30
|
|
|
configService.LoadDefaultConfig()
|
|
|
|
configService.WriteConfig()
|
|
|
|
log.Printf("Configuration written to %s", configService.ConfigPath)
|
2021-09-30 12:27:59 +09:30
|
|
|
} else {
|
2022-04-07 21:46:39 +09:30
|
|
|
err := configService.LoadConfig()
|
2021-09-30 12:27:59 +09:30
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-04-07 21:46:39 +09:30
|
|
|
log.Printf("Configuration loaded from %s", configService.ConfigPath)
|
2021-09-30 12:27:59 +09:30
|
|
|
}
|
2021-09-24 15:35:54 +09:30
|
|
|
|
2022-04-18 19:53:07 +09:30
|
|
|
// 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,
|
2022-04-07 20:39:14 +09:30
|
|
|
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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-11-21 16:19:49 +10:30
|
|
|
// start downloading queued downloads when slots available, and clean up
|
|
|
|
// old entries
|
2023-11-20 07:38:16 +10:30
|
|
|
go downloadManager.ManageQueue()
|
2023-11-19 20:58:26 +10:30
|
|
|
|
|
|
|
// add testdata if compiled with the '-tags testdata' flag
|
2023-11-20 07:38:16 +10:30
|
|
|
downloadManager.AddStressTestData(configService)
|
2022-07-05 20:43:32 +09:30
|
|
|
|
2022-04-07 21:46:39 +09:30
|
|
|
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())
|
2022-04-07 20:39:14 +09:30
|
|
|
|
2021-09-26 21:13:33 +09:30
|
|
|
}
|