Closed4

golang環境を作る(github codespaces/copilot agent)

not75743not75743

github codespaces

.devcontainer周りのファイルができたら実行する

マシンの作成

gh codespace create \
	--display-name <表示名> \
	--repo <リポジトリ名> \
	--branch main \
	--machine standardLinux32gb

作成出来るマシン一覧、コスト
https://docs.github.com/ja/billing/managing-billing-for-your-products/managing-billing-for-github-codespaces/about-billing-for-github-codespaces#pricing-for-paid-usage

マシンの利用(vscode)

gh codespace code

マシンの削除

gh codespace delete --force
not75743not75743

devcontainer編

golang + docker環境を作成する

.devcontainer/devcontainer.json, Dockerfile, docker-compose.yaml
.devcontainer/devcontainer.json
// README at: https://github.com/devcontainers/templates/tree/main/src
{
	"name": "golang",
	"dockerComposeFile": "docker-compose.yaml",
	"service": "app",
	"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
	"customizations": {
		"vscode": {
			"extensions": [
				"GitHub.copilot",
				"GitHub.copilot-chat"
			]
		}
	},

	// Features to add to the dev container. More info: https://containers.dev/features.
	"features": {
		"ghcr.io/devcontainers/features/docker-in-docker:2": {}
	},

	// Use 'postCreateCommand' to run commands after the container is created.
	"postCreateCommand": "bash .devcontainer/postCreateCommand.sh"
}
.devcontainer/Dockerfile
FROM mcr.microsoft.com/devcontainers/go:1-1.23-bookworm

# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
#     && apt-get -y install --no-install-recommends <your-package-list-here>

# install aqua
RUN mkdir aqua && curl -L https://github.com/aquaproj/aqua/releases/download/v2.48.1/aqua_linux_amd64.tar.gz -o aqua/aqua_linux_amd64.tar.gz \
    && tar -xzf aqua/aqua_linux_amd64.tar.gz -C aqua/ \
    && install -m 0755 aqua/aqua /usr/local/bin/aqua \
    && rm -r aqua
ENV PATH=/home/vscode/.local/share/aquaproj-aqua/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
.devcontainer/docker-compose.yaml
services:
  app:
    build: 
      context: .
      dockerfile: Dockerfile
    volumes:
      - ../..:/workspaces:cached
    command: sleep infinity

vscode拡張機能をインストールする

copilot agentを使えるように

.devcontainer/devcontainer.json
	"customizations": {
		"vscode": {
			"extensions": [
				"GitHub.copilot",
				"GitHub.copilot-chat"
			]
		}
	},

ツールをバックグラウンドでインストールする

postcreatecommandでツールインストールなどを実行させる

	// Use 'postCreateCommand' to run commands after the container is created.
	"postCreateCommand": "bash .devcontainer/postCreateCommand.sh"
.devcontainer/postCreateCommand.sh
# install tools
aqua i -l

# install go tools
## air
go install github.com/air-verse/air@v1.61.7

## swaggo
go install github.com/swaggo/swag/cmd/swag@v1.16.4
go get -u github.com/swaggo/http-swagger/v2@v2.0.2
not75743not75743

golang編

airによるホットリロード

https://github.com/air-verse/air

air init

swaggoによるAPIドキュメント環境

https://github.com/swaggo/swag
https://github.com/swaggo/http-swagger

サンプル
package main

import (
	"fmt"
	"log"
	"net/http"

	_ "hogefuga/docs" // 生成されたSwaggerドキュメントをインポート

	httpSwagger "github.com/swaggo/http-swagger/v2"
)

// @title hoge
// @version 1.0
// @description fuga
// @host localhost:8080
// @BasePath /
// @schemes http
func main() {
	// Hello World メッセージ表示
	fmt.Println("Starting Gazou API server...")

	// ルートハンドラ - Hello Worldを返す
	http.HandleFunc("/", HomeHandler)

	// 挨拶APIエンドポイント
	http.HandleFunc("/greeting", GreetingHandler)

	// Swagger UIエンドポイントを追加
	http.Handle("/swagger/", httpSwagger.Handler(
		httpSwagger.URL("http://localhost:8080/swagger/doc.json"),
		httpSwagger.DeepLinking(true),
		httpSwagger.DocExpansion("none"),
	))

	// サーバー起動
	log.Println("Server started at :8080")
	log.Fatal(http.ListenAndServe(":8080", nil))
}

// HomeHandler はルートパス用のハンドラ
func HomeHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello World!")
}

// GreetingHandler godoc
// @Summary 挨拶メッセージを返す
// @Description 名前を受け取り挨拶メッセージを返すAPI
// @Accept json
// @Produce json
// @Param name query string false "名前"
// @Success 200 {object} map[string]interface{}
// @Router /greeting [get]
func GreetingHandler(w http.ResponseWriter, r *http.Request) {
	name := r.URL.Query().Get("name")
	if name == "" {
		name = "Guest"
	}
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprintf(w, `{"message": "Hello, %s!"}`, name)
}
not75743not75743

postgresとminioをセットアップする

docker-compose.yaml
services:
  postgres:
    image: postgres:15
    container_name: image-api-postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: <db名>
    ports:
      - "5433:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    restart: unless-stopped

  minio:
    image: minio/minio:latest
    container_name: image-api-minio
    environment:
      MINIO_ROOT_USER: minio
      MINIO_ROOT_PASSWORD: minio123
    ports:
      - "9000:9000"
      - "9001:9001"
    volumes:
      - minio_data:/data
    command: server /data --console-address ":9001"
    restart: unless-stopped

volumes:
  postgres_data:
  minio_data:
このスクラップは5ヶ月前にクローズされました