go-dart/context.go
2025-04-22 06:59:36 +00:00

169 lines
3.7 KiB
Go

package dart
import (
"context"
"encoding/json"
"encoding/xml"
"errors"
"mime/multipart"
"net/http"
"github.com/gorilla/schema"
"github.com/julienschmidt/httprouter"
)
type Context struct {
response http.ResponseWriter
request *http.Request
params httprouter.Params
ctx context.Context
database *Database
websocketManager *WebsocketManager
}
type ContextKey string
func (c *Context) Request() *http.Request {
return c.request
}
func (c *Context) Response() http.ResponseWriter {
return c.response
}
func (c *Context) Param(key string) string {
return c.params.ByName(key)
}
func (c *Context) FormFile(field string) (multipart.File, error) {
file, _, err := c.request.FormFile(field)
if err != nil {
return nil, err
}
return file, nil
}
func (c *Context) Query(key string) string {
return c.request.URL.Query().Get(key)
}
func (c *Context) Header(key string) string {
return c.request.Header.Get(key)
}
func (c *Context) Cookie(name string) (*http.Cookie, error) {
return c.request.Cookie(name)
}
func (c *Context) Value(key ContextKey) any {
return c.ctx.Value(key)
}
func (c *Context) ClientIP() string {
ip := c.request.Header.Get("X-Forwarded-For")
if ip == "" {
ip = c.request.RemoteAddr
}
return ip
}
func (c *Context) UserAgent() string {
return c.request.UserAgent()
}
func (c *Context) SetHeader(key, value string) {
c.response.Header().Set(key, value)
}
func (c *Context) SetCookie(cookie *http.Cookie) {
http.SetCookie(c.response, cookie)
}
func (c *Context) SetValue(key ContextKey, value any) {
c.ctx = context.WithValue(c.ctx, key, value)
}
func (c *Context) Status(statusCode int) {
c.response.WriteHeader(statusCode)
}
func (c *Context) Text(statusCode int, message string) error {
c.response.Header().Set("Content-Type", "text/plain")
c.response.WriteHeader(statusCode)
_, err := c.response.Write([]byte(message))
return err
}
func (c *Context) JSON(statusCode int, v any) error {
c.response.Header().Set("Content-Type", "application/json")
c.response.WriteHeader(statusCode)
return json.NewEncoder(c.response).Encode(v)
}
func (c *Context) Bytes(statusCode int, data []byte, contentType string) error {
c.response.Header().Set("Content-Type", contentType)
c.response.WriteHeader(statusCode)
_, err := c.response.Write(data)
return err
}
func (c *Context) HTML(statusCode int, html string) error {
c.response.Header().Set("Content-Type", "text/html")
c.response.WriteHeader(statusCode)
_, err := c.response.Write([]byte(html))
return err
}
func (c *Context) File(filePath string) error {
http.ServeFile(c.response, c.request, filePath)
return nil
}
func (c *Context) ParseJSON(v any) error {
return json.NewDecoder(c.request.Body).Decode(v)
}
func (c *Context) ParseXML(v any) error {
return xml.NewDecoder(c.request.Body).Decode(v)
}
func (c *Context) ParseForm(v any) error {
if err := c.request.ParseForm(); err != nil {
return err
}
return schema.NewDecoder().Decode(v, c.request.Form)
}
func (c *Context) ParseMultipartForm(v any, maxMemory int64) error {
if err := c.request.ParseMultipartForm(maxMemory); err != nil {
return err
}
return schema.NewDecoder().Decode(v, c.request.MultipartForm.Value)
}
func (c *Context) ParseQuery(v any) error {
return schema.NewDecoder().Decode(v, c.request.URL.Query())
}
func (c *Context) Redirect(statusCode int, url string) error {
http.Redirect(c.response, c.request, url, statusCode)
return nil
}
func (c *Context) DB() (*Database, error) {
if c.database == nil {
return nil, errors.New("database is not enabled")
}
return c.database, nil
}
func (c *Context) WebsocketManager() (*WebsocketManager, error) {
if c.websocketManager == nil {
return nil, errors.New("websocket manager is not enabled")
}
return c.websocketManager, nil
}