英文:
How to access Database if function in different module?
问题
你遇到的问题是在中间件中访问数据库时出现了错误。问题出在以下代码中:
var DB *sql.DB
tx, err := DB.Begin()
log.Printf("(middleware) tx : %v", tx) // No detect in terminal
根据日志输出,DB
变量是一个空指针,因此在调用 Begin()
方法时会出现错误。要解决这个问题,你需要将 DB
实例传递给中间件。
在 main
包中,你可以将 db
实例传递给 AuthMiddleware
中间件。修改代码如下:
app.Use(func(c *fiber.Ctx) error {
defer func() {
if err := recover(); err != nil {
exception.ErrorHandler(c, err)
}
}()
// 将 db 实例传递给中间件
c.Locals("db", db)
return c.Next()
})
然后,在中间件中获取 db
实例并使用它执行数据库操作。修改 AuthMiddleware
中间件的代码如下:
func AuthMiddleware(c *fiber.Ctx) error {
// 获取 db 实例
db := c.Locals("db").(*sql.DB)
// 其他代码...
var tx *sql.Tx
tx, err = db.Begin()
if err != nil {
panic(exception.NewUserUnauthorized(errors.New("User Unauthorized").Error()))
}
// 其他代码...
}
通过这样的修改,你应该能够在中间件中成功访问数据库了。记得在其他需要访问数据库的地方也要传递 db
实例。
英文:
I try to atuhorize with jwt, but I get the problem which is when I check email with value email from claims I get the Error 500. That problem because the db is not ready.
This is the code
package route
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
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
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
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
}
and this part the problem code
var DB *sql.DB
tx, err := DB.Begin()
log.Printf("(middleware) tx : %v", tx) // No detect in terminal
this the ouput from log
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:John@gmail.com] 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:John@gmail.com]
2023/02/12 10:00:04 (middleware) ok : true
So, How to fix that problem to access db if different package?
答案1
得分: 1
你应该通过路由注册将DB处理程序传递给中间件。
修改你的AuthMiddleware,使其通过一个接受db *sql.DB作为参数的函数返回:
func NewAuthMiddleware(db *sql.DB) fiber.Handler {
return func(c *fiber.Ctx) error {
// 这里是你函数的其余部分,可以访问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)
}
然后在你的main函数中将db传递给RouteInit():
func main() {
// ...之前的代码
route.RouteInit(app, userController, db)
}
英文:
You should pass the DB handler down through your route registration to the middleware.
Change your AuthMiddleware so that it is returned by a function that takes db *sql.DB as an argument:
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
}
}
You then need to call NewAuthMiddleware in your RouteInit function
func RouteInit(app *fiber.App, userController controller.UserController, db *sql.DB) {
app.Get("/users", middleware.NewAuthMiddleware(db), userController.FindAll)
app.Post("/login", userController.Login)
}
Then in your main function pass in the db to RouteInit()
func main() {
// ... as before
route.RouteInit(app, userController, db)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论