main pico / pkg / apps / pastes / config.go
Antonio Mika  ·  2026-10-10
 1package pastes
 2
 3import (
 4	"fmt"
 5	"html/template"
 6	"log/slog"
 7	"net/url"
 8	"path/filepath"
 9	"strings"
10
11	"github.com/picosh/pico/pkg/shared"
12	"github.com/picosh/pico/pkg/shared/router"
13)
14
15// staticDir holds pastes' templates and public files.
16const staticDir = "pkg/apps/pastes"
17
18type Config struct {
19	Debug        bool
20	Domain       string
21	Port         string
22	Protocol     string
23	DbURL        string
24	Space        string
25	MaxAssetSize int64
26	Logger       *slog.Logger
27}
28
29func NewConfig(service string) *Config {
30	debug := shared.GetEnv("PASTES_DEBUG", "0")
31	domain := shared.GetEnv("PASTES_DOMAIN", "pastes.sh")
32	port := shared.GetEnv("PASTES_WEB_PORT", "3000")
33	dbURL := shared.GetEnv("DATABASE_URL", "")
34	protocol := shared.GetEnv("PASTES_PROTOCOL", "https")
35	withPipe := strings.ToLower(shared.GetEnv("PICO_PIPE_ENABLED", "true")) == "true"
36
37	return &Config{
38		Debug:        debug == "1",
39		Domain:       domain,
40		Port:         port,
41		Protocol:     protocol,
42		DbURL:        dbURL,
43		Space:        "pastes",
44		Logger:       shared.CreateLogger(service, withPipe),
45		MaxAssetSize: int64(3 * shared.MB),
46	}
47}
48
49func (c *Config) StaticPath(fname string) string {
50	return filepath.Join(staticDir, fname)
51}
52
53func (c *Config) SiteData() shared.SitePageData {
54	return shared.SitePageData{
55		Domain:  template.URL(c.Domain),
56		HomeURL: template.URL("//" + c.Domain),
57	}
58}
59
60func (c *Config) Static() router.Static {
61	return router.Static{Dir: staticDir, Logger: c.Logger, Site: c.SiteData()}
62}
63
64func (c *Config) BlogURL(username string) string {
65	return fmt.Sprintf("%s://%s.%s", c.Protocol, username, c.Domain)
66}
67
68func (c *Config) PostURL(username, slug string) string {
69	return fmt.Sprintf("%s://%s.%s/%s", c.Protocol, username, c.Domain, url.PathEscape(slug))
70}
71
72func (c *Config) RawPostURL(username, slug string) string {
73	return fmt.Sprintf("%s://%s.%s/raw/%s", c.Protocol, username, c.Domain, url.PathEscape(slug))
74}