cacert-boardvoting/cmd/boardvoting/main.go

132 lines
3.1 KiB
Go
Raw Normal View History

/*
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.
*/
// The CAcert board voting software.
package main
2022-05-09 19:09:24 +00:00
import (
"context"
2022-05-15 18:10:49 +00:00
"database/sql"
2022-05-09 19:09:24 +00:00
"flag"
2022-05-15 18:10:49 +00:00
"fmt"
2022-05-09 19:09:24 +00:00
"log"
"net/http"
"os"
2022-05-15 18:10:49 +00:00
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
2022-05-09 19:09:24 +00:00
2022-05-15 18:10:49 +00:00
"git.cacert.org/cacert-boardvoting/internal"
"git.cacert.org/cacert-boardvoting/internal/models"
2022-05-09 19:09:24 +00:00
)
var (
version = "undefined"
commit = "undefined"
date = "undefined"
)
2022-05-09 19:09:24 +00:00
type application struct {
errorLog, infoLog *log.Logger
2022-05-15 18:10:49 +00:00
voters *models.VoterModel
decisions *models.DecisionModel
ctx context.Context
jobScheduler *JobScheduler
mailNotifier *MailNotifier
mailConfig mailConfig
baseURL string
2022-05-09 19:09:24 +00:00
}
func main() {
2022-05-09 19:09:24 +00:00
configFile := flag.String("config", "config.yaml", "Configuration file name")
flag.Parse()
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
errorLog := log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime)
infoLog.Printf("CAcert Board Voting version %s, commit %s built at %s", version, commit, date)
ctx, err := parseConfig(context.Background(), *configFile)
if err != nil {
errorLog.Fatal(err)
}
2022-05-15 18:10:49 +00:00
config, err := GetConfig(ctx)
if err != nil {
errorLog.Fatal(err)
}
2022-05-09 19:09:24 +00:00
2022-05-15 18:10:49 +00:00
db, err := openDB(config.DatabaseFile)
if err != nil {
errorLog.Fatal(err)
2022-05-09 19:09:24 +00:00
}
2022-05-15 18:10:49 +00:00
defer func(db *sqlx.DB) {
_ = db.Close()
}(db)
2022-05-09 19:09:24 +00:00
2022-05-15 18:10:49 +00:00
if err != nil {
errorLog.Fatalf("could not setup decision model: %v", err)
}
2022-05-09 19:09:24 +00:00
app := &application{
2022-05-15 18:10:49 +00:00
errorLog: errorLog,
infoLog: infoLog,
decisions: &models.DecisionModel{DB: db, InfoLog: infoLog},
voters: &models.VoterModel{DB: db},
ctx: context.Background(),
mailConfig: config.MailConfig,
baseURL: config.BaseURL,
2022-05-09 19:09:24 +00:00
}
2022-05-15 18:10:49 +00:00
app.NewMailNotifier()
app.NewJobScheduler()
2022-05-09 19:09:24 +00:00
2022-05-15 18:10:49 +00:00
err = internal.InitializeDb(db.DB, infoLog)
2022-05-09 19:09:24 +00:00
if err != nil {
errorLog.Fatal(err)
}
srv := &http.Server{
2022-05-15 18:10:49 +00:00
Addr: config.HTTPAddress,
ErrorLog: errorLog,
Handler: app.routes(),
IdleTimeout: config.Timeouts.Idle,
ReadHeaderTimeout: config.Timeouts.ReadHeader,
ReadTimeout: config.Timeouts.Read,
WriteTimeout: config.Timeouts.Write,
2022-05-09 19:09:24 +00:00
}
infoLog.Printf("Starting server on %s", config.HTTPAddress)
err = srv.ListenAndServe()
2022-05-15 18:10:49 +00:00
2022-05-09 19:09:24 +00:00
errorLog.Fatal(err)
}
2022-05-15 18:10:49 +00:00
func openDB(dbFile string) (*sqlx.DB, error) {
db, err := sql.Open("sqlite3", dbFile)
if err != nil {
return nil, fmt.Errorf("could not open database file %s: %w", dbFile, err)
}
if err = db.Ping(); err != nil {
return nil, fmt.Errorf("could not ping database: %w", err)
}
return sqlx.NewDb(db, "sqlite3"), nil
}