diff options
Diffstat (limited to 'boardvoting.go')
-rw-r--r-- | boardvoting.go | 66 |
1 files changed, 20 insertions, 46 deletions
diff --git a/boardvoting.go b/boardvoting.go index e8a83e4..7c957d7 100644 --- a/boardvoting.go +++ b/boardvoting.go @@ -23,7 +23,7 @@ import ( "github.com/gorilla/csrf" "github.com/gorilla/sessions" _ "github.com/mattn/go-sqlite3" - "github.com/op/go-logging" + log "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" "git.cacert.org/cacert-boardvoting/boardvoting" @@ -35,7 +35,6 @@ var store *sessions.CookieStore var csrfKey []byte var version = "undefined" var build = "undefined" -var log *logging.Logger const sessionCookieName = "votesession" @@ -74,7 +73,7 @@ func renderTemplate(w http.ResponseWriter, r *http.Request, templates []string, type contextKey int const ( - ctxNeedsAuth contextKey = iota + ctxNeedsAuth contextKey = iota ctxVoter ctxDecision ctxVote @@ -128,7 +127,7 @@ type motionParameters struct { } type motionListParameters struct { - Page int64 + Page int64 Flags struct { Confirmed, Withdraw, Unvoted bool } @@ -178,7 +177,11 @@ func motionListHandler(w http.ResponseWriter, r *http.Request) { if flashes := session.Flashes(); len(flashes) > 0 { templateContext.Flashes = flashes } - session.Save(r, w) + err = session.Save(r, w) + if err != nil { + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } templateContext.Params = ¶ms if templateContext.Decisions, err = FindDecisionsForDisplayOnPage(params.Page, params.Flags.Unvoted, templateContext.Voter); err != nil { @@ -281,16 +284,15 @@ type FlashMessageAction struct{} func (a *FlashMessageAction) AddFlash(w http.ResponseWriter, r *http.Request, message interface{}, tags ...string) { session, err := store.Get(r, sessionCookieName) if err != nil { - log.Errorf("getting session failed: %v", err) + log.Warnf("could not get session cookie: %v", err) return } session.AddFlash(message, tags...) - session.Save(r, w) + err = session.Save(r, w) if err != nil { - log.Errorf("saving session failed: %v", err) + log.Warnf("could not save flash message: %v", err) return } - return } type withDrawMotionAction struct { @@ -530,7 +532,7 @@ func (h *directVoteHandler) Handle(w http.ResponseWriter, r *http.Request) { case http.MethodPost: voteResult := &Vote{ VoterId: voter.Id, Vote: vote, DecisionId: decision.Id, Voted: time.Now().UTC(), - Notes: fmt.Sprintf("Direct Vote\n\n%s", getPEMClientCert(r))} + Notes: fmt.Sprintf("Direct Vote\n\n%s", getPEMClientCert(r))} if err := voteResult.Save(); err != nil { log.Errorf("Problem saving vote: %v", err) http.Error(w, "Problem saving vote", http.StatusInternalServerError) @@ -566,7 +568,10 @@ type proxyVoteHandler struct { func getPEMClientCert(r *http.Request) string { clientCertPEM := bytes.NewBufferString("") authenticatedCertificate := r.Context().Value(ctxAuthenticatedCert).(*x509.Certificate) - pem.Encode(clientCertPEM, &pem.Block{Type: "CERTIFICATE", Bytes: authenticatedCertificate.Raw}) + err := pem.Encode(clientCertPEM, &pem.Block{Type: "CERTIFICATE", Bytes: authenticatedCertificate.Raw}) + if err != nil { + log.Errorf("error encoding client certificate: %v", err) + } return clientCertPEM.String() } @@ -692,42 +697,12 @@ type Config struct { MigrationsPath string `yaml:"migrations_path"` HttpAddress string `yaml:"http_address"` HttpsAddress string `yaml:"https_address"` - MailServer struct { + MailServer struct { Host string `yaml:"host"` Port int `yaml:"port"` } `yaml:"mail_server"` } -func setupLogging(ctx context.Context) { - log = logging.MustGetLogger("boardvoting") - consoleLogFormat := logging.MustStringFormatter(`%{color}%{time:20060102 15:04:05.000-0700} %{longfile} ▶ %{level:s} %{id:05d}%{color:reset} %{message}`) - fileLogFormat := logging.MustStringFormatter(`%{time:20060102 15:04:05.000-0700} %{level:s} %{id:05d} %{message}`) - - consoleBackend := logging.NewLogBackend(os.Stderr, "", 0) - - logfile, err := os.OpenFile("boardvoting.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.FileMode(0640)) - if err != nil { - panic("Could not open logfile") - } - - fileBackend := logging.NewLogBackend(logfile, "", 0) - fileBackendLeveled := logging.AddModuleLevel(logging.NewBackendFormatter(fileBackend, fileLogFormat)) - fileBackendLeveled.SetLevel(logging.INFO, "") - - logging.SetBackend(fileBackendLeveled, - logging.NewBackendFormatter(consoleBackend, consoleLogFormat)) - - go func() { - for range ctx.Done() { - if err = logfile.Close(); err != nil { - fmt.Fprintf(os.Stderr, "Problem closing the log file: %v", err) - } - } - }() - - log.Info("Setup logging") -} - func readConfig() { source, err := ioutil.ReadFile(configFile) if err != nil { @@ -773,7 +748,7 @@ func setupDbConfig(ctx context.Context) { go func() { for range ctx.Done() { if err := db.Close(); err != nil { - fmt.Fprintf(os.Stderr, "Problem closing the database: %v", err) + _, _ = fmt.Fprintf(os.Stderr, "Problem closing the database: %v", err) } } }() @@ -838,11 +813,12 @@ func init() { } func main() { + log.Infof("CAcert Board Voting version %s, build %s", version, build) + flag.Parse() var stopAll func() executionContext, stopAll := context.WithCancel(context.Background()) - setupLogging(executionContext) readConfig() setupDbConfig(executionContext) setupNotifications(executionContext) @@ -852,8 +828,6 @@ func main() { defer stopAll() - log.Infof("CAcert Board Voting version %s, build %s", version, build) - server := &http.Server{ Addr: config.HttpsAddress, TLSConfig: tlsConfig, |