Sofu is a lightweight, flexible HTTP server framework for Go that makes it easy to build web applications and APIs. Built as part of the CodeCrafters HTTP server challenge, Sofu provides a clean, intuitive interface for handling HTTP requests.
- Simple and intuitive routing system with path parameter support
- HTTP method-based route handling (
GET,POST,PUT,DELETE) - Context-based request handling
- Built-in response helpers
- Support for HTTP compression (content negotiation)
- File serving capabilities
- Persistent connection support (keep-alive)
- Easy setup with minimal configuration
- Directory-based file serving with command line configuration
go get github.com/AakashRawat04/sofupackage main
import "github.com/AakashRawat04/sofu/sofu"
func main() {
// Create a new server instance
server := sofu.New()
// Register routes
server.GET("/", func(c *sofu.Context) {
c.WriteResponse(sofu.StatusOK, "Hello, World!")
})
// Start the server
server.Start(":4221")
}// Define routes and their handlers
server.GET("/", func(c *sofu.Context) {
c.WriteResponse(sofu.StatusOK, "Welcome to Sofu!")
})
server.POST("/submit", func(c *sofu.Context) {
// Handle POST request
c.WriteResponse(sofu.StatusCreated, "Resource created")
})
server.PUT("/update", func(c *sofu.Context) {
// Handle PUT request
c.WriteResponse(sofu.StatusOK, "Resource updated")
})
server.DELETE("/remove", func(c *sofu.Context) {
// Handle DELETE request
c.WriteResponse(sofu.StatusOK, "Resource deleted")
})// Route with a parameter
server.GET("/echo/:message", func(c *sofu.Context) {
// Extract the parameter value
message := c.Param("message")
c.WriteResponse(sofu.StatusOK, message)
})server.GET("/user-agent", func(c *sofu.Context) {
userAgent := c.Request.Headers["User-Agent"]
c.WriteResponse(sofu.StatusOK, userAgent)
})server.GET("/files/:filename", func(c *sofu.Context) {
filename := c.Param("filename")
filePath := filepath.Join(server.Directory, filename)
content, err := os.ReadFile(filePath)
if err != nil {
c.WriteResponse(sofu.StatusNotFound, "File not found")
return
}
contentType := sofu.ContentTypeApplicationOctetStream
if strings.HasSuffix(filename, ".txt") {
contentType = sofu.ContentTypeTextPlain
} else if strings.HasSuffix(filename, ".html") {
contentType = sofu.ContentTypeTextHTML
}
c.SetHeader(sofu.HeaderContentType, contentType)
c.WriteResponse(sofu.StatusOK, string(content))
})server.POST("/files/:filename", func(c *sofu.Context) {
filename := c.Param("filename")
filePath := filepath.Join(server.Directory, filename)
err := os.WriteFile(filePath, []byte(c.Request.Body), 0644)
if err != nil {
c.WriteResponse(sofu.StatusInternalServerError, "Failed to save file")
return
}
c.WriteResponse(sofu.StatusCreated, "")
})The server accepts command line flags:
-directory: Specify the directory for serving files (default: "files")
Example:
./server -directory /path/to/filesNew() *Server: Creates a new server instance with default settings and parses command line flagsStart(addr string): Starts the server on the given addressGET(path string, handler HandlerFunc): Registers a GET routePOST(path string, handler HandlerFunc): Registers a POST routePUT(path string, handler HandlerFunc): Registers a PUT routeDELETE(path string, handler HandlerFunc): Registers a DELETE route
WriteResponse(statusCode int, body string): Writes a response with the given status code and bodySetHeader(key, value string): Sets a response headerParam(key string): Gets a URL parameter valueShouldCloseConnection(): Determines if the connection should be closed after the response
The framework provides constants for common HTTP headers and status codes:
HeaderContentType,HeaderContentLength, etc.StatusOK,StatusCreated,StatusNotFound, etc.- Content type constants like
ContentTypeTextPlain,ContentTypeTextHTML, etc.
The framework supports content negotiation for compression based on the Accept-Encoding header. When a client indicates support for compression methods like gzip, the response will be compressed accordingly.
Sofu handles HTTP persistent connections (keep-alive), maintaining connections open for multiple request-response cycles when supported by the client.
The routing system supports path parameters (like :filename in /files/:filename) that can be extracted using the c.Param() method.
This allows for dynamic routing and handling of requests with variable paths.
Sofu can serve static files from a specified directory. The server can be configured to serve files based on the URL path, and it automatically sets the appropriate content type based on the file extension.
Contributions are welcome! Please feel free to submit a Pull Request.
