Skip to content

romssc/fluxx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚠️ Status: package is under development - APIs may change.


logo

Fluxx is a lightweight, opinionated HTTP library for Go. It provides a thin abstraction over the Go net/http package, with utilities for request reading, response sending, and graceful server lifecycle management.

Designed to stay close to the Go standard library, but with just enough ergonomics to make your web server development simpler!

🕸️ Quick Start

This guide will follow you through your first steps to use this package.

Get it

go get -u github.com/romssc/fluxx

Use it

Getting started with Fluxx is easy. Here's a basic example to create a simple web server that responds with "hello!" on the root path. This example demonstrates initializing a new Fluxx App, setting up a route, and starting the server.

func main() {
    mux := http.NewServeMux()
	mux.HandleFunc("/", fluxx.HandlerFuncAdapter(func(c *fluxx.Ctx) {
        c.Send().JSON(http.StatusOK, "hello!")
	}))

	app := fluxx.New(fluxx.Config{
		Address:      ":8080",            // or "localhost:8080" just like in the standart net/http
		ReadTimeout:  5 * time.Second,
		WriteTimeout: 10 * time.Second,
		Mux: http.NewServeMux(),          // in this example it's the standart mux, might as well be chi or any other.
	})

	if err := app.Listen(); err != nil {
		panic(err)
	}
}

Run it

go run main.go

Check out the fancy startup message

This message will also contain some of your Config settings, just for convenience.

screenshot

🧬 Middleware

Here is a list of middleware that are included within the Fluxx.

Title Description
rid Allow to track Request ID, creates one if there's none.
timeout Sets a time to handle the request.
key Simple API Key validator.

📄 Documentation

Index

Variables

Functions

Types

Variables

ErrListening    = errors.New("[ERROR] fluxx: FAILED WHILE LISTENING")
ErrShuttingDown = errors.New("[ERROR] fluxx: FAILED WHILE SHUTTING DOWN")

Error values returned by server lifecycle operations.

Functions

func New

func New(c Config) *App

func HandlerFuncAdapter

func HandlerFuncAdapter(h FluxxHandlerFunc) http.HandlerFunc

HandlerFuncAdapter adapts a FluxxHandlerFunc into a standard http.HandlerFunc.

Types

type App

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

App represents the Fluxx HTTP application. It manages server configuration and lifecycle.

func Listen

func (a *App) Listen() error

Listen starts the HTTP server. It blocks until the server shuts down or fails.

func ListenTLS

func (a *App) ListenTLS(certificate, key string) error

ListenTLS starts the server with TLS enabled.

func GracefulShutdown

func (a *App) GracefulShutdown(timeout time.Duration) error

GracefulShutdown attempts to shut down the server gracefully within the given timeout.

type Config

type Config struct {
    Address      string
    ReadTimeout  time.Duration
    WriteTimeout time.Duration
    TLS          *tls.Config
    Mux          http.Handler
}

Config holds the configuration options for creating a new App.

type Ctx

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

Ctx wraps the HTTP request and response, providing ergonomic helpers for reading inputs and sending outputs.

func Read

func (c *Ctx) Read() *Reader

Read returns the request reader.

func Send

func (c *Ctx) Send() *Sender

Send returns the response sender.

type Reader

type Reader struct {
    Request *http.Request
}

Reader provides methods for accessing request data.

func QueryParam

func (r *Reader) QueryParam(key string, defaultValue ...string) (string, bool)

QueryParam returns the query parameter by key. If not present, an optional default value may be used. The second return value indicates whether a value was found or defaulted.

type Sender

type Sender struct {
    Writer http.ResponseWriter
    // contains filtered or unexported fields
}

Sender provides methods for writing responses.

func Error

func (s *Sender) Error(status int, message string)

Error sends an HTTP error response with the given status code and message.

func JSON

func (s *Sender) JSON(status int, data any, customHeaders ...map[string]string) error

JSON sends a JSON response with the given status code, data, and optional headers.

func File

func (s *Sender) File(content, filename, path string, customHeaders ...map[string]string)

File serves a file response with content type and disposition headers.

type FluxxHandlerFunc

type FluxxHandlerFunc func(c *Ctx)

FluxxHandlerFunc defines a handler that operates on a Fluxx context.

🏴󠁩󠁤󠁳󠁭󠁿 License

MIT

About

🕸️ lightweight http library for go

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages