forked from kataras/iris
/
main.go
75 lines (58 loc) · 2.74 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Package main an example on how to naming your routes & use the custom 'url' HTML Template Engine, same for other template engines.
package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
"github.com/kataras/iris/core/router"
"github.com/kataras/iris/view"
)
const (
host = "127.0.0.1:8080"
)
func main() {
app := iris.New()
// create a custom path reverser, iris let you define your own host and scheme
// which is useful when you have nginx or caddy in front of iris.
rv := router.NewRoutePathReverser(app, router.WithHost(host), router.WithScheme("http"))
// locate and define our templates as usual.
templates := view.HTML("./templates", ".html")
// add a custom func of "url" and pass the rv.URL as its template function body,
// so {{url "routename" "paramsOrSubdomainAsFirstArgument"}} will work inside our templates.
templates.AddFunc("url", rv.URL)
app.AttachView(templates)
// wildcard subdomain, will catch username1.... username2.... username3... username4.... username5...
// that our below links are providing via page.html's first argument which is the subdomain.
subdomain := app.Party("*.")
mypathRoute, _ := subdomain.Get("/mypath", emptyHandler)
mypathRoute.Name = "my-page1"
mypath2Route, _ := subdomain.Get("/mypath2/{paramfirst}/{paramsecond}", emptyHandler)
mypath2Route.Name = "my-page2"
mypath3Route, _ := subdomain.Get("/mypath3/{paramfirst}/statichere/{paramsecond}", emptyHandler)
mypath3Route.Name = "my-page3"
mypath4Route, _ := subdomain.Get("/mypath4/{paramfirst}/statichere/{paramsecond}/{otherparam}/{something:path}", emptyHandler)
mypath4Route.Name = "my-page4"
mypath5Route, _ := subdomain.Handle("GET", "/mypath5/{paramfirst}/statichere/{paramsecond}/{otherparam}/anything/{something:path}", emptyHandler)
mypath5Route.Name = "my-page5"
mypath6Route, err := subdomain.Get("/mypath6/{paramfirst}/{paramsecond}/staticParam/{paramThirdAfterStatic}", emptyHandler)
if err != nil { // catch any route problems when declare a route or on err := app.Run(...); err != nil { panic(err) }
panic(err)
}
mypath6Route.Name = "my-page6"
app.Get("/", func(ctx context.Context) {
// for username5./mypath6...
paramsAsArray := []string{"username5", "theParam1", "theParam2", "paramThirdAfterStatic"}
ctx.ViewData("ParamsAsArray", paramsAsArray)
if err := ctx.View("page.html"); err != nil {
panic(err)
}
})
// http://127.0.0.1:8080
app.Run(iris.Addr(host))
}
func emptyHandler(ctx context.Context) {
ctx.Writef("Hello from subdomain: %s , you're in path: %s", ctx.Subdomain(), ctx.Path())
}
// Note:
// If you got an empty string on {{ url }} or {{ urlpath }} it means that
// args length are not aligned with the route's parameters length
// or the route didn't found by the passed name.