cerrado @ d0e0c1eb99303e1000140d4b98c610077278dc42

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