repos / pico

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

pico / pkg / apps / pgs
Eric Bower  ·  2025-07-04

config.go

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