GOServe
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.
Install / Use
/learn @Fuad28/GOServeREADME
GoServe
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.
Table of Contents
Table of Contents
- Features
- Installation
- Quick Start
- Configuration
- Routing
- Middleware
- CORS Support
- Passing Data Around
- Contributing
- License
- Contributors
Features
- 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.
Installation
To install GOServe, use go get:
go get github.com/fuad28/goserve
Quick Start
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.
Configuration
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"},
})
Routing
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
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)
CORS Support
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"}))
Passing data around
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)
}
Contributing
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.
License
This project is licensed under the MIT license.
Contributors
<a href="https://github.com/fuad28/goserve/graphs/contributors"> <img src="https://contrib.rocks/image?repo=fuad28/goserve" /> </a>Made with contrib.rocks.
Related Skills
diffs
336.9kUse the diffs tool to produce real, shareable diffs (viewer URL, file artifact, or both) instead of manual edit summaries.
clearshot
Structured screenshot analysis for UI implementation and critique. Analyzes every UI screenshot with a 5×5 spatial grid, full element inventory, and design system extraction — facts and taste together, every time. Escalates to full implementation blueprint when building. Trigger on any digital interface image file (png, jpg, gif, webp — websites, apps, dashboards, mockups, wireframes) or commands like 'analyse this screenshot,' 'rebuild this,' 'match this design,' 'clone this.' Skip for non-UI images (photos, memes, charts) unless the user explicitly wants to build a UI from them. Does NOT trigger on HTML source code, CSS, SVGs, or any code pasted as text.
openpencil
1.8kThe world's first open-source AI-native vector design tool and the first to feature concurrent Agent Teams. Design-as-Code. Turn prompts into UI directly on the live canvas. A modern alternative to Pencil.
ui-ux-pro-max-skill
51.3kAn AI SKILL that provide design intelligence for building professional UI/UX multiple platforms
