all repos

mugit @ afcecfcfa4a38ad7a1008ab2f44cc2e978081f71

🐮 git server that your cow will love

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
cli: repo description, 4 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
	"olexsmir.xyz/mugit/internal/ssh"
17
)
18
19
func (c *Cli) serveAction(ctx context.Context, cmd *cli.Command) error {
20
	httpServer := &http.Server{
21
		Addr:    net.JoinHostPort(c.cfg.Server.Host, strconv.Itoa(c.cfg.Server.Port)),
22
		Handler: handlers.InitRoutes(c.cfg),
23
	}
24
	go func() {
25
		slog.Info("starting http server", "host", c.cfg.Server.Host, "port", c.cfg.Server.Port)
26
		if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
27
			slog.Error("HTTP server error", "err", err)
28
		}
29
	}()
30
31
	if c.cfg.SSH.Enable {
32
		sshServer := ssh.NewServer(c.cfg)
33
		go func() {
34
			slog.Info("starting ssh server", "port", c.cfg.SSH.Port)
35
			if err := sshServer.Start(); err != nil {
36
				slog.Error("ssh server error", "err", err)
37
			}
38
		}()
39
	}
40
41
	if c.cfg.Mirror.Enable {
42
		mirrorer := mirror.NewWorker(c.cfg)
43
		go func() {
44
			slog.Info("starting mirroring worker")
45
			mirrorer.Start(context.TODO())
46
		}()
47
	}
48
49
	sigChan := make(chan os.Signal, 1)
50
	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
51
52
	sig := <-sigChan
53
	slog.Info("received signal, starting graceful shutdown", "signal", sig)
54
55
	if err := httpServer.Shutdown(context.TODO()); err != nil {
56
		slog.Error("HTTP server shutdown error", "err", err)
57
	} else {
58
		slog.Info("HTTP server shutdown complete")
59
	}
60
61
	return nil
62
}