amwk/web is the HTTP adapter for the AMWK framework, providing the net adapter for building web applications.
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 tidyHere 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.
The web package can be configured using various options when creating a new application instance. Here are some of the available configuration options:
WithAddress(addr string): Sets the address for the server to listen on. Default is:8000.WithEnableShutdownSignal(enable bool, signals ...os.Signal): Enables or disables shutdown signal handling. By default, it is enabled and listens foros.Interrupt,syscall.SIGTERM, andsyscall.SIGQUITsignals.WithIdleTimeout(timeout time.Duration): Sets the maximum amount of time to wait for the next request when keep-alives are enabled. Default is60s.WithMaxHeaderBytes(size int): Sets the maximum size of request headers in bytes. Default is1 MB(1048576 bytes).WithMaxResponseBodyBytes(size int64): Sets the maximum size of the response body in bytes. Default is32 MB. Set to-1for unlimited body size.WithReadHeaderTimeout(timeout time.Duration): Sets the maximum duration for reading request headers. Default is10s.WithReadTimeout(timeout time.Duration): Sets the maximum duration for reading the entire request, including the body. Default is30s.WithShutdownTimeout(timeout time.Duration): Sets the maximum duration for gracefully shutting down the server. Default is5s.WithWriteTimeout(timeout time.Duration): Sets the maximum duration before timing out writes of the response. Default is30s.
- 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 vetandgo test ./...before submitting a PR.
The project is licensed under the Apache 2.0 License. See the LICENSE file for details.