middelware
This commit is contained in:
parent
5a0f51fd49
commit
e43e85dc51
3 changed files with 47 additions and 41 deletions
|
|
@ -1,41 +0,0 @@
|
||||||
package middelware
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/rs/zerolog"
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Logging struct {
|
|
||||||
log zerolog.Logger
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCLILogger() Logging {
|
|
||||||
log := log.Logger.With().Str("pkg", "middelware").Logger().Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
|
||||||
l := Logging{
|
|
||||||
log: log,
|
|
||||||
}
|
|
||||||
return l
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l Logging) Middelware(someThing string) func(http.Handler) http.Handler {
|
|
||||||
return func(next http.Handler) http.Handler {
|
|
||||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// Logic here
|
|
||||||
uid := uuid.NewString()
|
|
||||||
|
|
||||||
start := time.Now()
|
|
||||||
// Call the next handler
|
|
||||||
next.ServeHTTP(w, r)
|
|
||||||
elapsed := time.Since(start)
|
|
||||||
|
|
||||||
log.Info().Str("method", r.Method).Str("URI", r.URL.String()).Str("uuid", uid).Int64("request time ms", elapsed.Milliseconds()).Msg("Get Request")
|
|
||||||
}
|
|
||||||
|
|
||||||
return http.HandlerFunc(fn)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
40
web/middelware/logging/logging.go
Normal file
40
web/middelware/logging/logging.go
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
package logging
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Logging struct {
|
||||||
|
log zerolog.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCLILogger() Logging {
|
||||||
|
log := log.Logger.With().Str("pkg", "middelware").Logger().Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||||||
|
l := Logging{
|
||||||
|
log: log,
|
||||||
|
}
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l Logging) Middelware(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
uid := uuid.NewString()
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
// Call the next handler
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
|
||||||
|
log.Info().Str("method", r.Method).Str("URI", r.URL.String()).Str("uuid", uid).Int64("request-time-ms", elapsed.Milliseconds()).Msg("Got Request")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l Logging) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
||||||
|
l.Middelware(next).ServeHTTP(rw, r)
|
||||||
|
}
|
||||||
7
web/middelware/middelware.go
Normal file
7
web/middelware/middelware.go
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
package middelware
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func something() {
|
||||||
|
fmt.Println("something")
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue