This restructures dbcore (now the db package) and jwtcore (now the jwt package) to use a single struct. There is now a state package, which contains a struct with the full application state. After this, instead of initializing the API routes directly in the main function, the state object gets passed, and the API routes get initialized with their accompanying code. One fix done to reduce memory usage and increase speed is that the validator object is now persistent across requests, instead of recreating it each time. This should speed things up slightly, and improve memory usage. One additional chore done is that the database models have been moved to be a seperate file from the DB initialization itself.
24 lines
485 B
Go
24 lines
485 B
Go
package state
|
|
|
|
import (
|
|
"git.terah.dev/imterah/hermes/backend/api/db"
|
|
"git.terah.dev/imterah/hermes/backend/api/jwt"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/go-playground/validator/v10"
|
|
)
|
|
|
|
type State struct {
|
|
DB *db.DB
|
|
JWT *jwt.JWTCore
|
|
Engine *gin.Engine
|
|
Validator *validator.Validate
|
|
}
|
|
|
|
func New(db *db.DB, jwt *jwt.JWTCore, engine *gin.Engine) *State {
|
|
return &State{
|
|
DB: db,
|
|
JWT: jwt,
|
|
Engine: engine,
|
|
Validator: validator.New(),
|
|
}
|
|
}
|