Fast, simple, and lightweight HTTP router for Golang

Overview

Sariaf

Fast, simple and lightweight HTTP router for golang

Install

go get -u github.com/majidsajadi/sariaf

Features

  • Lightweight
  • compatible with net/http
  • No external dependencies
  • Panic handler
  • Custom not found handler
  • No external dependencies
  • Middleware support
  • URL parameters

Usage

package main

import (
	"net/http"

	"github.com/majidsajadi/sariaf"
)

func main() {
    r := sariaf.New()

    r.Get("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("welcome"))
    })

    http.ListenAndServe(":3000", r)
}

Advanced Usage

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/majidsajadi/sariaf"
)

func main() {
	router := sariaf.New()

	router.SetNotFound(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusNotFound)
		fmt.Fprint(w, "Not Found")
	})

	router.SetPanicHandler(func(w http.ResponseWriter, r *http.Request, err interface{}) {
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Println("error:", err)
		fmt.Fprint(w, "Internal Server Error")
	})

	router.GET("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello World"))
	})

	router.GET("/posts", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("GET: Get All Posts"))
	})

	router.GET("/posts/:id", func(w http.ResponseWriter, r *http.Request) {
		params, _ := sariaf.GetParams(r)
		w.Write([]byte("GET: Get Post With ID:" + params["id"]))
	})

	router.POST("/posts", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("POST: Create New Post"))
	})

	router.PATCH("/posts/:id", func(w http.ResponseWriter, r *http.Request) {
		params, _ := sariaf.GetParams(r)
		w.Write([]byte("PATCH: Update Post With ID:" + params["id"]))
	})

	router.PUT("/posts/:id", func(w http.ResponseWriter, r *http.Request) {
		params, _ := sariaf.GetParams(r)
		w.Write([]byte("PUT: Update Post With ID:" + params["id"]))
	})

	router.DELETE("/posts/:id", func(w http.ResponseWriter, r *http.Request) {
		params, _ := sariaf.GetParams(r)
		w.Write([]byte("DELETE: Delete Post With ID:" + params["id"]))
	})

	router.GET("/error", func(w http.ResponseWriter, r *http.Request) {
		panic("Some Error Message")
	})

	log.Fatal(http.ListenAndServe(":8181", router))
}
You might also like...
Simple router build on `net/http` supports custom middleWare.

XMUS-ROUTER Fast lightweight router build on net/http supports delegate and in url params. usage : Create new router using NewRouter() which need rout

Simple HTTP router for Go

ngamux Simple HTTP router for Go Installation Examples Todo Installation Run this command with correctly configured Go toolchain. go get github.com/ng

Simple http router.

Simple router based on tree structure. import ( "fmt" "log" "net/http" router "github.com/dmitruk-v/router/v1" ) func main() { ro := router.New

A powerful HTTP router and URL matcher for building Go web servers with 🦍

gorilla/mux https://www.gorillatoolkit.org/pkg/mux Package gorilla/mux implements a request router and dispatcher for matching incoming requests to th

Go HTTP request router and web framework benchmark

Go HTTP Router Benchmark This benchmark suite aims to compare the performance of HTTP request routers for Go by implementing the routing structure of

Go Server/API micro framework, HTTP request router, multiplexer, mux
Go Server/API micro framework, HTTP request router, multiplexer, mux

🍃 gorouter Go Server/API micro framework, HTTP request router, multiplexer, mux. 📖 ABOUT Contributors: Rafał Lorenz Want to contribute ? Feel free t

A high performance HTTP request router that scales well

HttpRouter HttpRouter is a lightweight high performance HTTP request router (also called multiplexer or just mux for short) for Go. In contrast to the

High-speed, flexible tree-based HTTP router for Go.

httptreemux High-speed, flexible, tree-based HTTP router for Go. This is inspired by Julien Schmidt's httprouter, in that it uses a patricia tree, but

Go HTTP router

violetear Go HTTP router http://violetear.org Design Goals Keep it simple and small, avoiding extra complexity at all cost. KISS Support for static an

Owner
defectivepixel
I code, therefore I am.
defectivepixel
Bxog is a simple and fast HTTP router for Go (HTTP request multiplexer).

Bxog is a simple and fast HTTP router for Go (HTTP request multiplexer). Usage An example of using the multiplexer: package main import ( "io" "net

Eduard 105 Sep 27, 2022
Lightweight Router for Golang using net/http standard library with custom route parsing, handler and context.

Go-Lightweight-Router Lightweight Router for Golang using net/http standard library with custom route parsing, handler and context. Further developmen

null 0 Nov 3, 2021
xujiajun/gorouter is a simple and fast HTTP router for Go. It is easy to build RESTful APIs and your web framework.

gorouter xujiajun/gorouter is a simple and fast HTTP router for Go. It is easy to build RESTful APIs and your web framework. Motivation I wanted a sim

徐佳军 532 Oct 30, 2022
lightweight, idiomatic and composable router for building Go HTTP services

chi is a lightweight, idiomatic and composable router for building Go HTTP services. It's especially good at helping you write large REST API services

go-chi 12.7k Dec 4, 2022
:tongue: CleverGo is a lightweight, feature rich and high performance HTTP router for Go.

CleverGo CleverGo is a lightweight, feature rich and trie based high performance HTTP request router. go get -u clevergo.tech/clevergo English 简体中文 Fe

CleverGo Web Framework 248 Nov 17, 2022
An extremely fast Go (golang) HTTP router that supports regular expression route matching. Comes with full support for building RESTful APIs.

ozzo-routing You may consider using go-rest-api to jumpstart your new RESTful applications with ozzo-routing. Description ozzo-routing is a Go package

Ozzo Framework 445 Nov 15, 2022
Fast and flexible HTTP router

treemux - fast and flexible HTTP router Basic example Debug logging CORS example Error handling Rate limiting using Redis Gzip compression OpenTelemet

Vladimir Mihailenco 507 Nov 29, 2022
FastRouter is a fast, flexible HTTP router written in Go.

FastRouter FastRouter is a fast, flexible HTTP router written in Go. FastRouter contains some customizable options, such as TrailingSlashesPolicy, Pan

Razon Yang 22 Sep 27, 2022
Pure is a fast radix-tree based HTTP router

package pure Pure is a fast radix-tree based HTTP router that sticks to the native implementations of Go's "net/http" package; in essence, keeping the

Go Playgound 132 Dec 1, 2022
Simple Golang HTTP router

Bellt Simple Golang HTTP router Bellt Package implements a request router with the aim of managing controller actions based on fixed and parameterized

Guilherme Caruso 55 Sep 27, 2022