Everything you need to get started with raw-http.
Add raw-http to your Go project:
go get github.com/codetesla51/raw-http@v1.0.0
go get github.com/codetesla51/raw-http@v1.0.0
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)
}
}
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)
}
}
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)
})
// 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)
})
Access all request data through the *server.Request struct:
| Field | Type | Description |
|---|---|---|
| Method | string | GET, POST, PUT, DELETE, etc. |
| Path | string | Request path (e.g., "/users/123") |
| PathParams | map[string]string | Dynamic route parameters |
| QueryParams | map[string]string | URL query string values |
| Headers | map[string]string | HTTP request headers |
| Body | []byte | Raw request body |
| ContentLength | int | Body size in bytes |
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() | 201 | Created |
| Serve204() | 204 | No Content |
| Serve400() | 400 | Bad Request |
| Serve401() | 401 | Unauthorized |
| Serve403() | 403 | Forbidden |
| Serve404() | 404 | Not Found |
| Serve405() | 405 | Method Not Allowed |
| Serve500() | 500 | Internal Server Error |
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.
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()
// 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()