diff --git a/web/errors.go b/web/errors.go index 76cb9068eec368d237d66fc8690eb9ef116d208b..c779d28ef90c44f63972211a4b0421d7628130c7 100644 --- a/web/errors.go +++ b/web/errors.go @@ -1,6 +1,9 @@ package web -import "github.com/gin-gonic/gin" +import ( + "github.com/gin-gonic/gin" + "net/http" +) func errorHandler(c *gin.Context) { c.Next() @@ -10,6 +13,8 @@ func errorHandler(c *gin.Context) { errorList[i] = e.Error() } if len(errorList) > 0 { - c.JSON(400, errorList) + c.HTML(http.StatusBadRequest, "error.html", gin.H{ + "errors": errorList, + }) } -} \ No newline at end of file +} diff --git a/web/router.go b/web/router.go index ed9be4681e52ac2b79d1c0163908ef51f746d199..fb9d2a83d8cce42a23c374669b1d3eaac5da9fc8 100644 --- a/web/router.go +++ b/web/router.go @@ -2,36 +2,36 @@ package web import ( "errors" + "fmt" "git.sch.bme.hu/blintmester/keresett-a-feri/route" "github.com/gin-gonic/gin" - "io/fs" "net/http" ) var Router = gin.Default() type StationURI struct { - Name string `uri:"name" binding:"required"` - Id string `uri:"uuid" binding:"required"` + //Name string `uri:"name" binding:"required"` + Id string `uri:"uuid" binding:"required"` } func init() { - staticfs, e := fs.Sub(static, "static") - if e != nil { - panic(e) - } + //staticfs, e := fs.Sub(static, "static") + //if e != nil { + // panic(e) + //} Router.Use(errorHandler) - //Router.SetHTMLTemplate(html) - - Router.StaticFS("/", http.FS(staticfs)) + Router.SetHTMLTemplate(html) - //for _, station := range route.Middle { - // Router.StaticFS("/" + station.Id, http.FS(staticfs)) - //} + Router.GET("/", mainPage) + //Router.StaticFS("/static", http.FS(staticfs)) + Router.GET("/station/:uuid", routeChooser) +} - Router.GET("/:name/:uuid", routeChooser) +func mainPage(c *gin.Context) { + c.HTML(http.StatusOK, "index.html", nil) } func routeChooser(c *gin.Context) { @@ -41,20 +41,16 @@ func routeChooser(c *gin.Context) { return } - if stationUri.Name != "station" { - c.Error(errors.New("wrong path")) // TODO: better error msg - return - } - if stationUri.Id == "first" { - c.HTML(http.StatusOK, "static/index.html", nil) + fmt.Println("First station") } else if stationUri.Id == "last" { - + fmt.Println("Last station") } else { found := false for _, station := range route.Middle { if stationUri.Id == station.Id { found = true + fmt.Println(station.Id) break } } diff --git a/web/templates.go b/web/templates.go index 4239ecc6429eedb0b0eeef6ffabf3c1a1d77643f..07da56ae67140587feeb750dbce01682d0175072 100644 --- a/web/templates.go +++ b/web/templates.go @@ -2,12 +2,13 @@ package web import ( "embed" + "html/template" ) -//go:embed static/* -var static embed.FS +////go:embed static/* +//var static embed.FS -////go:embed templates/* -//var templates embed.FS -// -//var html = template.Must(template.ParseFS(templates, "templates/*")) +//go:embed templates/* +var templates embed.FS + +var html = template.Must(template.ParseFS(templates, "templates/*")) diff --git a/web/templates/error.html b/web/templates/error.html new file mode 100644 index 0000000000000000000000000000000000000000..310dad9b0a9feebe38fec50568217e617f1323f5 --- /dev/null +++ b/web/templates/error.html @@ -0,0 +1,15 @@ +<!DOCTYPE html> +<html lang="hu"> +<head> + <meta charset="UTF-8"> + <title>Error</title> +</head> +<body> +<h1>Something fucked...</h1> + +<h2>Errors:</h2> +{{- range $key, $error := .errors }} +<ul>{{ $error }}</ul> +{{- end }} +</body> +</html> \ No newline at end of file diff --git a/web/static/index.html b/web/templates/index.html similarity index 100% rename from web/static/index.html rename to web/templates/index.html