feat: add redirect method

This commit is contained in:
meiseayoung
2025-12-30 14:25:35 +08:00
parent 6fe79d298d
commit 645d8c2dda
5 changed files with 469 additions and 0 deletions
+28
View File
@@ -256,6 +256,34 @@ pub fn (mut c Context) status(code int) {
c.status_code = code
}
// redirect method - redirect to a URL with optional status code
pub fn (mut c Context) redirect(url string, status_code ...int) http.Response {
// Set default status code to 302 (Found) if not provided
mut code := 302
if status_code.len > 0 {
code = status_code[0]
}
// Set the status code
c.status(code)
// Set the Location header
c.headers['Location'] = url
// Build and return the response
mut headers := http.new_header()
headers.add_custom('Connection', 'keep-alive') or { }
for key, value in c.headers {
headers.add_custom(key, value) or { continue }
}
return http.Response{
status_code: c.status_code
header: headers
body: ''
}
}
// 处理器接口,使用 Context