cacert-boardvoting/cmd/boardvoting/config.go
Jan Dittberner c2eef9cf7c
Some checks failed
cacert-boardvoting/pipeline/head There was a failure building this commit
Refactoring away from main package
This commit is a refactoring of code that has been located in the main
package. We introduce separate packages for the main application, jobs,
notifications, and request handlers.

Dependencies are injected from the main application, this will make
testing easier.
2022-10-15 19:58:58 +02:00

86 lines
2.7 KiB
Go

/*
Copyright 2017-2022 CAcert Inc.
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"os"
"time"
"gopkg.in/yaml.v2"
"git.cacert.org/cacert-boardvoting/internal/notifications"
)
const (
httpIdleTimeout = time.Minute
httpReadHeaderTimeout = 5 * time.Second
httpReadTimeout = 5 * time.Second
httpWriteTimeout = 10 * time.Second
smtpPort = 25
smtpTimeout = 10 * time.Second
)
type httpTimeoutConfig struct {
Idle time.Duration `yaml:"idle,omitempty"`
Read time.Duration `yaml:"read,omitempty"`
ReadHeader time.Duration `yaml:"read_header,omitempty"`
Write time.Duration `yaml:"write,omitempty"`
}
type Config struct {
DatabaseFile string `yaml:"database_file"`
ClientCACertificates string `yaml:"client_ca_certificates"`
ServerCert string `yaml:"server_certificate"`
ServerKey string `yaml:"server_key"`
CookieSecretStr string `yaml:"cookie_secret"`
CsrfKeyStr string `yaml:"csrf_key"`
HTTPAddress string `yaml:"http_address,omitempty"`
HTTPSAddress string `yaml:"https_address,omitempty"`
MailConfig *notifications.MailConfig `yaml:"mail_config"`
Timeouts *httpTimeoutConfig `yaml:"timeouts,omitempty"`
}
func parseConfig(configFile string) (*Config, error) {
source, err := os.ReadFile(configFile)
if err != nil {
return nil, fmt.Errorf("could not read configuration file %s: %w", configFile, err)
}
config := &Config{
HTTPAddress: "127.0.0.1:8000",
HTTPSAddress: "127.0.0.1:8443",
Timeouts: &httpTimeoutConfig{
Idle: httpIdleTimeout,
ReadHeader: httpReadHeaderTimeout,
Read: httpReadTimeout,
Write: httpWriteTimeout,
},
MailConfig: &notifications.MailConfig{
SMTPHost: "localhost",
SMTPPort: smtpPort,
SMTPTimeOut: smtpTimeout,
},
}
if err := yaml.Unmarshal(source, config); err != nil {
return nil, fmt.Errorf("could not parse configuration: %w", err)
}
return config, nil
}