package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
)
func main() {
app := iris.New()
app.Get("/", func(ctx context.Context) {
ctx.Writef("Hello from the SECURE server")
})
// start the server (HTTPS) on port 443
app.Run(iris.TLS("127.0.0.1:443", "mycert.cert", "mykey.key"))
}
Arquivo da tag: Iris
golang Iris – function to view
// Default template funcs are:
//
// - {{ urlpath "myNamedRoute" "pathParameter_ifNeeded" }}
// - {{ render "header.html" }}
// and partial relative path to current page:
// - {{ render_r "header.html" }}
// - {{ yield }}
// - {{ current }}
// Register a custom template func:
tmpl.AddFunc("greet", func(s string) string {
return "Greetings " + s + "!"
})
golang iris return directly template html
package controllers
import (
"github.com/kataras/iris/mvc"
)
type HelloController struct{}
var helloView = mvc.View{
Name: "hello/index.html",
Data: map[string]interface{}{
"Title": "Hello Page",
//"MyMessage": "Welcome to my awesome website",
},
}
func (c *HelloController) Get() mvc.Result {
return helloView
}
Golang Iris – ctx.ViewData(key,value) alternative for HTML output
@krokite I don’t know if this is the best way, but this is how I do it and it works:
ctx.ViewData("content", template.HTML("<p>This is HTML content.</p><br><h3>Another String</h3>"))
or create a tmpl func:
tmpl := iris.HTML(...) tmpl.AddFunc("html", func(text string) template.HTML { return template.HTML(text) }) ctx.ViewData("text", "<p>This is HTML content.</p><br><h3>Another String</h3>") {{html .text}}
Don’t forget to import the "html/template"
package.
Routing middleware
package main
import "github.com/kataras/iris/v12"
func main() {
app := iris.New()
// or app.Use(before) and app.Done(after).
app.Get("/", before, mainHandler, after)
app.Listen(":8080")
}
func before(ctx iris.Context) {
shareInformation := "this is a sharable information between handlers"
requestPath := ctx.Path()
println("Before the mainHandler: " + requestPath)
ctx.Values().Set("info", shareInformation)
ctx.Next() // execute the next handler, in this case the main one.
}
func after(ctx iris.Context) {
println("After the mainHandler")
}
func mainHandler(ctx iris.Context) {
println("Inside mainHandler")
// take the info from the "before" handler.
info := ctx.Values().GetString("info")
// write something to the client as a response.
ctx.HTML("<h1>Response</h1>")
ctx.HTML("<br/> Info: " + info)
ctx.Next() // execute the "after".
}
golang – Redirect in POST method in Iris Framework
// login_controllers.go
package controllers
import (
"github.com/kataras/iris/mvc"
"github.com/kataras/iris"
"fmt"
)
type LoginFormData struct {
Email string
Password string
}
type LoginController struct {
mvc.C
}
func (c *LoginController) Get() mvc.Result {
return mvc.View{
Name: "login.html",
}
}
func (c *LoginController) Post() {
userLoginData := LoginFormData{}
err := c.Ctx.ReadForm(&userLoginData)
if err != nil {
c.Ctx.StatusCode(iris.StatusInternalServerError)
c.Ctx.WriteString(err.Error())
}
if userLoginData.Email == "teste@teste.com" && userLoginData.Password == "123" {
c.Ctx.Redirect("/", iris.StatusSeeOther)
fmt.Printf("UserInner: %#v", userLoginData)
c.Ctx.Writef("UserInner: %#v", userLoginData)
}
}