1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-12 03:01:21 +00:00
fiber/application.go

496 lines
14 KiB
Go
Raw Normal View History

2020-02-22 17:03:30 -05:00
// 🚀 Fiber is an Express inspired web framework written in Go with 💖
// 📌 API Documentation: https://fiber.wiki
// 📝 Github Repository: https://github.com/gofiber/fiber
2020-02-21 18:07:43 +01:00
package fiber
import (
"bufio"
2020-03-04 12:30:29 +01:00
"crypto/tls"
2020-02-21 18:07:43 +01:00
"fmt"
2020-02-27 04:10:26 -05:00
"log"
2020-02-21 18:07:43 +01:00
"net"
"net/http"
"net/http/httputil"
"os"
"os/exec"
2020-02-27 04:10:26 -05:00
"reflect"
2020-02-21 18:07:43 +01:00
"runtime"
"strconv"
"strings"
"time"
fasthttp "github.com/valyala/fasthttp"
)
// Version of Fiber
2020-03-23 21:41:31 +01:00
const Version = "1.8.42"
2020-02-21 18:07:43 +01:00
type (
// App denotes the Fiber application.
App struct {
2020-03-04 12:30:29 +01:00
server *fasthttp.Server // Fasthttp server settings
routes []*Route // Route stack
child bool // If current process is a child ( for prefork )
Settings *Settings // Fiber settings
2020-02-21 18:07:43 +01:00
}
// Map defines a generic map of type `map[string]interface{}`.
Map map[string]interface{}
// Settings is a struct holding the server settings
Settings struct {
2020-03-04 12:30:29 +01:00
// This will spawn multiple Go processes listening on the same port
2020-03-23 21:52:37 +01:00
Prefork bool // Default: false / Spawns Multiple Go processes listening on the same port`default:"false"`
2020-03-04 12:30:29 +01:00
// Enable strict routing. When enabled, the router treats "/foo" and "/foo/" as different.
2020-02-21 18:07:43 +01:00
StrictRouting bool `default:"false"`
2020-03-04 12:30:29 +01:00
// Enable case sensitivity. When enabled, "/Foo" and "/foo" are different routes.
2020-02-21 18:07:43 +01:00
CaseSensitive bool `default:"false"`
// Enables the "Server: value" HTTP header.
ServerHeader string `default:""`
2020-02-26 19:31:43 -05:00
// Enables handler values to be immutable even if you return from handler
Immutable bool `default:"false"`
2020-03-04 12:30:29 +01:00
// Max body size that the server accepts
2020-03-01 06:56:41 +01:00
BodyLimit int `default:"4 * 1024 * 1024"`
2020-03-04 12:30:29 +01:00
// Folder containing template files
TemplateFolder string `default:""`
// Template engine: html, amber, handlebars , mustache or pug
2020-03-23 05:25:46 +01:00
TemplateEngine func(raw string, bind interface{}) (string, error) `default:"nil"`
2020-03-04 12:30:29 +01:00
// Extension for the template files
2020-02-27 05:12:41 -05:00
TemplateExtension string `default:""`
2020-03-23 21:41:24 +01:00
// The amount of time allowed to read the full request including body.
2020-03-23 05:25:46 +01:00
// The connection's read deadline is reset when the connection opens, or for
// keep-alive connections after the first byte has been read.
// Default timeout is unlimited.
ReadTimeout time.Duration
2020-03-23 21:41:24 +01:00
// The maximum duration before timing out writes of the response.
2020-03-23 05:25:46 +01:00
// It is reset after the request handler has returned.
// Default timeout is unlimited.
WriteTimeout time.Duration
// The maximum amount of time to wait for the next request when keep-alive is enabled.
// If IdleTimeout is zero, the value of ReadTimeout is used.
IdleTimeout time.Duration
2020-02-21 18:07:43 +01:00
}
)
// Group struct
type Group struct {
prefix string
app *App
}
2020-03-16 17:18:25 +01:00
// This method creates a new Fiber named instance.
// You can pass optional settings when creating a new instance.
2020-03-04 12:30:29 +01:00
func New(settings ...*Settings) *App {
var prefork, child bool
// Loop trought args without using flag.Parse()
2020-03-10 14:32:21 +08:00
for _, v := range os.Args[1:] {
if v == "-prefork" {
2020-02-26 19:31:43 -05:00
prefork = true
2020-03-10 14:32:21 +08:00
} else if v == "-child" {
2020-02-26 19:31:43 -05:00
child = true
}
}
2020-03-04 12:30:29 +01:00
// Create default app
app := &App{
2020-02-26 19:31:43 -05:00
child: child,
2020-03-04 12:30:29 +01:00
Settings: &Settings{
Prefork: prefork,
BodyLimit: 4 * 1024 * 1024,
},
2020-02-21 18:07:43 +01:00
}
2020-03-04 12:30:29 +01:00
// If settings exist, set some defaults
2020-02-21 18:07:43 +01:00
if len(settings) > 0 {
2020-03-14 12:30:21 +01:00
app.Settings = settings[0] // Set custom settings
if !app.Settings.Prefork { // Default to -prefork flag if false
app.Settings.Prefork = prefork
2020-02-26 19:31:43 -05:00
}
2020-03-14 12:30:21 +01:00
if app.Settings.BodyLimit == 0 { // Default MaxRequestBodySize
app.Settings.BodyLimit = 4 * 1024 * 1024
2020-02-21 18:07:43 +01:00
}
2020-03-14 12:30:21 +01:00
if app.Settings.Immutable { // Replace unsafe conversion funcs
2020-03-04 12:30:29 +01:00
getString = func(b []byte) string { return string(b) }
getBytes = func(s string) []byte { return []byte(s) }
2020-02-21 18:07:43 +01:00
}
2020-03-14 12:30:21 +01:00
}
2020-03-04 12:30:29 +01:00
return app
2020-02-21 18:07:43 +01:00
}
2020-03-16 17:18:25 +01:00
// You can group routes by creating a *Group struct.
2020-02-27 04:10:26 -05:00
func (app *App) Group(prefix string, handlers ...func(*Ctx)) *Group {
if len(handlers) > 0 {
2020-03-03 12:21:34 -05:00
app.registerMethod("USE", prefix, handlers...)
2020-02-27 04:10:26 -05:00
}
return &Group{
prefix: prefix,
app: app,
}
2020-02-21 22:36:26 -05:00
}
2020-03-16 17:18:25 +01:00
// Settings struct for serving static files
2020-03-15 14:45:57 +01:00
type Static struct {
2020-03-15 14:00:03 +01:00
// Transparently compresses responses if set to true
// This works differently than the github.com/gofiber/compression middleware
// The server tries minimizing CPU usage by caching compressed files.
// It adds ".fiber.gz" suffix to the original file name.
// Optional. Default value false
Compress bool
// Enables byte range requests if set to true.
// Optional. Default value false
ByteRange bool
// Enable directory browsing.
// Optional. Default value false.
Browse bool
// Index file for serving a directory.
// Optional. Default value "index.html".
Index string
}
2020-03-16 17:18:25 +01:00
// Serve static files such as images, CSS and JavaScript files, you can use the Static method.
2020-03-15 14:45:57 +01:00
func (app *App) Static(prefix, root string, config ...Static) *App {
2020-03-15 14:00:03 +01:00
app.registerStatic(prefix, root, config...)
2020-02-21 18:07:43 +01:00
return app
}
2020-03-16 17:18:25 +01:00
// Use only match requests starting with the specified prefix
// It's optional to provide a prefix, default: "/"
2020-02-27 04:10:26 -05:00
func (app *App) Use(args ...interface{}) *App {
var path = ""
var handlers []func(*Ctx)
for i := 0; i < len(args); i++ {
switch arg := args[i].(type) {
case string:
path = arg
case func(*Ctx):
handlers = append(handlers, arg)
default:
log.Fatalf("Invalid handler: %v", reflect.TypeOf(arg))
}
}
2020-03-03 12:21:34 -05:00
app.registerMethod("USE", path, handlers...)
2020-02-21 18:07:43 +01:00
return app
}
2020-02-26 19:31:43 -05:00
// Connect : https://fiber.wiki/application#http-methods
2020-02-27 04:10:26 -05:00
func (app *App) Connect(path string, handlers ...func(*Ctx)) *App {
2020-03-20 16:49:46 +01:00
app.registerMethod(MethodConnect, path, handlers...)
2020-02-21 18:07:43 +01:00
return app
}
2020-02-26 19:31:43 -05:00
// Put : https://fiber.wiki/application#http-methods
2020-02-27 04:10:26 -05:00
func (app *App) Put(path string, handlers ...func(*Ctx)) *App {
2020-03-20 16:49:46 +01:00
app.registerMethod(MethodPut, path, handlers...)
2020-02-21 18:07:43 +01:00
return app
}
2020-02-26 19:31:43 -05:00
// Post : https://fiber.wiki/application#http-methods
2020-02-27 04:10:26 -05:00
func (app *App) Post(path string, handlers ...func(*Ctx)) *App {
2020-03-20 16:49:46 +01:00
app.registerMethod(MethodPost, path, handlers...)
2020-02-21 18:07:43 +01:00
return app
}
2020-02-26 19:31:43 -05:00
// Delete : https://fiber.wiki/application#http-methods
2020-02-27 04:10:26 -05:00
func (app *App) Delete(path string, handlers ...func(*Ctx)) *App {
2020-03-20 16:49:46 +01:00
app.registerMethod(MethodDelete, path, handlers...)
2020-02-21 18:07:43 +01:00
return app
}
2020-02-26 19:31:43 -05:00
// Head : https://fiber.wiki/application#http-methods
2020-02-27 04:10:26 -05:00
func (app *App) Head(path string, handlers ...func(*Ctx)) *App {
2020-03-20 16:49:46 +01:00
app.registerMethod(MethodHead, path, handlers...)
2020-02-21 18:07:43 +01:00
return app
}
2020-02-26 19:31:43 -05:00
// Patch : https://fiber.wiki/application#http-methods
2020-02-27 04:10:26 -05:00
func (app *App) Patch(path string, handlers ...func(*Ctx)) *App {
2020-03-20 16:49:46 +01:00
app.registerMethod(MethodPatch, path, handlers...)
2020-02-21 18:07:43 +01:00
return app
}
2020-02-26 19:31:43 -05:00
// Options : https://fiber.wiki/application#http-methods
2020-02-27 04:10:26 -05:00
func (app *App) Options(path string, handlers ...func(*Ctx)) *App {
2020-03-20 16:49:46 +01:00
app.registerMethod(MethodOptions, path, handlers...)
2020-02-21 18:07:43 +01:00
return app
}
2020-02-26 19:31:43 -05:00
// Trace : https://fiber.wiki/application#http-methods
2020-02-27 04:10:26 -05:00
func (app *App) Trace(path string, handlers ...func(*Ctx)) *App {
2020-03-20 16:49:46 +01:00
app.registerMethod(MethodTrace, path, handlers...)
2020-02-21 18:07:43 +01:00
return app
}
2020-02-26 19:31:43 -05:00
// Get : https://fiber.wiki/application#http-methods
2020-02-27 04:10:26 -05:00
func (app *App) Get(path string, handlers ...func(*Ctx)) *App {
2020-03-20 16:49:46 +01:00
app.registerMethod(MethodGet, path, handlers...)
2020-02-21 18:07:43 +01:00
return app
}
2020-03-16 17:18:25 +01:00
// All matches all HTTP methods and complete paths
2020-02-27 04:10:26 -05:00
func (app *App) All(path string, handlers ...func(*Ctx)) *App {
2020-03-03 12:21:34 -05:00
app.registerMethod("ALL", path, handlers...)
2020-02-21 18:07:43 +01:00
return app
}
// Group : https://fiber.wiki/application#group
func (grp *Group) Group(prefix string, handlers ...func(*Ctx)) *Group {
prefix = groupPaths(grp.prefix, prefix)
if len(handlers) > 0 {
grp.app.registerMethod("USE", prefix, handlers...)
}
return &Group{
prefix: prefix,
app: grp.app,
}
}
// Static : https://fiber.wiki/application#static
func (grp *Group) Static(prefix, root string, config ...Static) *Group {
prefix = groupPaths(grp.prefix, prefix)
grp.app.registerStatic(prefix, root, config...)
return grp
}
// Use only match requests starting with the specified prefix
// It's optional to provide a prefix, default: "/"
func (grp *Group) Use(args ...interface{}) *Group {
var path = ""
var handlers []func(*Ctx)
for i := 0; i < len(args); i++ {
switch arg := args[i].(type) {
case string:
path = arg
case func(*Ctx):
handlers = append(handlers, arg)
default:
log.Fatalf("Invalid Use() arguments, must be (prefix, handler) or (handler)")
}
}
grp.app.registerMethod("USE", groupPaths(grp.prefix, path), handlers...)
return grp
}
// Connect : https://fiber.wiki/application#http-methods
func (grp *Group) Connect(path string, handlers ...func(*Ctx)) *Group {
grp.app.registerMethod(MethodConnect, groupPaths(grp.prefix, path), handlers...)
return grp
}
// Put : https://fiber.wiki/application#http-methods
func (grp *Group) Put(path string, handlers ...func(*Ctx)) *Group {
grp.app.registerMethod(MethodPut, groupPaths(grp.prefix, path), handlers...)
return grp
}
// Post : https://fiber.wiki/application#http-methods
func (grp *Group) Post(path string, handlers ...func(*Ctx)) *Group {
grp.app.registerMethod(MethodPost, groupPaths(grp.prefix, path), handlers...)
return grp
}
// Delete : https://fiber.wiki/application#http-methods
func (grp *Group) Delete(path string, handlers ...func(*Ctx)) *Group {
grp.app.registerMethod(MethodDelete, groupPaths(grp.prefix, path), handlers...)
return grp
}
// Head : https://fiber.wiki/application#http-methods
func (grp *Group) Head(path string, handlers ...func(*Ctx)) *Group {
grp.app.registerMethod(MethodHead, groupPaths(grp.prefix, path), handlers...)
return grp
}
// Patch : https://fiber.wiki/application#http-methods
func (grp *Group) Patch(path string, handlers ...func(*Ctx)) *Group {
grp.app.registerMethod(MethodPatch, groupPaths(grp.prefix, path), handlers...)
return grp
}
// Options : https://fiber.wiki/application#http-methods
func (grp *Group) Options(path string, handlers ...func(*Ctx)) *Group {
grp.app.registerMethod(MethodOptions, groupPaths(grp.prefix, path), handlers...)
return grp
}
// Trace : https://fiber.wiki/application#http-methods
func (grp *Group) Trace(path string, handlers ...func(*Ctx)) *Group {
grp.app.registerMethod(MethodTrace, groupPaths(grp.prefix, path), handlers...)
return grp
}
// Get : https://fiber.wiki/application#http-methods
func (grp *Group) Get(path string, handlers ...func(*Ctx)) *Group {
grp.app.registerMethod(MethodGet, groupPaths(grp.prefix, path), handlers...)
return grp
}
// All matches all HTTP methods and complete paths
func (grp *Group) All(path string, handlers ...func(*Ctx)) *Group {
grp.app.registerMethod("ALL", groupPaths(grp.prefix, path), handlers...)
return grp
}
2020-02-21 18:07:43 +01:00
// Listen : https://fiber.wiki/application#listen
2020-03-04 12:30:29 +01:00
func (app *App) Listen(address interface{}, tlsconfig ...*tls.Config) error {
2020-02-21 18:07:43 +01:00
addr, ok := address.(string)
if !ok {
port, ok := address.(int)
if !ok {
return fmt.Errorf("Listen: Host must be an INT port or STRING address")
}
addr = strconv.Itoa(port)
}
if !strings.Contains(addr, ":") {
addr = ":" + addr
}
// Create fasthttp server
app.server = app.newServer()
2020-02-26 19:31:43 -05:00
// Print listening message
if !app.child {
fmt.Printf("Fiber v%s listening on %s\n", Version, addr)
}
2020-02-21 18:07:43 +01:00
var ln net.Listener
var err error
// Prefork enabled
if app.Settings.Prefork && runtime.NumCPU() > 1 {
if ln, err = app.prefork(addr); err != nil {
return err
}
} else {
if ln, err = net.Listen("tcp4", addr); err != nil {
return err
}
}
2020-03-04 12:30:29 +01:00
// TLS config
if len(tlsconfig) > 0 {
ln = tls.NewListener(ln, tlsconfig[0])
2020-02-21 18:07:43 +01:00
}
return app.server.Serve(ln)
}
2020-02-26 19:31:43 -05:00
// Shutdown : TODO: Docs
// Shutsdown the server gracefully
2020-02-21 18:07:43 +01:00
func (app *App) Shutdown() error {
if app.server == nil {
return fmt.Errorf("Server is not running")
}
return app.server.Shutdown()
}
2020-02-26 19:31:43 -05:00
// Test : https://fiber.wiki/application#test
2020-03-22 17:35:12 +01:00
func (app *App) Test(request *http.Request, msTimeout ...int) (*http.Response, error) {
timeout := 200
if len(msTimeout) > 0 {
timeout = msTimeout[0]
}
2020-03-23 21:52:37 +01:00
// if timeout equals -1 we extend the timeout to 10 minutes
if timeout < 0 {
timeout = 600000 // 10 minutes
}
2020-03-20 16:43:28 +01:00
// Dump raw http request
dump, err := httputil.DumpRequest(request, true)
2020-02-21 18:07:43 +01:00
if err != nil {
return nil, err
}
2020-03-20 16:43:28 +01:00
// Setup server
2020-02-21 18:07:43 +01:00
app.server = app.newServer()
2020-03-20 16:43:28 +01:00
// Create conn
conn := new(testConn)
// Write raw http request
if _, err = conn.r.Write(dump); err != nil {
2020-02-21 18:07:43 +01:00
return nil, err
}
// Serve conn to server
channel := make(chan error)
go func() {
channel <- app.server.ServeConn(conn)
}()
2020-03-23 21:52:37 +01:00
// Wait for callback
select {
case err := <-channel:
if err != nil {
return nil, err
2020-02-21 18:07:43 +01:00
}
2020-03-23 21:52:37 +01:00
case <-time.After(time.Duration(timeout) * time.Millisecond):
return nil, fmt.Errorf("Timeout error")
2020-03-24 03:42:28 +01:00
2020-03-20 16:43:28 +01:00
// Read response
buffer := bufio.NewReader(&conn.w)
// Convert raw http response to *http.Response
2020-02-26 19:31:43 -05:00
resp, err := http.ReadResponse(buffer, request)
2020-02-21 18:07:43 +01:00
if err != nil {
return nil, err
}
// Return *http.Response
return resp, nil
}
// https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/
func (app *App) prefork(address string) (ln net.Listener, err error) {
// Master proc
if !app.child {
2020-03-01 07:31:14 +01:00
addr, err := net.ResolveTCPAddr("tcp", address)
2020-02-21 18:07:43 +01:00
if err != nil {
return ln, err
}
2020-03-01 07:31:14 +01:00
tcplistener, err := net.ListenTCP("tcp", addr)
2020-02-21 18:07:43 +01:00
if err != nil {
return ln, err
}
fl, err := tcplistener.File()
if err != nil {
return ln, err
}
2020-02-26 19:31:43 -05:00
files := []*os.File{fl}
2020-02-21 18:07:43 +01:00
childs := make([]*exec.Cmd, runtime.NumCPU()/2)
// #nosec G204
for i := range childs {
2020-02-26 19:31:43 -05:00
childs[i] = exec.Command(os.Args[0], append(os.Args[1:], "-prefork", "-child")...)
2020-02-21 18:07:43 +01:00
childs[i].Stdout = os.Stdout
childs[i].Stderr = os.Stderr
2020-02-26 19:31:43 -05:00
childs[i].ExtraFiles = files
2020-02-21 18:07:43 +01:00
if err := childs[i].Start(); err != nil {
return ln, err
}
}
for _, child := range childs {
if err := child.Wait(); err != nil {
return ln, err
}
}
os.Exit(0)
} else {
2020-02-26 19:31:43 -05:00
runtime.GOMAXPROCS(1)
2020-02-21 18:07:43 +01:00
ln, err = net.FileListener(os.NewFile(3, ""))
}
return ln, err
}
2020-03-20 16:43:28 +01:00
type customLogger struct{}
2020-03-01 06:56:41 +01:00
2020-03-20 16:43:28 +01:00
func (cl *customLogger) Printf(format string, args ...interface{}) {
2020-03-01 06:56:41 +01:00
// fmt.Println(fmt.Sprintf(format, args...))
}
2020-02-21 18:07:43 +01:00
func (app *App) newServer() *fasthttp.Server {
return &fasthttp.Server{
2020-03-04 12:30:29 +01:00
Handler: app.handler,
Name: app.Settings.ServerHeader,
2020-03-24 03:36:52 +01:00
MaxRequestBodySize: app.Settings.BodyLimit,
2020-03-24 03:42:28 +01:00
NoDefaultServerHeader: app.Settings.ServerHeader == "",
2020-03-23 05:25:46 +01:00
ReadTimeout: app.Settings.ReadTimeout,
WriteTimeout: app.Settings.WriteTimeout,
IdleTimeout: app.Settings.IdleTimeout,
2020-03-20 16:43:28 +01:00
Logger: &customLogger{},
2020-03-14 12:30:21 +01:00
LogAllErrors: false,
2020-02-21 18:07:43 +01:00
ErrorHandler: func(ctx *fasthttp.RequestCtx, err error) {
2020-03-01 06:56:41 +01:00
if err.Error() == "body size exceeds the given limit" {
ctx.Response.SetStatusCode(413)
ctx.Response.SetBodyString("Request Entity Too Large")
} else {
ctx.Response.SetStatusCode(400)
ctx.Response.SetBodyString("Bad Request")
}
2020-02-21 18:07:43 +01:00
},
}
}