Add database pointer to context.
This allow all middlewares to query the database. For alternative authorizations that need to create and read the database.
This commit is contained in:
		
							parent
							
								
									2fdafd34ca
								
							
						
					
					
						commit
						2b4eead680
					
				
							
								
								
									
										2
									
								
								main.go
								
								
								
								
							
							
						
						
									
										2
									
								
								main.go
								
								
								
								
							|  | @ -206,7 +206,7 @@ func main() { | |||
| 	} | ||||
| 
 | ||||
| 	// register routes
 | ||||
| 	app := router.New(tmplDir, extraData, util.SessionSecret) | ||||
| 	app := router.New(tmplDir, extraData, util.SessionSecret, db) | ||||
| 
 | ||||
| 	app.GET(util.BasePath, handler.WireGuardClients(db), handler.ValidSession, handler.RefreshSession) | ||||
| 
 | ||||
|  |  | |||
|  | @ -13,6 +13,7 @@ import ( | |||
| 	"github.com/labstack/echo/v4" | ||||
| 	"github.com/labstack/echo/v4/middleware" | ||||
| 	"github.com/labstack/gommon/log" | ||||
| 	"github.com/ngoduykhanh/wireguard-ui/store/jsondb" | ||||
| 	"github.com/ngoduykhanh/wireguard-ui/util" | ||||
| ) | ||||
| 
 | ||||
|  | @ -48,7 +49,7 @@ func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c | |||
| } | ||||
| 
 | ||||
| // New function
 | ||||
| func New(tmplDir fs.FS, extraData map[string]interface{}, secret [64]byte) *echo.Echo { | ||||
| func New(tmplDir fs.FS, extraData map[string]interface{}, secret [64]byte, db *jsondb.JsonDB) *echo.Echo { | ||||
| 	e := echo.New() | ||||
| 
 | ||||
| 	cookiePath := util.GetCookiePath() | ||||
|  | @ -60,6 +61,14 @@ func New(tmplDir fs.FS, extraData map[string]interface{}, secret [64]byte) *echo | |||
| 
 | ||||
| 	e.Use(session.Middleware(cookieStore)) | ||||
| 
 | ||||
| 	// Add db to context so middlewares can use it.
 | ||||
| 	e.Use(func(next echo.HandlerFunc) echo.HandlerFunc { | ||||
| 		return func(c echo.Context) error { | ||||
| 			c.Set("db", db) | ||||
| 			return next(c) | ||||
| 		} | ||||
| 	}) | ||||
| 
 | ||||
| 	// read html template file to string
 | ||||
| 	tmplBaseString, err := util.StringFromEmbedFile(tmplDir, "base.html") | ||||
| 	if err != nil { | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue