Skip to content

Commit bd0f4b9

Browse files
authored
feat(docs): Simplified Chinese + Japanese translations (#412)
feat(docs): Simplified Chinese + Japanese translations
2 parents b3df436 + 1088f33 commit bd0f4b9

119 files changed

Lines changed: 14516 additions & 3 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

site/astro.config.mjs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export default defineConfig({
1010
integrations: [
1111
starlight({
1212
title: 'Echo',
13+
defaultLocale: 'root',
14+
locales: {
15+
root: { label: 'English', lang: 'en' },
16+
'zh-cn': { label: '简体中文', lang: 'zh-CN' },
17+
ja: { label: '日本語', lang: 'ja' },
18+
},
1319
logo: {
1420
light: './src/assets/logo-light.svg',
1521
dark: './src/assets/logo-dark.svg',
@@ -73,9 +79,9 @@ export default defineConfig({
7379
// Autogenerated from the content dirs — new pages appear automatically,
7480
// ordered by each page's `sidebar.order` frontmatter.
7581
sidebar: [
76-
{ label: 'Guide', items: [{ autogenerate: { directory: 'guide' } }] },
77-
{ label: 'Middleware', items: [{ autogenerate: { directory: 'middleware' } }] },
78-
{ label: 'Cookbook', items: [{ autogenerate: { directory: 'cookbook' } }] },
82+
{ label: 'Guide', translations: { 'zh-CN': '指南', ja: 'ガイド' }, items: [{ autogenerate: { directory: 'guide' } }] },
83+
{ label: 'Middleware', translations: { 'zh-CN': '中间件', ja: 'ミドルウェア' }, items: [{ autogenerate: { directory: 'middleware' } }] },
84+
{ label: 'Cookbook', translations: { 'zh-CN': '示例', ja: 'クックブック' }, items: [{ autogenerate: { directory: 'cookbook' } }] },
7985
],
8086
// tune the built-in code theme toward our terminal palette
8187
expressiveCode: { themes: ['github-dark', 'github-light'] },
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
title: 自動 TLS
3+
description: Let's Encrypt から TLS 証明書を自動で取得し、更新します。
4+
sidebar:
5+
order: 3
6+
---
7+
8+
このレシピは、ドメインの TLS 証明書を Let's Encrypt から自動取得します。autocert manager の
9+
`TLSConfig` を使って `StartConfig` を設定し、ポート `443` で待ち受けます。
10+
11+
`https://<DOMAIN>` にアクセスしてください。すべて正しく設定されていれば、TLS 経由で提供される
12+
ウェルカムメッセージが表示されます。
13+
14+
:::tip
15+
- セキュリティを高めるには、autocert manager で host policy を指定してください。
16+
- [Let's Encrypt rate limits](https://letsencrypt.org/docs/rate-limits) に達しないよう、証明書をキャッシュしてください。
17+
- HTTP トラフィックを HTTPS にリダイレクトするには、[リダイレクトミドルウェア](/ja/middleware/redirect/#https-redirect)を使います。
18+
:::
19+
20+
## サーバー
21+
22+
```go
23+
package main
24+
25+
import (
26+
"context"
27+
"crypto/tls"
28+
"errors"
29+
"log/slog"
30+
"net/http"
31+
"os"
32+
33+
"golang.org/x/crypto/acme"
34+
35+
"github.com/labstack/echo/v5"
36+
"github.com/labstack/echo/v5/middleware"
37+
"golang.org/x/crypto/acme/autocert"
38+
)
39+
40+
func main() {
41+
e := echo.New()
42+
e.Logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
43+
44+
e.Use(middleware.Recover())
45+
e.Use(middleware.RequestLogger())
46+
47+
e.GET("/", func(c *echo.Context) error {
48+
return c.HTML(http.StatusOK, `
49+
<h1>Welcome to Echo!</h1>
50+
<h3>TLS certificates automatically installed from Let's Encrypt :)</h3>
51+
`)
52+
})
53+
54+
m := &autocert.Manager{
55+
Prompt: autocert.AcceptTOS,
56+
HostPolicy: autocert.HostWhitelist("example.com", "www.example.com"),
57+
// Cache certificates to avoid issues with rate limits (https://letsencrypt.org/docs/rate-limits)
58+
Cache: autocert.DirCache("/var/www/.cache"),
59+
// Email: "[email protected]", // optional but recommended
60+
}
61+
62+
sc := echo.StartConfig{
63+
Address: ":443",
64+
TLSConfig: m.TLSConfig(),
65+
}
66+
if err := sc.Start(context.Background(), e); err != nil {
67+
e.Logger.Error("failed to start server", "error", err)
68+
}
69+
}
70+
```
71+
72+
## カスタム HTTP サーバーを使う
73+
74+
`http.Server` を完全に制御したい場合は、代わりに autocert manager をカスタム `tls.Config` に接続します。
75+
76+
```go
77+
func customHTTPServer() {
78+
e := echo.New()
79+
e.Use(middleware.Recover())
80+
e.Use(middleware.RequestLogger())
81+
e.GET("/", func(c *echo.Context) error {
82+
return c.HTML(http.StatusOK, `
83+
<h1>Welcome to Echo!</h1>
84+
<h3>TLS certificates automatically installed from Let's Encrypt :)</h3>
85+
`)
86+
})
87+
88+
autoTLSManager := autocert.Manager{
89+
Prompt: autocert.AcceptTOS,
90+
// Cache certificates to avoid issues with rate limits (https://letsencrypt.org/docs/rate-limits)
91+
Cache: autocert.DirCache("/var/www/.cache"),
92+
//HostPolicy: autocert.HostWhitelist("<DOMAIN>"),
93+
}
94+
s := http.Server{
95+
Addr: ":443",
96+
Handler: e, // set Echo as handler
97+
TLSConfig: &tls.Config{
98+
//Certificates: nil, // <-- s.ListenAndServeTLS will populate this field
99+
GetCertificate: autoTLSManager.GetCertificate,
100+
NextProtos: []string{acme.ALPNProto},
101+
},
102+
//ReadTimeout: 30 * time.Second, // use custom timeouts
103+
}
104+
if err := s.ListenAndServeTLS("", ""); err != nil && !errors.Is(err, http.ErrServerClosed) {
105+
e.Logger.Error("failed to start server", "error", err)
106+
}
107+
}
108+
```
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
title: CORS
3+
description: allow list またはカスタム origin 関数で Cross-Origin Resource Sharing を有効にします。
4+
sidebar:
5+
order: 4
6+
---
7+
8+
[CORS ミドルウェア](/ja/middleware/cors/)は、どの origin が API にアクセスできるかを制御します。
9+
許可する origin の固定リストを渡すことも、リクエストごとに判断する関数を指定することもできます。
10+
11+
## origin の allow list
12+
13+
許可する origin を `middleware.CORS` に直接渡します。
14+
15+
```go
16+
package main
17+
18+
import (
19+
"context"
20+
"net/http"
21+
22+
"github.com/labstack/echo/v5"
23+
"github.com/labstack/echo/v5/middleware"
24+
)
25+
26+
var (
27+
users = []string{"Joe", "Veer", "Zion"}
28+
)
29+
30+
func getUsers(c *echo.Context) error {
31+
return c.JSON(http.StatusOK, users)
32+
}
33+
34+
func main() {
35+
e := echo.New()
36+
e.Use(middleware.RequestLogger())
37+
e.Use(middleware.Recover())
38+
39+
// CORS default
40+
// Allows requests from any origin wth GET, HEAD, PUT, POST or DELETE method.
41+
// e.Use(middleware.CORS("*"))
42+
43+
// CORS restricted
44+
// Allows requests from any `https://labstack.com` or `https://labstack.net` origin
45+
e.Use(middleware.CORS("https://labstack.com", "https://labstack.net"))
46+
47+
e.GET("/api/users", getUsers)
48+
49+
sc := echo.StartConfig{Address: ":1323"}
50+
if err := sc.Start(context.Background(), e); err != nil {
51+
e.Logger.Error("failed to start server", "error", err)
52+
}
53+
}
54+
```
55+
56+
## カスタム origin 関数
57+
58+
動的ポリシーには、`UnsafeAllowOriginFunc` を指定した `CORSWithConfig` を使います。
59+
この関数はリクエストコンテキストと origin を受け取り、返す origin、リクエストを許可するか、
60+
任意のエラーを返します。
61+
62+
```go
63+
package main
64+
65+
import (
66+
"context"
67+
"net/http"
68+
"strings"
69+
70+
"github.com/labstack/echo/v5"
71+
"github.com/labstack/echo/v5/middleware"
72+
)
73+
74+
var (
75+
users = []string{"Joe", "Veer", "Zion"}
76+
)
77+
78+
func getUsers(c *echo.Context) error {
79+
return c.JSON(http.StatusOK, users)
80+
}
81+
82+
// allowOrigin takes the origin as an argument and returns:
83+
// - origin to add to the response Access-Control-Allow-Origin header
84+
// - whether the request is allowed or not
85+
// - an optional error. this will stop handler chain execution and return an error response.
86+
//
87+
// return origin, true, err // blocks request with error
88+
// return origin, true, nil // allows CSRF request through
89+
// return origin, false, nil // falls back to legacy token logic
90+
func allowOrigin(c *echo.Context, origin string) (string, bool, error) {
91+
// In this example we use a naive suffix check but we can imagine various
92+
// kind of custom logic. For example, an external datasource could be used
93+
// to maintain the list of allowed origins.
94+
if strings.HasSuffix(origin, ".example.com") {
95+
return origin, true, nil
96+
}
97+
return "", false, nil
98+
}
99+
100+
func main() {
101+
e := echo.New()
102+
e.Use(middleware.RequestLogger())
103+
e.Use(middleware.Recover())
104+
105+
// CORS restricted with a custom function to allow origins
106+
// and with the GET, PUT, POST or DELETE methods allowed.
107+
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
108+
UnsafeAllowOriginFunc: allowOrigin,
109+
AllowMethods: []string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete},
110+
}))
111+
112+
e.GET("/api/users", getUsers)
113+
114+
sc := echo.StartConfig{Address: ":1323"}
115+
if err := sc.Start(context.Background(), e); err != nil {
116+
e.Logger.Error("failed to start server", "error", err)
117+
}
118+
}
119+
```

0 commit comments

Comments
 (0)