cerrado @ 306a8cdf6fac4e3ec2bfeda6c9952f603a5577e1

 1package main
 2
 3import (
 4	"context"
 5	"flag"
 6	"fmt"
 7	"log/slog"
 8	"os"
 9	"os/signal"
10	"time"
11
12	"golang.org/x/crypto/bcrypt"
13
14	"git.gabrielgio.me/cerrado/pkg/config"
15	"git.gabrielgio.me/cerrado/pkg/handler"
16	"git.gabrielgio.me/cerrado/pkg/service"
17	"git.gabrielgio.me/cerrado/pkg/worker"
18)
19
20func main() {
21	ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
22	defer stop()
23
24	if len(os.Args) == 4 && os.Args[1] == "hash" {
25		err := hash(os.Args[2], os.Args[3])
26		if err != nil {
27			slog.Error("Error", "error", err)
28			os.Exit(1)
29		}
30		return
31	}
32
33	if err := run(ctx); err != nil {
34		slog.Error("Error", "error", err)
35		os.Exit(1)
36	}
37}
38
39func hash(username string, password string) error {
40	passphrase := fmt.Sprintf("%s:%s", username, password)
41	bytes, err := bcrypt.GenerateFromPassword([]byte(passphrase), 14)
42	if err != nil {
43		return err
44	}
45	fmt.Println(string(bytes))
46	return nil
47}
48
49func run(ctx context.Context) error {
50	var (
51		configPath = flag.String("config", "/etc/cerrado.scfg", "File path for the configuration file")
52	)
53
54	flag.Parse()
55
56	// repositorie
57	configRepo, err := config.LoadConfigurationRepository(*configPath)
58	if err != nil {
59		return err
60	}
61
62	// services
63	gitService := service.NewGitService(configRepo)
64
65	handler, err := handler.MountHandler(gitService, configRepo)
66	if err != nil {
67		return err
68	}
69
70	serverTask := worker.NewServerTask(configRepo.GetListenAddr(), handler)
71
72	pool := worker.NewTaskPool()
73	pool.AddTask("http-server", 5*time.Second, serverTask)
74
75	return pool.Start(ctx)
76}