1package main
2
3import (
4 "context"
5 "flag"
6 "fmt"
7 "log/slog"
8 "os"
9 "os/signal"
10 "time"
11
12 "github.com/alecthomas/chroma/v2/styles"
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 if len(os.Args) == 4 && os.Args[1] == "hash" {
22 err := hash(os.Args[2], os.Args[3])
23 if err != nil {
24 slog.Error("Hash", "error", err)
25 os.Exit(1)
26 }
27 return
28 }
29
30 if len(os.Args) == 2 && os.Args[1] == "key" {
31 err := key()
32 if err != nil {
33 slog.Error("Key", "error", err)
34 os.Exit(1)
35 }
36 return
37 }
38
39 if err := run(); err != nil {
40 slog.Error("Server", "error", err)
41 os.Exit(1)
42 }
43}
44
45func hash(username string, password string) error {
46 hash, err := service.GenerateHash(username, password)
47 if err != nil {
48 return err
49 }
50 fmt.Println(hash)
51 return nil
52}
53
54func key() error {
55 en, err := service.GenerateAesKey()
56 if err != nil {
57 return err
58 }
59 fmt.Println(en)
60 return nil
61}
62
63func run() error {
64 ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
65 defer stop()
66
67 configPath := flag.String("config", "/etc/cerrado.scfg", "File path for the configuration file")
68
69 flag.Parse()
70
71 // repositorie
72 configRepo, err := config.LoadConfigurationRepository(*configPath)
73 if err != nil {
74 return err
75 }
76
77 // checking chroma configuration
78 if _, ok := styles.Registry[configRepo.GetSyntaxHighlight()]; !ok {
79 slog.Warn(
80 "Invalid Syntax highlight selected",
81 "invalid-style", configRepo.GetSyntaxHighlight(),
82 "using", "monokailight",
83 )
84 styles.Fallback = styles.Registry["monokailight"]
85 }
86
87 // services
88 gitService := service.NewGitService(configRepo)
89 authService := service.NewAuthService(configRepo)
90
91 handler, err := handler.MountHandler(
92 gitService,
93 authService,
94 configRepo,
95 )
96 if err != nil {
97 return err
98 }
99
100 serverTask := worker.NewServerTask(configRepo.GetListenAddr(), handler)
101
102 pool := worker.NewTaskPool()
103 pool.AddTask("http-server", 5*time.Second, serverTask)
104
105 return pool.Start(ctx)
106}