Restructure appp
This commit is contained in:
100
app/main.go
100
app/main.go
@ -4,68 +4,16 @@ import (
|
|||||||
"iris-test/app/cfg"
|
"iris-test/app/cfg"
|
||||||
"iris-test/app/db"
|
"iris-test/app/db"
|
||||||
"iris-test/app/logging"
|
"iris-test/app/logging"
|
||||||
|
"iris-test/app/views"
|
||||||
|
|
||||||
"github.com/kataras/iris/v12"
|
"github.com/kataras/iris/v12"
|
||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
func createApp() *iris.Application {
|
||||||
firstName string
|
app := iris.New()
|
||||||
lastName string
|
app.RegisterView(iris.Jet("./app/templates", ".jet").Reload(true))
|
||||||
email string
|
views.CreateRouter(app)
|
||||||
}
|
return app
|
||||||
|
|
||||||
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() {
|
func main() {
|
||||||
@ -73,40 +21,6 @@ func main() {
|
|||||||
logging.Init()
|
logging.Init()
|
||||||
db.InitDB()
|
db.InitDB()
|
||||||
|
|
||||||
app := iris.New()
|
app := createApp()
|
||||||
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")
|
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))
|
|
||||||
}
|
|
||||||
|
|||||||
43
app/views/index.go
Normal file
43
app/views/index.go
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
7
app/views/router.go
Normal file
7
app/views/router.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package views
|
||||||
|
|
||||||
|
import "github.com/kataras/iris/v12"
|
||||||
|
|
||||||
|
func CreateRouter(app *iris.Application) {
|
||||||
|
app.Get("/", GetIndexPage)
|
||||||
|
}
|
||||||
7
app/views/util.go
Normal file
7
app/views/util.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package views
|
||||||
|
|
||||||
|
import "github.com/kataras/iris/v12"
|
||||||
|
|
||||||
|
func showError(ctx iris.Context, err error) {
|
||||||
|
ctx.HTML("<h3>%s</h3>", err.Error())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user