Add POST /election
This commit is contained in:
7
cmd/web/handlers.go
Normal file
7
cmd/web/handlers.go
Normal file
@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
import "net/http"
|
||||
|
||||
func (app *application) createElection(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("TODO"))
|
||||
}
|
12
cmd/web/helpers.go
Normal file
12
cmd/web/helpers.go
Normal file
@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import "net/http"
|
||||
|
||||
func (app *application) serverError(w http.ResponseWriter, r *http.Request, err error) {
|
||||
var (
|
||||
method = r.Method
|
||||
uri = r.URL.RequestURI()
|
||||
)
|
||||
app.logger.Error(err.Error(), "method", method, "uri", uri)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
@ -5,11 +5,12 @@ package main
|
||||
import (
|
||||
"code.dlmw.ch/dlmw/qv/internal/models"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/go-playground/form/v4"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
type application struct {
|
||||
@ -18,8 +19,10 @@ type application struct {
|
||||
formDecoder *form.Decoder
|
||||
}
|
||||
|
||||
var addr = ":8080"
|
||||
|
||||
func main() {
|
||||
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
|
||||
db, err := openDb()
|
||||
if err != nil {
|
||||
@ -30,13 +33,26 @@ func main() {
|
||||
|
||||
formDecoder := form.NewDecoder()
|
||||
|
||||
app := application{
|
||||
app := &application{
|
||||
logger: logger,
|
||||
elections: &models.ElectionModel{DB: db},
|
||||
formDecoder: formDecoder,
|
||||
}
|
||||
|
||||
fmt.Println(app)
|
||||
logger.Info("Starting server", "addr", addr)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: app.routes(),
|
||||
ErrorLog: slog.NewLogLogger(logger.Handler(), slog.LevelError),
|
||||
IdleTimeout: time.Minute,
|
||||
ReadTimeout: 5 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
err = srv.ListenAndServe()
|
||||
logger.Error(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func openDb() (*sql.DB, error) {
|
||||
|
31
cmd/web/middleware.go
Normal file
31
cmd/web/middleware.go
Normal file
@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (app *application) recoverPanic(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
w.Header().Set("Connection", "close")
|
||||
app.serverError(w, r, fmt.Errorf("%s", err))
|
||||
}
|
||||
}()
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func (app *application) logRequest(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ip = r.RemoteAddr
|
||||
proto = r.Proto
|
||||
method = r.Method
|
||||
uri = r.URL.RequestURI()
|
||||
)
|
||||
app.logger.Info("received request", "ip", ip, "proto", proto, "method", method, "uri", uri)
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
15
cmd/web/routes.go
Normal file
15
cmd/web/routes.go
Normal file
@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/justinas/alice"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (app *application) routes() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("POST /election", app.createElection)
|
||||
|
||||
standard := alice.New(app.recoverPanic, app.logRequest)
|
||||
return standard.Then(mux)
|
||||
}
|
Reference in New Issue
Block a user