From e54a359acb7b308b75cb76ff8b3dba289903c3fb Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 11 Dec 2019 13:03:31 +0100 Subject: Export Server.{Sessions,Plugins} And unexport a few internal functions. --- handlers.go | 2 +- server.go | 17 ++++++++--------- session.go | 4 ++-- template.go | 2 +- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/handlers.go b/handlers.go index 4919d33..f53085c 100644 --- a/handlers.go +++ b/handlers.go @@ -82,7 +82,7 @@ func handleLogin(ectx echo.Context) error { username := ctx.FormValue("username") password := ctx.FormValue("password") if username != "" && password != "" { - s, err := ctx.Server.sessions.Put(username, password) + s, err := ctx.Server.Sessions.Put(username, password) if err != nil { if _, ok := err.(AuthError); ok { return ctx.Render(http.StatusOK, "login.html", nil) diff --git a/server.go b/server.go index f4b6e48..44392a0 100644 --- a/server.go +++ b/server.go @@ -15,7 +15,8 @@ const cookieName = "koushin_session" const messagesPerPage = 50 type Server struct { - sessions *SessionManager + Sessions *SessionManager + Plugins []Plugin imap struct { host string @@ -28,8 +29,6 @@ type Server struct { tls bool insecure bool } - - plugins []Plugin } func (s *Server) parseIMAPURL(imapURL string) error { @@ -76,7 +75,7 @@ func (s *Server) parseSMTPURL(smtpURL string) error { func newServer(imapURL, smtpURL string) (*Server, error) { s := &Server{} - s.sessions = newSessionManager(s.connectIMAP) + s.Sessions = newSessionManager(s.connectIMAP) if err := s.parseIMAPURL(imapURL); err != nil { return nil, err @@ -134,12 +133,12 @@ func New(e *echo.Echo, options *Options) error { return err } - s.plugins, err = loadAllLuaPlugins(e.Logger) + s.Plugins, err = loadAllLuaPlugins(e.Logger) if err != nil { return fmt.Errorf("failed to load plugins: %v", err) } - e.Renderer, err = loadTemplates(e.Logger, options.Theme, s.plugins) + e.Renderer, err = loadTemplates(e.Logger, options.Theme, s.Plugins) if err != nil { return fmt.Errorf("failed to load templates: %v", err) } @@ -172,14 +171,14 @@ func New(e *echo.Echo, options *Options) error { return err } - ctx.Session, err = ctx.Server.sessions.Get(cookie.Value) + ctx.Session, err = ctx.Server.Sessions.get(cookie.Value) if err == ErrSessionExpired { ctx.SetSession(nil) return ctx.Redirect(http.StatusFound, "/login") } else if err != nil { return err } - ctx.Session.Ping() + ctx.Session.ping() return next(ctx) } @@ -210,7 +209,7 @@ func New(e *echo.Echo, options *Options) error { e.Static("/assets", "public/assets") e.Static("/themes", "public/themes") - for _, p := range s.plugins { + for _, p := range s.Plugins { p.SetRoutes(e.Group("")) } diff --git a/session.go b/session.go index 63ba997..e4e2eb0 100644 --- a/session.go +++ b/session.go @@ -45,7 +45,7 @@ type Session struct { imapConn *imapclient.Client // protected by locker, can be nil } -func (s *Session) Ping() { +func (s *Session) ping() { s.pings <- struct{}{} } @@ -102,7 +102,7 @@ func (sm *SessionManager) connect(username, password string) (*imapclient.Client return c, nil } -func (sm *SessionManager) Get(token string) (*Session, error) { +func (sm *SessionManager) get(token string) (*Session, error) { sm.locker.Lock() defer sm.locker.Unlock() diff --git a/template.go b/template.go index 1c38c07..7f4d658 100644 --- a/template.go +++ b/template.go @@ -55,7 +55,7 @@ func (r *renderer) Render(w io.Writer, name string, data interface{}, ectx echo. // ectx is the raw *echo.context, not our own *Context ctx := ectx.Get("context").(*Context) - for _, plugin := range ctx.Server.plugins { + for _, plugin := range ctx.Server.Plugins { if err := plugin.Inject(name, data); err != nil { return fmt.Errorf("failed to run plugin '%v': %v", plugin.Name(), err) } -- cgit v1.2.3