66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/debridmediamanager.com/zurg/internal/config"
|
|
"github.com/debridmediamanager.com/zurg/internal/net"
|
|
"github.com/debridmediamanager.com/zurg/internal/torrent"
|
|
"github.com/debridmediamanager.com/zurg/pkg/logutil"
|
|
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
|
|
"github.com/hashicorp/golang-lru/v2/expirable"
|
|
)
|
|
|
|
func main() {
|
|
log := logutil.NewLogger().Named("zurg")
|
|
|
|
config, configErr := config.LoadZurgConfig("./config.yml")
|
|
if configErr != nil {
|
|
log.Panicf("Config failed to load: %v", configErr)
|
|
}
|
|
|
|
cache := expirable.NewLRU[string, string](1e4, nil, time.Hour)
|
|
|
|
rd := realdebrid.NewRealDebrid(config.GetToken(), config, logutil.NewLogger().Named("realdebrid"))
|
|
|
|
torrentMgr := torrent.NewTorrentManager(config, rd)
|
|
|
|
mux := http.NewServeMux()
|
|
net.Router(mux, config, torrentMgr, cache)
|
|
|
|
addr := fmt.Sprintf("%s:%s", config.GetHost(), config.GetPort())
|
|
server := &http.Server{Addr: addr, Handler: mux}
|
|
|
|
shutdown := make(chan os.Signal, 1)
|
|
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
log.Errorf("Server panic: %v\n", r)
|
|
}
|
|
}()
|
|
log.Infof("Starting server on %s", addr)
|
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.Panicf("Failed to start server: %v", err)
|
|
}
|
|
}()
|
|
|
|
<-shutdown
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
if err := server.Shutdown(ctx); err != nil {
|
|
log.Errorf("Server shutdown error: %v\n", err)
|
|
}
|
|
|
|
log.Info("BYE")
|
|
}
|