main pico / pkg / apps / prose / config.go
Antonio Mika  ·  2026-10-10
 1package prose
 2
 3import (
 4	"fmt"
 5	"html/template"
 6	"log/slog"
 7	"path/filepath"
 8	"strings"
 9
10	"github.com/picosh/pico/pkg/shared"
11	"github.com/picosh/pico/pkg/shared/router"
12)
13
14var MAX_FILE_SIZE = 3 * shared.MB
15
16// staticDir holds prose's templates and public files.
17const staticDir = "pkg/apps/prose"
18
19type Config struct {
20	Debug              bool
21	Domain             string
22	Port               string
23	Protocol           string
24	DbURL              string
25	Space              string
26	AllowedExt         []string
27	HiddenPosts        []string
28	MaxSize            uint64
29	MaxAssetSize       int64
30	MaxSpecialFileSize int64
31	Logger             *slog.Logger
32}
33
34func NewConfig(service string) *Config {
35	debug := shared.GetEnv("PROSE_DEBUG", "0")
36	domain := shared.GetEnv("PROSE_DOMAIN", "prose.sh")
37	port := shared.GetEnv("PROSE_WEB_PORT", "3000")
38	protocol := shared.GetEnv("PROSE_PROTOCOL", "https")
39	dbURL := shared.GetEnv("DATABASE_URL", "")
40	maxSize := uint64(25 * shared.MB)
41	maxImgSize := int64(10 * shared.MB)
42	withPipe := strings.ToLower(shared.GetEnv("PICO_PIPE_ENABLED", "true")) == "true"
43
44	return &Config{
45		Debug:    debug == "1",
46		Domain:   domain,
47		Port:     port,
48		Protocol: protocol,
49		DbURL:    dbURL,
50		Space:    "prose",
51		AllowedExt: []string{
52			".md",
53			".lxt",
54			".jpg",
55			".jpeg",
56			".png",
57			".gif",
58			".webp",
59			".svg",
60			".ico",
61		},
62		HiddenPosts:  []string{"_readme.md", "_styles.css", "_footer.md", "_404.md", "robots.txt"},
63		Logger:       shared.CreateLogger(service, withPipe),
64		MaxSize:      maxSize,
65		MaxAssetSize: maxImgSize,
66	}
67}
68
69func (c *Config) StaticPath(fname string) string {
70	return filepath.Join(staticDir, fname)
71}
72
73func (c *Config) SiteData() shared.SitePageData {
74	return shared.SitePageData{
75		Domain:  template.URL(c.Domain),
76		HomeURL: template.URL("//" + c.Domain),
77	}
78}
79
80func (c *Config) Static() router.Static {
81	return router.Static{Dir: staticDir, Logger: c.Logger, Site: c.SiteData()}
82}
83
84func (c *Config) ReadURL() string {
85	return fmt.Sprintf("%s://%s", c.Protocol, c.Domain)
86}
87
88func (c *Config) CssURL() string {
89	return "/_styles.css"
90}