Skip to content

Commit

Permalink
Add v5.2.0 changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
System-Glitch committed Jul 31, 2024
1 parent 36bfea1 commit 6fac0d0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/basics/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ The framework supports the following sql drivers out-of-the-box, defined in the
- `postgres`
- `sqlite3`
- `mssql`
- `clickhouse`

In order to be able connect to the database, Gorm needs a database driver to be imported. Add the following import to your `main.go`:

Expand Down
57 changes: 31 additions & 26 deletions src/basics/response.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Usually, the response's writer is replaced in a middleware. The current writer i
![Chained writers diagram](/diagrams/chained_writers.webp){data-zoomable}
:::

Chained writers are **components**, just like middleware and controllers.
Chained writers are **components**, just like middleware and controllers. They can compose with `goyave.CommonWriter`, a structure that implements all the basic methods a chained writer must implement: `goyave.PreWriter`, `io.Closer` and `goyave.Flusher`.

Note that at the time a chained writer's `Write()` method is called, the request header is already written. Therefore, changing headers or status doesn't have any effect. If you want to alter the headers, do so in a `PreWrite(b []byte)` function (from the `goyave.PreWriter` interface).

Expand All @@ -141,45 +141,27 @@ import (
)

type LogWriter struct {
goyave.Component
writer io.Writer
goyave.CommonWriter
response *goyave.Response
body []byte
}

func NewWriter(server *goyave.Server, response *goyave.Response) *LogWriter {
writer := &LogWriter{
writer: response.Writer(),
func NewWriter(response *goyave.Response) *LogWriter {
return &LogWriter{
writer: goyave.NewCommonWriter(response.Writer()),
response: response,
}
writer.Init(server)
return writer
}

func (w *LogWriter) PreWrite(b []byte) {
// All chained writers should implement goyave.PreWriter
// to allow the modification of headers and status before
// they are written.
if pr, ok := w.writer.(goyave.PreWriter); ok {
pr.PreWrite(b)
}
}

func (w *LogWriter) Write(b []byte) (int, error) {
w.body = append(w.body, b...)
n, err := w.writer.Write(b)
n, err := w.CommonWriter.Write(b)
return n, errors.New(err)
}

func (w *LogWriter) Close() error {
w.Logger().Info("RESPONSE", "body", string(w.body))

// The chained writer MUST be closed if it's closeable.
// Therefore, all chained writers should implement io.Closer.
if wr, ok := w.writer.(io.Closer); ok {
return errors.New(wr.Close())
}
return nil
return errors.New(w.CommonWriter.Close())
}

type LogMiddleware struct {
Expand All @@ -188,14 +170,37 @@ type LogMiddleware struct {

func (m *LogMiddleware) Handle(next goyave.Handler) goyave.Handler {
return func(response *goyave.Response, request *goyave.Request) {
logWriter := NewWriter(m.Server(), response)
logWriter := NewWriter(response)
response.SetWriter(logWriter)

next(response, request)
}
}
```

You can override the default behavior of `PreWrite()`, `Close()` and `Flush()`. If you do so, make sure to call the `CommonWriter`'s implementation so the writers are correctly chained for these operations:

```go
func (w *Writer) PreWrite(b []byte) {
//...
w.CommonWriter.PreWrite(b)
}

func (w *Writer) Close() error {
//...
return errors.New(w.CommonWriter.Close())
}

func (w *Writer) Flush() error {
//...
return errors.New(w.CommonWriter.Flush())
}
```

:::info
Chained writers support both `http.Flusher` and `goyave.Flusher` for their underlying writers. The only difference between those interfaces is that `http.Flusher`'s `Close()` method doesn't return an `error` but `goyave.Flusher`'s does.
:::

## Hijack

Goyave responses implement [`http.Hijacker`](https://golang.org/pkg/net/http/#Hijacker). Hijacking is the process of taking over the connection of the current HTTP request. This can be used for example for [websockets](/advanced/websockets.html).
Expand Down
1 change: 1 addition & 0 deletions src/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ You can also see the changelog on [Github](https://github.com/go-goyave/goyave/r
You can be notified of new releases by enabling notifications on Github or by joining our [Discord](https://discord.gg/mfemDMc).
:::

- [v5.2.0](./changelog/v5.2.0.md)
- [v5.1.1](./changelog/v5.1.1.md)
- [v5.1.0](./changelog/v5.1.0.md)
- [v5.0.1](./changelog/v5.0.1.md)
Expand Down
4 changes: 3 additions & 1 deletion src/changelog/v5.1.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ description: "The detailed release notes for v5.1.1"
prev:
text: 'v5.1.0'
link: '/changelog/v5.1.0'
next: false
next:
text: 'v5.2.0'
link: '/changelog/v5.2.0'
---

# v5.1.1 release notes
Expand Down
18 changes: 18 additions & 0 deletions src/changelog/v5.2.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: "v5.2.0 release notes"
description: "The detailed release notes for v5.2.0"
prev:
text: 'v5.1.1'
link: '/changelog/v5.1.1'
next: false
---

# v5.2.0 release notes

- Added support for response and chained writer flushing.
- Added support for the `clickhouse` database dialect.
- Updated the `UniqueArray`/`ExistsArray` validators to support ClickHouse.
- Changed the default status handler for code `400 Bad Request` to the new `goyave.ParseErrorStatusHandler`. This status handler's behavior is identical to the previous one except it returns explicit error messages in the response when the parse middleware fails to parse a user's request.
- Added `lang.Default` global variable, which contains the builtin default `en-US` language.
- Fixed a panic if no `Language` is given in the `validation.Options` when manually validating. Instead, `lang.Default` will be used.
- Added deflate, lzw, br and zstd encoders for the compress middleware.
4 changes: 2 additions & 2 deletions src/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ A big "Thank you" to the Goyave contributors:
- [Jason C Keller](https://github.com/imuni4fun) (Testify interface compatibility)
- [saltyFamiliar](https://github.com/saltyFamiliar) (`DoesntStartWithValidator`)
- [Muhammad Meganata Adam](https://github.com/bangadam) (`DoesntEndWithValidator`, `KeysInValidator`)
- [BowlOfSoup](https://github.com/BowlOfSoup) (`Required` flag in configuration entries)
- [AidanKenney](https://github.com/AidanKenney) (Clickhouse support)
- [BowlOfSoup](https://github.com/BowlOfSoup) (`Required` flag in configuration entries, `ParseErrorStatusHandler`)
- [AidanKenney](https://github.com/AidanKenney) (Clickhouse support, compression encoders)

## Goyave in production

Expand Down

0 comments on commit 6fac0d0

Please sign in to comment.