fiber

package module
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2020 License: MIT Imports: 22 Imported by: 1,154

README

🚀 Fiber ru ch

GitHub license

Fiber logo

Fiber is an Express.js styled HTTP web framework implementation running on Fasthttp, the fastest HTTP engine for Go (Golang). The package make use of similar framework convention as they are in Express.

People switching from Node.js to Go often end up in a bad learning curve to start building their webapps, this project is meant to ease things up for fast development, but with zero memory allocation and performance in mind.

API Documentation

📚 We created an extended API documentation (including examples), Visit fiber.wiki.

Benchmark

👉 Click here to see all benchmark results.

Features

  • Optimized for speed and low memory usage
  • Rapid Server-Side Programming
  • Easy routing with parameters
  • Static files with custom prefix
  • Middleware with Next support
  • Express API endpoints
  • Extended documentation

Installing

Assuming you’ve already installed Go 1.11+ 😉

Install the Fiber package by calling the following command:

go get -u github.com/gofiber/fiber

Hello, world!

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

// server.go

package main

import "github.com/gofiber/fiber"

func main() {
  // Create new Fiber instance
  app := fiber.New()

  // Create new route with GET method
  app.Get("/", func(c *fiber.Ctx) {
    c.Send("Hello, World!")
  })

  // Start server on http://localhost:8080
  app.Listen(8080)
}

Go to console and run:

go run server.go

And now, browse to http://localhost:8080 and you should see Hello, World! on the page! 🎉

Static files

To serve static files, use the Static method:

package main

import "github.com/gofiber/fiber"

func main() {
  // Create new Fiber instance
  app := fiber.New()

  // Serve all static files on ./public folder
  app.Static("./public")

  // Start server on http://localhost:8080
  app.Listen(8080)
}

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

http://localhost:8080/hello.html
http://localhost:8080/js/script.js
http://localhost:8080/css/style.css

Middleware

Middleware has never been so easy! Just like Express you call the Next() matching route function:

package main

import "github.com/gofiber/fiber"

func main() {
  // Create new Fiber instance
  app := fiber.New()

  // Define all used middlewares in Use()

  app.Use(func(c *fiber.Ctx) {
    c.Write("Match anything!\n")
    c.Next()
  })

  app.Use("/api", func(c *fiber.Ctx) {
    c.Write("Match starting with /api\n")
    c.Next()
  })

  app.Get("/api/user", func(c *fiber.Ctx) {
    c.Write("Match exact path /api/user\n")
  })

  // Start server on http://localhost:8080
  app.Listen(8080)
}

Project assistance

If you want to say thank you or/and support active development gofiber/fiber:

  1. Add a GitHub Star to project.
  2. Tweet about project on your Twitter.
  3. Help us to translate this README and API Docs to another language.

Thanks for your support! 😘 Together, we make Fiber.

Stars over time

Stars over time

License

⚠️ Please note: gofiber/fiber is free and open-source software licensed under the MIT License.

Documentation

Index

Constants

View Source
const (
	// Version : Fiber version
	Version = "1.3.3"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cookie struct {
	Expire int // time.Unix(1578981376, 0)
	MaxAge int
	Domain string
	Path   string

	HTTPOnly bool
	Secure   bool
	SameSite string
}

Cookie : struct

type Ctx

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

Ctx : struct

func (*Ctx) Accepts

func (ctx *Ctx) Accepts(offers ...string) string

Accepts : https://gofiber.github.io/fiber/#/context?id=accepts

func (*Ctx) AcceptsCharsets

func (ctx *Ctx) AcceptsCharsets(offers ...string) string

AcceptsCharsets : https://gofiber.github.io/fiber/#/context?id=acceptscharsets

func (*Ctx) AcceptsEncodings

func (ctx *Ctx) AcceptsEncodings(offers ...string) string

AcceptsEncodings : https://gofiber.github.io/fiber/#/context?id=acceptsencodings

func (*Ctx) AcceptsLanguages

func (ctx *Ctx) AcceptsLanguages(offers ...string) string

AcceptsLanguages : https://gofiber.github.io/fiber/#/context?id=acceptslanguages

func (*Ctx) Append

func (ctx *Ctx) Append(field string, values ...string)

Append : https://gofiber.github.io/fiber/#/context?id=append

func (*Ctx) Attachment

func (ctx *Ctx) Attachment(name ...string)

Attachment : https://gofiber.github.io/fiber/#/context?id=attachment

func (*Ctx) BaseURL added in v1.0.0

func (ctx *Ctx) BaseURL() string

BaseURL : https://gofiber.github.io/fiber/#/context?id=baseurl

func (*Ctx) BasicAuth

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

BasicAuth : https://gofiber.github.io/fiber/#/context?id=basicauth

func (*Ctx) Body

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

Body : https://gofiber.github.io/fiber/#/context?id=body

func (*Ctx) ClearCookie

func (ctx *Ctx) ClearCookie(name ...string)

ClearCookie : https://gofiber.github.io/fiber/#/context?id=clearcookie

func (*Ctx) Cookie

func (ctx *Ctx) Cookie(key, value string, options ...interface{})

Cookie : https://gofiber.github.io/fiber/#/context?id=cookie

func (*Ctx) Cookies

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

Cookies : https://gofiber.github.io/fiber/#/context?id=cookies

func (*Ctx) Download

func (ctx *Ctx) Download(file string, name ...string)

Download : https://gofiber.github.io/fiber/#/context?id=download

func (*Ctx) End

func (ctx *Ctx) End()

End : https://gofiber.github.io/fiber/#/context?id=end

func (*Ctx) FormValue

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

FormValue : https://gofiber.github.io/fiber/#/context?id=formvalue

func (*Ctx) Format

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

Format : https://gofiber.github.io/fiber/#/context?id=format

func (*Ctx) Fresh

func (ctx *Ctx) Fresh() bool

Fresh : https://gofiber.github.io/fiber/#/context?id=fresh

func (*Ctx) Get

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

Get : https://gofiber.github.io/fiber/#/context?id=get

func (*Ctx) HeadersSent

func (ctx *Ctx) HeadersSent()

HeadersSent : https://gofiber.github.io/fiber/#/context?id=headerssent

func (*Ctx) Hostname

func (ctx *Ctx) Hostname() string

Hostname : https://gofiber.github.io/fiber/#/context?id=hostname

func (*Ctx) IP added in v1.0.0

func (ctx *Ctx) IP() string

IP : https://gofiber.github.io/fiber/#/context?id=Ip

func (*Ctx) IPs added in v1.0.0

func (ctx *Ctx) IPs() []string

IPs : https://gofiber.github.io/fiber/#/context?id=ips

func (*Ctx) Ip

func (ctx *Ctx) Ip() string

Ip is deprecated, this will be removed in v2: Use c.IP() instead

func (*Ctx) Ips

func (ctx *Ctx) Ips() []string

Ips is deprecated, this will be removed in v2: Use c.IPs() instead

func (*Ctx) Is

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

Is : https://gofiber.github.io/fiber/#/context?id=is

func (*Ctx) JSON added in v1.0.0

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

JSON : https://gofiber.github.io/fiber/#/context?id=json

func (*Ctx) JSONBytes added in v1.3.1

func (ctx *Ctx) JSONBytes(raw []byte)

JSONBytes : https://gofiber.github.io/fiber/#/context?id=jsonbytes

func (*Ctx) JSONP added in v1.0.0

func (ctx *Ctx) JSONP(v interface{}, cb ...string) error

JSONP : https://gofiber.github.io/fiber/#/context?id=jsonp

func (*Ctx) JSONString added in v1.3.1

func (ctx *Ctx) JSONString(raw string)

JSONString : https://gofiber.github.io/fiber/#/context?id=jsonstring

func (*Ctx) Json

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

Json is deprecated, this will be removed in v2: Use c.JSON() instead

func (*Ctx) JsonBytes added in v1.2.3

func (ctx *Ctx) JsonBytes(raw []byte)

JsonBytes is deprecated, this will be removed in v2: Use c.JSONBytes() instead

func (*Ctx) JsonString added in v1.2.3

func (ctx *Ctx) JsonString(raw string)

JsonString is deprecated, this will be removed in v2: Use c.JSONString() instead

func (*Ctx) Jsonp

func (ctx *Ctx) Jsonp(v interface{}, cb ...string) error

Jsonp is deprecated, this will be removed in v2: Use c.JSONP() instead

func (ctx *Ctx) Links(link ...string)

Links : https://gofiber.github.io/fiber/#/context?id=links

func (*Ctx) Locals

func (ctx *Ctx) Locals(key string, val ...interface{}) interface{}

Locals : https://gofiber.github.io/fiber/#/context?id=locals

func (*Ctx) Location

func (ctx *Ctx) Location(path string)

Location : https://gofiber.github.io/fiber/#/context?id=location

func (*Ctx) Method

func (ctx *Ctx) Method() string

Method : https://gofiber.github.io/fiber/#/context?id=method

func (*Ctx) MultipartForm

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

MultipartForm : https://gofiber.github.io/fiber/#/context?id=multipartform

func (*Ctx) Next

func (ctx *Ctx) Next()

Next : https://gofiber.github.io/fiber/#/context?id=next

func (*Ctx) OriginalURL added in v1.0.0

func (ctx *Ctx) OriginalURL() string

OriginalURL : https://gofiber.github.io/fiber/#/context?id=originalurl

func (*Ctx) OriginalUrl

func (ctx *Ctx) OriginalUrl() string

OriginalUrl is deprecated, this will be removed in v2: Use c.OriginalURL() instead

func (*Ctx) Params

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

Params : https://gofiber.github.io/fiber/#/context?id=params

func (*Ctx) Path

func (ctx *Ctx) Path() string

Path : https://gofiber.github.io/fiber/#/context?id=path

func (*Ctx) Protocol

func (ctx *Ctx) Protocol() string

Protocol : https://gofiber.github.io/fiber/#/context?id=protocol

func (*Ctx) Query

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

Query : https://gofiber.github.io/fiber/#/context?id=query

func (*Ctx) Range

func (ctx *Ctx) Range()

Range : https://gofiber.github.io/fiber/#/context?id=range

func (*Ctx) Redirect

func (ctx *Ctx) Redirect(path string, status ...int)

Redirect : https://gofiber.github.io/fiber/#/context?id=redirect

func (*Ctx) Render

func (ctx *Ctx) Render()

Render : https://gofiber.github.io/fiber/#/context?id=render

func (*Ctx) Route

func (ctx *Ctx) Route() *Route

Route : https://gofiber.github.io/fiber/#/context?id=route

func (*Ctx) SaveFile added in v1.0.0

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

SaveFile : https://gofiber.github.io/fiber/#/context?id=secure

func (*Ctx) Secure

func (ctx *Ctx) Secure() bool

Secure : https://gofiber.github.io/fiber/#/context?id=secure

func (*Ctx) Send

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

Send : https://gofiber.github.io/fiber/#/context?id=send

func (*Ctx) SendBytes

func (ctx *Ctx) SendBytes(body []byte)

SendBytes : https://gofiber.github.io/fiber/#/context?id=sendbytes

func (*Ctx) SendFile

func (ctx *Ctx) SendFile(file string, gzip ...bool)

SendFile : https://gofiber.github.io/fiber/#/context?id=sendfile

func (*Ctx) SendStatus

func (ctx *Ctx) SendStatus(status int)

SendStatus : https://gofiber.github.io/fiber/#/context?id=sendstatus

func (*Ctx) SendString

func (ctx *Ctx) SendString(body string)

SendString : https://gofiber.github.io/fiber/#/context?id=sendstring

func (*Ctx) Set

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

Set : https://gofiber.github.io/fiber/#/context?id=set

func (*Ctx) SignedCookies

func (ctx *Ctx) SignedCookies()

SignedCookies : https://gofiber.github.io/fiber/#/context?id=signedcookies

func (*Ctx) Stale

func (ctx *Ctx) Stale() bool

Stale : https://gofiber.github.io/fiber/#/context?id=stale

func (*Ctx) Status

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

Status : https://gofiber.github.io/fiber/#/context?id=status

func (*Ctx) Subdomains

func (ctx *Ctx) Subdomains() (subs []string)

Subdomains : https://gofiber.github.io/fiber/#/context?id=subdomains

func (*Ctx) Type

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

Type : https://gofiber.github.io/fiber/#/context?id=type

func (*Ctx) Vary

func (ctx *Ctx) Vary(fields ...string)

Vary : https://gofiber.github.io/fiber/#/context?id=vary

func (*Ctx) Write

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

Write : https://gofiber.github.io/fiber/#/context?id=write

func (*Ctx) XHR added in v1.0.0

func (ctx *Ctx) XHR() bool

XHR : https://gofiber.github.io/fiber/#/context?id=xhr

func (*Ctx) XML added in v1.3.1

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

XML : https://gofiber.github.io/fiber/#/context?id=xml

func (*Ctx) Xhr

func (ctx *Ctx) Xhr() bool

Xhr is deprecated, this will be removed in v2: Use c.XHR() instead

func (*Ctx) Xml added in v1.1.0

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

Xml is deprecated, this will be removed in v2: Use c.XML() instead

type Fiber

type Fiber struct {
	// Server name header
	Server string
	// Show fiber banner
	Banner bool
	// https://github.com/valyala/fasthttp/blob/master/server.go#L150
	Engine *engine
	// https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/
	Prefork bool
	// contains filtered or unexported fields
}

Fiber structure

func New

func New() *Fiber

New creates a Fiber instance

func (*Fiber) All

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

All matches any HTTP method

func (*Fiber) Connect

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

Connect establishes a tunnel to the server identified by the target resource.

func (*Fiber) Delete

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

Delete deletes the specified resource.

func (*Fiber) Get

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

Get requests a representation of the specified resource. Requests using GET should only retrieve data.

func (*Fiber) Head

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

Head asks for a response identical to that of a GET request, but without the response body.

func (*Fiber) Listen

func (r *Fiber) Listen(address interface{}, tls ...string)

Listen : https://gofiber.github.io/fiber/#/application?id=listen

func (*Fiber) Options

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

Options is used to describe the communication options for the target resource.

func (*Fiber) Patch

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

Patch is used to apply partial modifications to a resource.

func (*Fiber) Post

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

Post is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.

func (*Fiber) Put

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

Put replaces all current representations of the target resource with the request payload.

func (*Fiber) Static

func (r *Fiber) Static(args ...string)

Static https://gofiber.github.io/fiber/#/application?id=static

func (*Fiber) Trace

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

Trace performs a message loop-back test along the path to the target resource.

func (*Fiber) Use

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

Use only matches the starting path

type Route added in v1.0.0

type Route struct {
	// HTTP method in uppercase, can be a * for Use() & All()
	Method string
	// Stores the original path
	Path string
	// Bool that defines if the route is a Use() middleware
	Midware bool
	// wildcard bool is for routes without a path, * and /*
	Wildcard bool
	// Stores compiled regex special routes :params, *wildcards, optionals?
	Regex *regexp.Regexp
	// Store params if special routes :params, *wildcards, optionals?
	Params []string
	// Callback function for specific route
	Handler func(*Ctx)
}

Route : struct

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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