This commit is contained in:
Dawid Wysokiński 2022-07-11 06:59:24 +02:00
commit 2e6d1f1e48
Signed by: Kichiyaki
GPG Key ID: 1ECC5DE481BE5184
4 changed files with 86 additions and 0 deletions

17
.gitignore vendored Normal file
View File

@ -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

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module gitea.dwysokinski.me/Kichiyaki/notificationarr
go 1.18

View File

@ -0,0 +1,4 @@
package service
type Notification struct {
}

62
main.go Normal file
View File

@ -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()
}