Use cgo-free SQL driver and add limit of 100 to maxVoters

This commit is contained in:
2025-01-21 09:17:40 +01:00
parent 1b6fc173d3
commit 5570dca6c9
6 changed files with 112 additions and 114 deletions

View File

@ -72,6 +72,7 @@ func (r *createElectionRequestWithValidator) isValid() bool {
r.CheckField(validator.After(r.ExpiresAt, time.Now()), "expiresAt", "must expire in a future date")
r.CheckField(validator.GreaterThan(len(r.Choices), 1), "choices", "there must be more than 1 choice")
r.CheckField(validator.UniqueValues(r.Choices), "choices", "must not contain duplicate values")
r.CheckField(validator.LesserThan(r.MaxVoters, 101), "maxVoters", "cannot create a known-voters election with more than 100 voters")
for _, choice := range r.Choices {
r.CheckField(validator.NotBlank(choice), "choice", "must not be blank")

View File

@ -7,8 +7,8 @@ import (
"code.dlmw.ch/dlmw/qv/internal/models"
"context"
"database/sql"
_ "github.com/mattn/go-sqlite3"
"log/slog"
_ "modernc.org/sqlite"
"net/http"
"os"
"os/signal"
@ -17,6 +17,15 @@ import (
)
var addr = ":8080"
var databasePath string
func init() {
if os.Getenv("QV_DATABASE_PATH") == "" {
databasePath = "./qv.sqlite"
} else {
databasePath = os.Getenv("QV_DATABASE_PATH")
}
}
func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
@ -46,9 +55,9 @@ func main() {
Addr: addr,
Handler: app.routes(),
ErrorLog: slog.NewLogLogger(logger.Handler(), slog.LevelError),
IdleTimeout: time.Minute,
IdleTimeout: 30 * time.Second,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second, // TODO: add conf to make maxVoters limit flexible, and also add for addr
WriteTimeout: 10 * time.Second,
}
go watchForQuitSignals(srv, logger)
@ -60,7 +69,7 @@ func main() {
}
func openDb() (*sql.DB, error) {
db, err := sql.Open("sqlite3", "./qv.sqlite?_foreign_keys=on&_busy_timeout=5000")
db, err := sql.Open("sqlite", databasePath+"?_foreign_keys=on&_busy_timeout=5000")
if err == nil {
err = db.Ping()
}