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