113 lines
2.9 KiB
Go
113 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"iris-test/app/cfg"
|
|
"iris-test/app/db"
|
|
"iris-test/app/logging"
|
|
|
|
"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("<h3>%s</h3>", 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 main() {
|
|
cfg.Init()
|
|
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.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))
|
|
}
|