You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
synctv/server/oauth2/render.go

39 lines
1.1 KiB
Go

2 years ago
package auth
import (
"embed"
"html/template"
"time"
"github.com/gin-gonic/gin"
"github.com/synctv-org/synctv/internal/provider"
"github.com/zijiren233/gencontainer/synccache"
2 years ago
)
2 years ago
//go:embed templates/*.html
2 years ago
var temp embed.FS
var (
redirectTemplate *template.Template
2 years ago
tokenTemplate *template.Template
states *synccache.SyncCache[string, stateHandler]
2 years ago
)
type stateHandler func(ctx *gin.Context, pi provider.Interface, code string)
2 years ago
func RenderRedirect(ctx *gin.Context, url string) error {
ctx.Header("Content-Type", "text/html; charset=utf-8")
return redirectTemplate.Execute(ctx.Writer, url)
}
func RenderToken(ctx *gin.Context, url, token string) error {
ctx.Header("Content-Type", "text/html; charset=utf-8")
return tokenTemplate.Execute(ctx.Writer, map[string]string{"Url": url, "Token": token})
2 years ago
}
func init() {
redirectTemplate = template.Must(template.ParseFS(temp, "templates/redirect.html"))
2 years ago
tokenTemplate = template.Must(template.ParseFS(temp, "templates/token.html"))
states = synccache.NewSyncCache[string, stateHandler](time.Minute * 10)
2 years ago
}