web

package module
v0.0.0-...-c5c8b81 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 17, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

README

HTTP Adapter for AMWK Framework

amwk/web is the HTTP adapter for the AMWK framework, providing the net adapter for building web applications.

Installation

Please ensure you have installed Go 1.22 or the later version, and use the following command to install the amwk framework and the web adapter:

go get github.com/go-amwk/web
go mod tidy

Getting Started

Here is a simple example demonstrating how to use the web package to create a basic web server.

package main

import (
  "github.com/go-amwk/core"
  "github.com/go-amwk/web"
)

func main() {
  // Create a new web application instance.
  app := web.Default()

  // Add a simple handler that responds with "Hello, World!" to any request.
  app.Use(func(ctx *core.Context) error {
    ctx.Write([]byte("Hello, World!"))
    return nil
  })

  // Start the server
  app.Start()
}

The server will listen on the default port (8000) and respond with "Hello, World!" to any incoming HTTP request.

For more detailed examples and usage, please refer to the examples repository.

Contributing

  • Contributions are welcome. Please open issues for bugs or feature requests.
  • For code changes, fork the repository, create a topic branch, and submit a pull request with a clear description of the change.
  • Follow the repository's code style and include tests where appropriate.
  • Run go vet and go test ./... before submitting a PR.

License

The project is licensed under the Apache 2.0 License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Default

func Default() core.Application

Default returns a default application instance with default settings.

func New

func New(opts ...Option) core.Application

New returns a new application instance with default settings. It allows for further customization before starting the server.

Types

type Application

type Application struct {
	// contains filtered or unexported fields
}

Application is an HTTP web application to serve HTTP requests.

func (*Application) Close

func (app *Application) Close() error

Close closes the application.

func (*Application) ServeHTTP

func (app *Application) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface to handle incoming HTTP requests.

func (*Application) Shutdown

func (app *Application) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the application server.

func (*Application) Start

func (app *Application) Start() error

Start starts the application server and listens for incoming requests. It returns an error if it fails to start.

func (*Application) Use

func (app *Application) Use(handlers ...core.HandlerFunc) core.Application

Use adds the given handlers to the application.

type Option

type Option func(*Application)

Option is a function that configures the Application.

func WithAddress

func WithAddress(addr string) Option

WithAddress sets the address for the application server to listen on.

type Request

type Request struct {
	// contains filtered or unexported fields
}

Request represents an HTTP request received by the application.

func (*Request) Body

func (req *Request) Body() (io.ReadCloser, error)

Body returns the request body as a readable stream.

func (*Request) ClientIP

func (req *Request) ClientIP() string

ClientIP returns the client's IP address from the request.

func (*Request) ContentLength

func (req *Request) ContentLength() int64

ContentLength returns the length of the request body in bytes. If the length is unknown, it returns -1.

func (*Request) Context

func (req *Request) Context() context.Context

Context returns the context associated with the request.

func (*Request) Cookie

func (req *Request) Cookie(name string) (*http.Cookie, error)

Cookie returns the named cookie provided in the request.

func (*Request) Cookies

func (req *Request) Cookies() []*http.Cookie

Cookies returns the cookies provided in the request.

func (*Request) Header

func (req *Request) Header(name string) string

Header returns the value of the specified header field from the request. If the header is not present, it returns an empty string.

func (*Request) HeaderValues

func (req *Request) HeaderValues(name string) []string

HeaderValues returns all values associated with the specified header field from the request. If the header is not present, it returns an empty slice.

func (*Request) Headers

func (req *Request) Headers() http.Header

Headers returns all headers associated with the request.

func (*Request) Method

func (req *Request) Method() string

Method returns the HTTP method of the request (e.g., GET, POST, etc.).

func (*Request) Path

func (req *Request) Path() string

Path returns the URL path of the request.

func (*Request) PathValue

func (req *Request) PathValue(name string) string

PathValue returns the value of the specified path parameter from the request. If the parameter is not present, it returns an empty string.

func (*Request) Protocol

func (req *Request) Protocol() string

Protocol returns the HTTP protocol version used in the request (e.g., HTTP/1.1, HTTP/2, etc.).

func (*Request) Queries

func (req *Request) Queries() url.Values

Queries returns all query parameters from the request URL as a url.Values map, where the keys are the parameter names and the values are slices of parameter values. If there are no query parameters, it returns an empty map.

func (*Request) Query

func (req *Request) Query(name string) string

Query returns the value of the specified query parameter from the request URL. If the parameter is not present, it returns an empty string.

func (*Request) QueryValues

func (req *Request) QueryValues(name string) []string

QueryValues returns all values associated with the specified query parameter from the request URL. If the parameter is not present, it returns an empty slice.

func (*Request) Request

func (req *Request) Request() any

Request returns the underlying http.Request associated with this Request. This can be used to access any additional information or functionality provided by the http.Request that is not exposed through the Request interface.

func (*Request) Resource

func (req *Request) Resource() string

Resource returns the resource associated with the request. The resource is a string that can be used to identify the type of request or the endpoint being accessed. It is typically set by the router based on the URL pattern matched for the request.

func (*Request) SetPathValue

func (req *Request) SetPathValue(name, value string)

SetPathValue sets the value of the specified path parameter in the request. This is typically used by the router to store path parameters extracted from the URL.

func (*Request) SetResource

func (req *Request) SetResource(resource string)

SetResource sets the resource associated with the request. This is typically used by the router to store the resource identifier based on the URL pattern matched for the request.

type Response

type Response struct {
	// contains filtered or unexported fields
}

func (*Response) AddHeader

func (resp *Response) AddHeader(key, value string)

AddHeader adds a header field with the specified key and value to the response. If the header field already exists, the new value will be appended to the existing values for that header.

func (*Response) DelHeader

func (resp *Response) DelHeader(key string)

DelHeader deletes the specified header field from the response. If the header field is not present, this method does nothing.

func (*Response) GetHeader

func (resp *Response) GetHeader(key string) string

GetHeader returns the value of the specified header field from the response. If the header field is not present, it returns an empty string.

func (*Response) Headers

func (resp *Response) Headers() http.Header

Headers returns the http.Header map containing all the header fields and their values in the response. This allows you to access and manipulate the headers of the response as needed before sending it back to the client.

func (*Response) Response

func (resp *Response) Response() any

Response returns the underlying http.ResponseWriter associated with this Response. This can be used to access any additional information or functionality provided by the http.ResponseWriter that is not exposed through the Response interface.

Note: Do not manipulate the returned http.ResponseWriter directly, as it may interfere with the proper handling of the response by the framework. Use the methods provided by the Response interface to manipulate the response instead.

func (*Response) SetHeader

func (resp *Response) SetHeader(key, value string)

SetHeader sets a header field with the specified key and value in the response. If the header field already exists, its value will be replaced with the new value.

func (*Response) Status

func (resp *Response) Status(code int)

Status sets the HTTP status code for the response. This method allows you to specify the status code that should be sent back to the client along with the response. If you do not set a status code, the default status code of 200 OK will be used when the response is sent.

func (*Response) StatusCode

func (resp *Response) StatusCode() int

StatusCode returns the HTTP status code that has been set for the response. If no status code has been set, it returns 0, which indicates that the default status code of 200 OK will be used when the response is sent.

func (*Response) Write

func (resp *Response) Write(data []byte) (int, error)

Write writes the given data to the response body. It appends the data to any existing content in the body. It returns the number of bytes written and any error encountered during the write operation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL