Skip to content

Commit

Permalink
Merge branch 'main' into fix/1-4_fmt-to-log
Browse files Browse the repository at this point in the history
  • Loading branch information
Takeno-hito authored Apr 12, 2024
2 parents 9b36494 + e1d7c69 commit a9dc994
Show file tree
Hide file tree
Showing 58 changed files with 3,711 additions and 555 deletions.
20 changes: 16 additions & 4 deletions .textlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"plugins": {},
"filters": {
"allowlist": {
"allow": [":::", "/:::.*/", "リーダブルコード"]
"allow": [
":::",
"/:::.*/",
"リーダブルコード"
]
}
},
"rules": {
Expand All @@ -14,7 +18,13 @@
"sentence-length": false,
"no-exclamation-question-mark": false,
"max-kanji-continuous-len": {
"allow": ["開発基礎講習会"]
"allow": [
"開発基礎講習会",
"静的経路制御",
"経路制御手法",
"動的経路制御",
"直接相互接続"
]
}
},
"preset-ja-spacing": {
Expand All @@ -24,7 +34,9 @@
},
"spellcheck-tech-word": true,
"prh": {
"rulePaths": ["./prh.yaml"]
"rulePaths": [
"./prh.yaml"
]
}
}
}
}
4 changes: 3 additions & 1 deletion docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineConfig } from 'vitepress'
import { chapter1SidebarItems } from './sidebarConfigs/chapters/chapter1/chapter1'
import { chapter2SidebarItems } from './sidebarConfigs/chapters/chapter2/chapter2'
import { chapter4SidebarItems } from './sidebarConfigs/chapters/chapter4/chapter4'

// https://vitepress.dev/reference/site-config
export default defineConfig({
Expand All @@ -20,7 +21,8 @@ export default defineConfig({

sidebar: {
'/chapter1/': chapter1SidebarItems,
'/chapter2/': chapter2SidebarItems
'/chapter2/': chapter2SidebarItems,
'/chapter4/': chapter4SidebarItems
},
socialLinks: [{ icon: 'github', link: 'https://github.com/traPtitech/naro-text' }],
search: {
Expand Down
9 changes: 9 additions & 0 deletions docs/.vitepress/sidebarConfigs/chapters/chapter4/chapter4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { DefaultTheme } from 'vitepress'
import { sidebarItems } from './sidebar'

export const chapter4SidebarItems: DefaultTheme.SidebarItem[] = [
{
text: '第四部',
items: [{ text: 'はじめに', link: '/chapter4/0_index' }, ...sidebarItems]
}
]
14 changes: 14 additions & 0 deletions docs/.vitepress/sidebarConfigs/chapters/chapter4/sidebar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { DefaultTheme } from 'vitepress'
export const sidebarItems: DefaultTheme.SidebarItem[] = [
{ text: 'Docs', link: '/chapter4/docs' },
{ text: '1. IP Address', link: '/chapter4/1_ip_address' },
{ text: '2. NAT', link: '/chapter4/2_nat' },
{ text: '3. Routing 1', link: '/chapter4/3_routing_1' },
{ text: '4. Routing 2', link: '/chapter4/4_routing_2' },
{ text: '5. DHCP', link: '/chapter4/5_dhcp' },
{ text: '6. IP Tunneling', link: '/chapter4/6_ip_tunneling' },
{ text: '7. BGP', link: '/chapter4/7_bgp' },
{ text: '8. DNS 1', link: '/chapter4/8_dns_1' },
{ text: '9. DNS 2', link: '/chapter4/9_dns_2' },
{ text: '10. DNS 3', link: '/chapter4/10_dns_3' }
]
4 changes: 2 additions & 2 deletions docs/.vitepress/theme/trap.css
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@
--vp-code-block-bg-light: #1e1e20;
--vp-code-block-divider-color: #000000;

--vp-code-line-highlight-color: rgba(0, 0, 0, 0.5);
--vp-code-line-highlight-color: rgba(255, 255, 0, 0.15);
--vp-code-line-number-color: var(--vp-c-code-dimm);

--vp-code-line-diff-add-color: var(--vp-c-green-dimm-2);
Expand Down Expand Up @@ -285,7 +285,7 @@
--vp-code-block-bg: #161618;
--vp-code-block-divider-color: var(--vp-c-divider);

--vp-code-line-highlight-color: #ececec;
--vp-code-line-highlight-color: rgba(255, 255, 0, 0.15);
--vp-code-line-number-color: var(--vp-c-code-dimm);

--vp-code-copy-code-bg: var(--vp-code-block-bg-light);
Expand Down
6 changes: 3 additions & 3 deletions docs/chapter1/dicts/ssh/0_index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SSH鍵の登録

GitHub に push/pull などをするには、SSH 鍵を登録する必要があります(Personal Access Token でも可能ですが、SSH の方が便利です)。
常に鍵を登録してあるかどうかは [SSH and GPG keys](https://github.com/settings/keys) で確認できます。
GitHub に push/pull などをするには、SSH 鍵を登録する必要があります(Personal Access Token でも可能ですが、SSH の方が便利です)。
既に鍵を登録してあるかどうかは [SSH and GPG keys](https://github.com/settings/keys) で確認できます。

まだ登録していない人は以下の手順で作成・登録してください。
### 鍵の生成
Expand Down Expand Up @@ -70,4 +70,4 @@ Hi {ユーザー名}! You've successfully authenticated, but GitHub does not pro
```bash
$ git remote set-url origin {SSHのURI}
```
```
6 changes: 3 additions & 3 deletions docs/chapter1/section1/0_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Windows: `Winキー + Shift + S`を押すと、矩形選択でスクリーンシ

## WSL の導入(Windows を使っている人のみ)

すでに WSL をインストールしている方はこの手順は飛ばして大丈夫です
すでに WSL をインストールしている方はこの手順を飛ばして大丈夫です

WSL は Windows 上で Linux を動かすための仕組みで、`Windows Subsystem for Linux`の略です。

Expand All @@ -34,7 +34,7 @@ Homebrew とは、様々なアプリケーションをインストールしや

## VSCode の導入

すでに VSCode をインストールしている方はこの手順は飛ばして大丈夫です
すでに VSCode をインストールしている方はこの手順を飛ばして大丈夫です

以下のサイトから使用している OS に合った VSCode のインストーラーをダウンロードして、それを実行してインストールしてください。

Expand Down Expand Up @@ -66,7 +66,7 @@ https://golang.org/doc/install

Mac のタブを選択し、ダウンロードページに飛んで自分のアーキテクチャの pkg をダウンロード=>インストーラ起動で設定完了です。

もしくはHomebrewがすでにインストールされている人は`brew install [email protected]`を実行することでも導入できます。
もしくは Homebrew がすでにインストールされている人は`brew install [email protected]`を実行することでも導入できます。

::: info
M1/M2 Mac の人は Apple macOS (ARM64) を、Intel Mac の人は Apple macOS (x86-64) を選択してください。
Expand Down
4 changes: 2 additions & 2 deletions docs/chapter1/section2/0_vue-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ $ brew install node
```
2. PATH を通す

1.を実行すると、最後に`If you need to have node first in your PATH, run:`というメッセージが出るので、これに続くコマンドを実行してください。
前述のコマンドを実行すると、最後に`If you need to have node first in your PATH, run:`というメッセージが出るので、これに続くコマンドを実行してください。

3. バージョンを確認

```zsh
$ node -v
```

を実行して、バージョン番号が表示されれば OK。
上記のコマンドを実行して、バージョン番号が表示されれば OK。

#### Windows(WSL)

Expand Down
3 changes: 2 additions & 1 deletion docs/chapter1/section4/src/practice_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ func getCityInfoHandler(c echo.Context) error {
log.Println(cityName)

var city City
if err := db.Get(&city, "SELECT * FROM city WHERE Name=?", cityName); errors.Is(err, sql.ErrNoRows) {
err := db.Get(&city, "SELECT * FROM city WHERE Name=?", cityName)
if errors.Is(err, sql.ErrNoRows) {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("No such city Name = %s", cityName))
}
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion docs/chapter1/section4/src/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ func getCityInfoHandler(c echo.Context) error {
log.Println(cityName)

var city City
if err := db.Get(&city, "SELECT * FROM city WHERE Name=?", cityName); errors.Is(err, sql.ErrNoRows) {
err := db.Get(&city, "SELECT * FROM city WHERE Name=?", cityName)
if errors.Is(err, sql.ErrNoRows) {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("No such city Name = %s", cityName))
}
if err != nil {
Expand Down
65 changes: 45 additions & 20 deletions docs/chapter2/section1/0_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,70 @@
2. `.env` ファイルを作成し、以下のように編集しましょう。

```sh
export DB_USERNAME="root"
export DB_PASSWORD="password"
export DB_HOSTNAME="localhost"
export DB_PORT="3306"
export DB_DATABASE="world"
DB_USERNAME="root"
DB_PASSWORD="password"
DB_HOSTNAME="localhost"
DB_PORT="3306"
DB_DATABASE="world"
```

3. `source .env` を実行しましょう。

4. `go mod tidy` を実行しましょう。
3. `go mod tidy` を実行しましょう。

以上でセットアップはできているはずです。

## ファイルの分割

このまま演習を始めてしまうとファイルが長くなりすぎてしまうので、ファイルを分割します。
各エンドポイントでの処理はハンドラーと呼ばれますが、それを `handler.go` に移動してみましょう。
このまま演習を始めてしまうとファイルが長くなりすぎてしまうので、ファイルを別のパッケージとして分割します。
:::tip
パッケージとは、関連する複数のファイルをまとめる単位のことです。
ディレクトリとパッケージは一対一に対応しています。原則的に、ディレクトリ名とパッケージ名は同じにします。
パッケージによって、機能を分離でき、変数や関数の公開範囲を最低限にできる等沢山の恩恵が得られます。
パッケージの外部に公開する変数や関数などのシンボルは、先頭を大文字にする必要があります。
逆に言えば、先頭が大文字でないシンボルは、パッケージの外部からはアクセスできません。
詳しくは以下を参照してください。
[A Tour of Go - Packages](https://go.dev/tour/basics/1)
[Effective Go - package-names](https://golang.org/doc/effective_go#package-names)
:::
各エンドポイントでの処理はハンドラーと呼ばれますが、それを `handler/handler.go` に移動してみましょう。手順は以下の通りです。

## main.go の作成
### handler.go の作成

main.go を以下のようにしましょう。
1. `handler` というディレクトリを新しく作成し、その中に `handler.go` というファイルを作成する。
2. `handler.go` を以下のように記述する。

<<<@/chapter2/section1/src/0/main.go{go:line-numbers}
<<<@/chapter2/section1/src/first/handler.go{go:line-numbers}

ファイルを編集したら、`go mod tidy` を実行しましょう。

## handler.go の作成
### main.go の編集

1. 同じディレクトリに新しく `handler.go` というファイルを作成する
2. `handler.go` を以下のように記述する
`main.go`を以下のように編集しましょう。

<<<@/chapter2/section1/src/0/handler.go{go:line-numbers}
<<<@/chapter2/section1/src/first/main.go{go:line-numbers}

ファイルを編集したら、`go mod tidy` を実行しましょう。
ファイルを編集したら、`go mod tidy` を実行しましょう。
![](images/0/file-tree.png)
ここまで出来たら、画像のようになっているはずです。

## 準備完了

今回は `main.go` 以外に `handler.go` も存在するので、どちらも指定して `go run *.go` を実行しましょう。
ファイルの分割で変更したのは、以下の 3 点です。

1. `handler`パッケージを作成し、コードを分割した。
2. `handler`という`db`をフィールドに持つ構造体を作成し、その構造体のメソッドとして`GetCityInfoHandler``PostCityHandler`を定義した。
3. `.env`ファイルの環境変数を、プログラムで読むようにした。

それでは、`go run main.go` で実行してみましょう。
:::tip
`main package`を複数ファイルに分割した場合、`go run main.go`だと`main.go`のみがビルドされるため、
`go run .``go run main1.go main2.go`のようにして複数ファイルを読み込む必要があります。
詳しくは`go help run`を参照してください。
:::

![](images/0/echo.png)

無事起動が確認できたら、 `Ctrl+C` で一旦止めましょう。
無事起動が出来たら、ターミナルで`task up`を実行してデーターベースを起動し、<a href="http://localhost:8080/cities/Tokyo">localhost:8080/cities/Tokyo</a>にアクセスして実際に動いていることを確認しましょう。

![](images/0/Tokyo.png)

上手く動いていることを確認できたら、 `Ctrl+C` で一旦止めましょう。
Loading

0 comments on commit a9dc994

Please sign in to comment.