shiori/internal/webserver/handler.go

132 lines
2.9 KiB
Go
Raw Normal View History

2019-05-27 18:01:53 +08:00
package webserver
import (
"fmt"
2019-10-07 14:38:40 +08:00
"html/template"
2019-05-27 18:01:53 +08:00
"net/http"
"github.com/go-shiori/shiori/internal/database"
2019-10-07 14:38:40 +08:00
"github.com/go-shiori/shiori/internal/model"
2019-10-09 21:10:12 +08:00
"github.com/go-shiori/warc"
2019-05-27 18:01:53 +08:00
cch "github.com/patrickmn/go-cache"
)
var developmentMode = false
// Handler is handler for serving the web interface.
type handler struct {
DB database.DB
DataDir string
2019-10-07 14:38:40 +08:00
RootPath string
2019-05-27 18:01:53 +08:00
UserCache *cch.Cache
SessionCache *cch.Cache
2019-08-05 19:26:37 +08:00
ArchiveCache *cch.Cache
Log bool
2019-10-07 14:38:40 +08:00
templates map[string]*template.Template
2019-05-27 18:01:53 +08:00
}
2019-10-07 14:38:40 +08:00
func (h *handler) prepareSessionCache() {
2019-05-27 18:01:53 +08:00
h.SessionCache.OnEvicted(func(key string, val interface{}) {
2019-10-07 14:38:40 +08:00
account := val.(model.Account)
arr, found := h.UserCache.Get(account.Username)
2019-05-27 18:01:53 +08:00
if !found {
return
}
sessionIDs := arr.([]string)
for i := 0; i < len(sessionIDs); i++ {
if sessionIDs[i] == key {
sessionIDs = append(sessionIDs[:i], sessionIDs[i+1:]...)
break
}
}
2019-10-07 14:38:40 +08:00
h.UserCache.Set(account.Username, sessionIDs, -1)
})
}
func (h *handler) prepareArchiveCache() {
h.ArchiveCache.OnEvicted(func(key string, data interface{}) {
archive := data.(*warc.Archive)
archive.Close()
2019-05-27 18:01:53 +08:00
})
}
2019-10-07 14:38:40 +08:00
func (h *handler) prepareTemplates() error {
// Prepare variables
var err error
h.templates = make(map[string]*template.Template)
// Prepare func map
funcMap := template.FuncMap{
"html": func(s string) template.HTML {
return template.HTML(s)
},
}
// Create template for login, index and content
for _, name := range []string{"login", "index", "content"} {
h.templates[name], err = createTemplate(name+".html", funcMap)
if err != nil {
return err
}
}
// Create template for archive overlay
h.templates["archive"], err = template.New("archive").Delims("$$", "$$").Parse(
`<div id="shiori-archive-header">
<p id="shiori-logo"><span></span>shiori</p>
<div class="spacer"></div>
<a href="$$.URL$$" target="_blank">View Original</a>
$$if .HasContent$$
<a href="/bookmark/$$.ID$$/content">View Readable</a>
$$end$$
</div>`)
if err != nil {
return err
}
return nil
}
func (h *handler) getSessionID(r *http.Request) string {
// Try to get session ID from the header
sessionID := r.Header.Get("X-Session-Id")
// If not, try it from the cookie
if sessionID == "" {
cookie, err := r.Cookie("session-id")
if err != nil {
return ""
}
sessionID = cookie.Value
}
return sessionID
}
2019-05-27 18:01:53 +08:00
// validateSession checks whether user session is still valid or not
func (h *handler) validateSession(r *http.Request) error {
sessionID := h.getSessionID(r)
if sessionID == "" {
return fmt.Errorf("session is not exist")
2019-05-27 18:01:53 +08:00
}
// Make sure session is not expired yet
val, found := h.SessionCache.Get(sessionID)
2019-08-12 20:57:18 +08:00
if !found {
2019-05-27 18:01:53 +08:00
return fmt.Errorf("session has been expired")
}
2019-08-12 20:57:18 +08:00
// If this is not get request, make sure it's owner
if r.Method != "" && r.Method != "GET" {
2019-10-07 14:38:40 +08:00
if account := val.(model.Account); !account.Owner {
2019-08-12 20:57:18 +08:00
return fmt.Errorf("account level is not sufficient")
}
}
2019-05-27 18:01:53 +08:00
return nil
}