~evanj/cms

ref: 5ea155bcf5f48d777fb2e6b6d66d34e60119d24e cms/internal/c/doc/doc.go -rw-r--r-- 1.2 KiB
5ea155bcEvan J Feat(dynamic): Dynamic content pages have been added (rather than 4 months ago
                                                                                
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package doc

import (
	"context"
	"html/template"
	"log"
	"net/http"

	"git.sr.ht/~evanj/cms/internal/c"
	"git.sr.ht/~evanj/cms/internal/m/tier"
	"git.sr.ht/~evanj/cms/internal/m/user"
	"git.sr.ht/~evanj/cms/internal/v"
)

// TODO: Consolidate this with dynamic?
// TODO: rename doc to something else?
var pages = map[string]*template.Template{
	"/page/stripe":  v.MustParse("html/stripe.html"),
	"/page/billing": v.MustParse("html/billing.html"),
}

type Doc struct {
	*c.Controller
	log *log.Logger
}

type dber interface {
	UserGet(ctx context.Context, username, password string) (user.User, error)
	UserGetFromToken(ctx context.Context, token string) (user.User, error)
}

func New(c *c.Controller, log *log.Logger) *Doc {
	return &Doc{c, log}
}

func (d *Doc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	user, _ := d.GetCookieUser(w, r) // Don't need a user for documentation.
	page, ok := pages[r.URL.Path]
	if !ok {
		http.NotFound(w, r)
		return
	}

	q := r.URL.Query()

	d.HTML(w, r, page, map[string]interface{}{
		"User":  user,
		"Tiers": tier.Tiers,
		// Only used for signup billing.
		"StripeCheckoutSessionID": q.Get("StripeCheckoutSessionID"),
		"StripePK":                q.Get("StripePK"),
	})
}