Documentation

Everything you need to get started with raw-http.

Installation

Add raw-http to your Go project:

go get github.com/codetesla51/raw-http@v1.0.0

Quick Start

A minimal server with one route:

package main

import (
    "log"
    "github.com/codetesla51/raw-http/server"
)

func main() {
    // Create server
    srv := server.NewServer(":8080")

    // Register a route
    srv.Register("GET", "/ping", func(req *server.Request) ([]byte, string) {
        return server.CreateResponseBytes("200", "text/plain", "OK", []byte("pong"))
    })

    // Start server (graceful shutdown on Ctrl+C)
    if err := srv.ListenAndServe(); err != nil {
        log.Fatal(err)
    }
}

Routing

Register routes with HTTP methods and path parameters:

// Exact route
router.Register("GET", "/api/health", handler)

// Path parameter
router.Register("GET", "/users/:id", func(req *server.Request) ([]byte, string) {
    userID := req.PathParams["id"]  // Access the :id parameter
    return server.CreateResponseBytes("200", "text/plain", "OK", []byte(userID))
})

// Multiple parameters
router.Register("GET", "/posts/:postId/comments/:commentId", handler)

// POST with body
router.Register("POST", "/api/data", func(req *server.Request) ([]byte, string) {
    body := req.Body
    return server.CreateResponseBytes("201", "application/json", "Created", body)
})

Request Object

Access all request data through the *server.Request struct:

Field Type Description
MethodstringGET, POST, PUT, DELETE, etc.
PathstringRequest path (e.g., "/users/123")
PathParamsmap[string]stringDynamic route parameters
QueryParamsmap[string]stringURL query string values
Headersmap[string]stringHTTP request headers
Body[]byteRaw request body
ContentLengthintBody size in bytes

Responses

Create responses with CreateResponseBytes or use helper functions:

// Manual response
server.CreateResponseBytes(statusCode, contentType, statusText, body)

// Example
return server.CreateResponseBytes("200", "application/json", "OK", jsonData)

Built-in helper functions:

Function Status Use Case
Serve201()201Created
Serve204()204No Content
Serve400()400Bad Request
Serve401()401Unauthorized
Serve403()403Forbidden
Serve404()404Not Found
Serve405()405Method Not Allowed
Serve500()500Internal Server Error

Static Files

Put files in the pages/ directory:

pages/
├── index.html      → /
├── about.html      → /about.html
├── styles.css      → /styles.css
└── 404.html        → Custom 404 page

MIME types are auto-detected. Supported: HTML, CSS, JS, JSON, images, fonts, and more.

Configuration

Customize server behavior with Config:

// Create server with custom config
config := &server.Config{
    ReadTimeout:     60 * time.Second,
    WriteTimeout:    30 * time.Second,
    MaxBodySize:     50 * 1024 * 1024,  // 50MB
    EnableKeepAlive: true,
}

srv := server.NewServerWithConfig(":8080", config)

// Enable HTTPS
srv.EnableTLS(":8443", "server.crt", "server.key")

srv.Register("GET", "/ping", handler)
srv.ListenAndServe()

Need more?

Check out the full README on GitHub for advanced usage.

View on GitHub