php小编子墨在这里为大家解答一个常见的问题:如果在不同模块中访问数据库,应该如何操作呢?在开发过程中,我们经常需要在不同的功能模块中访问数据库,这涉及到如何管理数据库连接和执行数据库操作的问题。为了解决这个问题,我们可以采用多种方式,例如使用全局变量、单例模式、依赖注入等。具体选择哪种方式取决于项目的需求和开发团队的习惯。接下来,我将对这些方式进行详细介绍,希望能帮助到大家。
我尝试使用 jwt 进行 atuhorize,但遇到问题,当我检查来自声明的有价值电子邮件的电子邮件时,我收到错误 500。这个问题是因为数据库尚未准备好。
这是代码
套餐路线
package route import ( "go_fiber/controller" "go_fiber/middleware" "github.com/gofiber/fiber/v2" ) func routeinit(app *fiber.app, usercontroller controller.usercontroller) { app.get("/users", middleware.authmiddleware, usercontroller.findall) app.post("/login", usercontroller.login) }
打包应用
package app import ( "database/sql" "fmt" "go_fiber/helper" "os" "time" _ "github.com/go-sql-driver/mysql" "github.com/joho/godotenv" ) func newdb() *sql.db { var ( username = envvariable("db_username") password = envvariable("db_password") host = envvariable("db_host") port = envvariable("db_port") db_name = envvariable("db_name") ) dns := fmt.sprintf("%s:%s@tcp(%s:%s)/%s?parsetime=true", username, password, host, port, db_name) db, err := sql.open("mysql", dns) if err != nil { panic(err) } db.setmaxidleconns(10) db.setmaxopenconns(100) db.setconnmaxidletime(5 * time.minute) db.setconnmaxlifetime(60 * time.minute) return db }
包主
package main import ( "go_fiber/app" "go_fiber/controller" "go_fiber/exception" "go_fiber/repository" "go_fiber/route" "go_fiber/service" "github.com/go-playground/validator/v10" "github.com/gofiber/fiber/v2" ) func main(){ var db = app.newdb() app := fiber.new() app.use(func(c *fiber.ctx) error { defer func() { if err := recover(); err != nil { exception.errorhandler(c, err) } }() return c.next() }) validator := validator.new() userrepository := repository.newuserrepository() userservice := service.newuserservice(userrepository, db, validator) usercontroller := controller.newusercontroller(userservice) route.routeinit(app, usercontroller) app.listen(":3000") }
封装中间件
package middleware import ( "database/sql" "errors" "fmt" "go_fiber/exception" "log" "os" "time" "github.com/gofiber/fiber/v2" "github.com/golang-jwt/jwt/v4" ) func authmiddleware(c *fiber.ctx) error{ tokenstring := c.cookies("authorization") log.printf("(middleware) token : %v", tokenstring) if tokenstring == "" { panic(exception.newuserunauthorized(errors.new("user unauthorized").error())) } token, err := jwt.parse(tokenstring, func(token *jwt.token) (interface{}, error) { if _, ok := token.method.(*jwt.signingmethodhmac); !ok { return nil, fmt.errorf("unexpected signing method :%v", token.header["sub"]) } return []byte(os.getenv("secret")), nil }) log.printf("(middleware) token : %v", token) log.printf("(middleware) error : %v", err) if claims, ok := token.claims.(jwt.mapclaims); ok && token.valid { if float64(time.now().unix()) > claims["exp"].(float64) { panic(exception.newuserunauthorized(errors.new("user unauthorized").error())) } log.printf("(middleware) claims : %v", claims) log.printf("(middleware) ok : %v", ok) var db *sql.db tx, err := db.begin() log.printf("(middleware) tx : %v", tx) log.printf("(middleware) error : %v", err) query :=` select email from users where email = ? limit 1 ` log.printf("(middleware) query : %v", query) user, err := tx.querycontext(c.context(), query, claims["sub"]) log.printf("(middleware) user : %v", user) log.printf("(middleware) error : %v", err) if err != nil { panic(exception.newuserunauthorized(errors.new("user unauthorized").error())) } c.locals("user", user) c.next() }else { panic(exception.newuserunauthorized(errors.new("user unauthorized").error())) } return nil }
这部分是问题代码
var db *sql.db tx, err := db.begin() log.printf("(middleware) tx : %v", tx) // no detect in terminal
这是日志的输出
2023/02/12 10:00:04 (middleware) token : eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NzYxNzEzOTIsInN1YiI6IkphZmFyQGdtYWlsLmNvbSJ9.6nn_IaNACHfe7flFMOXfj1ygZS-U_yrnU_Gvjn8xCp8 2023/02/12 10:00:04 (middleware) token : &{eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NzYxNzEzOTIsInN1YiI6IkphZmFyQGdtYWlsLmNvbSJ9.6nn_IaNACHfe7flFMOXfj1ygZS-U_yrnU_Gvjn8xCp8 0xc0000a6780 map[alg:HS256 typ:JWT] map[exp:1.676171392e+09 sub:[email protected]] 6nn_IaNACHfe7flFMOXfj1ygZS-U_yrnU_Gvjn8xCp8 true} 2023/02/12 10:00:04 (middleware) error : <nil> 2023/02/12 10:00:04 (middleware) claims : map[exp:1.676171392e+09 sub:[email protected]] 2023/02/12 10:00:04 (middleware) ok : true
那么,如果不同的包如何解决访问数据库的问题?
您应该通过路由注册将数据库处理程序向下传递到中间件。
更改您的 authmiddleware,以便它由采用 db *sql.db 作为参数的函数返回:
func newauthmiddleware(db *sql.db) fiber.handler { return func(c *fiber.ctx) error { // the rest of your function is here and has access to db } }
然后您需要在 routeinit 函数中调用 newauthmiddleware
func routeinit(app *fiber.app, usercontroller controller.usercontroller, db *sql.db) { app.get("/users", middleware.newauthmiddleware(db), usercontroller.findall) app.post("/login", usercontroller.login) }
然后在主函数中将数据库传递给 routeinit()
func main() { // ... as before route.RouteInit(app, userController, db) }
以上是如果功能在不同模块中如何访问数据库?的详细内容。更多信息请关注PHP中文网其他相关文章!