Export Server.{Sessions,Plugins}
And unexport a few internal functions.
This commit is contained in:
parent
28a8f3f6ac
commit
e54a359acb
4 changed files with 12 additions and 13 deletions
|
@ -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)
|
||||
|
|
17
server.go
17
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(""))
|
||||
}
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue