1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-08 12:31:58 +00:00

20 days of unstoppable work. Waiting fo go 1.8, I didn't finish yet, some touches remains.

Former-commit-id: ed84f99c89f43fe5e980a8e6d0ee22c186f0e1b9
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-02-14 05:54:11 +02:00
parent 2b2a205e63
commit 244a59e055
108 changed files with 9016 additions and 7596 deletions

View File

@@ -0,0 +1,53 @@
package main
import (
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
"gopkg.in/kataras/iris.v6/middleware/logger"
)
func main() {
app := iris.New()
app.Adapt(iris.DevLogger()) // it just enables the print of the iris.DevMode logs. Enable it to view the middleware's messages.
app.Adapt(httprouter.New())
customLogger := logger.New(logger.Config{
// Status displays status code
Status: true,
// IP displays request's remote address
IP: true,
// Method displays the http method
Method: true,
// Path displays the request path
Path: true,
})
app.Use(customLogger)
app.Get("/", func(ctx *iris.Context) {
ctx.Writef("hello")
})
app.Get("/1", func(ctx *iris.Context) {
ctx.Writef("hello")
})
app.Get("/2", func(ctx *iris.Context) {
ctx.Writef("hello")
})
// log http errors
errorLogger := logger.New()
app.OnError(iris.StatusNotFound, func(ctx *iris.Context) {
errorLogger.Serve(ctx)
ctx.Writef("My Custom 404 error page ")
})
// http://localhost:8080
// http://localhost:8080/1
// http://localhost:8080/2
app.Listen(":8080")
}

View File

@@ -0,0 +1,21 @@
package logger
// Config are the options of the logger middlweare
// contains 4 bools
// Status, IP, Method, Path
// if set to true then these will print
type Config struct {
// Status displays status code (bool)
Status bool
// IP displays request's remote address (bool)
IP bool
// Method displays the http method (bool)
Method bool
// Path displays the request path (bool)
Path bool
}
// DefaultConfig returns an options which all properties are true except EnableColors
func DefaultConfig() Config {
return Config{true, true, true, true}
}

View File

@@ -0,0 +1,62 @@
package logger
import (
"fmt"
"strconv"
"time"
"gopkg.in/kataras/iris.v6"
)
type loggerMiddleware struct {
config Config
}
// Serve serves the middleware
func (l *loggerMiddleware) Serve(ctx *iris.Context) {
//all except latency to string
var date, status, ip, method, path string
var latency time.Duration
var startTime, endTime time.Time
path = ctx.Path()
method = ctx.Method()
startTime = time.Now()
ctx.Next()
//no time.Since in order to format it well after
endTime = time.Now()
date = endTime.Format("01/02 - 15:04:05")
latency = endTime.Sub(startTime)
if l.config.Status {
status = strconv.Itoa(ctx.ResponseWriter.StatusCode())
}
if l.config.IP {
ip = ctx.RemoteAddr()
}
if !l.config.Method {
method = ""
}
if !l.config.Path {
path = ""
}
//finally print the logs
ctx.Log(iris.DevMode, fmt.Sprintf("%s %v %4v %s %s %s \n", date, status, latency, ip, method, path))
}
// New returns the logger middleware
// receives optional configs(logger.Config)
func New(cfg ...Config) iris.HandlerFunc {
c := DefaultConfig()
if len(cfg) > 0 {
c = cfg[0]
}
l := &loggerMiddleware{config: c}
return l.Serve
}