cerrado @ 349a3d1ff36a436261b1b65b870f8f262f06584f

 1package handler
 2
 3import (
 4	"bytes"
 5	"encoding/json"
 6	"log/slog"
 7	"net/http"
 8	"os"
 9
10	"github.com/alecthomas/chroma/v2/formatters/html"
11	"github.com/alecthomas/chroma/v2/lexers"
12	"github.com/alecthomas/chroma/v2/styles"
13
14	"git.gabrielgio.me/cerrado/pkg/config"
15	"git.gabrielgio.me/cerrado/templates"
16)
17
18func ConfigFile(configPath string) func(http.ResponseWriter, *http.Request) {
19	return func(w http.ResponseWriter, _ *http.Request) {
20		f, err := os.Open(configPath)
21		if err != nil {
22			slog.Error("Error openning config file json", "error", err, "path", configPath)
23			return
24		}
25
26		c, err := config.Parse(f)
27		if err != nil {
28			slog.Error("Error parsing config", "error", err, "path", configPath)
29			return
30		}
31
32		b, err := json.MarshalIndent(c, "", "	")
33		if err != nil {
34			slog.Error("Error parsing json", "error", err)
35			return
36		}
37
38		lexer := lexers.Get("json")
39		style := styles.Get("monokailight")
40		formatter := html.New(
41			html.WithLineNumbers(true),
42		)
43		iterator, err := lexer.Tokenise(nil, string(b))
44		if err != nil {
45			slog.Error("Error tokenise", "error", err)
46			return
47		}
48
49		var code bytes.Buffer
50		err = formatter.Format(&code, style, iterator)
51		if err != nil {
52			slog.Error("Error format", "error", err)
53			return
54		}
55
56		hello := &templates.HelloPage{
57			Body: code.String(),
58		}
59
60		templates.WritePageTemplate(w, hello)
61	}
62}