Added Server interface
This commit is contained in:
16
Context.go
16
Context.go
@ -24,16 +24,18 @@ type Context interface {
|
||||
Reader(io.Reader) error
|
||||
RequestHeader(key string) string
|
||||
ResponseHeader(key string) string
|
||||
Scheme() string
|
||||
Status(status int) Context
|
||||
String(string) error
|
||||
Write([]byte) (int, error)
|
||||
WriteString(string) (int, error)
|
||||
}
|
||||
|
||||
// ctx represents a request & response context.
|
||||
type ctx struct {
|
||||
request *http.Request
|
||||
response http.ResponseWriter
|
||||
server *Server
|
||||
server *server
|
||||
paramNames [maxParams]string
|
||||
paramValues [maxParams]string
|
||||
paramCount int
|
||||
@ -64,7 +66,7 @@ func (ctx *ctx) Error(messages ...any) error {
|
||||
|
||||
// Get retrieves a parameter.
|
||||
func (ctx *ctx) Get(param string) string {
|
||||
for i := 0; i < ctx.paramCount; i++ {
|
||||
for i := range ctx.paramCount {
|
||||
if ctx.paramNames[i] == param {
|
||||
return ctx.paramValues[i]
|
||||
}
|
||||
@ -120,6 +122,11 @@ func (ctx *ctx) Reader(reader io.Reader) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Scheme returns either `http` or `https`.
|
||||
func (ctx *ctx) Scheme() string {
|
||||
return ctx.request.URL.Scheme
|
||||
}
|
||||
|
||||
// Status sets the HTTP status of the response.
|
||||
func (ctx *ctx) Status(status int) Context {
|
||||
ctx.response.WriteHeader(status)
|
||||
@ -137,6 +144,11 @@ func (ctx *ctx) Write(body []byte) (int, error) {
|
||||
return ctx.response.Write(body)
|
||||
}
|
||||
|
||||
// WriteString implements the io.StringWriter interface.
|
||||
func (ctx *ctx) WriteString(body string) (int, error) {
|
||||
return ctx.response.(io.StringWriter).WriteString(body)
|
||||
}
|
||||
|
||||
// addParameter adds a new parameter to the context.
|
||||
func (ctx *ctx) addParameter(name string, value string) {
|
||||
ctx.paramNames[ctx.paramCount] = name
|
||||
|
Reference in New Issue
Block a user