summaryrefslogtreecommitdiff
path: root/internal/server/server.go
blob: a2f2d452c6da8d2cc8f5e293cdf819e96b1fd072 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package server

import (
	"log/slog"
	"net"
	"time"

	"net/http"

	"codeberg.org/mihna123/openstore/internal/config"
	"codeberg.org/mihna123/openstore/internal/server/handlers"
)

type Server struct {
	config *config.Config
	mux    *http.ServeMux
}

func (s *Server) Serve() error {
	httpSrv := &http.Server{
		Handler:           logRequests(s.mux),
		ReadHeaderTimeout: 5 * time.Second,
		WriteTimeout:      10 * time.Second,
	}

	ln, err := net.Listen("tcp", s.config.Serve.Addr)
	if err != nil {
		return err
	}

	slog.Info("listening", "name", s.config.Instance.Name, "addr", s.config.Serve.Addr)
	return httpSrv.Serve(ln)
}

func New(c *config.Config) *Server {
	mux := handlers.Routes(c)
	s := &Server{
		config: c,
		mux:    mux,
	}

	return s
}