mirror of
https://github.com/tgdrive/teldrive.git
synced 2025-02-23 22:45:11 +08:00
32 lines
617 B
Go
32 lines
617 B
Go
package appcontext
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
)
|
|
|
|
type Context struct {
|
|
Writer http.ResponseWriter
|
|
Request *http.Request
|
|
context.Context
|
|
}
|
|
|
|
func newAppContext(w http.ResponseWriter, r *http.Request) *Context {
|
|
return &Context{
|
|
Writer: w,
|
|
Request: r,
|
|
Context: r.Context(),
|
|
}
|
|
}
|
|
|
|
func (c *Context) Write(code int, message string) {
|
|
c.Writer.WriteHeader(code)
|
|
c.Writer.Write([]byte(message))
|
|
}
|
|
|
|
func Middleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
ctx := newAppContext(w, r)
|
|
next.ServeHTTP(ctx.Writer, r.WithContext(ctx))
|
|
})
|
|
}
|