cerrado @ e775b5356ea3adaeeca2d4be5ef874b36fa183d0

 1package config
 2
 3import (
 4	"bytes"
 5	"encoding/json"
 6	"net/http"
 7
 8	"github.com/alecthomas/chroma/v2/formatters/html"
 9	"github.com/alecthomas/chroma/v2/lexers"
10	"github.com/alecthomas/chroma/v2/styles"
11
12	"git.gabrielgio.me/cerrado/pkg/config"
13	"git.gabrielgio.me/cerrado/pkg/ext"
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) ext.ErrorRequestHandler {
25	return func(w http.ResponseWriter, _ *http.Request) error {
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			return err
38		}
39
40		lexer := lexers.Get("json")
41		style := styles.Get("monokailight")
42		formatter := html.New(
43			html.WithLineNumbers(true),
44		)
45		iterator, err := lexer.Tokenise(nil, string(b))
46		if err != nil {
47			return err
48		}
49
50		var code bytes.Buffer
51		err = formatter.Format(&code, style, iterator)
52		if err != nil {
53			return err
54		}
55
56		hello := &templates.ConfigPage{
57			Body: code.Bytes(),
58		}
59
60		templates.WritePageTemplate(w, hello)
61		return nil
62	}
63}