fiber

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2020 License: MIT Imports: 18 Imported by: 0

README

Fiber

Latest Release GoDoc Go Report GitHub license

Fiber is a router framework build on top of FastHTTP, the fastest HTTP package for Go.
This library is inspired by Express, one of the most populair and well known web framework for Nodejs.

Full API Documentation

Click here

Installing

Assuming you’ve already installed Go, install the Fiber package by calling the following command:

$ go get -u github.com/fenny/fiber

Hello world

Embedded below is essentially the simplest Fiber app you can create.

$ create server.go
package main

import "github.com/fenny/fiber"

func main() {
	app := fiber.New()
	app.Get("/", func(c *fiber.Ctx) {
		c.Send("Hello, World!")
	})
	app.Listen(8080)
}
$ go run server.go

Browse to http://localhost:8080 and you should see Hello, World! on the page.

Basic routing

Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).

Each route can have one handler function, that is executed when the route is matched.

Route definition takes the following structures:

// Function signature
app.Method(path string, static string)
app.Method(path string, func(*fiber.Ctx))
app.Method(static string)
app.Method(func(*fiber.Ctx))
  • app is an instance of Fiber.
  • Method is an HTTP request method, in capitalization: Get, Put, Post etc
  • path string is a path or prefix (for static files) on the server.
  • static string is a file path or directory.
  • *func(fiber.Ctx) is a function executed when the route is matched.

This tutorial assumes that an instance of fiber named app is created and the server is running. If you are not familiar with creating an app and starting it, see the Hello world example.

The following examples illustrate defining simple routes.

// Respond with Hello, World! on the homepage:
app.Get("/", func(c *fiber.Ctx) {
  c.Send("Hello, World!")
})

//Respond to POST request on the root route (/), the application’s home page:
app.Post("/", func(c *fiber.Ctx) {
  c.Send("Got a POST request")
})

// Respond to a PUT request to the /user route:
app.Put("/user", func(c *fiber.Ctx) {
  c.Send("Got a PUT request at /user")
})

// Respond to a DELETE request to the /user route:
app.Delete("/user", func(c *fiber.Ctx) {
  c.Send("Got a DELETE request at /user")
})

Static files

To serve static files such as images, CSS files, and JavaScript files, replace your function handler with a file or directory string.

// Function signature
app.Method(static string)
app.Method(path string, static string)

For example, use the following code to serve images, CSS files, and JavaScript files in a directory named public:

app.Get("./public")

Now, you can load the files that are in the public directory:

http://localhost:8080/images/kitten.jpg
http://localhost:8080/css/style.css
http://localhost:8080/js/app.js
http://localhost:8080/images/bg.png
http://localhost:8080/hello.html

To use multiple static assets directories, call the express.static middleware function multiple times:

app.Get("./public")
app.Get("./files")

?>For best results, use a reverse proxy cache like NGINX to improve performance of serving static assets.

To create a virtual path prefix (where the path does not actually exist in the file system) for files that are served by the express.static function, specify a mount path for the static directory, as shown below:

app.Get("/static", "./public")

Now, you can load the files that are in the public directory from the /static path prefix.

http://localhost:8080/static/images/kitten.jpg
http://localhost:8080/static/css/style.css
http://localhost:8080/static/js/app.js
http://localhost:8080/static/images/bg.png
http://localhost:8080/static/hello.html

Caught a mistake? Edit this page on GitHub!

Documentation

Index

Constants

View Source
const (
	Version = `v0.4.0`
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Ctx

type Ctx struct {
	Fasthttp *fasthttp.RequestCtx
	// contains filtered or unexported fields
}

Context struct

func (*Ctx) Attachment

func (ctx *Ctx) Attachment(args ...interface{})

Attachment :

func (*Ctx) BasicAuth

func (ctx *Ctx) BasicAuth() (user, pass string, ok bool)

BasicAuth :

func (*Ctx) Body

func (ctx *Ctx) Body(args ...interface{}) string

Body :

func (*Ctx) ClearCookies

func (ctx *Ctx) ClearCookies(args ...interface{})

ClearCookies :

func (*Ctx) Cookies

func (ctx *Ctx) Cookies(args ...interface{}) string

Cookies :

func (*Ctx) Download

func (ctx *Ctx) Download(args ...interface{})

Download :

func (*Ctx) FormFile

func (ctx *Ctx) FormFile(key string) (*multipart.FileHeader, error)

FormFile :

func (*Ctx) FormValue

func (ctx *Ctx) FormValue(key string) string

FormValue :

func (*Ctx) Get

func (ctx *Ctx) Get(key string) string

Get :

func (*Ctx) Hostname

func (ctx *Ctx) Hostname() string

Hostname :

func (*Ctx) IP

func (ctx *Ctx) IP() string

IP :

func (*Ctx) Is

func (ctx *Ctx) Is(ext string) bool

Is :

func (*Ctx) Json

func (ctx *Ctx) Json(v interface{}) error

Json :

func (*Ctx) Location

func (ctx *Ctx) Location(path string)

Location :

func (*Ctx) Method

func (ctx *Ctx) Method() string

Method :

func (*Ctx) MultipartForm

func (ctx *Ctx) MultipartForm() (*multipart.Form, error)

MultipartForm :

func (*Ctx) Next

func (ctx *Ctx) Next()

Next :

func (*Ctx) OriginalURL

func (ctx *Ctx) OriginalURL() string

OriginalURL :

func (*Ctx) Params

func (ctx *Ctx) Params(key string) string

Params :

func (*Ctx) Path

func (ctx *Ctx) Path() string

Path :

func (*Ctx) Protocol

func (ctx *Ctx) Protocol() string

Protocol :

func (*Ctx) Query

func (ctx *Ctx) Query(key string) string

Query :

func (*Ctx) Redirect

func (ctx *Ctx) Redirect(args ...interface{})

Redirect :

func (*Ctx) SaveFile

func (ctx *Ctx) SaveFile(fh *multipart.FileHeader, path string)

SaveFile :

func (*Ctx) Secure

func (ctx *Ctx) Secure() bool

Secure :

func (*Ctx) Send

func (ctx *Ctx) Send(args ...interface{})

Send :

func (*Ctx) SendFile

func (ctx *Ctx) SendFile(file string)

SendFile :

func (*Ctx) Set

func (ctx *Ctx) Set(key string, val string)

Set :

func (*Ctx) Status

func (ctx *Ctx) Status(status int) *Ctx

Status :

func (*Ctx) Type

func (ctx *Ctx) Type(ext string) *Ctx

Type :

func (*Ctx) Write

func (ctx *Ctx) Write(args ...interface{})

Write :

func (*Ctx) Xhr

func (ctx *Ctx) Xhr() bool

Xhr :

type Fiber

type Fiber struct {
	Settings *Settings
	// contains filtered or unexported fields
}

Fiber :

func New

func New() *Fiber

New :

func (*Fiber) All

func (r *Fiber) All(args ...interface{})

All :

func (*Fiber) Connect

func (r *Fiber) Connect(args ...interface{})

Connect :

func (*Fiber) Delete

func (r *Fiber) Delete(args ...interface{})

Delete :

func (*Fiber) Get

func (r *Fiber) Get(args ...interface{})

Get :

func (*Fiber) Head

func (r *Fiber) Head(args ...interface{})

Head :

func (*Fiber) Listen

func (r *Fiber) Listen(args ...interface{})

Listen :

func (*Fiber) Options

func (r *Fiber) Options(args ...interface{})

Options :

func (*Fiber) Patch

func (r *Fiber) Patch(args ...interface{})

Patch :

func (*Fiber) Post

func (r *Fiber) Post(args ...interface{})

Post :

func (*Fiber) Put

func (r *Fiber) Put(args ...interface{})

Put :

func (*Fiber) Trace

func (r *Fiber) Trace(args ...interface{})

Trace :

func (*Fiber) Use

func (r *Fiber) Use(args ...interface{})

Use :

type Settings

type Settings struct {
	Name                               string
	ClearTerminal                      bool
	HideBanner                         bool
	TLSEnable                          bool
	CertKey                            string
	CertFile                           string
	Concurrency                        int
	DisableKeepAlive                   bool
	ReadBufferSize                     int
	WriteBufferSize                    int
	ReadTimeout                        time.Duration
	WriteTimeout                       time.Duration
	IdleTimeout                        time.Duration
	MaxConnsPerIP                      int
	MaxRequestsPerConn                 int
	TCPKeepalive                       bool
	TCPKeepalivePeriod                 time.Duration
	MaxRequestBodySize                 int
	ReduceMemoryUsage                  bool
	GetOnly                            bool
	DisableHeaderNamesNormalizing      bool
	SleepWhenConcurrencyLimitsExceeded time.Duration
	NoDefaultServerHeader              bool
	NoDefaultContentType               bool
	KeepHijackedConns                  bool
}

Settings :

Jump to

Keyboard shortcuts

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