Use httprouter.NotFound and httprouter.PanicHandler

main
Jan Dittberner 2 years ago
parent 0ad88fe5f4
commit 47af34f1cd

@ -29,11 +29,12 @@ import (
"strings" "strings"
"time" "time"
"git.cacert.org/cacert-boardvoting/internal/models"
"git.cacert.org/cacert-boardvoting/ui"
"github.com/Masterminds/sprig/v3" "github.com/Masterminds/sprig/v3"
"github.com/gorilla/csrf" "github.com/gorilla/csrf"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"git.cacert.org/cacert-boardvoting/internal/models"
"git.cacert.org/cacert-boardvoting/ui"
) )
func newTemplateCache() (map[string]*template.Template, error) { func newTemplateCache() (map[string]*template.Template, error) {

@ -119,34 +119,30 @@ func main() {
errChan := make(chan error, 1) errChan := make(chan error, 1)
go func() { infoLog.Printf("TLS config setup, starting TLS server on %s", config.HTTPSAddress)
redirect := &http.Server{
Addr: config.HTTPAddress,
Handler: http.RedirectHandler(config.BaseURL, http.StatusMovedPermanently),
IdleTimeout: config.Timeouts.Idle,
ReadHeaderTimeout: config.Timeouts.ReadHeader,
ReadTimeout: config.Timeouts.Read,
WriteTimeout: config.Timeouts.Write,
}
if err := redirect.ListenAndServe(); err != nil { go setupHTTPRedirect(config, errChan)
errChan <- err
err = app.startHTTPSServer(config)
if err != nil {
errorLog.Fatalf("ListenAndServeTLS (HTTPS) failed: %v", err)
} }
close(errChan) if err := <-errChan; err != nil {
}() errorLog.Fatalf("ListenAndServe (HTTP) failed: %v", err)
}
}
func (app *application) startHTTPSServer(config *Config) error {
tlsConfig, err := setupTLSConfig(config) tlsConfig, err := setupTLSConfig(config)
if err != nil { if err != nil {
errorLog.Fatalf("could not setup TLS configuration: %v", err) return fmt.Errorf("could not setup TLS configuration: %w", err)
} }
infoLog.Printf("TLS config setup, starting TLS server on %s", config.HTTPSAddress)
srv := &http.Server{ srv := &http.Server{
Addr: config.HTTPSAddress, Addr: config.HTTPSAddress,
TLSConfig: tlsConfig, TLSConfig: tlsConfig,
ErrorLog: errorLog, ErrorLog: app.errorLog,
Handler: app.routes(), Handler: app.routes(),
IdleTimeout: config.Timeouts.Idle, IdleTimeout: config.Timeouts.Idle,
ReadHeaderTimeout: config.Timeouts.ReadHeader, ReadHeaderTimeout: config.Timeouts.ReadHeader,
@ -156,12 +152,27 @@ func main() {
err = srv.ListenAndServeTLS(config.ServerCert, config.ServerKey) err = srv.ListenAndServeTLS(config.ServerCert, config.ServerKey)
if err != nil { if err != nil {
errorLog.Fatalf("ListenAndServeTLS (HTTPS) failed: %v", err) return fmt.Errorf("")
} }
if err := <-errChan; err != nil { return nil
errorLog.Fatalf("ListenAndServe (HTTP) failed: %v", err)
} }
func setupHTTPRedirect(config *Config, errChan chan error) {
redirect := &http.Server{
Addr: config.HTTPAddress,
Handler: http.RedirectHandler(config.BaseURL, http.StatusMovedPermanently),
IdleTimeout: config.Timeouts.Idle,
ReadHeaderTimeout: config.Timeouts.ReadHeader,
ReadTimeout: config.Timeouts.Read,
WriteTimeout: config.Timeouts.Write,
}
if err := redirect.ListenAndServe(); err != nil {
errChan <- err
}
close(errChan)
} }
func setupTLSConfig(config *Config) (*tls.Config, error) { func setupTLSConfig(config *Config) (*tls.Config, error) {

@ -18,7 +18,6 @@ limitations under the License.
package main package main
import ( import (
"fmt"
"net/http" "net/http"
) )
@ -42,16 +41,3 @@ func (app *application) logRequest(next http.Handler) http.Handler {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}) })
} }
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, fmt.Errorf("%s", err))
}
}()
next.ServeHTTP(w, r)
})
}

@ -18,6 +18,7 @@ limitations under the License.
package main package main
import ( import (
"fmt"
"io/fs" "io/fs"
"net/http" "net/http"
@ -30,8 +31,6 @@ import (
) )
func (app *application) routes() http.Handler { func (app *application) routes() http.Handler {
router := httprouter.New()
staticDir, _ := fs.Sub(ui.Files, "static") staticDir, _ := fs.Sub(ui.Files, "static")
staticData, ok := staticDir.(fs.ReadDirFS) staticData, ok := staticDir.(fs.ReadDirFS)
@ -41,6 +40,14 @@ func (app *application) routes() http.Handler {
fileServer := statigz.FileServer(staticData, brotli.AddEncoding, statigz.EncodeOnInit) fileServer := statigz.FileServer(staticData, brotli.AddEncoding, statigz.EncodeOnInit)
router := httprouter.New()
router.NotFound = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { app.notFound(w) })
router.PanicHandler = func(w http.ResponseWriter, _ *http.Request, err interface{}) {
w.Header().Set("Connection", "close")
app.serverError(w, fmt.Errorf("%s", err))
}
router.Handler(http.MethodGet, "/", http.RedirectHandler("/motions/", http.StatusMovedPermanently)) router.Handler(http.MethodGet, "/", http.RedirectHandler("/motions/", http.StatusMovedPermanently))
router.Handler( router.Handler(
http.MethodGet, http.MethodGet,
@ -50,14 +57,14 @@ func (app *application) routes() http.Handler {
router.Handler(http.MethodGet, "/static/*filepath", http.StripPrefix("/static", fileServer)) router.Handler(http.MethodGet, "/static/*filepath", http.StripPrefix("/static", fileServer))
router.HandlerFunc(http.MethodGet, "/motions/", app.motionList) router.HandlerFunc(http.MethodGet, "/motions/", app.motionList)
router.HandlerFunc(http.MethodGet, "/motions/:tag", app.motionDetails) router.HandlerFunc(http.MethodGet, "/motions/:tag", app.motionDetails)
router.HandlerFunc(http.MethodGet, "/vote/:tag", app.voteForm)
router.HandlerFunc(http.MethodPost, "/vote/:tag", app.voteSubmit)
router.HandlerFunc(http.MethodGet, "/proxy/:tag", app.proxyVoteForm)
router.HandlerFunc(http.MethodPost, "/proxy/:tag", app.proxyVoteSubmit)
router.HandlerFunc(http.MethodGet, "/newmotion/", app.newMotionForm) router.HandlerFunc(http.MethodGet, "/newmotion/", app.newMotionForm)
router.HandlerFunc(http.MethodPost, "/newmotion/", app.newMotionSubmit) router.HandlerFunc(http.MethodPost, "/newmotion/", app.newMotionSubmit)
router.HandlerFunc(http.MethodGet, "/vote/", app.voteForm)
router.HandlerFunc(http.MethodPost, "/vote/", app.voteSubmit)
router.HandlerFunc(http.MethodGet, "/proxy/", app.proxyVoteForm)
router.HandlerFunc(http.MethodPost, "/proxy/", app.proxyVoteSubmit)
standard := alice.New(app.recoverPanic, app.logRequest, secureHeaders) standard := alice.New(app.logRequest, secureHeaders)
return standard.Then(router) return standard.Then(router)
} }

Loading…
Cancel
Save