From 5730966d3525a0bf8a07a40bf0fa6b108ee25b7a Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 17 Apr 2021 14:03:21 +0200 Subject: [PATCH] feat: add api context and middleware interface --- api/context.go | 13 +++++++++++++ api/middleware.go | 14 ++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 api/context.go create mode 100644 api/middleware.go diff --git a/api/context.go b/api/context.go new file mode 100644 index 0000000..dab6f17 --- /dev/null +++ b/api/context.go @@ -0,0 +1,13 @@ +package api + +import ( + "context" + "net/http" +) + +// Context for api handlers +type Context struct { + context.Context + Request *http.Request + Response http.ResponseWriter +} diff --git a/api/middleware.go b/api/middleware.go new file mode 100644 index 0000000..3044ff7 --- /dev/null +++ b/api/middleware.go @@ -0,0 +1,14 @@ +package api + +// Middleware for the api for middle ware management (authentication, storing data) +type Middleware interface { + Handle(ctx *Context) *Context +} + +// MiddlewareFunc proxies to a Middleware +type MiddlewareFunc func(ctx *Context) *Context + +// Handle implements the Middleware interface +func (mwf MiddlewareFunc) Handle(ctx *Context) *Context { + return mwf(ctx) +}