diff --git a/app/main.go b/app/main.go index 434cf16..ff92f4c 100644 --- a/app/main.go +++ b/app/main.go @@ -4,68 +4,16 @@ import ( "iris-test/app/cfg" "iris-test/app/db" "iris-test/app/logging" + "iris-test/app/views" "github.com/kataras/iris/v12" ) -type User struct { - firstName string - lastName string - email string -} - -var users = []User{ - { - firstName: "Pero", - lastName: "Perić", - email: "pero@gmail.com", - }, - { - firstName: "Mirko", - lastName: "Mirković", - email: "mirko@gmail.com", - }, - { - firstName: "Ivo", - lastName: "Ivić", - email: "ivo@gmail.com", - }, - { - firstName: "Slavko", - lastName: "Slavković", - email: "slavko@gmail.com", - }, -} - -func createRouter(app *iris.Application) { - app.Get("/", func(ctx iris.Context) { - params1 := []string{"param 1", "param 2", "param 3"} - ctx.ViewData("params1", params1) - ctx.ViewData("users", users) - - if err := ctx.View("pages/index.jet"); err != nil { - ctx.HTML("

%s

", err.Error()) - return - } - }) - - app.Get("/redirect/{namedRoute}", func(ctx iris.Context) { - routeName := ctx.Params().Get("namedRoute") - r := app.GetRoute(routeName) - if r == nil { - ctx.StatusCode(iris.StatusNotFound) - ctx.Writef("Route with name %s not found", routeName) - return - } - - println("The path of " + routeName + "is: " + r.Path) - // if routeName == "my-page1" - // prints: The path of of my-page1 is: /mypath - // if it's a path which takes named parameters - // then use "r.ResolvePath(paramValuesHere)" - ctx.Redirect(r.Path) - // http://localhost:8080/redirect/my-page1 will redirect to -> http://localhost:8080/mypath - }) +func createApp() *iris.Application { + app := iris.New() + app.RegisterView(iris.Jet("./app/templates", ".jet").Reload(true)) + views.CreateRouter(app) + return app } func main() { @@ -73,40 +21,6 @@ func main() { logging.Init() db.InitDB() - app := iris.New() - app.RegisterView(iris.Jet("./app/templates", ".jet").Reload(true)) - - // mypathRoute := app.Get("/mypath", writePathHandler) - // mypathRoute.Name = "my-page1" - - // mypath2Route := app.Get("/mypath2/{paramfirst}/{paramsecond}", writePathHandler) - // mypath2Route.Name = "my-page2" - - // mypath3Route := app.Get("/mypath3/{paramfirst}/statichere/{paramsecond}", writePathHandler) - // mypath3Route.Name = "my-page3" - - // mypath4Route := app.Get("/mypath4/{paramfirst}/statichere/{paramsecond}/{otherparam}/{something:path}", writePathHandler) - // // same as: app.Get("/mypath4/:paramfirst/statichere/:paramsecond/:otherparam/*something", writePathHandler) - // mypath4Route.Name = "my-page4" - - // // same with Handle/Func - // mypath5Route := app.Handle("GET", "/mypath5/{paramfirst:int}/statichere/{paramsecond}/{otherparam}/anything/{something:path}", writePathHandlerPage5) - // mypath5Route.Name = "my-page5" - - // mypath6Route := app.Get("/mypath6/{paramfirst}/{paramsecond}/statichere/{paramThirdAfterStatic}", writePathHandler) - // mypath6Route.Name = "my-page6" - - createRouter(app) - - // http://localhost:8080 - // http://localhost:8080/redirect/my-page1 + app := createApp() app.Listen(":8000") } - -func writePathHandler(ctx iris.Context) { - ctx.Writef("Hello from %s.", ctx.Path()) -} - -func writePathHandlerPage5(ctx iris.Context) { - ctx.Writef("Hello from %s.\nparamfirst(int)=%d", ctx.Path(), ctx.Params().GetIntDefault("paramfirst", 0)) -} diff --git a/app/views/index.go b/app/views/index.go new file mode 100644 index 0000000..40c7df6 --- /dev/null +++ b/app/views/index.go @@ -0,0 +1,43 @@ +package views + +import "github.com/kataras/iris/v12" + +type User struct { + firstName string + lastName string + email string +} + +var users = []User{ + { + firstName: "Pero", + lastName: "Perić", + email: "pero@gmail.com", + }, + { + firstName: "Mirko", + lastName: "Mirković", + email: "mirko@gmail.com", + }, + { + firstName: "Ivo", + lastName: "Ivić", + email: "ivo@gmail.com", + }, + { + firstName: "Slavko", + lastName: "Slavković", + email: "slavko@gmail.com", + }, +} + +func GetIndexPage(ctx iris.Context) { + params1 := []string{"param 1", "param 2", "param 3"} + ctx.ViewData("params1", params1) + ctx.ViewData("users", users) + + if err := ctx.View("pages/index.jet"); err != nil { + showError(ctx, err) + return + } +} diff --git a/app/views/router.go b/app/views/router.go new file mode 100644 index 0000000..ff8512e --- /dev/null +++ b/app/views/router.go @@ -0,0 +1,7 @@ +package views + +import "github.com/kataras/iris/v12" + +func CreateRouter(app *iris.Application) { + app.Get("/", GetIndexPage) +} diff --git a/app/views/util.go b/app/views/util.go new file mode 100644 index 0000000..4974d67 --- /dev/null +++ b/app/views/util.go @@ -0,0 +1,7 @@ +package views + +import "github.com/kataras/iris/v12" + +func showError(ctx iris.Context, err error) { + ctx.HTML("

%s

", err.Error()) +}