Skip to content

YuMinami/keyauth

 
 

Repository files navigation

keyauth(This is a community driven project)

keyauth for hertz

Install

go get github.com/hertz-contrib/keyauth

import

import "github.com/hertz-contrib/keyauth"

Example

package main

import (
	"context"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/cloudwego/hertz/pkg/common/utils"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
	"github.com/hertz-contrib/keyauth"
)

func main() {
	h := server.Default()
	h.Use(keyauth.New(
		keyauth.WithContextKey("token"),
		keyauth.WithKeyLookUp("query:token", ""),
	))
	h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
		value, _ := ctx.Get("token")
		ctx.JSON(consts.StatusOK, utils.H{"ping": value})
	})
	h.Spin()
}

Test

curl 'http://127.0.0.1:8888/ping?token=token'

Packages

No packages published

Languages

  • Go 100.0%