aboutsummaryrefslogtreecommitdiff
path: root/server.go
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2019-12-04 18:30:01 +0100
committerSimon Ser <contact@emersion.fr>2019-12-04 18:30:01 +0100
commite94b1311ded705b0af3a6977cf5fb248a9530387 (patch)
tree9ebf02e45469967c6f05300cfc5436cbee2e850f /server.go
parent4ab5fb7f65fe8d8f0b291dbead9f0134a28eaba2 (diff)
downloadalps-e94b1311ded705b0af3a6977cf5fb248a9530387.tar.gz
alps-e94b1311ded705b0af3a6977cf5fb248a9530387.zip
Add basic theme support
References: https://todo.sr.ht/~sircmpwn/koushin/1
Diffstat (limited to 'server.go')
-rw-r--r--server.go33
1 files changed, 21 insertions, 12 deletions
diff --git a/server.go b/server.go
index 021ecff..d9542b5 100644
--- a/server.go
+++ b/server.go
@@ -79,7 +79,7 @@ func (s *Server) parseSMTPURL(smtpURL string) error {
return nil
}
-func NewServer(imapURL, smtpURL string) (*Server, error) {
+func newServer(imapURL, smtpURL string) (*Server, error) {
s := &Server{}
if err := s.parseIMAPURL(imapURL); err != nil {
@@ -310,12 +310,25 @@ func handleCompose(ectx echo.Context) error {
})
}
-func New(imapURL, smtpURL string) *echo.Echo {
- e := echo.New()
+func isPublic(path string) bool {
+ return path == "/login" || strings.HasPrefix(path, "/assets/") ||
+ strings.HasPrefix(path, "/themes/")
+}
+
+type Options struct {
+ IMAPURL, SMTPURL string
+ Theme string
+}
- s, err := NewServer(imapURL, smtpURL)
+func New(e *echo.Echo, options *Options) error {
+ s, err := newServer(options.IMAPURL, options.SMTPURL)
if err != nil {
- e.Logger.Fatal(err)
+ return err
+ }
+
+ e.Renderer, err = loadTemplates(e.Logger, options.Theme)
+ if err != nil {
+ return fmt.Errorf("failed to load templates: %v", err)
}
e.HTTPErrorHandler = func(err error, c echo.Context) {
@@ -336,7 +349,7 @@ func New(imapURL, smtpURL string) *echo.Echo {
cookie, err := ctx.Cookie(cookieName)
if err == http.ErrNoCookie {
// Require auth for all pages except /login
- if ctx.Path() == "/login" || strings.HasPrefix(ctx.Path(), "/assets/") {
+ if isPublic(ctx.Path()) {
return next(ctx)
} else {
return ctx.Redirect(http.StatusFound, "/login")
@@ -357,11 +370,6 @@ func New(imapURL, smtpURL string) *echo.Echo {
}
})
- e.Renderer, err = loadTemplates()
- if err != nil {
- e.Logger.Fatal("Failed to load templates:", err)
- }
-
e.GET("/mailbox/:mbox", func(ectx echo.Context) error {
ctx := ectx.(*context)
@@ -446,6 +454,7 @@ func New(imapURL, smtpURL string) *echo.Echo {
e.POST("/message/:mbox/:uid/reply", handleCompose)
e.Static("/assets", "public/assets")
+ e.Static("/themes", "public/themes")
- return e
+ return nil
}