hyper
is a lightweight toy and extensible HTTP server framework for building TCP-based web applications in Go.
- Customizable Route Handlers: Define HTTP methods like
GET
,POST
,PUT
, etc., and their handlers. - Dynamic Path Matching: Supports route patterns with regex for flexibility.
- Query and Header Parsing: Automatically parses query parameters and HTTP headers.
- Concurrency: Efficient connection handling using goroutines and semaphores.
- Template Support: Configurable templates directory for rendering responses.
go get github.com/VarthanV/hyper
package main
import (
"fmt"
"github.com/VarthanV/hyper"
)
func main() {
server := hyper.New()
// Define routes
server.GET("/hello", func(w hyper.ResponseWriter, r *hyper.Request) {
w.Write([]byte("Hello, World!"))
})
// Start the server
server.ListenAndServe("127.0.0.1", "8080", "Server is running...")
}
Define routes with HTTP methods using the built-in methods:
GET
POST
PUT
PATCH
DELETE
OPTIONS
CONNECT
TRACE
Example:
server.POST("/submit", func(w hyper.ResponseWriter, r *hyper.Request) {
fmt.Fprintf(w, "Received POST data: %s", string(r.Body))
})
Creates a new instance of the Hyper server.
Starts the server on the specified host and port. Logs the startupMessage
.
Registers a route for the specified HTTP method (GET
, POST
, etc.).
- Parameters:
path
: Route path (supports regex).handler
: Function to handle the request.
type Request struct {
Method HttpMethod
Path string
Protocol string
headers map[string]string
queryParams map[string]string
Body []byte
RemoteHostAddr net.Addr
}
type ResponseWriter interface {
WriteStatus(code int)
Write([]byte) (int, error)
WriteHeader(key, val string)
WriteJSON(status int, b interface{})
WriteHTML(status int, html string)
WriteString(status int, val string)
ToRaw() string
StatusCode() int
}
This project is licensed under the MIT License.