feather

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2025 License: GPL-3.0 Imports: 10 Imported by: 0

README

Feather v1

Introduction

Feather is a lightweight and flexible HTTP framework for building web applications and APIs in Go. It provides an easy-to-use routing system, middleware support, request parsing, and error handling.

Installation

To install Feather, run:

go get git.trcreatives.com/triegebauer/go-feather

Quick Start

Create a simple Feather application:

package main

import (
	"fmt"
	"net/http"
	"git.trcreatives.com/triegebauer/go-feather"
	"git.trcreatives.com/triegebauer/go-feather/middleware"
)

func main() {
	app := feather.New()

	// Use logging middleware
	app.Use(middleware.Logging)

	app.GET("/", func(ctx *feather.Context) error {
		return ctx.Text(http.StatusOK, "Hello, Feather!")
	})

	app.Start(":8080")
}

Run the application and visit http://localhost:8080/ in your browser.

Core Features

Routing

Feather supports various HTTP methods:

app.GET("/hello", helloHandler)
app.POST("/submit", submitHandler)
app.PUT("/update", updateHandler)
app.DELETE("/delete", deleteHandler)
app.PATCH("/modify", modifyHandler)
app.HEAD("/head", headHandler)
app.OPTIONS("/options", optionsHandler)
Route Parameters

Capture URL parameters using the Param method:

app.GET("/user/:id", func(ctx *feather.Context) error {
	id := ctx.Param("id")
	return ctx.Text(http.StatusOK, "User ID: " + id)
})
Query Parameters

Retrieve query parameters with the Query method:

app.GET("/search", func(ctx *feather.Context) error {
	query := ctx.Query("q")
	return ctx.Text(http.StatusOK, "Search: " + query)
})
Request and Response Access

Access the underlying request and response objects with the Request and Response methods:

app.GET("/info", func(ctx *feather.Context) error {
	req := ctx.Request()
	res := ctx.Response()
	// Use req and res for custom logic...
	fmt.Println("User Agent:", req.UserAgent())
	res.Header().Set("X-Custom-Header", "value")
	return ctx.Text(http.StatusOK, "Check console for user agent info")
})
Handling Files

Upload files using the FormFile method:

app.POST("/upload", func(ctx *feather.Context) error {
	file, err := ctx.FormFile("upload")
	if err != nil {
		return ctx.Text(http.StatusBadRequest, "Failed to get file")
	}
	// Process the file (dereference the pointer)
	defer file.Close()
	return ctx.Text(http.StatusOK, "File uploaded successfully")
})

Built-in Middleware

Feather provides built-in middleware for common functionalities.

Logging Middleware

The logging middleware logs every request along with its duration and status code:

import "git.trcreatives.com/triegebauer/go-feather/middleware"

app.Use(middleware.Logging)

Example log output:

GET / in 120ms with status '200 OK'

Middleware

Apply middleware globally:

app.Use(loggingMiddleware)

Or to specific routes:

app.GET("/secure", secureHandler, authMiddleware)
Static Files

Serve static files:

app.Static("/static", "./public")
Request Parsing

Feather supports parsing of JSON, XML, form, and multipart form data.

Example with JSON:

type User struct {
	Name  string `json:"name"`
	Email string `json:"email"`
}

app.POST("/register", func(ctx *feather.Context) error {
	var user User
	// Parse JSON
	if err := ctx.ParseJSON(&user); err != nil {
		return err
	}
	return ctx.JSON(http.StatusOK, feather.Json{"message": "User registered", "user": user})
})

Other parsing methods include:

  • ParseXML: Parses XML data.
  • ParseForm: Parses URL-encoded form data.
  • ParseMultipartForm: Parses multipart form data (useful for file uploads).
  • ParseQuery: Parses URL query parameters.
Response Shortcuts

The Context API provides helper methods for sending responses:

  • Text: Sends plain text responses.
  • JSON: Sends JSON responses.
  • Bytes: Sends arbitrary byte data with a specified content type.
  • HTML: Sends HTML content.
  • File: Serves a static file.
  • NoContent: Sends a 204 No Content response.
  • NotFound: Sends a 404 Not Found response.
  • InternalServerError: Sends a 500 Internal Server Error response.
  • Redirect: Redirects the client to a different URL.

Example of sending an HTML response:

app.GET("/welcome", func(ctx *feather.Context) error {
	htmlContent := `<html><body><h1>Welcome to Feather!</h1></body></html>`
	return ctx.HTML(http.StatusOK, htmlContent)
})
Groups

Group related routes to keep your code organized:

authGroup := app.Group("/auth")
authGroup.POST("/login", loginHandler)
authGroup.POST("/register", registerHandler)
Error Handling

Customize error handling globally:

app.SetErrorHandler(func(err error, ctx *feather.Context) {
	ctx.JSON(http.StatusInternalServerError, feather.Json{"error": err.Error()})
})

Conclusion

Feather is a minimal yet powerful framework designed for developers who want a simple and efficient way to build web applications in Go. With a clear and straightforward Context API, you have complete control over request and response handling and a variety of response shortcuts.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	// contains filtered or unexported fields
}

func (*Context)