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