GOServe is a lightweight and extensible HTTP server framework written in Go, designed to simplify the process of building JSON REST APIs. It’s ideal for projects requiring a minimalistic and efficient server.
- Features
- Installation
- Quick Start
- Configuration
- Routing
- Middleware
- CORS Support
- Passing Data Around
- Contributing
- License
- Contributors
- Lightweight: Minimal dependencies, designed to be fast and efficient.
- JSON-Native: Built-in support for JSON request and response bodies.
- Flexible Routing: Supports dynamic routing and path parameters.
- Middleware Support: Easily extend functionality with custom middleware.
- CORS Handling: Built-in support for Cross-Origin Resource Sharing (CORS).
- Error Handling: Simplified error handling with customizable responses.
To install GOServe, use go get
:
go get github.com/fuad28/goserve
Here's a simple example to get you started with GOServe:
package main
import (
"github.com/fuad28/goserve"
"github.com/fuad28/goserve/status"
)
func main() {
server := goserve.NewServer(goserve.Config{
Port: 8000,
})
server.Get("/hello", func(req *goserve.Request, res goserve.IResponse) goserve.IResponse {
return res.SetStatus(status.HTTP_200_OK).Send("Hello World!")
})
server.ListenAndServe()
}
Full examples can be found in the example folder.
GOServe can be configured using the goserve.Config
struct. Key options include:
- Port: The port on which the server listens defaults to 8000.
- MaxRequestSize: Maximum size of the request body defaults to 1MB.
- AllowedOrigins: Origins allowed for CORS.
Example:
server := goserve.NewServer(goserve.Config{
Port: 8000,
MaxRequestSize: goserve.ONE_MB * 10,
AllowedOrigins: []string{"http://localhost:3000"},
})
Define routes with Get
, Post
, Put
, Delete
, and other HTTP methods. Routes support dynamic path and query parameters.
Example:
server.Get("/tasks/:id", func(req *goserve.Request, res goserve.IResponse) goserve.IResponse {
userID := req.PathParams().Get("id")
queryParameters := req.QueryParams().GetAll()
// Logic to fetch task by ID
return res.SetStatus(status.HTTP_200_OK).Send(goserve.JSON{"task": task})
})
Middleware allows you to extend functionality with custom middleware easily. In a middleware, you have access to the request and response throughout the request-response lifecycle. Use middleware to implement logging, authentication, etc.
- Note: Ensure to call the req.Next() method in your middleware function to pass control to the next handler in the handlerChain
Example:
func loggingMiddleware(req *goserve.Request, res goserve.IResponse) goserve.IResponse {
// request logging logic
res = req.Next(res)
// response logging logic
return res
}
server.AddMiddlewares(loggingMiddleware)
GOServe has built-in CORS support, configurable via middleware. Allow specific origins, methods, and headers.
- **Note: When testing with non-browser clients like Postman and Curl, you have to set your Origin header manually.
Example:
server.AddMiddlewares(goserve.CORSMiddleware([]string{"http://example.com"}))
GOServe requests have Store attribute that allows you to pass and retrieve data throughout the request lifecycle
Example:
func authenticationMiddlware(req *goserve.Request, res goserve.IResponse) goserve.IResponse {
if token, exists := req.Headers().Get("Authorization"); exists {
// Token authentication logic
userId := token
req.Store.Set("userId", userId)
} else {
return res.SetStatus(status.HTTP_401_UNAUTHORIZED).Send(
goserve.JSON{"message": "unauthorized"},
)
}
// pass control to the next handler in the handlerChain
return req.Next(res)
}
server.AddMiddlewares(authenticationMiddlware)
In your handler, you would access the userId as:
func allTasksHandler(req *goserve.Request, res goserve.IResponse) goserve.IResponse {
userId, exists := req.Store.Get("userId", userId)
}
Contributions are welcome! Please read the contributing guide to learn about our development process, how to propose bug fixes and improvements, and how to build and test your changes to GOServe.
Check out the currently open issues.
This project is licensed under the MIT license.
Made with contrib.rocks.