repos / pico

pico services mono repo
git clone https://github.com/picosh/pico.git

pico / pkg / apps / pgs
Eric Bower  ·  2026-04-22

config.go

  1package pgs
  2
  3import (
  4	"fmt"
  5	"log/slog"
  6	"path/filepath"
  7	"strconv"
  8	"time"
  9
 10	pgsdb "github.com/picosh/pico/pkg/apps/pgs/db"
 11	"github.com/picosh/pico/pkg/shared"
 12	"github.com/picosh/pico/pkg/storage"
 13)
 14
 15type PgsConfig struct {
 16	CacheTTL           time.Duration
 17	CacheMaxItems      int
 18	Domain             string
 19	MaxAssetSize       int64
 20	MaxSize            uint64
 21	MaxSpecialFileSize int64
 22	SshHost            string
 23	SshPort            string
 24	WebPort            string
 25	WebProtocol        string
 26	TxtPrefix          string
 27
 28	// This channel will receive the surrogate key for a project (e.g. static site)
 29	// which will inform the caching layer to clear the cache for that site.
 30	CacheClearingQueue chan string
 31	// Database layer; it's just an interface that could be implemented
 32	// with anything.
 33	DB     pgsdb.PgsDB
 34	Logger *slog.Logger
 35	// Where we store the static assets uploaded to our service.
 36	Storage storage.StorageServe
 37	Pubsub  PicoPubsub
 38}
 39
 40func (c *PgsConfig) AssetURL(username, projectName, fpath string) string {
 41	if username == projectName {
 42		return fmt.Sprintf(
 43			"%s://%s.%s/%s",
 44			c.WebProtocol,
 45			username,
 46			c.Domain,
 47			fpath,
 48		)
 49	}
 50
 51	return fmt.Sprintf(
 52		"%s://%s-%s.%s/%s",
 53		c.WebProtocol,
 54		username,
 55		projectName,
 56		c.Domain,
 57		fpath,
 58	)
 59}
 60
 61func (c *PgsConfig) StaticPath(fname string) string {
 62	return filepath.Join("pkg", "apps", "pgs", fname)
 63}
 64
 65var maxSize = uint64(25 * shared.MB)
 66var maxAssetSize = int64(10 * shared.MB)
 67
 68// Needs to be small for caching files like _headers and _redirects.
 69var maxSpecialFileSize = int64(5 * shared.KB)
 70
 71func NewPgsConfig(logger *slog.Logger, dbpool pgsdb.PgsDB, st storage.StorageServe, pubsub PicoPubsub) *PgsConfig {
 72	domain := shared.GetEnv("PGS_DOMAIN", "pgs.sh")
 73	port := shared.GetEnv("PGS_WEB_PORT", "3000")
 74	protocol := shared.GetEnv("PGS_PROTOCOL", "https")
 75	cacheTTL, err := time.ParseDuration(shared.GetEnv("PGS_CACHE_TTL", ""))
 76	if err != nil {
 77		cacheTTL = 600 * time.Second
 78	}
 79	cacheMaxItemsStr := shared.GetEnv("PGS_CACHE_MAX_ITEMS", "")
 80	cacheMaxItems := 0
 81	if cacheMaxItemsStr != "" {
 82		cacheMaxItems, _ = strconv.Atoi(cacheMaxItemsStr)
 83	}
 84
 85	sshHost := shared.GetEnv("PGS_SSH_HOST", "0.0.0.0")
 86	sshPort := shared.GetEnv("PGS_SSH_PORT", "2222")
 87
 88	cfg := PgsConfig{
 89		CacheTTL:           cacheTTL,
 90		CacheMaxItems:      cacheMaxItems,
 91		Domain:             domain,
 92		MaxAssetSize:       maxAssetSize,
 93		MaxSize:            maxSize,
 94		MaxSpecialFileSize: maxSpecialFileSize,
 95		SshHost:            sshHost,
 96		SshPort:            sshPort,
 97		TxtPrefix:          "pgs",
 98		WebPort:            port,
 99		WebProtocol:        protocol,
100
101		CacheClearingQueue: make(chan string, 100),
102		DB:                 dbpool,
103		Logger:             logger,
104		Storage:            st,
105		Pubsub:             pubsub,
106	}
107
108	return &cfg
109}