package main import ( "github.com/kataras/iris/v12" ) // User is just a bindable object structure. type User struct { Username string `json:"username"` Firstname string `json:"firstname"` Lastname string `json:"lastname"` City string `json:"city"` Age int `json:"age"` } func main() { app := iris.New() app.Logger().SetLevel("debug") // app.Logger().SetLevel("disable") to disable the logger. // Define templates using the std html/template engine. // Parse and load all files inside "./views" folder with ".html" file extension. // Reload the templates on each request (development mode). app.RegisterView(iris.HTML("./views", ".html").Reload(true)) // Register custom handler for specific http errors. app.OnErrorCode(iris.StatusInternalServerError, func(ctx iris.Context) { // .Values are used to communicate between handlers, middleware. errMessage := ctx.Values().GetString("error") if errMessage != "" { ctx.Writef("Internal server error: %s", errMessage) return } ctx.Writef("(Unexpected) internal server error") }) app.Use(func(ctx iris.Context) { ctx.Application().Logger().Infof("Begin request for path: %s", ctx.Path()) ctx.Next() }) // app.Done(func(ctx iris.Context) {]}) // POST: scheme://mysubdomain.$domain.com/decode app.Subdomain("mysubdomain.").Post("/decode", func(ctx iris.Context) {}) // Method POST: http://localhost:8080/decode app.Post("/decode", func(ctx iris.Context) { var user User ctx.ReadJSON(&user) ctx.Writef("%s %s is %d years old and comes from %s", user.Firstname, user.Lastname, user.Age, user.City) }) // Method GET: http://localhost:8080/encode app.Get("/encode", func(ctx iris.Context) { doe := User{ Username: "Johndoe", Firstname: "John", Lastname: "Doe", City: "Neither FBI knows!!!", Age: 25, } ctx.JSON(doe) }) // Method GET: http://localhost:8080/profile/anytypeofstring app.Get("/profile/{username:string}", profileByUsername) usersRoutes := app.Party("/users", logThisMiddleware) { // Method GET: http://localhost:8080/users/42 usersRoutes.Get("/{id:int min(1)}", getUserByID) // Method POST: http://localhost:8080/users/create usersRoutes.Post("/create", createUser) } app.Get("/", func(ctx iris.Context) { ctx.HTML("