cerrado @ 2e4b2fb52a539188b27212005d626f0849e78196

  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 configurationo
 78
 79	if _, ok := styles.Registry[configRepo.GetSyntaxHighlight()]; !ok {
 80		slog.Warn(
 81			"Invalid Syntax highlight selected",
 82			"invalid-style", configRepo.GetSyntaxHighlight(),
 83			"using", "monokailight",
 84		)
 85		styles.Fallback = styles.Registry["monokailight"]
 86	}
 87
 88	// services
 89	gitService := service.NewGitService(configRepo)
 90
 91	handler, err := handler.MountHandler(gitService, configRepo)
 92	if err != nil {
 93		return err
 94	}
 95
 96	serverTask := worker.NewServerTask(configRepo.GetListenAddr(), handler)
 97
 98	pool := worker.NewTaskPool()
 99	pool.AddTask("http-server", 5*time.Second, serverTask)
100
101	return pool.Start(ctx)
102}