all repos

mugit @ d00a3f1ada9adfc98d435b4417f4a271db92ee0c

🐮 git server that your cow will love

mugit/internal/cli/serve.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
ssh!: switch to sshd, 2 months ago
1
package cli
2
3
import (
4
	"context"
5
	"log/slog"
6
	"net"
7
	"net/http"
8
	"os"
9
	"os/signal"
10
	"strconv"
11
	"syscall"
12
13
	"github.com/urfave/cli/v3"
14
	"olexsmir.xyz/mugit/internal/handlers"
15
	"olexsmir.xyz/mugit/internal/mirror"
16
)
17
18
func (c *Cli) serveAction(ctx context.Context, cmd *cli.Command) error {
19
	httpServer := &http.Server{
20
		Addr:    net.JoinHostPort(c.cfg.Server.Host, strconv.Itoa(c.cfg.Server.Port)),
21
		Handler: handlers.InitRoutes(c.cfg),
22
	}
23
	go func() {
24
		slog.Info("starting http server", "host", c.cfg.Server.Host, "port", c.cfg.Server.Port)
25
		if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
26
			slog.Error("HTTP server error", "err", err)
27
		}
28
	}()
29
30
	if c.cfg.Mirror.Enable {
31
		mirrorer := mirror.NewWorker(c.cfg)
32
		go func() {
33
			slog.Info("starting mirroring worker")
34
			mirrorer.Start(ctx)
35
		}()
36
	}
37
38
	sigChan := make(chan os.Signal, 1)
39
	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
40
41
	sig := <-sigChan
42
	slog.Info("received signal, starting graceful shutdown", "signal", sig)
43
44
	if err := httpServer.Shutdown(ctx); err != nil {
45
		slog.Error("HTTP server shutdown error", "err", err)
46
	} else {
47
		slog.Info("HTTP server shutdown complete")
48
	}
49
50
	return nil
51
}