~evanj/cms

ref: 991b370ac6b6208e5f2fd7dcada177d52be58d9f cms/internal/c/doc/doc.go -rw-r--r-- 1.3 KiB
991b370aEvan M Jones Feat(Big): Added a few things here... 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/v"
)

var pages = map[string]*template.Template{
	"/page/doc":     v.MustParse("html/doc.html"),
	"/page/faq":     v.MustParse("html/faq.html"),
	"/page/terms":   v.MustParse("html/terms.html"),
	"/page/privacy": v.MustParse("html/privacy.html"),
	"/page/contact": v.MustParse("html/contact.html"),
	"/page/billing": v.MustParse("html/billing.html"),
	"/page/stripe":  v.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"),
	})
}