1package main
2
3import (
4 "context"
5 "encoding/json"
6 "flag"
7 "log/slog"
8 "net/http"
9 "os"
10 "os/signal"
11 "time"
12
13 "git.gabrielgio.me/cerrado/pkg/config"
14 "git.gabrielgio.me/cerrado/pkg/worker"
15)
16
17func main() {
18
19 ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
20 defer stop()
21 if err := run(ctx); err != nil {
22 os.Exit(1)
23 }
24}
25
26func run(ctx context.Context) error {
27 var (
28 configPath = flag.String("config", "config.example.scfg", "File path for the configuration file")
29 )
30
31 flag.Parse()
32
33 mux := http.NewServeMux()
34 mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
35
36 f, err := os.Open(*configPath)
37 if err != nil {
38 slog.Error("Error openning config file json", "error", err, "path", *configPath)
39 return
40 }
41
42 c, err := config.Parse(f)
43 if err != nil {
44 slog.Error("Error parsing config", "error", err, "path", *configPath)
45 return
46 }
47
48 b, err := json.MarshalIndent(c, "", " ")
49 if err != nil {
50 slog.Error("Error parsing json", "error", err)
51 return
52 }
53
54 if _, err := w.Write(b); err != nil {
55 slog.Error("Error handling index", "error", err)
56 return
57 }
58 })
59 serverTask := worker.NewServerTask(&http.Server{Handler: mux, Addr: "0.0.0.0:8080"})
60
61 pool := worker.NewTaskPool()
62 pool.AddTask("http-server", 5*time.Second, serverTask)
63
64 return pool.Start(ctx)
65}