Rumah > Artikel > pembangunan bahagian belakang > Golang Gin: Tajuk sudah tertulis. Ingin mengatasi kod status 301 dengan 200
Saya sedang membangunkan panel kawalan dan mengupah beberapa orang untuk membinanya untuk saya. Mereka semua melarikan diri dan saya dibiarkan membersihkan pasta.
Apa yang perlu saya lakukan ialah:
Hanya proses log masuk yang mudah. Masalahnya ialah apabila log masuk berjaya, konsol masuk ke gelung ubah hala ini seperti ini:
[gin] 2023/02/21 - 15:43:32 | 301 | 1.224601041s | ::1 | post "/login" [gin-debug] [warning] headers were already written. wanted to override status code 301 with 200 [gin] 2023/02/21 - 15:43:33 | 200 | 787.3905ms | ::1 | get "/dashboard" [gin-debug] [warning] headers were already written. wanted to override status code 301 with 200 [gin] 2023/02/21 - 15:43:33 | 200 | 197.989875ms | ::1 | get "/login" [gin-debug] [warning] headers were already written. wanted to override status code 301 with 200 [gin] 2023/02/21 - 15:43:34 | 200 | 817.293166ms | ::1 | get "/dashboard" [gin-debug] [warning] headers were already written. wanted to override status code 301 with 200 [gin] 2023/02/21 - 15:43:34 | 200 | 206.107791ms | ::1 | get "/login" [gin-debug] [warning] headers were already written. wanted to override status code 301 with 200 [gin] 2023/02/21 - 15:43:35 | 200 | 792.954375ms | ::1 | get "/dashboard" [gin-debug] [warning] headers were already written. wanted to override status code 301 with 200 [gin] 2023/02/21 - 15:43:35 | 200 | 201.972708ms | ::1 | get "/login" [gin-debug] [warning] headers were already written. wanted to override status code 301 with 200 [gin] 2023/02/21 - 15:43:36 | 200 | 840.773625ms | ::1 | get "/dashboard" [gin-debug] [warning] headers were already written. wanted to override status code 301 with 200 [gin] 2023/02/21 - 15:43:36 | 200 | 198.680125ms | ::1 | get "/login" [gin-debug] [warning] headers were already written. wanted to override status code 301 with 200 [gin] 2023/02/21 - 15:43:37 | 200 | 897.679708ms | ::1 | get "/dashboard" [gin-debug] [warning] headers were already written. wanted to override status code 301 with 200 [gin] 2023/02/21 - 15:43:37 | 200 | 200.759917ms | ::1 | get "/login" [gin-debug] [warning] headers were already written. wanted to override status code 301 with 200 [gin] 2023/02/21 - 15:43:38 | 200 | 795.39975ms | ::1 | get "/dashboard" [gin-debug] [warning] headers were already written. wanted to override status code 301 with 200 [gin] 2023/02/21 - 15:43:38 | 200 | 196.538ms | ::1 | get "/login" [gin-debug] [warning] headers were already written. wanted to override status code 301 with 200 [gin] 2023/02/21 - 15:43:39 | 200 | 844.680709ms | ::1 | get "/dashboard" [gin-debug] [warning] headers were already written. wanted to override status code 301 with 200 [gin] 2023/02/21 - 15:43:39 | 200 | 180.598084ms | ::1 | get "/login" [gin-debug] [warning] headers were already written. wanted to override status code 301 with 200 [gin] 2023/02/21 - 15:43:40 | 200 | 814.666208ms | ::1 | get "/dashboard" [gin-debug] [warning] headers were already written. wanted to override status code 301 with 200 [gin] 2023/02/21 - 15:43:40 | 200 | 210.281ms | ::1 | get "/login"
Sekarang ini, saya masih belajar/baru untuk golang dan gin memandangkan saya sedang mengisi pemaju yang lebih lama, jadi bagi saya ia hanya kosong...
Dari apa yang saya faham, main()
sedang mengkonfigurasi laluan, perisian tengah, memuatkan templat dan kemudian menghidupkan enjin.
main.go
func main() { //gin.setmode(gin.releasemode) // uncomment for production // startup tasks startup() logging.loginfo("ran startup tasks...") // configure engine hostport := fmt.sprintf( "%s:%d", datamanagers.loadconfig().bshost, datamanagers.loadconfig().bsport) webengine := gin.default() webengine.settrustedproxies([]string{hostport}) logging.loginfo("configured engine...") // load middleware store := cookie.newstore([]byte(randstr.string(64))) webengine.use(sessions.sessions("session", store)) webengine.use(errorhandler.errorshandler500()) logging.loginfo("loaded middleware...") // configure routes pubroutes := webengine.group("/") privroutes := webengine.group("/") routes.publicroutes(pubroutes) privroutes.use(middleware.authrequired) routes.privateroutes(privroutes) logging.loginfo("configured routes...") // non routables webengine.noroute(errorhandler.errorshandler404()) logging.loginfo("configured non-routables...") // load template files loadtemplates(webengine) logging.loginfo("loaded templates...") // start the gin engine err := webengine.run(hostport) logging.loginfo("...blocksuite-webui loaded") logging.catch(err) }
Apabila mengakses /
时,我会被重定向到 /login
, ini akan muncul borang log masuk.
Saya menyerahkan borang dengan kelayakan yang sah dan ia mengubah hala saya ke /dashboard
. Saya tidak tahu sama ada mengubah hala selepas log masuk berjaya adalah betul, itulah yang dilakukan oleh pembangun asal dan ia berfungsi dengan baik.
routes.go
func publicroutes(webengine *gin.routergroup) { webengine.get("/login", entry.logingethandler) webengine.post("/login", entry.loginposthandler) webengine.get("/", other.indexgethandler()) } func privateroutes(webengine *gin.routergroup) { dashboardroutes := webengine.group("/dashboard") { dashboardroutes.get("/", dashboard.dashboardgethandler) } }
log masuk.go
func logingethandler(context *gin.context) { user := utility.getusersession(context).get("useremail") if user != nil { context.redirect(http.statusmovedpermanently, "/dashboard") } context.html(http.statusok, "login.html", gin.h{ "sitekey": datamanagers.getrecaptchasettings().sitekey, "enabled": datamanagers.getrecaptchasettings().enabled, "content": "", "success": "", "serverlogo": brand.getbrandlogo(), "title": "welcome back", }) } func loginposthandler(context *gin.context) { user := utility.getusersession(context).get("useremail") if user != nil { context.redirect(http.statusmovedpermanently, "/dashboard") //return } useremail := utility.sanitize(context.postform("email")) password := utility.sanitize(context.postform("password")) rememberme := utility.sanitize(context.postform("rememberme")) //captcha := context.postform("g-recaptcha-response") if !utility.isemailvalid(useremail) { context.html(http.statusbadrequest, "login.html", gin.h{"content": "please enter a valid email address"}) return } /*if helpers2.recaptchacheck(captcha) || datamanagers.getconfig().sitekey != "" { // success } else { if datamanagers.getconfig().enabled { context.html(http.statusbadrequest, "login.html", gin.h{"content": "please verify captcha"}) return } }*/ if utility.emptyuserpass(useremail, password) { context.html(http.statusbadrequest, "login.html", gin.h{"content": "email and password cannot be empty"}) return } if utility.checkforwhitespaces(useremail, password) != nil { context.html(http.statusbadrequest, "login.html", gin.h{"content": "username and password can't contain spaces"}) return } if !utility.checkuserpass(useremail, password) { context.html(http.statusunauthorized, "login.html", gin.h{"content": "incorrect username or password"}) return } utility.newusersession(context, useremail) if rememberme == "yes" { utility.setsessionage(context) } context.redirect(http.statusmovedpermanently, "/dashboard") }
Kemudian, halaman /dashboard
harus dimuatkan.
papan pemuka.go
func dashboardgethandler(context *gin.context) { user := utility.getusersession(context).get("useremail") db := datamanagers.getdb() if user == nil { context.redirect(http.statusmovedpermanently, "/login") } [...] context.html(http.statusok, "dashboard.html", gin.h{ "info": info, "imageurl": utility.getimage(user), "serverlogo": brand.getbrandicon(), "title": "dashboard", "servername": datamanagers.getserverinfo().servername, }) }
(Dalam kod dashboard.go
, saya meninggalkan kod yang menarik data ke dalam papan pemuka kerana ia panjang dan tidak fikir ia perlu.)
http.statusok
dan tiada dadu. func DashboardGetHandler() gin.HandlerFunc { return func(context *gin.Context) { [...] } }
Saya langsung tidak tahu ke mana hendak pergi seterusnya. Terima kasih!
Terima kasih kepada semua yang membantu. Saya telah berhubung dengan pembangun terdahulu dan dia membantu saya memikirkan masalahnya.
Dalam kodnya dia mencipta fungsi middleware yang atas sebab tertentu menyemak sesi itu semula. Kod itu sedang menyemak pembolehubah lama dalam kuki sesi yang tidak wujud. Jadi saya ditendang kembali ke skrin log masuk.
Jadi apa yang saya lakukan ialah mengalih keluar perisian tengah itu kerana saya mengendalikannya dalam log masuk.go juga.
Atas ialah kandungan terperinci Golang Gin: Tajuk sudah tertulis. Ingin mengatasi kod status 301 dengan 200. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!