summaryrefslogtreecommitdiff
path: root/internal/server/main.go
diff options
context:
space:
mode:
authorGrailFinder <wohilas@gmail.com>2024-03-17 13:13:01 +0300
committerGrailFinder <wohilas@gmail.com>2024-03-17 13:13:01 +0300
commit33db3abdadd6687eb16305014c70654e03168fe5 (patch)
tree62b8f8766b896227fdc9ce15c0cac8530ced3099 /internal/server/main.go
init
Diffstat (limited to 'internal/server/main.go')
-rw-r--r--internal/server/main.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/internal/server/main.go b/internal/server/main.go
new file mode 100644
index 0000000..45f3d41
--- /dev/null
+++ b/internal/server/main.go
@@ -0,0 +1,60 @@
+package server
+
+import (
+ "apjournal/config"
+ "apjournal/internal/handlers"
+
+ "context"
+ "log/slog"
+ "os"
+ "os/signal"
+ "syscall"
+)
+
+// Server interface
+type Server interface {
+ Listen()
+}
+
+type server struct {
+ config config.Config
+ actions *handlers.Handlers
+ ctx context.Context
+ close context.CancelFunc
+}
+
+func (srv *server) stopOnSignal(close context.CancelFunc) {
+ // listen for termination signals
+ sigc := make(chan os.Signal, 1)
+ signal.Notify(sigc, os.Interrupt, syscall.SIGINT)
+ signal.Notify(sigc, os.Interrupt, syscall.SIGTERM)
+ sig := <-sigc
+
+ log := slog.New(slog.NewJSONHandler(os.Stdout, nil))
+ log.Info("Shutting down services",
+ "section", "server",
+ "app_event", "terminate",
+ "signal", sig.String())
+ close()
+ os.Exit(0)
+}
+
+func NewServer(cfg config.Config, log *slog.Logger) Server {
+ ctx, close := context.WithCancel(context.Background())
+ // s := service.NewService(ctx, cfg, store.MemCache)
+ actions := handlers.NewHandlers(cfg, log)
+
+ return &server{
+ config: cfg,
+ actions: actions,
+ ctx: ctx,
+ close: close,
+ }
+}
+
+// Listen for new events that affect the market and process them
+func (srv *server) Listen() {
+ // start the http server
+ go srv.ListenToRequests()
+ srv.stopOnSignal(srv.close)
+}