alf

package module
v0.0.0-...-8d3c269 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: MIT Imports: 9 Imported by: 0

README

🦌 ALF (API Like Flutter) Router

🔴 This library is actually well tested but the API may change with the time introducing breaking changes
Description

👉 This is a micro-framework / router built on ⬆ top of the fasthttp package. Alf relies on scalability and his simple structure wich is similar to Flutter projects 📴

✨ Inspired by Flutter & Fiber

Purpouse

👷‍♂️ Make a router to start faster and simpler my backend projects

📖 Learn

  • Explore the fasthttp package ⏭
  • Publish my own package 📦
  • Learn more deeply how a web server works 🌐

Technologies used 📘

  • Go (Golang)
Core External Packages 📦:
  1. fasthttp (http ☁)
  2. ffjson (parse interfaces to json fast)
  3. pterm (show info, errors & warnings)

Features :

  • Fast Router 💨
  • Concurrent Route Setup ⌚
  • Send JSON Responses (docs)
  • HTML/Go Templates Out of the BOX ✨ (docs)
  • Plugins 🧩 :
    • Generate Types for TS (tygo pkg) (docs)

Docs

Prerequisites 📌
Set up your first project 💻

Download the package ⬇

go get github.com/PiterWeb/Alf-Router

Import it into your code 🔠

    import (
	    alf "github.com/PiterWeb/Alf-Router"
    )

Write this simple structure

    err := alf.App(&alf.AppConfig{
		Port: "3000",
		Routes: alf.CreateRouter([]alf.Route{
			{
				Path: "/",
				Handle: func(ctx *alf.Ctx) error {
					_, err := ctx.WriteString("Hello World")
					return err
				},
				Method: "get",
			},
		}),
	})

	if err != nil {
		panic(err)
	}

Now you have setup 🔨 an index route

Documentation

Index

Constants

View Source
const (
	GET     method = "GET"
	POST    method = "POST"
	DELETE  method = "DELETE"
	PUT     method = "PUT"
	PATCH   method = "PATCH"
	HEAD    method = "HEAD"
	OPTIONS method = "OPTIONS"
)

Variables

This section is empty.

Functions

func App

func App(config *AppConfig) error

func CreateRouter

func CreateRouter(r []Route) methodRoutes

Types

type AppConfig

type AppConfig struct {
	Routes      methodRoutes   // routes of the app
	Middleware  []Middleware   // global middlewares
	Headers     []Header       // global headers
	Port        string         // port of the app | default value '8080'
	NotFound    func(ctx *Ctx) // func that handles NotFound requests
	ServeStatic bool           // if true, the app will serve static files on "/static"
	Plugins     []Plugin       // structs that implements the Plugin interface and runs Init_plugin() before starting the server
}

type Ctx

type Ctx = fasthttp.RequestCtx // alias for Ctx
type Header struct {
	Name  string // name of the header
	Value string // value of the header
}

type Middleware

type Middleware func(ctx *Ctx) bool // func that returns true if passed and false if an error ocurred

type Plugin

type Plugin interface {
	Init_plugin(*AppConfig) error
}

type Route

type Route struct {
	Path       string                    // path
	Method     method                    // method (only one)
	Handle     func(ctx *Ctx) error      // func that handles the route
	Children   []Route                   // children of the route (if the route isnt the root route) [all childrens inherit parents Middlewares and Headers ]
	Error      func(ctx *Ctx, err error) // func that handles errors of the route (if err returned on handle method will be invoked with the error in the parameters)
	Middleware []Middleware              // middlewares of the route
	Headers    []Header                  // headers
}

Directories

Path Synopsis
plugins
ts
utils

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL