1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 18:07:01 +00:00

Add example for struct validation(3rd-party) through json request body binding

Former-commit-id: 78bbbe068f219e5a264951c900b77cb9b70f2079
This commit is contained in:
Gerasimos (Makis) Maropoulos
2018-07-24 04:33:53 +03:00
parent 7337396e3c
commit 247a558394
6 changed files with 480 additions and 293 deletions

View File

@@ -7,8 +7,7 @@ import (
"github.com/kataras/iris"
)
// get a filename based on the date, file logs works that way the most times
// but these are just a sugar.
// Get a filename based on the date, just for the sugar.
func todayFilename() string {
today := time.Now().Format("Jan 02 2006")
return today + ".txt"
@@ -16,7 +15,7 @@ func todayFilename() string {
func newLogFile() *os.File {
filename := todayFilename()
// open an output file, this will append to the today's file if server restarted.
// Open the file, this will append to the today's file if server restarted.
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
panic(err)
@@ -30,20 +29,20 @@ func main() {
defer f.Close()
app := iris.New()
// attach the file as logger, remember, iris' app logger is just an io.Writer.
app.Logger().SetOutput(newLogFile())
// Attach the file as logger, remember, iris' app logger is just an io.Writer.
// Use the following code if you need to write the logs to file and console at the same time.
// app.Logger().SetOutput(io.MultiWriter(f, os.Stdout))
app.Logger().SetOutput(f)
app.Get("/", func(ctx iris.Context) {
app.Get("/ping", func(ctx iris.Context) {
// for the sake of simplicity, in order see the logs at the ./_today_.txt
ctx.Application().Logger().Info("Request path: " + ctx.Path())
ctx.Writef("hello")
ctx.Application().Logger().Infof("Request path: %s", ctx.Path())
ctx.WriteString("pong")
})
// navigate to http://localhost:8080
// and open the ./logs.txt file
if err := app.Run(iris.Addr(":8080"), iris.WithoutBanner); err != nil {
if err != iris.ErrServerClosed {
app.Logger().Warn("Shutdown with error: " + err.Error())
}
// Navigate to http://localhost:8080/ping
// and open the ./logs{TODAY}.txt file.
if err := app.Run(iris.Addr(":8080"), iris.WithoutBanner, iris.WithoutServerError(iris.ErrServerClosed)); err != nil {
app.Logger().Warn("Shutdown with error: " + err.Error())
}
}