fn project 扩展

目前支持的扩展方式
 
  1. Listeners - listen to API events such as a route getting updated and react accordingly.
  2. Middleware - a chain of middleware is executed before an API handler is called.
  3. Add API Endpoints - extend the default Fn API.
 
具体的官方介绍
 

Listeners

Listeners are the main way to extend Fn.

The following listener types are supported:

    • App Listeners - GoDoc
    • Runner Listeners - GoDoc

Creating a Listener

You can easily use app and runner listeners by creating a struct with valid methods satisfying the interface for the respective listener and adding it to the Fn API

Example:

package main

import (
    "context"

    "github.com/fnproject/functions/api/server"
    "github.com/fnproject/functions/api/models"
)

type myCustomListener struct{}

func (c *myCustomListener) BeforeAppCreate(ctx context.Context, app *models.App) error { return nil }
func (c *myCustomListener) AfterAppCreate(ctx context.Context, app *models.App) error { return nil }

func (c *myCustomListener) BeforeAppUpdate(ctx context.Context, app *models.App) error { return nil }
func (c *myCustomListener) AfterAppUpdate(ctx context.Context, app *models.App) error { return nil }

func (c *myCustomListener) BeforeAppDelete(ctx context.Context, app *models.App) error { return nil }
func (c *myCustomListener) BeforeAppDelete(ctx context.Context, app *models.App) error { return nil }

function main () {
    srv := server.New(/* Here all required parameters to initialize the server */)

    srv.AddAppListener(myCustomListener)

    srv.Run()
}

Middleware

Middleware enables you to add functionality to every API request. For every request, the chain of Middleware will be called in order, allowing you to modify or reject requests, as well as write output and cancel the chain.

NOTES:

      • middleware is responsible for writing output if it's going to cancel the chain.
      • cancel the chain by returning an error from your Middleware's Serve method.

See examples of this in examples/middleware/main.go.

Adding API Endpoints

You can add API endpoints to the Fn server by using the AddEndpoint and AddEndpointFunc methods.

See examples of this in examples/extensions/main.go.

 

posted on   荣锋亮  阅读(283)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2014-10-05 p/Invoke工具

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示