96 lines
2.7 KiB
Go
96 lines
2.7 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.riou.xyz/jriou/coller/internal"
|
|
)
|
|
|
|
type Config struct {
|
|
Title string `json:"title"`
|
|
DatabaseType string `json:"database_type"`
|
|
DatabaseDsn string `json:"database_dsn"`
|
|
IDLength int `json:"id_length"`
|
|
PasswordLength int `json:"password_length"`
|
|
ExpirationInterval int `json:"expiration_interval"`
|
|
ListenAddress string `json:"listen_address"`
|
|
ListenPort int `json:"listen_port"`
|
|
Expirations []int `json:"expirations"`
|
|
Expiration int `json:"expiration"`
|
|
MaxUploadSize int64 `json:"max_upload_size"`
|
|
ShowVersion bool `json:"show_version"`
|
|
EnableMetrics bool `json:"enable_metrics"`
|
|
PrometheusRoute string `json:"prometheus_route"`
|
|
PrometheusNotesMetric string `json:"prometheus_notes_metric"`
|
|
ObservationInterval int `json:"observation_internal"`
|
|
Languages []string `json:"languages"`
|
|
Language string `json:"language"`
|
|
UploadFileButton bool `json:"upload_file_button"`
|
|
}
|
|
|
|
func NewConfig() *Config {
|
|
return &Config{
|
|
Title: "Coller",
|
|
DatabaseType: "sqlite",
|
|
DatabaseDsn: "collerd.db",
|
|
IDLength: 5,
|
|
PasswordLength: 16,
|
|
ExpirationInterval: 60, // 1 minute
|
|
ListenAddress: "0.0.0.0",
|
|
ListenPort: 8080,
|
|
Expirations: []int{
|
|
300, // 5 minutes
|
|
3600, // 1 hour
|
|
86400, // 1 day
|
|
604800, // 7 days
|
|
18144000, // 30 days
|
|
},
|
|
Expiration: 604800, // 7 days
|
|
MaxUploadSize: 10485760, // 10MiB (encoded)
|
|
ShowVersion: false,
|
|
EnableMetrics: true,
|
|
PrometheusRoute: "/metrics",
|
|
PrometheusNotesMetric: "collerd_notes",
|
|
ObservationInterval: 60,
|
|
Languages: []string{
|
|
"Text",
|
|
"CSS",
|
|
"Dockerfile",
|
|
"Go",
|
|
"HCL",
|
|
"HTML",
|
|
"Javascript",
|
|
"JSON",
|
|
"Markdown",
|
|
"Perl",
|
|
"Python",
|
|
"Ruby",
|
|
"Rust",
|
|
"Shell",
|
|
"SQL",
|
|
"YAML",
|
|
},
|
|
Language: "text",
|
|
UploadFileButton: true,
|
|
}
|
|
}
|
|
|
|
func (c *Config) Check() error {
|
|
if len(c.Expirations) == 0 {
|
|
return fmt.Errorf("expirations are required")
|
|
}
|
|
for _, e := range c.Expirations {
|
|
if e <= 0 {
|
|
return fmt.Errorf("invalid expiration %d: must be greater than zero", e)
|
|
}
|
|
}
|
|
|
|
if c.IDLength <= 0 {
|
|
return fmt.Errorf("identifiers length must be greater than zero")
|
|
}
|
|
|
|
if c.PasswordLength < internal.MIN_PASSWORD_LENGTH || c.PasswordLength > internal.MAX_PASSWORD_LENGTH {
|
|
return fmt.Errorf("password length must be between %d and %d", internal.MIN_PASSWORD_LENGTH, internal.MAX_PASSWORD_LENGTH)
|
|
}
|
|
return nil
|
|
}
|