2022-05-09 19:09:24 +00:00
|
|
|
/*
|
2024-06-08 08:29:35 +00:00
|
|
|
Copyright CAcert Inc.
|
2022-05-09 19:09:24 +00:00
|
|
|
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"
|
2022-09-26 09:58:36 +00:00
|
|
|
"os"
|
2022-05-15 18:10:49 +00:00
|
|
|
"time"
|
2022-05-09 19:09:24 +00:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
2022-10-15 17:58:58 +00:00
|
|
|
|
|
|
|
"git.cacert.org/cacert-boardvoting/internal/notifications"
|
2022-05-09 19:09:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-05-26 14:53:52 +00:00
|
|
|
httpIdleTimeout = time.Minute
|
|
|
|
httpReadHeaderTimeout = 5 * time.Second
|
|
|
|
httpReadTimeout = 5 * time.Second
|
|
|
|
httpWriteTimeout = 10 * time.Second
|
2022-05-29 13:43:45 +00:00
|
|
|
smtpPort = 25
|
2022-05-29 13:36:27 +00:00
|
|
|
smtpTimeout = 10 * time.Second
|
2022-05-09 19:09:24 +00:00
|
|
|
)
|
|
|
|
|
2022-05-15 18:10:49 +00:00
|
|
|
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 {
|
2022-10-15 17:58:58 +00:00
|
|
|
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"`
|
2022-05-09 19:09:24 +00:00
|
|
|
}
|
|
|
|
|
2022-05-22 19:15:54 +00:00
|
|
|
func parseConfig(configFile string) (*Config, error) {
|
2022-09-26 09:58:36 +00:00
|
|
|
source, err := os.ReadFile(configFile)
|
2022-05-09 19:09:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not read configuration file %s: %w", configFile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &Config{
|
|
|
|
HTTPAddress: "127.0.0.1:8000",
|
2022-05-22 09:02:37 +00:00
|
|
|
HTTPSAddress: "127.0.0.1:8443",
|
2022-05-21 11:51:17 +00:00
|
|
|
Timeouts: &httpTimeoutConfig{
|
2022-05-15 18:10:49 +00:00
|
|
|
Idle: httpIdleTimeout,
|
|
|
|
ReadHeader: httpReadHeaderTimeout,
|
|
|
|
Read: httpReadTimeout,
|
|
|
|
Write: httpWriteTimeout,
|
|
|
|
},
|
2022-10-15 17:58:58 +00:00
|
|
|
MailConfig: ¬ifications.MailConfig{
|
2022-05-29 13:36:27 +00:00
|
|
|
SMTPHost: "localhost",
|
2022-05-29 13:43:45 +00:00
|
|
|
SMTPPort: smtpPort,
|
2022-05-29 13:36:27 +00:00
|
|
|
SMTPTimeOut: smtpTimeout,
|
|
|
|
},
|
2022-05-09 19:09:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := yaml.Unmarshal(source, config); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse configuration: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|