gorouter

gorouter

  • Docs
  • Help
  • GitHub

›Examples

Quick Start

  • Installation
  • Basic example

Router

  • Routing
  • Middleware
  • Mounting Sub-Router

Examples

    Authentication

    • Basic Authentication

    Serving Files

    • Static Files
  • HTTPS
  • HTTP2
  • Multidomain
  • Panic Recovery
  • App Handler

Benchmark

  • Benchmark

App Handler

Use custom handler type in your application

net/http
valyala/fasthttp
package main

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

"github.com/vardius/gorouter/v4"
)

type AppHandlerFunc func(http.ResponseWriter, *http.Request) error

// ServeHTTP calls f(w, r) and handles error
func (f AppHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := f(w, r); err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprint(w, err.Error())
}
}

func Index(w http.ResponseWriter, r *http.Request) error {
return errors.New("I am app handler which can return error")
}

func main() {
router := gorouter.New()
router.GET("/", AppHandlerFunc(Index))

log.Fatal(http.ListenAndServe(":8080", router))
}
package main

import (
"errors"
"log"

"github.com/valyala/fasthttp"
"github.com/vardius/gorouter/v4"
)

type AppHandlerFunc func(ctx *fasthttp.RequestCtx) error

// HandleFastHTTP calls f(ctx) and handles error
func (f AppHandlerFunc) HandleFastHTTP(ctx *fasthttp.RequestCtx) {
if err := f(ctx); err != nil {
ctx.SetBody([]byte(err.Error()))
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
}
}

func index(_ *fasthttp.RequestCtx) error {
return errors.New("I am app handler which can return error")
}

func main() {
router := gorouter.NewFastHTTPRouter()
router.GET("/", AppHandlerFunc(index))

log.Fatal(fasthttp.ListenAndServe(":8080", router.HandleFastHTTP))
}
← Panic RecoveryBenchmark →
  • Use custom handler type in your application
gorouter
Docs
DocumentationGoDoc
Community
Support
More
rafallorenz.comGitHubStar
Copyright © 2024 Rafał Lorenz