ExampleBeginner
Basic Server Example
A simple HTTP server demonstrating basic Squirrel functionality
Complete Example
Heres a complete basic server implementation:
package main
import (
"fmt"
"log"
"github.com/user001/squirrel"
)
func main() {
// Create a new Squirrel mux
mux := server.SpawnServer()
// Define routes
mux.Get("/home", homeHandler)
mux.Get("/echo", echoHandler)
// Start the server
fmt.Println("Server starting on http://localhost:8080")
log.Fatal(squirrel.ListenAndServe(":8080", mux))
}
// Home page handler
func homeHandler(w *squirrel.Response, r *squirrel.Request) {
w.Header().Set("Content-Type", "text/html")
w.WriteString(`
<html>
<head><title>Squirrel Basic Server</title></head>
<body>
<h1>Welcome to Squirrel!</h1>
<p>This is a basic server example.</p>
<ul>
<li><a href="/hello">Hello World</a></li>
<li><a href="/hello/John">Hello John</a></li>
<li><a href="/api/status">API Status</a></li>
</ul>
</body>
</html>
`)
}
// Simple hello handler
func helloHandler(w *squirrel.Response, r *squirrel.Request) {
w.WriteString("Hello, World!")
}
// Hello with name parameter
func helloNameHandler(w *squirrel.Response, r *squirrel.Request) {
name := r.PathValue("name")
w.WriteString("Hello, " + name + "!")
}
// API status endpoint
func statusHandler(w *squirrel.Response, r *squirrel.Request) {
status := map[string]interface{}{
"status": "ok",
"version": "1.0.0",
"message": "Server is running",
}
w.JSON(status)
}
// Echo endpoint that returns the request body
func echoHandler(w *squirrel.Response, r *squirrel.Request) {
var data map[string]interface{}
response := map[string]interface{}{
"echo": data,
"method": r.Method,
"path": r.URL.Path,
}
w.JSON(response)
}Running the Example
To run this example:
# Create a new Go module
go mod init basic-server-example
# Install Squirrel
go get github.com/useranonymous001/squirrel
# Save the code above as main.go and run
go run main.goTesting the Endpoints
Test the server with these curl commands:
# Test the home page
curl http://localhost:8080/
# Test hello endpoint
curl http://localhost:8080/hello
# Test hello with name
curl http://localhost:8080/hello/Alice
# Test API status
curl http://localhost:8080/api/status
# Test echo endpoint
curl -X POST http://localhost:8080/api/echo \
-H "Content-Type: application/json" \
-d '{"message": "Hello from client", "timestamp": "2024-01-01T00:00:00Z"}'Key Features Demonstrated
Routing
- • Static routes (/hello)
- • Dynamic routes (/hello/"e;fe"e;)
- • Method-specific routes (GET, POST)
Responses
- • Plain text responses
- • HTML responses
- • JSON responses
Middleware
- • Logging middleware
- • Recovery middleware
- • Request/response processing
Request Handling
- • Path parameters
- • JSON parsing
- • Error handling
Next Steps
After running this basic example, you can:
- Add more routes and handlers
- Implement custom middleware
- Add database integration
- Explore the REST API example
- Learn about advanced middleware patterns
This basic server example demonstrates the fundamental concepts of Squirrel Framework. Its a great starting point for understanding how to handle different types of requests and responses. Try modifying the handlers to experiment with different functionality!