refactor: unify error handling

This commit is contained in:
2025-09-03 21:09:42 +02:00
parent 6db6127522
commit 863171f66b
6 changed files with 54 additions and 40 deletions

View File

@@ -7,7 +7,6 @@ import (
"io"
"log"
"net/http"
"os"
"git.0x0001f346.de/andreas/ablage/config"
"github.com/julienschmidt/httprouter"
@@ -25,7 +24,7 @@ var assetScriptJS []byte
//go:embed assets/style.css
var assetStyleCSS []byte
func Init() {
func Init() error {
router := httprouter.New()
router.NotFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -57,16 +56,14 @@ func Init() {
config.PrintStartupBanner()
err := http.ListenAndServe(fmt.Sprintf(":%d", config.GetPortToListenOn()), handler)
if err != nil {
fmt.Fprintf(os.Stderr, "Ablage exited with error:\n%v\n", err)
os.Exit(1)
return fmt.Errorf("Webserver exited with error: %v", err)
}
return
return nil
}
tlsCert, err := tls.X509KeyPair(config.GetTLSCertificate(), config.GetTLSKey())
if err != nil {
fmt.Fprintf(os.Stderr, "Faild to parse PEM encoded public/private key pair:\n%v\n", err)
os.Exit(1)
return fmt.Errorf("Faild to parse PEM encoded public/private key pair: %v", err)
}
server := &http.Server{
@@ -83,9 +80,10 @@ func Init() {
err = server.ListenAndServeTLS("", "")
if err != nil {
fmt.Fprintf(os.Stderr, "Ablage exited with error:\n%v\n", err)
os.Exit(1)
return fmt.Errorf("Webserver exited with error: %v", err)
}
return nil
}
func getClientIP(r *http.Request) string {