From 6fac0d05cc0551f5fafaf7ec16ed1f6ca88abd81 Mon Sep 17 00:00:00 2001 From: SystemGlitch Date: Tue, 23 Jul 2024 16:30:04 +0200 Subject: [PATCH] Add v5.2.0 changelog --- src/basics/database.md | 1 + src/basics/response.md | 57 ++++++++++++++++++++++------------------- src/changelog.md | 1 + src/changelog/v5.1.1.md | 4 ++- src/changelog/v5.2.0.md | 18 +++++++++++++ src/introduction.md | 4 +-- 6 files changed, 56 insertions(+), 29 deletions(-) create mode 100644 src/changelog/v5.2.0.md diff --git a/src/basics/database.md b/src/basics/database.md index 5aed45f..7028672 100644 --- a/src/basics/database.md +++ b/src/basics/database.md @@ -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`: diff --git a/src/basics/response.md b/src/basics/response.md index 455c52b..d9c0a34 100644 --- a/src/basics/response.md +++ b/src/basics/response.md @@ -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). @@ -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 { @@ -188,7 +170,7 @@ 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) @@ -196,6 +178,29 @@ func (m *LogMiddleware) Handle(next goyave.Handler) goyave.Handler { } ``` +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). diff --git a/src/changelog.md b/src/changelog.md index d8d41cb..fbe3410 100644 --- a/src/changelog.md +++ b/src/changelog.md @@ -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) diff --git a/src/changelog/v5.1.1.md b/src/changelog/v5.1.1.md index 5bb4c61..799a7ac 100644 --- a/src/changelog/v5.1.1.md +++ b/src/changelog/v5.1.1.md @@ -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 diff --git a/src/changelog/v5.2.0.md b/src/changelog/v5.2.0.md new file mode 100644 index 0000000..54407d9 --- /dev/null +++ b/src/changelog/v5.2.0.md @@ -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. \ No newline at end of file diff --git a/src/introduction.md b/src/introduction.md index 42d0227..de35c55 100644 --- a/src/introduction.md +++ b/src/introduction.md @@ -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