mirror of https://github.com/gofiber/fiber.git
commit
d0c44b8f93
|
@ -5,4 +5,4 @@ patreon: # Replace with a single Patreon username
|
|||
open_collective: # Replace with a single Open Collective username
|
||||
ko_fi: # Replace with a single Ko-fi username
|
||||
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
||||
custom: # Replace with a single custom sponsorship URL
|
||||
custom: https://github.com/gofiber/fiber/
|
||||
|
|
10
README.md
10
README.md
|
@ -4,7 +4,7 @@
|
|||
|
||||
<img align="right" height="180px" src="docs/static/logo_320px_trans.png" alt="Fiber logo" />
|
||||
|
||||
**[Fiber](https://github.com/gofiber/fiber)** is an [Express](https://expressjs.com/en/4x/api.html)-styled HTTP web framework implementation running on [Fasthttp](https://github.com/valyala/fasthttp), the **fastest** HTTP engine for Go (Golang). The package make use of **similar framework convention** as they are in Express.
|
||||
**[Fiber](https://github.com/gofiber/fiber)** is an [Express.js](https://expressjs.com/en/4x/api.html) styled HTTP web framework implementation running on [Fasthttp](https://github.com/valyala/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](https://nodejs.org/en/about/) to [Go](https://golang.org/doc/) 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.
|
||||
|
||||
|
@ -12,7 +12,7 @@ People switching from [Node.js](https://nodejs.org/en/about/) to [Go](https://go
|
|||
|
||||
## API Documentation
|
||||
|
||||
📚 We created an extended API documentation (_including examples_), **[click here](https://gofiber.github.io/fiber/)**.
|
||||
📚 We created an extended **API documentation** (_including examples_), **[click here](https://gofiber.github.io/fiber/)**.
|
||||
|
||||
## Benchmark
|
||||
|
||||
|
@ -36,7 +36,7 @@ Assuming you’ve already installed Go `1.11+` 😉
|
|||
|
||||
Install the [Fiber](https://github.com/gofiber/fiber) package by calling the following command:
|
||||
|
||||
```console
|
||||
```bash
|
||||
go get -u github.com/gofiber/fiber
|
||||
```
|
||||
|
||||
|
@ -67,7 +67,7 @@ func main() {
|
|||
|
||||
Go to console and run:
|
||||
|
||||
```console
|
||||
```bash
|
||||
go run server.go
|
||||
```
|
||||
|
||||
|
@ -96,7 +96,7 @@ func main() {
|
|||
|
||||
Now, you can load the files that are in the public directory:
|
||||
|
||||
```console
|
||||
```bash
|
||||
http://localhost:8080/hello.html
|
||||
http://localhost:8080/js/script.js
|
||||
http://localhost:8080/css/style.css
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
# 🔌 Fiber Web Framework
|
||||
|
||||
[](https://github.com/gofiber/fiber/releases)  [](https://godoc.org/github.com/gofiber/fiber)  [](https://github.com/gofiber/fiber/blob/master/LICENSE) [](https://gitter.im/FiberGo/community)
|
||||
|
||||
<img align="right" height="180px" src="docs/static/logo_320px_trans.png" alt="Fiber logo" />
|
||||
|
||||
Help us to translate [this README](https://github.com/gofiber/fiber/blob/master/README_CH.md) in chinese.
|
||||
|
||||
## License
|
||||
|
||||
⚠️ _Please note:_ `gofiber/fiber` is free and open-source software licensed under the [MIT License](https://github.com/gofiber/fiber/edit/master/LICENSE).
|
10
README_RU.md
10
README_RU.md
|
@ -4,7 +4,7 @@
|
|||
|
||||
<img align="right" height="180px" src="docs/static/logo_320px_trans.png" alt="Fiber logo" />
|
||||
|
||||
**[Fiber](https://github.com/gofiber/fiber)** — это [Express](https://expressjs.com/en/4x/api.html)-подобный HTTP веб фреймворк, использующий всю мощь [Fasthttp](https://github.com/valyala/fasthttp), самого **быстрого** HTTP движка для Go (Golang). Мы используем **аналогичную** схему именования методов, как и у Express.
|
||||
**[Fiber](https://github.com/gofiber/fiber)** — это [Express.js](https://expressjs.com/en/4x/api.html) подобный HTTP веб фреймворк, использующий всю мощь [Fasthttp](https://github.com/valyala/fasthttp), самого **быстрого** HTTP движка для Go (Golang). Мы используем **аналогичную** схему именования методов, как и у Express.
|
||||
|
||||
Разработчики, пришедшие из [Node.js](https://nodejs.org/en/about/) в [Go](https://golang.org/doc/) очень часто испытывают трудности при создании своих первых веб-приложений. Данный проект призван, в том числе, **облегчить** процесс перехода для таких разработчиков.
|
||||
|
||||
|
@ -12,7 +12,7 @@
|
|||
|
||||
## Документация по API
|
||||
|
||||
📚 Мы создали расширенную документацию по API (_включая примеры_), **[посмотреть](https://gofiber.github.io/fiber/)**.
|
||||
📚 Мы создали расширенную **документацию по API** (_включая примеры_), **[посмотреть](https://gofiber.github.io/fiber/)**.
|
||||
|
||||
## Бенчмарк
|
||||
|
||||
|
@ -36,7 +36,7 @@
|
|||
|
||||
Установит пакет [Fiber](https://github.com/gofiber/fiber) с помощью следующей команды в консоле:
|
||||
|
||||
```console
|
||||
```bash
|
||||
go get -u github.com/gofiber/fiber
|
||||
```
|
||||
|
||||
|
@ -67,7 +67,7 @@ func main() {
|
|||
|
||||
Перейдите в консоль и запустите:
|
||||
|
||||
```console
|
||||
```bash
|
||||
go run server.go
|
||||
```
|
||||
|
||||
|
@ -96,7 +96,7 @@ func main() {
|
|||
|
||||
Теперь вы можете получить доступ ко всем файлам, которые находятся в папке `./public`:
|
||||
|
||||
```console
|
||||
```bash
|
||||
http://localhost:8080/hello.html
|
||||
http://localhost:8080/js/script.js
|
||||
http://localhost:8080/css/style.css
|
||||
|
|
|
@ -14,7 +14,7 @@ import (
|
|||
|
||||
const (
|
||||
// Version : Fiber version
|
||||
Version = "1.2.3"
|
||||
Version = "1.3.0"
|
||||
// https://play.golang.org/p/r6GNeV1gbH
|
||||
banner = "" +
|
||||
" \x1b[1;32m _____ _ _\n" +
|
||||
|
|
|
@ -459,33 +459,33 @@ app.Get("/", func(c *fiber.Ctx) {
|
|||
})
|
||||
```
|
||||
|
||||
#### Ip
|
||||
#### IP
|
||||
|
||||
Contains the remote IP address of the request.
|
||||
|
||||
```go
|
||||
// Function signature
|
||||
c.Ip() string
|
||||
c.IP() string
|
||||
|
||||
// Example
|
||||
app.Get("/", func(c *fiber.Ctx) {
|
||||
c.Ip()
|
||||
c.IP()
|
||||
// => "127.0.0.1"
|
||||
})
|
||||
```
|
||||
|
||||
#### Ips
|
||||
#### IPs
|
||||
|
||||
contains an array of IP addresses specified in the X-Forwarded-For request header.
|
||||
|
||||
```go
|
||||
// Function signature
|
||||
c.Ips() []string
|
||||
c.IPs() []string
|
||||
|
||||
// Example
|
||||
// X-Forwarded-For: proxy1, 127.0.0.1", proxy3
|
||||
app.Get("/", func(c *fiber.Ctx) {
|
||||
c.Ips()
|
||||
c.IPs()
|
||||
// => ["proxy1", "127.0.0.1", "proxy3"]
|
||||
})
|
||||
```
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
|
||||
<script>
|
||||
window.$docsify = {
|
||||
name: 'Fiber v1.2.3',
|
||||
name: 'Fiber v1.3.0',
|
||||
repo: 'gofiber/fiber',
|
||||
loadSidebar: "sidebar.md",
|
||||
homepage: 'getting_started.md',
|
||||
|
|
|
@ -97,6 +97,7 @@ func (r *Fiber) prefork(server *fasthttp.Server, host string, tls ...string) {
|
|||
// Create babies
|
||||
childs := make([]*exec.Cmd, runtime.NumCPU())
|
||||
|
||||
// #nosec G204
|
||||
for i := range childs {
|
||||
childs[i] = exec.Command(os.Args[0], "-prefork", "-child")
|
||||
childs[i].Stdout = os.Stdout
|
||||
|
|
|
@ -3,8 +3,12 @@ package middleware
|
|||
import "github.com/gofiber/fiber"
|
||||
|
||||
// Cors : Enable cross-origin resource sharing (CORS) with various options.
|
||||
func Cors(c *fiber.Ctx, d string) {
|
||||
c.Set("Access-Control-Allow-Origin", d) // Set d to "*" for allow all domains
|
||||
func Cors(c *fiber.Ctx, origin ...string) {
|
||||
o := "*"
|
||||
if len(origin) > 0 {
|
||||
o = origin[0]
|
||||
}
|
||||
c.Set("Access-Control-Allow-Origin", o)
|
||||
c.Set("Access-Control-Allow-Headers", "X-Requested-With")
|
||||
c.Next()
|
||||
}
|
||||
|
|
|
@ -8,6 +8,6 @@ import (
|
|||
|
||||
// Helmet : Helps secure your apps by setting various HTTP headers.
|
||||
func Helmet(c *fiber.Ctx) {
|
||||
fmt.Println("Helmet is still under development, disable until v1.0.0")
|
||||
fmt.Println("Helmet is still under development, this middleware does nothing yet.")
|
||||
c.Next()
|
||||
}
|
||||
|
|
29
request.go
29
request.go
|
@ -9,6 +9,7 @@ package fiber
|
|||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"log"
|
||||
"mime"
|
||||
"mime/multipart"
|
||||
|
@ -239,13 +240,25 @@ func (ctx *Ctx) Hostname() string {
|
|||
return getString(ctx.Fasthttp.URI().Host())
|
||||
}
|
||||
|
||||
// Ip : DEPRECATED
|
||||
func (ctx *Ctx) Ip() string { // NOLINT
|
||||
fmt.Println("Fiber deprecated c.Ip(): Use c.IP() instead")
|
||||
return ctx.IP()
|
||||
}
|
||||
|
||||
// IP : https://gofiber.github.io/fiber/#/context?id=Ip
|
||||
func (ctx *Ctx) IP() string {
|
||||
return ctx.Fasthttp.RemoteIP().String()
|
||||
}
|
||||
|
||||
// Ips : https://gofiber.github.io/fiber/#/context?id=ips
|
||||
func (ctx *Ctx) Ips() []string {
|
||||
// Ips : DEPRECATED
|
||||
func (ctx *Ctx) Ips() []string { // NOLINT
|
||||
fmt.Println("Fiber deprecated c.Ips(): Use c.IPs() instead")
|
||||
return ctx.IPs()
|
||||
}
|
||||
|
||||
// IPs : https://gofiber.github.io/fiber/#/context?id=ips
|
||||
func (ctx *Ctx) IPs() []string {
|
||||
ips := strings.Split(ctx.Get(fasthttp.HeaderXForwardedFor), ",")
|
||||
for i := range ips {
|
||||
ips[i] = strings.TrimSpace(ips[i])
|
||||
|
@ -290,6 +303,12 @@ func (ctx *Ctx) MultipartForm() (*multipart.Form, error) {
|
|||
return ctx.Fasthttp.MultipartForm()
|
||||
}
|
||||
|
||||
// OriginalUrl : DEPRECATED
|
||||
func (ctx *Ctx) OriginalUrl() string {
|
||||
fmt.Println("Fiber deprecated c.OriginalUrl(): Use c.OriginalURL() instead")
|
||||
return ctx.OriginalURL()
|
||||
}
|
||||
|
||||
// OriginalURL : https://gofiber.github.io/fiber/#/context?id=originalurl
|
||||
func (ctx *Ctx) OriginalURL() string {
|
||||
return getString(ctx.Fasthttp.Request.Header.RequestURI())
|
||||
|
@ -366,6 +385,12 @@ func (ctx *Ctx) Subdomains() (subs []string) {
|
|||
return subs
|
||||
}
|
||||
|
||||
// Xhr : DEPRECATED
|
||||
func (ctx *Ctx) Xhr() bool {
|
||||
fmt.Println("Fiber deprecated c.Xhr(): Use c.XHR() instead")
|
||||
return ctx.XHR()
|
||||
}
|
||||
|
||||
// XHR : https://gofiber.github.io/fiber/#/context?id=xhr
|
||||
func (ctx *Ctx) XHR() bool {
|
||||
return ctx.Get("X-Requested-With") == "XMLHttpRequest"
|
||||
|
|
30
response.go
30
response.go
|
@ -156,6 +156,12 @@ func (ctx *Ctx) HeadersSent() {
|
|||
|
||||
}
|
||||
|
||||
// Json : DEPRECATED
|
||||
func (ctx *Ctx) Json(v interface{}) error {
|
||||
fmt.Println("Fiber deprecated c.Json(): Use c.JSON() instead")
|
||||
return ctx.JSON(v)
|
||||
}
|
||||
|
||||
// JSON : https://gofiber.github.io/fiber/#/context?id=json
|
||||
func (ctx *Ctx) JSON(v interface{}) error {
|
||||
raw, err := jsoniter.Marshal(&v)
|
||||
|
@ -169,12 +175,24 @@ func (ctx *Ctx) JSON(v interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// JsonBytes : DEPRECATED
|
||||
func (ctx *Ctx) JsonBytes(raw []byte) {
|
||||
fmt.Println("Fiber deprecated c.JsonBytes(): Use c.JSONBytes() instead")
|
||||
ctx.JSONBytes(raw)
|
||||
}
|
||||
|
||||
// JSONBytes : https://gofiber.github.io/fiber/#/context?id=jsonbytes
|
||||
func (ctx *Ctx) JSONBytes(raw []byte) {
|
||||
ctx.Fasthttp.Response.Header.SetContentType(contentTypeJSON)
|
||||
ctx.Fasthttp.Response.SetBodyString(getString(raw))
|
||||
}
|
||||
|
||||
// Jsonp : DEPRECATED
|
||||
func (ctx *Ctx) Jsonp(v interface{}, cb ...string) error {
|
||||
fmt.Println("Fiber deprecated c.Jsonp(): Use c.JSONP() instead")
|
||||
return ctx.JSONP(v, cb...)
|
||||
}
|
||||
|
||||
// JSONP : https://gofiber.github.io/fiber/#/context?id=jsonp
|
||||
func (ctx *Ctx) JSONP(v interface{}, cb ...string) error {
|
||||
raw, err := jsoniter.Marshal(&v)
|
||||
|
@ -195,6 +213,12 @@ func (ctx *Ctx) JSONP(v interface{}, cb ...string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// JsonString : DEPRECATED
|
||||
func (ctx *Ctx) JsonString(raw string) {
|
||||
fmt.Println("Fiber deprecated c.JsonString(): Use c.JSONString() instead")
|
||||
ctx.JSONString(raw)
|
||||
}
|
||||
|
||||
// JSONString : https://gofiber.github.io/fiber/#/context?id=jsonstring
|
||||
func (ctx *Ctx) JSONString(raw string) {
|
||||
ctx.Fasthttp.Response.Header.SetContentType(contentTypeJSON)
|
||||
|
@ -347,6 +371,12 @@ func (ctx *Ctx) Write(args ...interface{}) {
|
|||
}
|
||||
}
|
||||
|
||||
// Xml : DEPRECATED
|
||||
func (ctx *Ctx) Xml(v interface{}) error {
|
||||
fmt.Println("Fiber deprecated c.Xml(): Use c.XML() instead")
|
||||
return ctx.XML(v)
|
||||
}
|
||||
|
||||
// XML : https://gofiber.github.io/fiber/#/context?id=xml
|
||||
func (ctx *Ctx) XML(v interface{}) error {
|
||||
raw, err := xml.Marshal(v)
|
||||
|
|
17
utils.go
17
utils.go
|
@ -10,6 +10,7 @@ package fiber
|
|||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
@ -78,10 +79,22 @@ func getStatus(status int) (msg string) {
|
|||
return statusMessages[status]
|
||||
}
|
||||
|
||||
// #nosec G103
|
||||
// getString converts byte slice to a string without memory allocation.
|
||||
// See https://groups.google.com/forum/#!msg/Golang-Nuts/ENgbUzYvCuU/90yGx7GUAgAJ .
|
||||
func getString(b []byte) string {
|
||||
return *(*string)(unsafe.Pointer(&b))
|
||||
}
|
||||
|
||||
func getBytes(s string) []byte {
|
||||
return *(*[]byte)(unsafe.Pointer(&s))
|
||||
// #nosec G103
|
||||
// getBytes converts string to a byte slice without memory allocation.
|
||||
// See https://groups.google.com/forum/#!msg/Golang-Nuts/ENgbUzYvCuU/90yGx7GUAgAJ .
|
||||
func getBytes(s string) (b []byte) {
|
||||
// return *(*[]byte)(unsafe.Pointer(&s))
|
||||
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
||||
sh := *(*reflect.StringHeader)(unsafe.Pointer(&s))
|
||||
bh.Data = sh.Data
|
||||
bh.Len = sh.Len
|
||||
bh.Cap = sh.Len
|
||||
return b
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue