1package main
2
3import (
4 "context"
5 "flag"
6 "log/slog"
7 "net/http"
8 "os"
9 "os/signal"
10 "time"
11
12 "git.gabrielgio.me/cerrado/pkg/config"
13 "git.gabrielgio.me/cerrado/pkg/git"
14 "git.gabrielgio.me/cerrado/pkg/handler"
15 "git.gabrielgio.me/cerrado/pkg/service"
16 "git.gabrielgio.me/cerrado/pkg/worker"
17)
18
19func main() {
20
21 ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
22 defer stop()
23 if err := run(ctx); err != nil {
24 slog.Error("Error", "error", err)
25 os.Exit(1)
26 }
27}
28
29func run(ctx context.Context) error {
30 var (
31 configPath = flag.String("config", "config.example.scfg", "File path for the configuration file")
32 )
33
34 flag.Parse()
35
36 mux := http.NewServeMux()
37
38 staticHandler, err := handler.NewStaticHander("/static/")
39 if err != nil {
40 return err
41 }
42
43 f, err := os.Open(*configPath)
44 if err != nil {
45 return err
46 }
47
48 config, err := config.Parse(f)
49 if err != nil {
50 return err
51 }
52
53 // repositories
54 gitServer := git.NewGitServerRepository(config.Scan.Path)
55
56 // services
57 gitService := service.NewGitService(gitServer)
58
59 //handlers
60 gitHandler := handler.NewGitHandler(gitService)
61 aboutHandler := handler.NewAboutHandler(config.RootReadme)
62
63 mux.Handle("/static/", staticHandler)
64 mux.HandleFunc("/config", handler.ConfigFile(*configPath))
65 mux.HandleFunc("/about", aboutHandler.About)
66 mux.HandleFunc("/", gitHandler.List)
67 serverTask := worker.NewServerTask(&http.Server{Handler: mux, Addr: "0.0.0.0:8080"})
68
69 pool := worker.NewTaskPool()
70 pool.AddTask("http-server", 5*time.Second, serverTask)
71
72 return pool.Start(ctx)
73}