all repos

mugit @ 3d9ab8a

🐮 git server that your cow will love

mugit/main.go(view raw)

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main

import (
	"context"
	"log"
	"log/slog"
	"net"
	"net/http"
	"os"
	"os/signal"
	"strconv"
	"syscall"

	"olexsmir.xyz/mugit/internal/config"
	"olexsmir.xyz/mugit/internal/handlers"
	"olexsmir.xyz/mugit/internal/ssh"
)

func main() {
	if err := run(); err != nil {
		log.Fatalf("main: %s", err)
		os.Exit(1)
	}
}

func run() error {
	cfg, err := config.Load("/home/olex/code/mugit/config.yml")
	if err != nil {
		slog.Error("config error", "err", err)
		return err
	}

	httpServer := &http.Server{
		Addr:    net.JoinHostPort(cfg.Server.Host, strconv.Itoa(cfg.Server.Port)),
		Handler: handlers.InitRoutes(cfg),
	}

	go func() {
		slog.Info("starting http server", "host", cfg.Server.Host, "port", cfg.Server.Port)

		if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			slog.Error("HTTP server error", "err", err)
		}
	}()

	sshServer := ssh.NewServer(cfg)
	if cfg.SSH.Enable {
		slog.Info("starting ssh server", "port", cfg.SSH.Port)
		if err := sshServer.Start(); err != nil {
			slog.Error("ssh server error", "err", err)
		}
	}

	// Wait for interrupt signal
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)

	sig := <-sigChan
	slog.Info("received signal, starting graceful shutdown", "signal", sig)

	if err := httpServer.Shutdown(context.TODO()); err != nil {
		slog.Error("HTTP server shutdown error", "err", err)
	} else {
		slog.Info("HTTP server shutdown complete")
	}

	return nil
}