-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
108 lines (89 loc) · 2.89 KB
/
Copy pathconfig.go
File metadata and controls
108 lines (89 loc) · 2.89 KB
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
103
104
105
106
107
108
package deeplink
import (
"log/slog"
"net/http"
"strings"
"time"
)
// defaultLocale is the og:locale fallback when neither Link.Locale nor
// Config.Locale is set. en_US is the most widely-recognized OG locale value.
const defaultLocale = "en_US"
// Config configures a deeplink [Service].
type Config struct {
// BaseURL is the prefix for generated short links
// (e.g. "https://link.example.com" or "https://link.example.com/").
// A trailing slash is added automatically if missing.
BaseURL string
// IDLength is the length of generated short IDs. Default: 17.
IDLength int
// Store is the persistence backend for links. Required.
Store Store
// Logger for structured logging. Default: [slog.Default].
Logger *slog.Logger
// HTTPClient is available to processors via [Service.Config].
// Default: client with 20s timeout.
HTTPClient *http.Client
// SkipPaths are regex patterns matched against incoming short IDs.
// Matching requests get a 404 instead of a store lookup.
SkipPaths []string
// TemplateDir is the directory containing link.html and preview.html.
// If empty or if a template file is missing, the preview handler
// falls back to a 302 redirect.
TemplateDir string
// AllowedOrigins for CORS. Empty means no CORS headers.
AllowedOrigins []string
// ClickBufferSize is the capacity of the async click event channel.
// Default: 1024.
ClickBufferSize int
// ClickFlushInterval controls how often buffered clicks are flushed
// to the store. Default: 1s.
ClickFlushInterval time.Duration
// AndroidStoreURL, IOSStoreURL, and WebFallbackURL enable the
// /redirect, /preview/, and /.well-known/ routes when any is set.
AndroidStoreURL string
IOSStoreURL string
WebFallbackURL string
// SiteName is emitted as og:site_name on every preview page.
SiteName string
// Locale is the default og:locale value (e.g. "en_US"). A non-empty
// Link.Locale overrides it. Defaults to "en_US" when empty.
Locale string
// TwitterSite is the @handle emitted as twitter:site (e.g. "@example").
TwitterSite string
// FediverseCreator is the fediverse account emitted as
// fediverse:creator (e.g. "@user@instance.tld").
FediverseCreator string
}
func (c *Config) defaults() {
if c.IDLength == 0 {
c.IDLength = 17
}
if c.BaseURL != "" && !strings.HasSuffix(c.BaseURL, "/") {
c.BaseURL += "/"
}
if c.Logger == nil {
c.Logger = slog.Default()
}
if c.ClickBufferSize == 0 {
c.ClickBufferSize = 1024
}
if c.ClickFlushInterval == 0 {
c.ClickFlushInterval = time.Second
}
if c.Locale == "" {
c.Locale = defaultLocale
}
if c.HTTPClient == nil {
c.HTTPClient = &http.Client{
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 10,
IdleConnTimeout: 90 * time.Second,
},
Timeout: 20 * time.Second,
}
}
}
func (c *Config) hasMobileRoutes() bool {
return c.AndroidStoreURL != "" || c.IOSStoreURL != "" || c.WebFallbackURL != ""
}