web

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 29 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.

Documentation

Overview

web/handlers.go

web/headers.go

web/markdown.go

web/render.go

Index

Constants

View Source
const MarkdownAccept = `.*text/markdown.*`

MarkdownAccept is the vercel.json `has` value matched against the Accept header.

View Source
const MarkdownRoute = `^/(?!api/|auth/|result(?:/|$))([^.]*)$`

MarkdownRoute is the vercel.json route source (PCRE) for paths that answer Accept: text/markdown: every page path, but not files (a dot in the path) and not the functions, whose HTML has no Markdown twin. $1 is the path without its leading slash. WantsMarkdown applies the same rule in Go; cmd/devserver and the tests rely on it.

View Source
const StateCookie = "gha_oauth"

StateCookie carries the signed sign-in state between /auth/login and /api/private.

View Source
const StateMaxAge = 10 * time.Minute

StateMaxAge is how long a sign-in may take between GitHub's consent screen and our callback.

Variables

View Source
var (
	ErrState    = errors.New("sign-in state is missing, invalid or expired")
	ErrExchange = errors.New("GitHub did not accept the sign-in code")
)

Functions

func Challenge

func Challenge(verifier string) string

Challenge is the PKCE S256 code_challenge for verifier.

func Markdown

func Markdown(w http.ResponseWriter, r *http.Request)

Markdown serves GET /api/markdown?path=<path>, which vercel.json routes every page request with Accept: text/markdown to. An unknown path is a 404 with a Markdown body that points at the sitemap and llms.txt.

func RenderErrorPage

func RenderErrorPage(status int, reason, message, user string) ([]byte, error)

RenderErrorPage renders the check form with one message.

func RenderResultFragment

func RenderResultFragment(v ResultView) ([]byte, error)

RenderResultFragment is the result section alone, for /api/private.

func RenderResultPage

func RenderResultPage(v ResultView) ([]byte, error)

RenderResultPage is the full /result page. Its <h1> is the @login in the result header.

func Serve

Serve is what each Vercel function calls.

func SignState

func SignState(secret []byte, c StateClaims) string

SignState encodes claims as payload.signature.

func WantsMarkdown

func WantsMarkdown(r *http.Request) bool

WantsMarkdown reports whether a request should get the Markdown twin instead of HTML.

Types

type Card

type Card struct {
	Slug, Name, HowToEarn, Image, ImageAlt, Initials, TierLabel, Line string
	Source, SourceMeaning, BadgeURL, Unlocked, TierText               string
	Tier                                                              int
	Earned, Locked                                                    bool
	Progress                                                          *Meter
	Notes                                                             []string
	Milestones                                                        []Link
}

Card is one badge on the results page.

type Config

type Config struct {
	ClientID, ClientSecret, BotToken string
	StateSecret                      []byte
	BaseURL                          string // https://gitverse.vercel.app or http://localhost:8787
	GitHub                           GitHub
	Official                         report.Official
	NewCounts                        func(token string) (counters.Source, error)
	Timeout                          time.Duration
	Now                              func() time.Time
	Log                              *log.Logger
}

Config is everything the handlers need. Tests replace GitHub, Official, NewCounts and Now.

func FromEnv

func FromEnv() (*Config, error)

FromEnv reads the Vercel (or .env.local) environment. It never logs the values.

func (*Config) Login

func (c *Config) Login(w http.ResponseWriter, r *http.Request)

Login serves GET /auth/login: a signed state cookie, then GitHub's consent screen.

func (*Config) Private

func (c *Config) Private(w http.ResponseWriter, r *http.Request)

Private serves POST /api/private from callback.js: code → token → report → revoke.

func (*Config) Result

func (c *Config) Result(w http.ResponseWriter, r *http.Request)

Result serves GET /result?u=<login>: public data, cached at the CDN, shareable.

type ErrorView

type ErrorView struct {
	Status          int
	Reason, Message string
	User            string // pre-fills the username box
}

ErrorView is shown instead of a result.

type GitHub

type GitHub struct {
	AuthorizeURL, TokenURL, APIURL string
	HTTP                           *http.Client
}

GitHub holds the endpoints, so tests can point them at a fake.

func DefaultGitHub

func DefaultGitHub() GitHub

DefaultGitHub is github.com.

func (GitHub) Authorize

func (g GitHub) Authorize(clientID, redirectURI, nonce, challenge string) string

Authorize is the URL of GitHub's consent screen.

func (GitHub) Exchange

func (g GitHub) Exchange(ctx context.Context, clientID, secret, code, redirectURI, verifier string) (string, error)

Exchange trades the code for a user token. A refresh token, if any, is ignored.

func (GitHub) Login

func (g GitHub) Login(ctx context.Context, token string) (string, error)

Login reads the signed-in user's login.

func (GitHub) Revoke

func (g GitHub) Revoke(ctx context.Context, clientID, secret, token string) error

Revoke deletes the token. The app's grant stays, so the next sign-in skips consent.

type Header struct{ Key, Value string }

Header is one response header.

func SecurityHeaders

func SecurityHeaders() []Header

SecurityHeaders apply to every response. cmd/gendocs writes the same list into vercel.json for static files, so functions and static pages cannot drift.

type Link struct{ Label, URL string }

Link is a milestone row; URL is empty for a private one.

type Meter

type Meter struct {
	Value, Max int
	Label      string
}

Meter feeds a <progress> element: progress from the current tier's floor to the next.

type ResultView

type ResultView struct {
	User, Avatar                     string
	Private, Hidden, Authenticated   bool
	Standalone                       bool // the full /result page: @login is its <h1>
	Earned, EarnableTotal, TierTotal int
	Closest                          *Card
	Display                          []Card // "On display": every earned badge, any kind
	Collect                          []Card // "Still to collect": earnable, not earned
	Archive                          []Card // "The archive": cannot be earned, not earned
	Highlights                       []report.HighlightRow
	Verified                         string
}

ResultView is everything result.html shows.

func NewResultView

func NewResultView(r *report.Report, private bool) ResultView

NewResultView turns a report into the page model. private marks a signed-in result.

type StateClaims

type StateClaims struct {
	Nonce    string `json:"n"`
	Verifier string `json:"v"` // PKCE code_verifier
	Issued   int64  `json:"t"`
}

StateClaims travel in an HMAC-signed, HttpOnly cookie between /auth/login and /api/private.

func NewState

func NewState(now time.Time) (StateClaims, error)

NewState makes a fresh nonce and PKCE verifier.

func VerifyState

func VerifyState(secret []byte, cookie, nonce string, now time.Time) (StateClaims, error)

VerifyState checks the signature, that nonce is the one GitHub echoed back, and the age.

Jump to

Keyboard shortcuts

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