From 33db3abdadd6687eb16305014c70654e03168fe5 Mon Sep 17 00:00:00 2001 From: GrailFinder Date: Sun, 17 Mar 2024 13:13:01 +0300 Subject: init --- internal/handlers/main.go | 36 ++++++++++++++++++++++++++++ internal/server/main.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++ internal/server/router.go | 23 ++++++++++++++++++ internal/service/main.go | 0 4 files changed, 119 insertions(+) create mode 100644 internal/handlers/main.go create mode 100644 internal/server/main.go create mode 100644 internal/server/router.go create mode 100644 internal/service/main.go (limited to 'internal') diff --git a/internal/handlers/main.go b/internal/handlers/main.go new file mode 100644 index 0000000..3b551f1 --- /dev/null +++ b/internal/handlers/main.go @@ -0,0 +1,36 @@ +package handlers + +import ( + "log/slog" + "net/http" + "os" + "apjournal/config" +) + +// Handlers structure +type Handlers struct { + cfg config.Config + // s *service.Service + log *slog.Logger +} + +// NewHandlers constructor +func NewHandlers( + // cfg config.Config, s *service.Service, l *slog.Logger, + cfg config.Config, l *slog.Logger, +) *Handlers { + if l == nil { + l = slog.New(slog.NewJSONHandler(os.Stdout, nil)) + } + h := &Handlers{ + cfg: cfg, + // s: s, + log: l, + } + return h +} + +func (h *Handlers) Ping(w http.ResponseWriter, r *http.Request) { + h.log.Info("got ping request") + w.Write([]byte("pong")) +} 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) +} diff --git a/internal/server/router.go b/internal/server/router.go new file mode 100644 index 0000000..ed33c19 --- /dev/null +++ b/internal/server/router.go @@ -0,0 +1,23 @@ +package server + +import ( + "fmt" + "net/http" + "time" +) + +func (srv *server) ListenToRequests() { + h := srv.actions + mux := http.NewServeMux() + server := &http.Server{ + Addr: "localhost:9000", + Handler: mux, + ReadTimeout: time.Second * 5, + WriteTimeout: time.Second * 5, + } + + mux.HandleFunc("/ping", h.Ping) + + fmt.Printf("Listening on %v\n", server.Addr) + server.ListenAndServe() +} diff --git a/internal/service/main.go b/internal/service/main.go new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3