検索
ホームページバックエンド開発GolangGin、FerretDB、oapi-codegen を使用したブログ API の構築

Building a Blog API with Gin, FerretDB, and oapi-codegen

このチュートリアルでは、Go を使用して単純なブログ アプリケーション用の RESTful API を作成するプロセスを説明します。次のテクノロジーを使用します:

  1. Gin: Go 用の Web フレームワーク
  2. FerretDB: MongoDB 互換データベース
  3. oapi-codegen: OpenAPI 3.0 仕様から Go サーバー定型文を生成するツール

目次

  1. プロジェクトのセットアップ
  2. API 仕様の定義
  3. サーバーコードの生成
  4. データベース層の実装
  5. API ハンドラーの実装
  6. アプリケーションの実行
  7. API のテスト
  8. 結論

プロジェクトのセットアップ

まず、Go プロジェクトをセットアップし、必要な依存関係をインストールしましょう:

mkdir blog-api
cd blog-api
go mod init github.com/yourusername/blog-api
go get github.com/gin-gonic/gin
go get github.com/deepmap/oapi-codegen/cmd/oapi-codegen
go get github.com/FerretDB/FerretDB

API仕様の定義

プロジェクト ルートに api.yaml という名前のファイルを作成し、ブログ API の OpenAPI 3.0 仕様を定義します。

openapi: 3.0.0
info:
  title: Blog API
  version: 1.0.0
paths:
  /posts:
    get:
      summary: List all posts
      responses:
        '200':
          description: Successful response
          content:
            application/json:    
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Post'
    post:
      summary: Create a new post
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/NewPost'
      responses:
        '201':
          description: Created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Post'
  /posts/{id}:
    get:
      summary: Get a post by ID
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Post'
    put:
      summary: Update a post
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/NewPost'
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Post'
    delete:
      summary: Delete a post
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      responses:
        '204':
          description: Successful response

components:
  schemas:
    Post:
      type: object
      properties:
        id:
          type: string
        title:
          type: string
        content:
          type: string
        createdAt:
          type: string
          format: date-time
        updatedAt:
          type: string
          format: date-time
    NewPost:
      type: object
      required:
        - title
        - content
      properties:
        title:
          type: string
        content:
          type: string

サーバーコードの生成

ここで、oapi-codegen を使用して、API 仕様に基づいてサーバー コードを生成しましょう。

oapi-codegen -package api api.yaml > api/api.go

このコマンドは、api という新しいディレクトリを作成し、サーバー インターフェイスとモデルを含む api.go ファイルを生成します。

データベース層の実装

db/db.go という新しいファイルを作成し、FerretDB を使用してデータベース層を実装します。

package db

import (
    "context"
    "time"

    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/bson/primitive"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)

type Post struct {
    ID primitive.ObjectID `bson:"_id,omitempty"`
    Title string `bson:"title"`
    Content string `bson:"content"`
    CreatedAt time.Time `bson:"createdAt"`
    UpdatedAt time.Time `bson:"updatedAt"`
}

type DB struct {
    client *mongo.Client
    posts *mongo.Collection
}

func NewDB(uri string) (*DB, error) {
    client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(uri))
    if err != nil {
        return nil, err
    }

    db := client.Database("blog")
    posts := db.Collection("posts")

    return &DB{
        client: client,
        posts: posts,
    }, nil
}

func (db *DB) Close() error {
    return db.client.Disconnect(context.Background())
}

func (db *DB) CreatePost(title, content string) (*Post, error) {
    post := &Post{
        Title: title,
        Content: content,
        CreatedAt: time.Now(),
        UpdatedAt: time.Now(),
    }

    result, err := db.posts.InsertOne(context.Background(), post)
    if err != nil {
        return nil, err
    }

    post.ID = result.InsertedID.(primitive.ObjectID)
    return post, nil
}

func (db *DB) GetPost(id string) (*Post, error) {
    objectID, err := primitive.ObjectIDFromHex(id)
    if err != nil {
        return nil, err
    }

    var post Post
    err = db.posts.FindOne(context.Background(), bson.M{"_id": objectID}).Decode(&post)
    if err != nil {
        return nil, err
    }

    return &post, nil
}

func (db *DB) UpdatePost(id, title, content string) (*Post, error) {
    objectID, err := primitive.ObjectIDFromHex(id)
    if err != nil {
        return nil, err
    }

    update := bson.M{
        "$set": bson.M{
            "title": title,
            "content": content,
            "updatedAt": time.Now(),
        },
    }

    var post Post
    err = db.posts.FindOneAndUpdate(
        context.Background(),
        bson.M{"_id": objectID},
        update,
        options.FindOneAndUpdate().SetReturnDocument(options.After),
    ).Decode(&post)

    if err != nil {
        return nil, err
    }

    return &post, nil
}

func (db *DB) DeletePost(id string) error {
    objectID, err := primitive.ObjectIDFromHex(id)
    if err != nil {
        return err
    }

    _, err = db.posts.DeleteOne(context.Background(), bson.M{"_id": objectID})
    return err
}

func (db *DB) ListPosts() ([]*Post, error) {
    cursor, err := db.posts.Find(context.Background(), bson.M{})
    if err != nil {
        return nil, err
    }
    defer cursor.Close(context.Background())

    var posts []*Post
    for cursor.Next(context.Background()) {
        var post Post
        if err := cursor.Decode(&post); err != nil {
            return nil, err
        }
        posts = append(posts, &post)
    }

    return posts, nil
}

API ハンドラーの実装

API ハンドラーを実装するには、handlers/handlers.go という新しいファイルを作成します。

package handlers

import (
    "net/http"
    "time"

    "github.com/gin-gonic/gin"
    "github.com/yourusername/blog-api/api"
    "github.com/yourusername/blog-api/db"
)

type BlogAPI struct {
    db *db.DB
}

func NewBlogAPI(db *db.DB) *BlogAPI {
    return &BlogAPI{db: db}
}

func (b *BlogAPI) ListPosts(c *gin.Context) {
    posts, err := b.db.ListPosts()
    if err != nil {
        c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
        return
    }

    apiPosts := make([]api.Post, len(posts))
    for i, post := range posts {
        apiPosts[i] = api.Post{
            Id: post.ID.Hex(),
            Title: post.Title,
            Content: post.Content,
            CreatedAt: post.CreatedAt,
            UpdatedAt: post.UpdatedAt,
        }
    }

    c.JSON(http.StatusOK, apiPosts)
}

func (b *BlogAPI) CreatePost(c *gin.Context) {
    var newPost api.NewPost
    if err := c.ShouldBindJSON(&newPost); err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
        return
    }

    post, err := b.db.CreatePost(newPost.Title, newPost.Content)
    if err != nil {
        c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
        return
    }

    c.JSON(http.StatusCreated, api.Post{
        Id: post.ID.Hex(),
        Title: post.Title,
        Content: post.Content,
        CreatedAt: post.CreatedAt,
        UpdatedAt: post.UpdatedAt,
    })
}

func (b *BlogAPI) GetPost(c *gin.Context) {
    id := c.Param("id")
    post, err := b.db.GetPost(id)
    if err != nil {
        c.JSON(http.StatusNotFound, gin.H{"error": "Post not found"})
        return
    }

    c.JSON(http.StatusOK, api.Post{
        Id: post.ID.Hex(),
        Title: post.Title,
        Content: post.Content,
        CreatedAt: post.CreatedAt,
        UpdatedAt: post.UpdatedAt,
    })
}

func (b *BlogAPI) UpdatePost(c *gin.Context) {
    id := c.Param("id")
    var updatePost api.NewPost
    if err := c.ShouldBindJSON(&updatePost); err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
        return
    }

    post, err := b.db.UpdatePost(id, updatePost.Title, updatePost.Content)
    if err != nil {
        c.JSON(http.StatusNotFound, gin.H{"error": "Post not found"})
        return
    }

    c.JSON(http.StatusOK, api.Post{
        Id: post.ID.Hex(),
        Title: post.Title,
        Content: post.Content,
        CreatedAt: post.CreatedAt,
        UpdatedAt: post.UpdatedAt,
    })
}

func (b *BlogAPI) DeletePost(c *gin.Context) {
    id := c.Param("id")
    err := b.db.DeletePost(id)
    if err != nil {
        c.JSON(http.StatusNotFound, gin.H{"error": "Post not found"})
        return
    }

    c.Status(http.StatusNoContent)
}

アプリケーションの実行

アプリケーションをセットアップして実行するために、プロジェクト ルートに main.go という新しいファイルを作成します。

package main

import (
    "log"

    "github.com/gin-gonic/gin"
    "github.com/yourusername/blog-api/api"
    "github.com/yourusername/blog-api/db"
    "github.com/yourusername/blog-api/handlers"
)

func main() {
    // Initialize the database connection
    database, err := db.NewDB("mongodb://localhost:27017")
    if err != nil {
        log.Fatalf("Failed to connect to the database: %v", err)
    }
    defer database.Close()

    // Create a new Gin router
    router := gin.Default()

    // Initialize the BlogAPI handlers
    blogAPI := handlers.NewBlogAPI(database)

    // Register the API routes
    api.RegisterHandlers(router, blogAPI)

    // Start the server
    log.Println("Starting server on :8080")
    if err := router.Run(":8080"); err != nil {
        log.Fatalf("Failed to start server: %v", err)
    }
}

API のテスト

API を起動して実行できるようになったので、curl コマンドを使用してテストしてみましょう:

  1. 新しい投稿を作成します:
curl -X POST -H "Content-Type: application/json" -d '{"title":"My First Post","content":"This is the content of my first post."}' http://localhost:8080/posts

  1. すべての投稿をリストします:
curl http://localhost:8080/posts

  1. 特定の投稿を取得します ({id} を実際の投稿 ID に置き換えます):
curl http://localhost:8080/posts/{id}

  1. 投稿を更新します ({id} を実際の投稿 ID に置き換えます):
curl -X PUT -H "Content-Type: application/json" -d '{"title":"Updated Post","content":"This is the updated content."}' http://localhost:8080/posts/{id}

  1. 投稿を削除します ({id} を実際の投稿 ID に置き換えます):
curl -X DELETE http://localhost:8080/posts/{id}

結論

このチュートリアルでは、Gin フレームワーク、FerretDB、oapi-codegen を使用してシンプルなブログ API を構築しました。以下の手順を説明しました:

  1. プロジェクトのセットアップと依存関係のインストール
  2. OpenAPI 3.0 を使用した API 仕様の定義
  3. oapi-codegen を使用したサーバー コードの生成
  4. FerretDB を使用したデータベース層の実装
  5. API ハンドラーの実装
  6. アプリケーションの実行
  7. curl コマンドを使用した API のテスト

このプロジェクトでは、コード生成と MongoDB 互換データベースの力を活用して、Go で RESTful API を作成する方法を示します。認証、ページネーション、より複雑なクエリ機能を追加することで、この API をさらに拡張できます。

この API を運用環境にデプロイする前に、エラーを適切に処理し、適切なログを追加し、セキュリティ対策を実装してください。


助けが必要ですか?

困難な問題に直面していますか、それとも新しいアイデアやプロジェクトに関して外部の視点が必要ですか?お手伝いできます!大規模な投資を行う前にテクノロジーの概念実証を構築したい場合でも、難しい問題についてのガイダンスが必要な場合でも、私がお手伝いいたします。

提供されるサービス:

  • 問題解決: 革新的なソリューションで複雑な問題に取り組みます。
  • コンサルティング: プロジェクトに関する専門家のアドバイスと新鮮な視点を提供します。
  • 概念実証: アイデアをテストおよび検証するための予備モデルを開発します。

私との仕事にご興味がございましたら、hungaikevin@gmail.com まで電子メールでご連絡ください。

課題をチャンスに変えましょう!

以上がGin、FerretDB、oapi-codegen を使用したブログ API の構築の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
Go String Manipulationを学ぶ:「文字列」パッケージを使用しますGo String Manipulationを学ぶ:「文字列」パッケージを使用しますMay 09, 2025 am 12:07 AM

Goの「文字列」パッケージは、文字列操作を効率的かつシンプルにするための豊富な機能を提供します。 1)文字列を使用して()サブストリングを確認します。 2)Strings.split()を使用してデータを解析できますが、パフォーマンスの問題を回避するには注意して使用する必要があります。 3)文字列join()は文字列のフォーマットに適していますが、小さなデータセットの場合、ループ=はより効率的です。 4)大きな文字列の場合、文字列を使用して文字列を構築する方が効率的です。

GO:標準の「文字列」パッケージを使用した文字列操作GO:標準の「文字列」パッケージを使用した文字列操作May 09, 2025 am 12:07 AM

GOは、文字列操作に「文字列」パッケージを使用します。 1)文字列を使用して、関数を調整して文字列をスプライスします。 2)文字列を使用して、コンテイン関数を使用してサブストリングを見つけます。 3)文字列を使用して、文字列を交換します。これらの機能は効率的で使いやすく、さまざまな文字列処理タスクに適しています。

Goの「バイト」パッケージを使用したバイトスライス操作の習得:実用的なガイドGoの「バイト」パッケージを使用したバイトスライス操作の習得:実用的なガイドMay 09, 2025 am 12:02 AM

byteSpackageIngoisESSENTINEFOREFFICTIENTBYTESLICEMANIPULATION、functionslikeContains、andReplaceforseding andmodyifiedbinarydata.itenhancesperformance andCodereadability、make dakeatavitaltoolfor forhandlingbingbinarydata、networkprotocols、andfilei

Go Binary Encoding/Decoding:「エンコード/バイナリ」パッケージを使用してくださいGo Binary Encoding/Decoding:「エンコード/バイナリ」パッケージを使用してくださいMay 08, 2025 am 12:13 AM

GOは、バイナリエンコードとデコードに「エンコード/バイナリ」パッケージを使用します。 1)このパッケージは、binary.writeとbinary.read関数を作成して、データを書き込み、読み取ります。 2)正しいエンディアン(BigendianやLittleendianなど)の選択に注意してください。 3)データのアラインメントとエラー処理も重要です。データの正確性とパフォーマンスを確保します。

GO:標準の「バイト」パッケージを使用したバイトスライス操作GO:標準の「バイト」パッケージを使用したバイトスライス操作May 08, 2025 am 12:09 AM

「バイト」パッケージを包装してください

エンコード/バイナリパッケージに移動:バイナリ操作のパフォーマンスの最適化エンコード/バイナリパッケージに移動:バイナリ操作のパフォーマンスの最適化May 08, 2025 am 12:06 AM

Encoding/binaryPackageIngoiseffictevectiveforptimizingdueToitssuportforendiannessandannessandAhandling.toenhanceperformance:1)usebinary.native.nativedianfornatiannesstoavoidbyteswapping.2)batchedandandandwriteTerationtoredutei/ober

BYTESパッケージに移動:短いリファレンスとヒントBYTESパッケージに移動:短いリファレンスとヒントMay 08, 2025 am 12:05 AM

GOのBYTESパッケージは、主にバイトスライスを効率的に処理するために使用されます。 1)bytes.bufferを使用すると、弦のスプライシングを効率的に実行して、不必要なメモリの割り当てを避けます。 2)バイト機能を使用して、バイトスライスをすばやく比較します。 3)bytes.index、bytes.split、bytes.replaceall関数は、バイトスライスの検索と操作に使用できますが、パフォーマンスの問題に注意する必要があります。

BYTESパッケージに移動:バイトスライス操作の実用的な例BYTESパッケージに移動:バイトスライス操作の実用的な例May 08, 2025 am 12:01 AM

バイトパッケージは、バイトスライスを効率的に処理するためのさまざまな機能を提供します。 1)bytes.containsを使用して、バイトシーケンスを確認します。 2)bytes.splitを使用してバイトスライスを分割します。 3)バイトシーケンスバイトを交換します。 4)bytes.joinを使用して、複数のバイトスライスを接続します。 5)bytes.bufferを使用してデータを作成します。 6)エラー処理とデータ検証のためのBYTES.MAPの組み合わせ。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター

Dreamweaver Mac版

Dreamweaver Mac版

ビジュアル Web 開発ツール

mPDF

mPDF

mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール