Documentation
Learn how to install, configure, and deploy your Go framework.
Introduction
Falcon is a minimal and fast Go framework for building HTTP services. It provides a clear API, predictable performance, and a smooth developer experience.
Installation
Install Falcon using:
go get github.com/ascendingheavens/falconGetting Started
Create a new main.go and start a Falcon server:
package main
import (
"net/http"
framework "github.com/AscendingHeavens/falcon"
)
func main() {
app := framework.New()
app.GET("/", func(c framework.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
app.Start(":8080")
}Server Setup
The Server struct initializes Falcon with middleware, routes, and a database connection.
type Server struct {
Router *falcon.Server
db db.DB
config config.Config
}
func NewServer(cfg config.Config) (*Server, error) {
server := &Server{
config: cfg,
db: db.NewInMemoryUserDB(),
}
server.SetupRouter()
return server, nil
}Routing
Define routes and map them to handler methods. Example from SetupRouter:
func (server *Server) SetupRouter() {
router := falcon.New()
router.Use(middleware.CORS())
router.Use(middleware.Logger())
router.GET("/hello", func(c *falcon.Context) *falcon.Response {
return c.JSON(true, "Works", map[string]any{
"message": "Hello, World!",
}, http.StatusOK)
})
// Users
router.GET("/users", server.FetchAllUsers)
router.POST("/users", server.CreateUser)
router.GET("/users/:id", server.FetchUser)
router.PUT("/users/:id", server.UpdateUser)
router.DELETE("/users/:id", server.DeleteUser)
// Blob Example
router.GET("/blob", server.Blob)
server.Router = router
}Blob Route
Falcon can also serve binary files (like images). Example /blob route:
func (server *Server) Blob(ctx *falcon.Context) *falcon.Response {
data, err := os.ReadFile("falcon.png")
if err != nil {
return ctx.ErrorJSON("Could not load image", err.Error(), http.StatusInternalServerError)
}
return ctx.Blob(http.StatusOK, data, "image/png")
}User CRUD
Falcon makes it easy to implement REST-style CRUD endpoints.
Create User
func (server *Server) CreateUser(ctx *falcon.Context) *falcon.Response {
var request db.User
if err := ctx.Bind(&request); err != nil {
return ctx.ErrorJSON("Invalid request body", nil, http.StatusBadRequest)
}
user, err := server.db.CreateUser(request)
if err != nil {
return ctx.ErrorJSON("Could not create user", nil, http.StatusInternalServerError)
}
return ctx.JSON(true, "User created successfully!", user, http.StatusCreated)
}Fetch User
func (server *Server) FetchUser(ctx *falcon.Context) *falcon.Response {
id := ctx.Param("id")
intId, _ := strconv.Atoi(id)
user, _ := server.db.GetUser(intId)
return ctx.JSON(true, "User fetched successfully", user, http.StatusOK)
}Update User
func (server *Server) UpdateUser(ctx *falcon.Context) *falcon.Response {
id := ctx.Param("id")
intId, _ := strconv.Atoi(id)
var request db.User
ctx.Bind(&request)
updatedUser, _ := server.db.UpdateUser(intId, request)
return ctx.JSON(true, "User updated successfully!", updatedUser, http.StatusOK)
}Delete User
func (server *Server) DeleteUser(ctx *falcon.Context) *falcon.Response {
id := ctx.Param("id")
intId, _ := strconv.Atoi(id)
server.db.DeleteUser(intId)
return ctx.JSON(true, "User deleted successfully!", nil, http.StatusOK)
}Middleware
Use middleware for logging, authentication, and more:
app.Use(framework.Logger())
app.Use(func(next framework.HandlerFunc) framework.HandlerFunc {
return func(c framework.Context) error {
// before
err := next(c)
// after
return err
}
})Constants
Falcon currently does not define any constants.
Variables
Falcon currently does not define any variables.
Functions
func NewTemplateRenderer
Encapsulates server.NewTemplateRenderer.
func NewTemplateRenderer(pattern string, devMode bool, funcs template.FuncMap) *server.TemplateRenderer
// Creates a new template renderer with optional development mode and custom functionsTypes
type ConditionalMiddleware
Pairs a pattern (e.g. "/api/*") with a Middleware function.
type ConditionalMiddleware = middleware.ConditionalMiddleware
// Associates a URL pattern with a middleware functiontype Context
Wraps the request and response writer and provides convenience methods (params, body parsing, etc.).
type Context = server.Context
// Provides methods for accessing request parameters, body, and writing responsestype Group
type Group struct {
Prefix string
Server *Server
Middlewares []middleware.Middleware
}Represents a collection of routes sharing a common prefix and middleware stack. Useful for organizing related endpoints like /api/v1/*.
type HandlerFunc
type HandlerFunc = server.HandlerFunc
// Function signature for route handlers: accepts *Context and returns *Responsetype Middleware
type Middleware = middleware.Middleware
// Wraps and modifies a HandlerFunctype Response
type Response = server.Response
// Unified return type from handlers, encoded as JSONtype Server
type Server struct {
// internal fields
}type TLSStarter
type TLSStarter interface {
// startTLSServer method
}type TemplateRenderer
type TemplateRenderer = server.TemplateRenderer
// Responsible for rendering HTML templates within FalconServer Methods
func New
func New() *Server
// Creates a new Falcon Server instancefunc (*Server) GET / POST / PUT / PATCH / DELETE
func (s *Server) GET(path string, handler HandlerFunc)
func (s *Server) POST(path string, handler HandlerFunc)
func (s *Server) PUT(path string, handler HandlerFunc)
func (s *Server) PATCH(path string, handler HandlerFunc)
func (s *Server) DELETE(path string, handler HandlerFunc)
// Registers a route with the corresponding HTTP methodfunc (*Server) Group
func (s *Server) Group(prefix string) *Group
// Creates a route group with common prefix and middleware stackfunc (*Server) Use / UseIf
func (s *Server) Use(mw Middleware)
func (s *Server) UseIf(pattern string, mw Middleware)
// Registers global or conditional middlewarefunc (*Server) ServeHTTP
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
// Implements http.Handler, executes the route handler, writes Responsefunc (*Server) Start / StartTLS / StartAutoTLS / StartAutoTLSWithStarter
func (s *Server) Start(addr string)
func (s *Server) StartTLS(addr, certFile, keyFile string)
func (s *Server) StartAutoTLS(domain string)
func (s *Server) StartAutoTLSWithStarter(domain string, starter TLSStarter)
// Starts the server with optional TLS or automatic Let's Encrypt support