cerrado @ f5b5f72f7eaf14692b8036c3698037c647b2423a

 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	"git.gabrielgio.me/cerrado/templates"
16)
17
18func main() {
19
20	ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
21	defer stop()
22	if err := run(ctx); err != nil {
23		os.Exit(1)
24	}
25}
26
27func run(ctx context.Context) error {
28	var (
29		configPath = flag.String("config", "config.example.scfg", "File path for the configuration file")
30	)
31
32	flag.Parse()
33
34	mux := http.NewServeMux()
35	mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
36
37		f, err := os.Open(*configPath)
38		if err != nil {
39			slog.Error("Error openning config file json", "error", err, "path", *configPath)
40			return
41		}
42
43		c, err := config.Parse(f)
44		if err != nil {
45			slog.Error("Error parsing config", "error", err, "path", *configPath)
46			return
47		}
48
49		b, err := json.MarshalIndent(c, "", "	")
50		if err != nil {
51			slog.Error("Error parsing json", "error", err)
52			return
53		}
54
55		hello := &templates.HelloPage{
56			Body: string(b),
57		}
58
59		templates.WritePageTemplate(w, hello)
60	})
61	serverTask := worker.NewServerTask(&http.Server{Handler: mux, Addr: "0.0.0.0:8080"})
62
63	pool := worker.NewTaskPool()
64	pool.AddTask("http-server", 5*time.Second, serverTask)
65
66	return pool.Start(ctx)
67}