ekzyis 7558655458 refactor: Structure code into different packages
I have put too much code into the same files.

Also, I put everything into the same package: main.

This package is only meant for executables.

Therefore, I have refactored my code to use multiple packages. These packages also guarantee separation of concerns since Golang doesn't allow cyclic imports.
2023-09-10 23:13:08 +02:00

46 lines
865 B
Go

package server
import (
"fmt"
"net/http"
"os"
"github.com/labstack/echo/v4"
)
func httpErrorHandler(err error, c echo.Context) {
c.Logger().Error(err)
code := http.StatusInternalServerError
if httpError, ok := err.(*echo.HTTPError); ok {
code = httpError.Code
}
filePath := fmt.Sprintf("public/%d.html", code)
var f *os.File
if f, err = os.Open(filePath); err != nil {
c.Logger().Error(err)
serveError(c, 500)
return
}
if err = c.Stream(code, "text/html", f); err != nil {
c.Logger().Error(err)
serveError(c, 500)
return
}
}
func serveError(c echo.Context, code int) error {
var (
f *os.File
err error
)
if f, err = os.Open(fmt.Sprintf("public/%d.html", code)); err != nil {
c.Logger().Error(err)
return err
}
if err = c.Stream(code, "text/html", f); err != nil {
c.Logger().Error(err)
return err
}
return nil
}