From 2e6d1f1e48281bab136077f2979ce4bb06f8c9aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Wysoki=C5=84ski?= Date: Mon, 11 Jul 2022 06:59:24 +0200 Subject: [PATCH] init --- .gitignore | 17 +++++++++ go.mod | 3 ++ internal/service/notification.go | 4 +++ main.go | 62 ++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 internal/service/notification.go create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..398baf2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +.idea diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e14444d --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gitea.dwysokinski.me/Kichiyaki/notificationarr + +go 1.18 diff --git a/internal/service/notification.go b/internal/service/notification.go new file mode 100644 index 0000000..7d91e40 --- /dev/null +++ b/internal/service/notification.go @@ -0,0 +1,4 @@ +package service + +type Notification struct { +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..e6a6a54 --- /dev/null +++ b/main.go @@ -0,0 +1,62 @@ +package main + +import ( + "context" + "io" + "log" + "net/http" + "os" + "os/signal" + "syscall" + "time" +) + +const ( + defaultPort = "9234" + readTimeout = 2 * time.Second + readHeaderTimeout = 2 * time.Second + writeTimeout = 2 * time.Second + idleTimeout = 2 * time.Second + serverShutdownTimeout = 10 * time.Second +) + +func main() { + srv := newServer() + + go func(srv *http.Server) { + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + log.Fatalln("srv.ListenAndServe:", err) + } + }(srv) + + log.Println("Server is listening on the port", defaultPort) + + waitForSignal(context.Background()) + + ctxShutdown, cancelShutdown := context.WithTimeout(context.Background(), serverShutdownTimeout) + defer cancelShutdown() + err := srv.Shutdown(ctxShutdown) + if err != nil { + log.Fatalln("srv.Shutdown:", err) + } +} + +func newServer() *http.Server { + 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) + }), + ReadTimeout: readTimeout, + ReadHeaderTimeout: readHeaderTimeout, + WriteTimeout: writeTimeout, + IdleTimeout: idleTimeout, + } +} + +func waitForSignal(ctx context.Context) { + ctx, stop := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM) + defer stop() + <-ctx.Done() +}