~evanj/cms

ref: 8efa6eb4fd1aad49d40bb3850530c531add998da cms/internal/c/doc/doc.go -rw-r--r-- 1.3 KiB
8efa6eb4Evan M Jones WIP(stripe): *. 1 year, 2 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
54
55
56
package doc

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

	"git.sr.ht/~evanj/cms/internal/c"
	"git.sr.ht/~evanj/cms/internal/m/user"
	"git.sr.ht/~evanj/cms/internal/s/tmpl"
)

var pages = map[string]*template.Template{
	"/page/doc":     tmpl.MustParse("html/doc.html"),
	"/page/faq":     tmpl.MustParse("html/faq.html"),
	"/page/terms":   tmpl.MustParse("html/terms.html"),
	"/page/privacy": tmpl.MustParse("html/privacy.html"),
	"/page/contact": tmpl.MustParse("html/contact.html"),
	"/page/billing": tmpl.MustParse("html/billing.html"),
	"/page/stripe":  tmpl.MustParse("html/stripe.html"),
}

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

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

func New(c *c.Controller, log *log.Logger, db dber) *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,
		// Only used for signup billing.
		"StripeCheckoutSessionID": q.Get("StripeCheckoutSessionID"),
		"StripePK":                q.Get("StripePK"),
	})
}