~evanj/cms

ref: 76738226cae5c57a518db8a20b0525d32223f741 cms/cms.go -rw-r--r-- 3.9 KiB
76738226Evan M Jones Feat(email+upgrade): MVP support for user emails (after signup) and 7 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// cms is a free and open source content management system.
package main

import (
	"log"
	"net/http"
	"os"
	"strings"

	"git.sr.ht/~evanj/cms/internal/c"
	"git.sr.ht/~evanj/cms/internal/c/content"
	"git.sr.ht/~evanj/cms/internal/c/contenttype"
	"git.sr.ht/~evanj/cms/internal/c/doc"
	"git.sr.ht/~evanj/cms/internal/c/file"
	"git.sr.ht/~evanj/cms/internal/c/hook"
	"git.sr.ht/~evanj/cms/internal/c/redirect"
	"git.sr.ht/~evanj/cms/internal/c/space"
	"git.sr.ht/~evanj/cms/internal/c/stripe"
	"git.sr.ht/~evanj/cms/internal/c/user"
	"git.sr.ht/~evanj/cms/internal/s/cache"
	"git.sr.ht/~evanj/cms/internal/s/db"
	webhook "git.sr.ht/~evanj/cms/internal/s/hook"
	"git.sr.ht/~evanj/cms/internal/s/rl"
	libstripe "git.sr.ht/~evanj/cms/internal/s/stripe"
	"git.sr.ht/~evanj/cms/pkg/e3"
	"git.sr.ht/~evanj/security"

	_ "git.sr.ht/~evanj/embed" // For embedding static assets during build.
)

var (
	app   *App
	build string

	port             = os.Getenv("PORT")
	dbtype           = os.Getenv("DBTYPE")
	dbcreds          = os.Getenv("DB")
	url              = os.Getenv("URL")
	secret           = os.Getenv("SECRET")
	memcacheKey      = os.Getenv("MEMCACHE_KEY")
	memcacheServer   = os.Getenv("MEMCACHE_SERVER")
	e3user           = os.Getenv("E3_USER")
	e3pass           = os.Getenv("E3_PASS")
	e3url            = os.Getenv("E3_URL")
	signupEnabled    = os.Getenv("SIGNUP_ENABLE") == "true"
	staticDir        = os.Getenv("STATIC_DIR")
	analyticsEnabled = os.Getenv("ANALYTICS_ENABLE") == "true"
	stripeSuccessURL = os.Getenv("STRIPE_SUCCESS_URL")
	stripeErrorURL   = os.Getenv("STRIPE_ERROR_URL")
	stripePK         = os.Getenv("STRIPE_PK")
	stripeSK         = os.Getenv("STRIPE_SK")
)

type App struct {
	log *log.Logger

	// NOTE: Concurrent read (only) is OK. This is never wrote (but defined on
	// server startup).
	handlers map[string]http.Handler
}

func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	parts := strings.Split(r.URL.Path, "/")
	if len(parts) < 2 {
		http.NotFound(w, r)
		return
	}

	namespace := parts[1]
	if namespace == "" {
		namespace = "user"
	}

	h, ok := a.handlers[namespace]
	if !ok {
		http.NotFound(w, r)
		return
	}

	h.ServeHTTP(w, r)
}

func init() {
	w := os.Stdout
	applogger := log.New(w, "[cms] ", 0)

	db, err := db.New(
		log.New(w, "[cms:db] ", 0),
		dbtype,
		dbcreds,
		security.Default(secret),
	)
	if err != nil {
		applogger.Fatal(err)
	}
	if err := db.EnsureSetup(); err != nil {
		applogger.Fatal(err)
	}

	cacher, err := cache.New(
		log.New(w, "[cms:cache] ", 0),
		db,
		memcacheKey,
		memcacheServer,
	)
	if err != nil {
		applogger.Fatal(err)
	}

	fs := e3.New(e3user, e3pass, e3url)
	rl := rl.New(log.New(w, "[cms:ratelimit] ", 0), cacher, fs)
	c := c.New(log.New(w, "[cms:content] ", 0), rl, analyticsEnabled, build)
	libs := libstripe.New(stripeSuccessURL, stripeErrorURL, stripePK, stripeSK, rl)

	app = &App{
		applogger,
		map[string]http.Handler{
			"content": content.New(
				c,
				log.New(w, "[cms:content] ", 0),
				rl,
				fs,
				webhook.New(log.New(w, "[cms:hook] ", 0), rl),
				url,
			),
			"contenttype": contenttype.New(
				c,
				log.New(w, "[cms:contenttype] ", 0),
				rl,
			),
			"space": space.New(
				c,
				log.New(w, "[cms:space] ", 0),
				rl,
			),
			"user": user.New(
				c,
				log.New(w, "[cms:user] ", 0),
				rl,
				signupEnabled,
				libs,
			),
			"hook": hook.New(
				c,
				log.New(w, "[cms:hook] ", 0),
				rl,
			),
			"file": file.New(
				c,
				log.New(w, "[cms:file] ", 0),
				rl,
				fs,
				url,
			),
			"static": http.StripPrefix("/static", http.FileServer(http.Dir(staticDir))),
			"redirect": redirect.New(
				c,
				log.New(w, "[cms:redirect] ", 0),
				rl,
			),
			"page": doc.New(
				c,
				log.New(w, "[cms:doc] ", 0),
				rl,
			),
			"stripe": http.StripPrefix("/stripe", stripe.New(
				c,
				log.New(w, "[cms:stripe] ", 0),
				rl,
				libs,
			)),
		},
	}
}

func main() {
	app.log.Println("listening on", url)
	app.log.Fatal(http.ListenAndServe(port, app))
}