Skip to content

Commit

Permalink
Merge pull request #198 from koddr/master
Browse files Browse the repository at this point in the history
Add new examples to README (EN, RU)
  • Loading branch information
Vic Shóstak authored Mar 2, 2020
2 parents d6f5356 + 8acb1df commit c4b1270
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 1 deletion.
54 changes: 54 additions & 0 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,60 @@ func main() {
}
```

### Built-in logger

```go
import (
"github.com/gofiber/fiber"
"github.com/gofiber/fiber/middleware"
)

func main() {
app := fiber.New()

// If you want to change default Logger config
loggerConfig := middleware.LoggerConfig{
Format: "${time} - ${method} ${path}\n",
TimeFormat: "Mon, 2 Jan 2006 15:04:05 MST",
}

// Middleware for Logger with config
app.Use(middleware.Logger(loggerConfig))

// ...
}
```

### Cross-Origin Resource Sharing (CORS)

[CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) is a mechanism that uses additional HTTP headers to tell browsers to give a web application running at one origin, access to selected resources from a different origin. A web application executes a cross-origin HTTP request when it requests a resource that has a different origin (domain, protocol, or port) from its own.

```go
import (
"github.com/gofiber/fiber"
"github.com/gofiber/fiber/middleware"
)

func main() {
app := fiber.New()

// Connect CORS for each route as middleware
app.Use(middleware.CORS())

app.Get("/", func(c *fiber.Ctx) {
c.Send("CORS is enabled!")
})

app.Listen(3000)
}
```

Check CORS by passing any domain in `Origin` header:

```bash
curl -H "Origin: http://example.com" --verbose http://localhost:3000
```

### Custom 404 response

```go
Expand Down
88 changes: 87 additions & 1 deletion .github/README_ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func main() {
Установка выполняется с помощью команды [`go get`](https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them):

```bash
go get -u github.com/gofiber/fiber
go get -u github.com/gofiber/fiber/...
```

## 🤖 Бенчмарки
Expand Down Expand Up @@ -265,6 +265,60 @@ func main() {
}
```

### Встроенный логгер

```go
import (
"github.com/gofiber/fiber"
"github.com/gofiber/fiber/middleware"
)

func main() {
app := fiber.New()

// Если вы хотите изменить конфигурацию логгера по умолчанию
loggerConfig := middleware.LoggerConfig{
Format: "${time} - ${method} ${path}\n",
TimeFormat: "Mon, 2 Jan 2006 15:04:05 MST",
}

// Middleware для логгера с кастомным конфигом
app.Use(middleware.Logger(loggerConfig))

// ...
}
```

### Cross-Origin Resource Sharing (CORS)

[CORS](https://developer.mozilla.org/ru/docs/Web/HTTP/CORS) — это механизм, использующий дополнительные HTTP-заголовки, чтобы дать возможность агенту пользователя получать разрешения на доступ к выбранным ресурсам с сервера на источнике (домене), отличном от того, что сайт использует в данный момент.

```go
import (
"github.com/gofiber/fiber"
"github.com/gofiber/fiber/middleware"
)

func main() {
app := fiber.New()

// Подключаем CORS для каждого роута в качестве middleware
app.Use(middleware.CORS())

app.Get("/", func(c *fiber.Ctx) {
c.Send("CORS is enabled!")
})

app.Listen(3000)
}
```

Проверьте работу CORS, передав любой домен в заголовке `Origin`:

```bash
curl -H "Origin: http://example.com" --verbose http://localhost:3000
```

### Обработка 404 ошибки

```go
Expand Down Expand Up @@ -311,6 +365,37 @@ func main() {
}
```

### Поддержка WebSocket

```go
func main() {
app := fiber.New()

app.WebSocket("/ws/:name", func(c *fiber.Conn) {
log.Println(c.Params("name"))

for {
mt, msg, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}

log.Printf("recovery: %s", msg)

err = c.WriteMessage(mt, msg)
if err != nil {
log.Println("write:", err)
break
}
}
})

// Слушаем вебсокет по адресу ws://localhost:3000/ws/john
app.Listen(3000)
}
```

### Восстановление работы после `panic`

```go
Expand All @@ -329,6 +414,7 @@ func main() {
app.Listen(3000)
}
```

</details>

## 💬 Медиа
Expand Down

0 comments on commit c4b1270

Please sign in to comment.