ファイバーフックス

Fiberフレームワークが提供するフックを通じて、フレームワークの動作をインターセプトし、重要なポイントでロジックをカスタマイズすることができます。

Fiber v2.30.0からは、特定のメソッドを実行する際にカスタムユーザー関数を実行できます。以下はこれらのフックのリストです。

  • OnRoute(ルートフック)
  • OnName(名前フック)
  • OnGroup(グループフック)
  • OnGroupName(グループ名フック)
  • OnListen(リッスンフック)
  • OnFork(フォークフック)
  • OnShutdown(シャットダウンフック)
  • OnMount(マウントフック)

定数

// Handlers define a function to create hooks for Fiber.
type OnRouteHandler = func(Route) error
type OnNameHandler = OnRouteHandler
type OnGroupHandler = func(Group) error
type OnGroupNameHandler = OnGroupHandler
type OnListenHandler = func(ListenData) error
type OnForkHandler = func(int) error
type OnShutdownHandler = func() error
type OnMountHandler = func(*App) error

OnRoute(ルートフック)

OnRouteは、ルートが登録されるたびにユーザー関数を実行するフックです。route パラメータを介してルートのプロパティにアクセスすることもできます。

シグネチャ

func (app *App) OnRoute(handler ...OnRouteHandler)

OnName(名前フック)

OnNameは、ルートに名前が付けられるたびにユーザー関数を実行するフックです。route パラメータを介してルートのプロパティにアクセスすることもできます。

注:OnNameは名前付きルートにのみ適用され、グループには適用されません。

シグネチャ

func (app *App) OnName(handler ...OnNameHandler)
  • OnNameの例
package main

import (
    "fmt"
    "github.com/gofiber/fiber/v2"
)

func main() {
    app := fiber.New()

    app.Get("/", func(c *fiber.Ctx) error {
        return c.SendString(c.Route().Name)
    }).Name("index")

    app.Hooks().OnName(func(r fiber.Route) error {
        fmt.Print("Name: " + r.Name + ", ")
        return nil
    })

    app.Hooks().OnName(func(r fiber.Route) error {
        fmt.Print("Method: " + r.Method + "\n")
        return nil
    })

    app.Get("/add/user", func(c *fiber.Ctx) error {
        return c.SendString(c.Route().Name)
    }).Name("addUser")

    app.Delete("/destroy/user", func(c *fiber.Ctx) error {
        return c.SendString(c.Route().Name)
    }).Name("destroyUser")

    app.Listen(":5000")
}

// 結果:
// Name: addUser, Method: GET
// Name: destroyUser, Method: DELETE

OnGroup(グループフック)

OnGroupは、グループが登録されるたびにユーザー関数を実行するフックです。group パラメータを介してグループのプロパティにアクセスすることもできます。

シグネチャ

func (app *App) OnGroup(handler ...OnGroupHandler)

OnGroupName(グループ名フック)

OnGroupNameは、グループに名前が付けられるたびにユーザー関数を実行するフックです。group パラメータを介してグループのプロパティにアクセスすることもできます。

注:OnGroupNameは名前付きグループにのみ適用され、ルートには適用されません。

シグネチャ

func (app *App) OnGroupName(handler ...OnGroupNameHandler)

OnListen(リッスンフック)

OnListen フックは、Listen、ListenTLS、またはListenerの操作を実行する際にユーザー定義の関数を実行するために使用されます。

シグネチャ

func (app *App) OnListen(handler ...OnListenHandler)
  • OnListenの例
app := fiber.New(fiber.Config{
  DisableStartupMessage: true,
})

app.Hooks().OnListen(func(listenData fiber.ListenData) error {
  if fiber.IsChild() {
      return nil
  }
  scheme := "http"
  if listenData.TLS {
    scheme = "https"
  }
  log.Println(scheme + "://" + listenData.Host + ":" + listenData.Port)
  return nil
})

app.Listen(":5000")

OnFork(フォークフック)

OnFork フックは、Forkの操作を実行する際にユーザー定義の関数を実行するために使用されます。

シグネチャ

func (app *App) OnFork(handler ...OnForkHandler)

OnShutdown(シャットダウンフック)

OnShutdown フックは、シャットダウンの操作を実行した後にユーザー定義の関数を実行するために使用されます。

シグネチャ

func (app *App) OnShutdown(handler ...OnShutdownHandler)

OnMount(マウント時)

OnMount は、マウント処理の後にユーザー定義の関数を実行するフックです。サブアプリケーションが親アプリケーションにマウントされると、マウントイベントがトリガーされ、親アプリケーションがパラメータとして渡されます。アプリケーションやグループのマウントに適しています。

シグネチャ

func (h *Hooks) OnMount(handler ...OnMountHandler) 
  • OnMountの例
package main

import (
    "fmt"
    "github.com/gofiber/fiber/v2"
)

func main() {
    app := New()
    app.Get("/", testSimpleHandler).Name("x")

    subApp := New()
    subApp.Get("/test", testSimpleHandler)
    
    subApp.Hooks().OnMount(func(parent *fiber.App) error {
        fmt.Print("親アプリケーションのマウントパス: "+parent.MountPath())
        // ...

        return nil
    })

    app.Mount("/sub", subApp)
}

// 結果:
// 親アプリケーションのマウントパス: 

注意: OnName/OnRoute/OnGroup/OnGroupName フックはマウントに対してセンシティブです。サブアプリケーションでこれらのルートを使用し、マウントすると、ルートとグループのパスはマウントプレフィックスで始まります。