Skip to content

Instantly share code, notes, and snippets.

@OmisNomis
Created April 9, 2018 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OmisNomis/ae01c39d91dbd290858c8c65eb134903 to your computer and use it in GitHub Desktop.
Save OmisNomis/ae01c39d91dbd290858c8c65eb134903 to your computer and use it in GitHub Desktop.
Go Tutorial Series
package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/middleware/logger"
"github.com/kataras/iris/middleware/recover"
)
func main() {
app := iris.New()
/**
* Logging
*/
app.Logger().SetLevel("debug")
// Recover from panics and log the panic message to the application's logger ("Warn" level).
app.Use(recover.New())
// logs HTTP requests to the application's logger ("Info" level)
app.Use(logger.New())
/**
* Routes
*/
// GET http://localhost:8080/regex/id
// 'id' must be a string which contacts 10 or more numbers
app.Get("/regex/{id:string regexp(^[0-9]{10}.*$)}", func(ctx iris.Context) {
ctx.Writef("Hello %s", ctx.Params().Get("id"))
})
// POST http://localhost:8080/panic
app.Post("/panic", func(ctx iris.Context) {
// app will handle this panic message thanks to the app.use statement above
panic("recover will log this panic message to the logger")
})
// GET http://localhost:8080/html
app.Get("/html", func(ctx iris.Context) {
// Return HTML
ctx.HTML("<h1>Welcome</h1>")
})
// Put http://localhost:8080/ping
app.Put("/ping", func(ctx iris.Context) {
// Return a string
ctx.WriteString("pong")
})
// Delete http://localhost:8080/hello
app.Delete("/hello", func(ctx iris.Context) {
// Return JSON
ctx.JSON(iris.Map{"message": "Hello Iris!"})
})
// Start the server on port 8080
// ignore server closed errors ([ERRO] 2018/04/09 12:25 http: Server closed)
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment