Documentation
¶
Index ¶
- type CookieOptions
- type Ctx
- func (c *Ctx) Abort()
- func (c *Ctx) AbortJSON(status int, data any)
- func (c *Ctx) AbortStatus(status int)
- func (c *Ctx) BadRequest(v any)
- func (c *Ctx) BindJSON(v any) error
- func (c *Ctx) Body() ([]byte, error)
- func (c *Ctx) ContentType() string
- func (c *Ctx) Cookie(name string) (string, error)
- func (c *Ctx) Cookies() []*http.Cookie
- func (c *Ctx) Created(v any)
- func (c *Ctx) DeleteCookie(name string, options CookieOptions)
- func (c *Ctx) Download(path, filename string)
- func (c *Ctx) File(path string)
- func (c *Ctx) FileUpload(name string) (multipart.File, *multipart.FileHeader, error)
- func (c *Ctx) Forbidden(v any)
- func (c *Ctx) Form(key string) string
- func (c *Ctx) FormFile(name string) (*UploadedFile, error)
- func (c *Ctx) Get(key string) (any, bool)
- func (c *Ctx) HTML(status int, html string)
- func (c *Ctx) Header(key string) string
- func (c *Ctx) Headers() http.Header
- func (c *Ctx) Host() string
- func (c *Ctx) IP() string
- func (c *Ctx) InternalServerError(v any)
- func (c *Ctx) JSON(status int, data any)
- func (c *Ctx) Method() string
- func (c *Ctx) Next()
- func (c *Ctx) NoContent()
- func (c *Ctx) NotFound(v any)
- func (c *Ctx) OK(v any)
- func (c *Ctx) Param(name string) string
- func (c *Ctx) Path() string
- func (c *Ctx) Queries() map[string][]string
- func (c *Ctx) Query(key string) string
- func (c *Ctx) QueryDefault(key, defaultValue string) string
- func (c *Ctx) Redirect(status int, url string)
- func (c *Ctx) Scheme() string
- func (c *Ctx) Set(key string, value any)
- func (c *Ctx) SetCookie(cookie *http.Cookie)
- func (c *Ctx) SetCookieValue(name string, value string, options CookieOptions)
- func (c *Ctx) Status(status int) *Ctx
- func (c *Ctx) String(status int, message string)
- func (c *Ctx) Unauthorized(v any)
- func (c *Ctx) UserAgent() string
- type EventHandler
- type Handler
- type JSON
- type Packet
- type Route
- type Router
- func (r *Router) DELETE(path string, handlers ...Handler)
- func (r *Router) GET(path string, handlers ...Handler)
- func (r *Router) OPTIONS(path string, handlers ...Handler)
- func (r *Router) POST(path string, handlers ...Handler)
- func (r *Router) PUT(path string, handlers ...Handler)
- func (r *Router) Use(handlers ...Handler)
- type Server
- func (s *Server) DELETE(path string, handlers ...Handler)
- func (s *Server) GET(path string, handlers ...Handler)
- func (s *Server) Mount(prefix string, router *Router)
- func (s *Server) OPTIONS(path string, handlers ...Handler)
- func (s *Server) POST(path string, handlers ...Handler)
- func (s *Server) PUT(path string, handlers ...Handler)
- func (s *Server) Start() error
- func (s *Server) Static(prefix, dir string)
- func (s *Server) Use(h Handler)
- type UploadedFile
- type WSClient
- func (c *WSClient) Close() error
- func (c *WSClient) Delete(key string)
- func (c *WSClient) Emit(event string, data any) error
- func (c *WSClient) EmitJSON(event string, v any) error
- func (c *WSClient) Get(key string) (any, bool)
- func (c *WSClient) Join(room string)
- func (c *WSClient) Leave(room string)
- func (c *WSClient) LeaveAll()
- func (c *WSClient) Listen()
- func (c *WSClient) On(event string, handler EventHandler)
- func (c *WSClient) OnConnect(fn func(*WSClient))
- func (c *WSClient) OnDisconnect(fn func(*WSClient))
- func (c *WSClient) Raw(messageType coderws.MessageType, data []byte) error
- func (c *WSClient) Set(key string, value any)
- type WSHandler
- type WSManager
- func (m *WSManager) AddOrigins(origins ...string)
- func (m *WSManager) AllowAllOrigins()
- func (m *WSManager) Broadcast(room, event string, data any)
- func (m *WSManager) Handle(path string, handler WSHandler)
- func (m *WSManager) Options() *coderws.AcceptOptions
- func (m *WSManager) SetOptions(options coderws.AcceptOptions)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CookieOptions ¶
type CookieOptions struct {
Path string
Domain string
MaxAge int
Expires time.Time
Secure bool
HttpOnly bool
SameSite http.SameSite
}
CookieOptions defines optional settings used when creating cookies.
type Ctx ¶
type Ctx struct {
Writer http.ResponseWriter
Request *http.Request
Params map[string]string
Keys map[string]any
// contains filtered or unexported fields
}
Ctx represents the context of the current HTTP request.
It provides access to the request, response writer, route parameters, middleware state, and helper methods for building HTTP responses.
func (*Ctx) Abort ¶
func (c *Ctx) Abort()
Abort stops execution of any remaining middleware or handlers.
func (*Ctx) AbortStatus ¶
func (*Ctx) BadRequest ¶
BadRequest sends a 400 Bad Request JSON response.
func (*Ctx) ContentType ¶
ContentType returns the Content-Type request header.
func (*Ctx) DeleteCookie ¶
func (c *Ctx) DeleteCookie(name string, options CookieOptions)
DeleteCookie removes the specified cookie from the client.
func (*Ctx) Download ¶
Download serves a file as an attachment.
The browser will download the file using the provided filename.
func (*Ctx) FileUpload ¶
It is equivalent to calling Request.FormFile.
func (*Ctx) Form ¶
Form returns the value of the named form field.
It supports both application/x-www-form-urlencoded and multipart/form-data requests.
func (*Ctx) FormFile ¶
func (c *Ctx) FormFile(name string) (*UploadedFile, error)
FormFile returns the uploaded file associated with the given form field.
The returned UploadedFile contains the file stream, metadata, filename, size, and content type.
func (*Ctx) Get ¶
Set stores a value in the request context.
Stored values are only available during the current request.
func (*Ctx) IP ¶
IP returns the client's IP address.
It checks X-Forwarded-For and X-Real-IP before falling back to the remote address.
func (*Ctx) InternalServerError ¶
InternalServerError sends a 500 Internal Server Error JSON response.
func (*Ctx) JSON ¶
JSON sends a JSON response with the specified HTTP status code.
The response Content-Type is automatically set to "application/json".
func (*Ctx) Param ¶
Param returns the value of the named route parameter.
It returns an empty string if the parameter does not exist.
func (*Ctx) Query ¶
Query returns the value of the named query parameter.
Example:
GET /users?page=2
c.Query("page") // "2"
func (*Ctx) QueryDefault ¶
QueryDefault returns the query parameter value if present, otherwise it returns defaultValue.
func (*Ctx) Redirect ¶
Redirect redirects the client to the provided URL using the specified HTTP status code.
func (*Ctx) Scheme ¶
Scheme returns "https" when the request is using TLS, otherwise it returns "http".
func (*Ctx) Set ¶
Set stores a value in the request context.
Stored values are only available during the current request.
func (*Ctx) SetCookie ¶
SetCookie adds the provided cookie to the response.
c.SetCookie(&http.Cookie{
Name: "token",
Value: jwt,
Path: "/",
HttpOnly: true,
Secure: true,
MaxAge: 3600,
})
func (*Ctx) SetCookieValue ¶
func (c *Ctx) SetCookieValue( name string, value string, options CookieOptions, )
SetCookieValue creates and sends a cookie using the provided name, value, and options.
func (*Ctx) Status ¶
Status sets the HTTP status code and returns the current context.
This allows method chaining.
func (*Ctx) Unauthorized ¶
Unauthorized sends a 401 Unauthorized JSON response.
type EventHandler ¶
type EventHandler func(*WSClient, json.RawMessage)
EventHandler handles an incoming WebSocket event.
The event payload is provided as raw JSON and can be unmarshaled into the desired Go type.
type JSON ¶
JSON is a convenience type for constructing JSON responses.
Example:
c.JSON(http.StatusOK, gorbit.JSON{
"message": "Hello",
})
type Packet ¶
type Packet struct {
Event string `json:"event"`
Data json.RawMessage `json:"data"`
}
Packet represents a WebSocket event packet exchanged between the client and server.
type Route ¶
type Route struct {
Method string
Path string
Segments []string
ParamKeys []string
Handlers []Handler
WebSocket bool
WSHandler WSHandler
}
Route represents a registered HTTP or WebSocket route.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router groups routes and middleware that can be mounted onto a Server.
func NewRouter ¶
func NewRouter() *Router
NewRouter creates a new Router.
Routers allow routes and middleware to be grouped before mounting them onto a Server.
type Server ¶
type Server struct {
WS *WSManager
// contains filtered or unexported fields
}
Server represents a Gorbit application.
A Server manages routes, middleware, static file serving, and WebSocket endpoints.
func New ¶
New creates a new Server listening on the specified port.
Example:
app := gorbit.New(8080)
func (*Server) Mount ¶
Mount registers all routes and middleware from the provided router under the specified path prefix.
Example:
api := gorbit.NewRouter()
api.GET("/users", GetUsers)
app.Mount("/api", api)
func (*Server) Start ¶
Start starts the HTTP server.
Start blocks until the server stops or returns an error.
Example:
app := gorbit.New(8080)
app.GET("/", func(c *gorbit.Ctx) {
c.String(200, "Hello, World!")
})
log.Fatal(app.Start())
type UploadedFile ¶
type UploadedFile struct {
File multipart.File
Header *multipart.FileHeader
Filename string
Size int64
ContentType string
}
UploadedFile represents a file uploaded through a multipart/form-data request.
func (*UploadedFile) SaveTo ¶
func (f *UploadedFile) SaveTo(path string) error
SaveTo writes the uploaded file to the specified destination path.
type WSClient ¶
type WSClient struct {
ID string
Conn *coderws.Conn
Context context.Context
Values map[string]any
// contains filtered or unexported fields
}
WSClient represents a connected WebSocket client.
It provides methods for sending and receiving events, joining rooms, storing per-connection values, and managing the connection lifecycle.
func (*WSClient) Emit ¶
Emit sends an event and payload to the connected client.
The payload is automatically encoded as JSON.
func (*WSClient) Get ¶
Get retrieves a value previously stored using Set.
The returned boolean reports whether the key exists.
func (*WSClient) Join ¶
Join adds the client to the specified room.
If the room does not already exist, it is created.
func (*WSClient) Leave ¶
Leave removes the client from the specified room.
Empty rooms are automatically removed.
func (*WSClient) LeaveAll ¶
func (c *WSClient) LeaveAll()
LeaveAll removes the client from every room it has joined.
func (*WSClient) Listen ¶
func (c *WSClient) Listen()
Listen begins reading incoming WebSocket messages.
Incoming packets are decoded and dispatched to their registered event handlers. Listen blocks until the connection is closed or an error occurs.
func (*WSClient) On ¶
func (c *WSClient) On(event string, handler EventHandler)
On registers a handler for the specified event.
When a packet with the matching event name is received,
func (*WSClient) OnConnect ¶
OnConnect registers a callback executed after the client successfully connects.
func (*WSClient) OnDisconnect ¶
OnDisconnect registers a callback executed when the client disconnects.
type WSHandler ¶
type WSHandler func(*WSClient)
WSHandler represents a WebSocket connection handler.
type WSManager ¶
type WSManager struct {
// contains filtered or unexported fields
}
WSManager manages WebSocket routes, connected clients, rooms, and connection options.
func WS ¶
func WS() *WSManager
WS returns the application's global WebSocket manager.
It panics if the WebSocket manager has not been initialized.
func (*WSManager) AddOrigins ¶
AddOrigins appends one or more allowed origin patterns.
Connections originating from these origins are permitted during the WebSocket handshake.
func (*WSManager) AllowAllOrigins ¶
func (m *WSManager) AllowAllOrigins()
AllowAllOrigins disables origin verification for incoming WebSocket connections.
This should generally only be used during development or in trusted environments.
func (*WSManager) Broadcast ¶
Broadcast sends an event with the provided payload to every client currently joined to the specified room.
If the room does not exist, Broadcast does nothing.
func (*WSManager) Options ¶
func (m *WSManager) Options() *coderws.AcceptOptions
Options returns the current WebSocket accept options.
func (*WSManager) SetOptions ¶
func (m *WSManager) SetOptions(options coderws.AcceptOptions)
SetOptions replaces the WebSocket accept options used when accepting new connections.
Directories
¶
| Path | Synopsis |
|---|---|
|
example
|
|
|
quickstart
command
|
|
|
Package middleware provides reusable middleware for Gorbit.
|
Package middleware provides reusable middleware for Gorbit. |