Cloudy is a lightweight, flexible web framework for Go that emphasizes simplicity and productivity. It provides a clean architecture for building web applications with features like routing, sessions, middleware, and more.
- Simple and intuitive API
- Built-in session management
- Flash message support
- Middleware pipeline
- Controller-based architecture
- Easy routing with method mapping
go get github.com/CloudyKit/cloudy
Below is a basic example of how to set up a Cloudy web application:
myapp/
├── app/
│ └── app.go
├── cmd/
│ └── server.go
└── controllers/
└── home.go
package main
import "github.com/CloudyKit/cloudy-example/app"
func main() {
app.Kernel.RunServer(":8888")
}
package app
import (
"github.com/CloudyKit/cloudy"
"github.com/CloudyKit/cloudy-example/controllers"
"github.com/CloudyKit/cloudy/flash"
"github.com/CloudyKit/cloudy/session"
)
var Kernel = cloudy.NewKernel()
func init() {
// Register components
Kernel.AddComponents(
&session.Component{
Manager: session.DefaultManager,
CookieOptions: session.DefaultCookieOptions,
},
&flash.Component{},
)
// Register controllers
Kernel.AddControllers(
&controllers.Home{},
)
// Add middleware
Kernel.AddMiddlewareFunc(func(ctx *cloudy.Context) {
ctx.Response.Header().Set("X-Cloudy", "CloudyKit")
})
}
package controllers
import (
"github.com/CloudyKit/cloudy"
"github.com/CloudyKit/cloudy/session"
)
type Home struct {
Context *cloudy.Context
SessionData *session.Session
}
// Mx maps HTTP methods to controller actions
func (h *Home) Mx(mx *cloudy.Mapper) {
mx.BindAction("GET", "/", "Index")
}
// Index handles the root path
func (h *Home) Index() {
counter, _ := h.SessionData.Get("counter").(int)
_, _ = h.Context.PrintfWriteStringfWriteString("Counter: %d", counter)
counter++
defer h.SessionData.Set("counter", counter)
}
The Kernel is the central component that manages the application lifecycle. It handles routing, middleware execution, and component initialization.
Controllers handle incoming requests and produce responses. In Cloudy, controllers are Go structs with methods that correspond to HTTP endpoints.
Middleware functions can be added to the request processing pipeline to handle cross-cutting concerns like authentication, logging, etc.
Components provide additional functionality like sessions and flash messages. They can be easily added to the application's kernel.
The Mapper binds HTTP methods and paths to controller methods, making routing simple and intuitive.
To run the example application:
go run cmd/server.go
Then visit http://localhost:8888 in your browser. You should see a counter that increments on each page refresh, demonstrating the session functionality.
[MIT License]
Contributions are welcome! Please feel free to submit a Pull Request.'