forked from coalaura/whiskr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.go
More file actions
286 lines (223 loc) · 7.29 KB
/
Copy pathenv.go
File metadata and controls
286 lines (223 loc) · 7.29 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"bytes"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"strings"
"sync"
"github.com/goccy/go-yaml"
"golang.org/x/crypto/bcrypt"
)
type EnvTokens struct {
Secret string `yaml:"secret"`
OpenRouter string `yaml:"openrouter"`
Exa string `yaml:"exa"`
GitHub string `yaml:"github"`
}
type EnvServer struct {
Port int64 `yaml:"port"`
}
type EnvSettings struct {
CleanContent bool `yaml:"cleanup"`
Timeout int64 `yaml:"timeout"`
RefreshInterval int64 `yaml:"refresh-interval"`
}
type EnvModels struct {
TitleModel string `yaml:"title-model"`
ImageGeneration bool `yaml:"image-generation"`
Transformation string `yaml:"transformation"`
Filters string `yaml:"filters"`
filters *Filters
}
type EnvUI struct {
ReducedMotion bool `yaml:"reduced-motion"`
}
type EnvUser struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
}
type EnvAuthentication struct {
lookup map[string]*EnvUser
Enabled bool `yaml:"enabled"`
Users []*EnvUser `yaml:"users"`
}
type Environment struct {
dmx sync.RWMutex // data mutex
fmx sync.Mutex // file mutex
Debug bool `yaml:"debug"`
Tokens EnvTokens `yaml:"tokens"`
Server EnvServer `yaml:"server"`
Settings EnvSettings `yaml:"settings"`
Models EnvModels `yaml:"models"`
UI EnvUI `yaml:"ui"`
Authentication EnvAuthentication `yaml:"authentication"`
}
func LoadEnv() (*Environment, error) {
// defaults
cfg := &Environment{
Server: EnvServer{
Port: 3443,
},
Settings: EnvSettings{
CleanContent: true,
Timeout: 1200,
RefreshInterval: 30,
},
Models: EnvModels{
ImageGeneration: true,
},
}
file, err := os.OpenFile("config.yml", os.O_RDONLY, 0)
if err != nil {
return nil, err
}
defer file.Close()
err = yaml.NewDecoder(file).Decode(cfg)
if err != nil {
return nil, err
}
err = cfg.Init()
if err != nil {
return nil, err
}
return cfg, nil
}
func (e *Environment) Addr() string {
return fmt.Sprintf(":%d", e.Server.Port)
}
func (e *Environment) Init() error {
var store bool
// print if debug is enabled
if e.Debug {
log.Warnln("Debug mode enabled")
}
// print if image generation is enabled
if e.Models.ImageGeneration {
log.Warnln("Image generation enabled")
} else {
log.Warnln("Image generation disabled")
}
// check if server secret is set
if e.Tokens.Secret == "" {
log.Warnln("Missing tokens.secret, generating new")
secret, err := CreateSecret(32)
if err != nil {
return err
}
e.Tokens.Secret = secret
store = true
}
// check if openrouter token is set
if e.Tokens.OpenRouter == "" {
return errors.New("missing tokens.openrouter")
}
// check if exa token is set
if e.Tokens.Exa == "" {
log.Warnln("Missing token.exa, web search unavailable")
}
// check if github token is set
if e.Tokens.GitHub == "" {
log.Warnln("Missing token.github, limited api requests")
}
// check if port is valid
if e.Server.Port <= 0 || e.Server.Port >= 65535 {
return fmt.Errorf("invalid port %d", e.Server.Port)
}
// default title model
if e.Models.TitleModel == "" {
e.Models.TitleModel = "google/gemini-2.5-flash-lite"
}
// default transformation method
if e.Models.Transformation == "" {
e.Models.Transformation = "middle-out"
}
filters, err := ParseFilters(e.Models.Filters)
if err != nil {
return err
}
e.Models.filters = filters
// default timeout
if e.Settings.Timeout <= 0 {
e.Settings.Timeout = 300
}
// default model refresh interval
if e.Settings.RefreshInterval <= 0 {
e.Settings.RefreshInterval = 30
}
// make it harder to disable auth accidentally
if !e.Authentication.Enabled && len(e.Authentication.Users) > 0 {
return errors.New("authentication disabled but users defined")
}
// create user lookup map
e.Authentication.lookup = make(map[string]*EnvUser)
for _, user := range e.Authentication.Users {
if strings.HasPrefix(user.Password, "text=") {
log.Warnf("User %q has plaintext password, generating hash\n", user.Username)
hash, err := bcrypt.GenerateFromPassword([]byte(user.Password[5:]), bcrypt.DefaultCost)
if err != nil {
return err
}
user.Password = string(hash)
store = true
}
e.Authentication.lookup[user.Username] = user
}
if store {
if err := e.Store(); err != nil {
return err
}
log.Println("Updated config.yml")
}
return nil
}
func (e *Environment) Store() error {
var (
buffer bytes.Buffer
comments = yaml.CommentMap{
"$.debug": {yaml.HeadComment(" enable verbose logging and diagnostics")},
"$.tokens": {yaml.HeadComment("")},
"$.server": {yaml.HeadComment("")},
"$.settings": {yaml.HeadComment("")},
"$.models": {yaml.HeadComment("")},
"$.ui": {yaml.HeadComment("")},
"$.authentication": {yaml.HeadComment("")},
"$.tokens.secret": {yaml.HeadComment(" server secret for signing auth tokens; auto-generated if empty")},
"$.tokens.openrouter": {yaml.HeadComment(" openrouter.ai api token (required)")},
"$.tokens.exa": {yaml.HeadComment(" exa search api token (optional; used by search tools)")},
"$.tokens.github": {yaml.HeadComment(" github api token (optional; used by search tools)")},
"$.server.port": {yaml.HeadComment(" port to serve whiskr on (required; default 3443)")},
"$.settings.cleanup": {yaml.HeadComment(" normalize unicode in assistant output (optional; default: true)")},
"$.settings.timeout": {yaml.HeadComment(" the http timeout to use for completion requests in seconds (optional; default: 300s)")},
"$.settings.refresh-interval": {yaml.HeadComment(" the interval in which the model list is refreshed in minutes (optional; default: 30m)")},
"$.models.title-model": {yaml.HeadComment(" model used to generate titles (needs to have structured output support; set to \"-\" to disable title; default: google/gemini-2.5-flash-lite)")},
"$.models.image-generation": {yaml.HeadComment(" allow image generation (optional; default: true)")},
"$.models.transformation": {yaml.HeadComment(" what transformation method to use for too long contexts (optional; default: middle-out)")},
"$.models.filters": {yaml.HeadComment(" boolean expression to filter available models (optional; fields: `price`, `slug`, `name`, `tags`, `created`; operators: `<`, `>`, `==`, `!=`, `~` (contains), `^` (starts-with), `$` (ends-with); Logic: `&&`, `||`, `!`, `( )`)")},
"$.ui.reduced-motion": {yaml.HeadComment(" disables things like the floating stars in the background (optional; default: false)")},
"$.authentication.enabled": {yaml.HeadComment(" require login with username and password")},
"$.authentication.users": {yaml.HeadComment(" list of users with bcrypt password hashes")},
}
)
e.dmx.RLock()
err := yaml.NewEncoder(&buffer, yaml.WithComment(comments)).Encode(e)
e.dmx.RUnlock()
if err != nil {
return err
}
body := bytes.ReplaceAll(buffer.Bytes(), []byte("#\n"), []byte("\n"))
e.fmx.Lock()
defer e.fmx.Unlock()
return os.WriteFile("config.yml", body, 0644)
}
func CreateSecret(length int) (string, error) {
key := make([]byte, length)
_, err := io.ReadFull(rand.Reader, key)
if err != nil {
return "", err
}
return hex.EncodeToString(key), nil
}