cacert-boardvoting/cmd/boardvoting/main.go

95 lines
2.1 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.
*/
// The CAcert board voting software.
package main
import (
"context"
"flag"
"io/fs"
"log"
"net/http"
"os"
"github.com/vearutop/statigz"
"github.com/vearutop/statigz/brotli"
"git.cacert.org/cacert-boardvoting/ui"
)
var (
version = "undefined"
commit = "undefined"
date = "undefined"
)
type application struct {
errorLog, infoLog *log.Logger
}
func main() {
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)
}
mux := http.NewServeMux()
staticDir, _ := fs.Sub(ui.Files, "static")
staticData, ok := staticDir.(fs.ReadDirFS)
if !ok {
errorLog.Fatal("could not use uiStaticDir as fs.ReadDirFS")
}
fileServer := statigz.FileServer(staticData, brotli.AddEncoding, statigz.EncodeOnInit)
mux.Handle("/static/", http.StripPrefix("/static", fileServer))
app := &application{
errorLog: errorLog,
infoLog: infoLog,
}
mux.HandleFunc("/", app.home)
mux.HandleFunc("/motions/", app.motions)
config, err := GetConfig(ctx)
if err != nil {
errorLog.Fatal(err)
}
srv := &http.Server{
Addr: config.HTTPAddress,
ErrorLog: errorLog,
Handler: mux,
}
infoLog.Printf("Starting server on %s", config.HTTPAddress)
err = srv.ListenAndServe()
errorLog.Fatal(err)
}