hibiragin

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Hibira for Gin

github.com/hibira-io/gin connects Hibira to Gin: channel authorization and user sign-in endpoints for pusher-js, webhook and Approvals callback receivers, and a waiting room middleware. Your callbacks get Gin's own context, so whatever your auth middleware stored there is at hand.

go get github.com/hibira-io/gin

Go 1.21 or later. The core client is github.com/hibira-io/go; this module adds only the Gin glue.

client := hibira.FromEnv()
r := gin.Default()

api := r.Group("/hibira", requireLogin)
api.POST("/auth", hibiragin.Auth(client, func(c *gin.Context, channel string) (bool, *hibira.Member) {
	u := c.MustGet("user").(*User)
	if channel == "private-user-"+u.ID {
		return true, nil
	}
	return strings.HasPrefix(channel, "presence-room-"), &hibira.Member{UserID: u.ID, UserInfo: gin.H{"name": u.Name}}
}))
api.POST("/user-auth", hibiragin.UserAuth(client, func(c *gin.Context) *hibira.User {
	return &hibira.User{ID: c.MustGet("user").(*User).ID}
}))

r.POST("/hibira/webhook", hibiragin.Webhook(client, func(c *gin.Context, hook *hibira.Webhook) error {
	for _, e := range hook.Events {
		log.Println(e.Name, e.Channel)
	}
	return nil
}))
r.POST("/hibira/approvals", hibiragin.ApprovalCallback(os.Getenv("HIBIRA_APP_SECRET"), func(c *gin.Context, e *approvals.Event) error {
	return markDecided(e.Data.Approval.ID, e.Data.Approval.Status)
}))

room := waitingroom.Guard{AppKey: os.Getenv("HIBIRA_APP_KEY"), Secret: []string{os.Getenv("HIBIRA_APP_SECRET")}, Room: "presale"}
r.GET("/drop", hibiragin.WaitingRoom(room), func(c *gin.Context) {
	c.String(200, "Your number: %d", hibiragin.Pass(c).Number)
})
Function Does
Auth(client, fn) Answers pusher-js channel authorization (channelAuthorization.endpoint). fn returns whether the user may subscribe, and the member for presence- channels.
UserAuth(client, fn) Answers user sign-in (pusher.signin()), which the Chat API and server-to-user events need. fn returns the user, or nil.
Webhook(client, fn) Verifies webhook deliveries (401 when forged) and passes them to fn; an error from fn answers 500 so the delivery is retried.
ApprovalCallback(secret, fn) Verifies Approvals callback_url deliveries (Hibira-Signature) and passes the decision to fn.
WaitingRoom(guard) Sends visitors without a pass to the hosted queue page (403 for requests other than GET or HEAD); Pass(c) returns the pass.

Stream LLM output from a handler

s, _ := stream.New(ctx, client, "private-conversation-"+id, stream.Options{})
for delta := range deltas { // text deltas from your model SDK
	s.WriteString(delta)
}
s.Close()

See stream and the hibira-io/go README for publishing, push notifications and the Chat API.

Releases

Tagged vX.Y.Z and listed under Releases and in CHANGELOG.md.

Development

gofmt -l .
go vet ./...
go test -race ./...

To test against a local checkout of the core, add replace github.com/hibira-io/go => ../go to go.mod (do not commit it).

License

MIT

中文摘要

Gin 專用模組:pusher-js 的頻道授權與使用者登入端點、webhook 與 Approvals callback 驗證、等候室 middleware。callback 拿到 *gin.Context,可以直接讀取登入 middleware 存的使用者。需要 Go 1.21 以上。

Documentation

Overview

Package hibiragin connects Hibira to Gin: channel authorization and user sign-in endpoints for pusher-js, webhook and Approvals callback receivers, and a waiting room middleware. Your functions get the *gin.Context, so whatever your auth middleware stored there is at hand.

client := hibira.FromEnv()
r := gin.Default()
api := r.Group("/hibira", requireLogin)
api.POST("/auth", hibiragin.Auth(client, func(c *gin.Context, channel string) (bool, *hibira.Member) {
	u := c.MustGet("user").(*User)
	if channel == "private-user-"+u.ID {
		return true, nil
	}
	return strings.HasPrefix(channel, "presence-room-"), &hibira.Member{UserID: u.ID, UserInfo: gin.H{"name": u.Name}}
}))
api.POST("/user-auth", hibiragin.UserAuth(client, func(c *gin.Context) *hibira.User {
	return &hibira.User{ID: c.MustGet("user").(*User).ID}
}))
r.POST("/hibira/webhook", hibiragin.Webhook(client, onWebhook))
r.GET("/drop", hibiragin.WaitingRoom(room), dropPage)

Index

Constants

View Source
const PassKey = "hibira.pass"

PassKey is the gin.Context key WaitingRoom stores the visitor's pass under.

Variables

This section is empty.

Functions

func ApprovalCallback

func ApprovalCallback(secret string, fn func(c *gin.Context, e *approvals.Event) error) gin.HandlerFunc

ApprovalCallback receives Approvals decisions sent to a request's callback_url (or a workspace webhook, with its apwh_ secret). secret is the app secret for callback_url deliveries.

func Auth

func Auth(client *hibira.Client, fn func(c *gin.Context, channel string) (bool, *hibira.Member)) gin.HandlerFunc

Auth answers pusher-js channel authorization requests. fn decides whether the request's user may subscribe to channel, and for presence- channels returns the member to show.

func Pass

func Pass(c *gin.Context) *waitingroom.Pass

Pass returns the waiting room pass WaitingRoom let through, or nil.

func UserAuth

func UserAuth(client *hibira.Client, fn func(c *gin.Context) *hibira.User) gin.HandlerFunc

UserAuth answers pusher-js user sign-in requests (pusher.signin()), which the Chat API and server-to-user events need. fn returns the signed-in user, or nil to refuse.

func WaitingRoom

func WaitingRoom(g waitingroom.Guard) gin.HandlerFunc

WaitingRoom lets only visitors with a valid waiting room pass through; others go to the hosted queue page (requests other than GET or HEAD get 403). Read the pass with Pass.

func Webhook

func Webhook(client *hibira.Client, fn func(c *gin.Context, hook *hibira.Webhook) error) gin.HandlerFunc

Webhook verifies Hibira webhook deliveries and passes them to fn. A forged delivery gets 401; an error from fn answers 500 so Hibira retries.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL