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"` NodeID int64 `json:"node_id"` EncryptionKeyLength int `json:"encryption_key_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"` EnableUploadFileButton bool `json:"enable_upload_file_button"` TLSCertFile string `json:"tls_cert_file"` TLSKeyFile string `json:"tls_key_file"` BootstrapDirectory string `json:"bootstrap_directory"` } func NewConfig() *Config { return &Config{ Title: "Coller", DatabaseType: "sqlite", DatabaseDsn: "collerd.db", NodeID: 1, EncryptionKeyLength: 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", EnableUploadFileButton: 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.NodeID < 0 || c.NodeID > 1023 { return fmt.Errorf("node id must be between 0 and 1023") } if c.EncryptionKeyLength < internal.MIN_ENCRYPTION_KEY_LENGTH || c.EncryptionKeyLength > internal.MAX_ENCRYPTION_KEY_LENGTH { return fmt.Errorf("encryption key length must be between %d and %d", internal.MIN_ENCRYPTION_KEY_LENGTH, internal.MAX_ENCRYPTION_KEY_LENGTH) } return nil } func (c *Config) HasTLS() bool { return c.TLSCertFile != "" && c.TLSKeyFile != "" }