diff options
Diffstat (limited to 'web/middleware/config_context.go')
-rw-r--r-- | web/middleware/config_context.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/web/middleware/config_context.go b/web/middleware/config_context.go new file mode 100644 index 0000000..d474022 --- /dev/null +++ b/web/middleware/config_context.go | |||
@@ -0,0 +1,26 @@ | |||
1 | package middleware | ||
2 | |||
3 | import ( | ||
4 | "github.com/gin-gonic/gin" | ||
5 | |||
6 | "code.crute.me/mcrute/go_ddns_manager/web" | ||
7 | ) | ||
8 | |||
9 | const serverConfig = "ServerConfig" | ||
10 | |||
11 | func ConfigContextMiddleware(cfg *web.ServerConfig) func(*gin.Context) { | ||
12 | return func(c *gin.Context) { | ||
13 | c.Set(serverConfig, cfg) | ||
14 | c.Next() | ||
15 | } | ||
16 | } | ||
17 | |||
18 | func GetServerConfig(c *gin.Context) *web.ServerConfig { | ||
19 | v, ok := c.Get(serverConfig) | ||
20 | if !ok { | ||
21 | // This should never happen if the config context middlware is in place | ||
22 | panic("Unable to get config from request") | ||
23 | } | ||
24 | |||
25 | return v.(*web.ServerConfig) | ||
26 | } | ||