cerrado @ 349a3d1ff36a436261b1b65b870f8f262f06584f

 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/handler"
13	"git.gabrielgio.me/cerrado/pkg/service"
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		slog.Error("Error", "error", err)
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
36	staticHandler, err := handler.NewStaticHander("/static/")
37	if err != nil {
38		return err
39	}
40
41	// services
42	gitService := service.NewGitService()
43
44	//handlers
45	gitHandler := handler.NewGitHandler(gitService)
46
47	mux.Handle("/static/", staticHandler)
48	mux.HandleFunc("/config", handler.ConfigFile(*configPath))
49	mux.HandleFunc("/", gitHandler.List)
50	serverTask := worker.NewServerTask(&http.Server{Handler: mux, Addr: "0.0.0.0:8080"})
51
52	pool := worker.NewTaskPool()
53	pool.AddTask("http-server", 5*time.Second, serverTask)
54
55	return pool.Start(ctx)
56}