feat: add sonarr support
continuous-integration/drone/pr Build is failing
Details
continuous-integration/drone/pr Build is failing
Details
This commit is contained in:
parent
e9f35e9bf4
commit
159f1c1a6c
96
main.go
96
main.go
|
@ -2,26 +2,38 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"gitea.dwysokinski.me/Kichiyaki/notificationarr/internal/rest"
|
||||
"gitea.dwysokinski.me/Kichiyaki/notificationarr/internal/service"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultPort = "9234"
|
||||
readTimeout = 2 * time.Second
|
||||
readHeaderTimeout = 2 * time.Second
|
||||
writeTimeout = 2 * time.Second
|
||||
idleTimeout = 2 * time.Second
|
||||
serverShutdownTimeout = 10 * time.Second
|
||||
defaultPort = "9234"
|
||||
readTimeout = 2 * time.Second
|
||||
readHeaderTimeout = 2 * time.Second
|
||||
writeTimeout = 2 * time.Second
|
||||
idleTimeout = 2 * time.Second
|
||||
serverShutdownTimeout = 10 * time.Second
|
||||
ntfyDefaultUrl = "https://ntfy.sh"
|
||||
publisherDefaultTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
func main() {
|
||||
srv := newServer()
|
||||
srv, err := newServer()
|
||||
if err != nil {
|
||||
log.Fatalln("newServer:", err)
|
||||
}
|
||||
|
||||
go func(srv *http.Server) {
|
||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
|
@ -35,24 +47,78 @@ func main() {
|
|||
|
||||
ctxShutdown, cancelShutdown := context.WithTimeout(context.Background(), serverShutdownTimeout)
|
||||
defer cancelShutdown()
|
||||
err := srv.Shutdown(ctxShutdown)
|
||||
err = srv.Shutdown(ctxShutdown)
|
||||
if err != nil {
|
||||
log.Println("srv.Shutdown:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func newServer() *http.Server {
|
||||
func newServer() (*http.Server, error) {
|
||||
router, err := newRouter()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("newRouter: %w", err)
|
||||
}
|
||||
|
||||
return &http.Server{
|
||||
Addr: ":" + defaultPort,
|
||||
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = io.Copy(os.Stdout, r.Body)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}),
|
||||
Addr: ":" + defaultPort,
|
||||
Handler: router,
|
||||
ReadTimeout: readTimeout,
|
||||
ReadHeaderTimeout: readHeaderTimeout,
|
||||
WriteTimeout: writeTimeout,
|
||||
IdleTimeout: idleTimeout,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func newRouter() (*chi.Mux, error) {
|
||||
sonarrSvc, err := newSonarrService()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("newSonarrService: %w", err)
|
||||
}
|
||||
|
||||
r := chi.NewRouter()
|
||||
r.Use(
|
||||
middleware.RealIP,
|
||||
middleware.RequestLogger(&middleware.DefaultLogFormatter{
|
||||
NoColor: true,
|
||||
Logger: log.Default(),
|
||||
}),
|
||||
middleware.Recoverer,
|
||||
middleware.Heartbeat("/health"),
|
||||
)
|
||||
rest.NewWebhookHandler(sonarrSvc).Register(r)
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func newSonarrService() (*service.Sonarr, error) {
|
||||
url := os.Getenv("SONARR_NTFY_URL")
|
||||
if url == "" {
|
||||
url = ntfyDefaultUrl
|
||||
}
|
||||
|
||||
topic := os.Getenv("SONARR_NTFY_TOPIC")
|
||||
if topic == "" {
|
||||
return nil, errors.New(`env "SONARR_NTFY_TOPIC" is required`)
|
||||
}
|
||||
|
||||
timeout := os.Getenv("SONARR_NTFY_REQ_TIMEOUT")
|
||||
parsedTimeout := publisherDefaultTimeout
|
||||
var err error
|
||||
if timeout != "" {
|
||||
parsedTimeout, err = time.ParseDuration(timeout)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(`os.Getenv("SONARR_NTFY_REQ_TIMEOUT"): %w`, err)
|
||||
}
|
||||
|
||||
return service.NewSonaar(
|
||||
service.NewNtfy(
|
||||
&http.Client{Timeout: parsedTimeout},
|
||||
url,
|
||||
topic,
|
||||
os.Getenv("SONARR_NTFY_USERNAME"),
|
||||
os.Getenv("SONARR_NTFY_PASSWORD"),
|
||||
),
|
||||
), nil
|
||||
}
|
||||
|
||||
func waitForSignal(ctx context.Context) {
|
||||
|
|
Reference in New Issue