aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cache/cache.go56
-rw-r--r--main.go11
-rw-r--r--modules/grils/gril.go4
-rw-r--r--modules/grils/grils.go16
-rw-r--r--modules/lists/lists.go44
-rw-r--r--modules/user/user.go14
6 files changed, 126 insertions, 19 deletions
diff --git a/cache/cache.go b/cache/cache.go
new file mode 100644
index 0000000..ca588ae
--- /dev/null
+++ b/cache/cache.go
@@ -0,0 +1,56 @@
1package cache
2
3import (
4 "time"
5 "log"
6)
7
8type CacheItem struct {
9 Expires time.Time
10 Value interface{}
11}
12type Cache struct {
13 items map[int]CacheItem
14}
15
16func New() *Cache {
17 c := &Cache{ make(map[int]CacheItem) }
18 go c.ticker()
19 return c
20}
21
22func (c *Cache) ticker() {
23 for _ = range time.NewTicker(time.Second * 1).C {
24 t := time.Now()
25 for key, item := range c.items {
26 if t.Sub(item.Expires) > 0 {
27 log.Println("cache delete", key)
28 delete(c.items, key)
29 }
30 }
31 }
32}
33
34func (c *Cache) Insert(key int, item interface{}) {
35 c.items[key] = CacheItem{ time.Now().Add(time.Minute * 5), item }
36}
37
38func (c *Cache) Remove(key int) {
39 delete(c.items, key)
40}
41
42func (c *Cache) Clear() {
43 c.items = make(map[int]CacheItem)
44}
45
46func (c *Cache) Get(key int) (interface{}, bool) {
47 if ci, ok := c.items[key]; ok {
48 return ci.Value, true
49 }
50 return nil, false
51}
52
53func (c *Cache) Has(key int) bool {
54 _, ok := c.items[key]
55 return ok
56} \ No newline at end of file
diff --git a/main.go b/main.go
index 6311304..dd24d01 100644
--- a/main.go
+++ b/main.go
@@ -17,12 +17,17 @@ import (
17 17
18 "github.com/julienschmidt/httprouter" 18 "github.com/julienschmidt/httprouter"
19 _ "github.com/lib/pq" 19 _ "github.com/lib/pq"
20 "strings"
20) 21)
21 22
22var app *grilist.Grilist 23var app *grilist.Grilist
23 24
24func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { 25func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
25 http.Redirect(w, r, "/dashboard", 302) 26 if strings.Index(r.Referer(), "127.0.0.1") != -1 {
27 w.WriteHeader(200)
28 return
29 }
30 http.Redirect(w, r, "/dashboard", 301)
26} 31}
27 32
28func loadModule(mod grilist.Module) error { 33func loadModule(mod grilist.Module) error {
@@ -61,6 +66,7 @@ func main() {
61 router, 66 router,
62 } 67 }
63 68
69 router.GET("/", index)
64 router.HandlerFunc("GET", "/login", login.DisplayLogin) 70 router.HandlerFunc("GET", "/login", login.DisplayLogin)
65 router.HandlerFunc("POST", "/login", login.Login) 71 router.HandlerFunc("POST", "/login", login.Login)
66 router.HandlerFunc("GET", "/settings", login.DisplayUserSettings) 72 router.HandlerFunc("GET", "/settings", login.DisplayUserSettings)
@@ -68,8 +74,6 @@ func main() {
68 router.HandlerFunc("GET", "/logout", login.Logout) 74 router.HandlerFunc("GET", "/logout", login.Logout)
69 router.HandlerFunc("GET", "/register", login.DisplayRegistration) 75 router.HandlerFunc("GET", "/register", login.DisplayRegistration)
70 router.HandlerFunc("POST", "/register", login.Register) 76 router.HandlerFunc("POST", "/register", login.Register)
71 router.GET("/", index)
72 router.GET("/dashboard", viewDashboard)
73 77
74 fs := http.FileServer(http.Dir("assets")) 78 fs := http.FileServer(http.Dir("assets"))
75 http.Handle("/assets/", http.StripPrefix("/assets/", fs)) 79 http.Handle("/assets/", http.StripPrefix("/assets/", fs))
@@ -81,6 +85,7 @@ func main() {
81 loadModule(tags.New()) 85 loadModule(tags.New())
82 loadModule(search.New()) 86 loadModule(search.New())
83 loadModule(user.New()) 87 loadModule(user.New())
88 router.GET("/dashboard", viewDashboard)
84 89
85 log.Fatal(http.ListenAndServe(":8080", nil)) 90 log.Fatal(http.ListenAndServe(":8080", nil))
86} 91}
diff --git a/modules/grils/gril.go b/modules/grils/gril.go
index 7b782b3..f78d31f 100644
--- a/modules/grils/gril.go
+++ b/modules/grils/gril.go
@@ -46,12 +46,12 @@ func ImagePath(id int, prioritizeThumbnail bool) string {
46 // Image Priority 46 // Image Priority
47 // without prioritizeThumbnail: Anilist > ACD (big) > ACD (thumbnail) 47 // without prioritizeThumbnail: Anilist > ACD (big) > ACD (thumbnail)
48 // with prioritizeThumbnail: Anilist > ACD (thumbnail) > ACD (big) 48 // with prioritizeThumbnail: Anilist > ACD (thumbnail) > ACD (big)
49 49
50 anilistPath := fmt.Sprintf("assets/img/gril/%d/%d.jpg", DataSourceAnilist, id) 50 anilistPath := fmt.Sprintf("assets/img/gril/%d/%d.jpg", DataSourceAnilist, id)
51 if _, err := os.Stat(anilistPath); err == nil { 51 if _, err := os.Stat(anilistPath); err == nil {
52 return fmt.Sprintf(anilistPath) 52 return fmt.Sprintf(anilistPath)
53 } 53 }
54 54
55 var big string 55 var big string
56 exts := []string{"png", "jpg", "gif", "jpeg"} 56 exts := []string{"png", "jpg", "gif", "jpeg"}
57 for _, ext := range exts { 57 for _, ext := range exts {
diff --git a/modules/grils/grils.go b/modules/grils/grils.go
index fe947bc..791ea5e 100644
--- a/modules/grils/grils.go
+++ b/modules/grils/grils.go
@@ -6,16 +6,22 @@ import (
6 "net/http" 6 "net/http"
7 "regexp" 7 "regexp"
8 "strconv" 8 "strconv"
9 "time"
9 "strings" 10 "strings"
10 "time"
11 11
12 "fagott.pw/charakterin" 12 "fagott.pw/charakterin"
13 "fagott.pw/grilist/frontend" 13 "fagott.pw/grilist/frontend"
14 "fagott.pw/grilist/grilist" 14 "fagott.pw/grilist/grilist"
15 "fagott.pw/grilist/cache"
15 16
16 "github.com/julienschmidt/httprouter" 17 "github.com/julienschmidt/httprouter"
17) 18)
18 19
20type CachedGril struct {
21 Created time.Time
22 Gril *Gril
23}
24
19var ( 25var (
20 pgArrayReg = regexp.MustCompile(`(((?P<value>(([^",\\{}\s(NULL)])+|"([^"\\]|\\"|\\\\)*")))(,)?)`) 26 pgArrayReg = regexp.MustCompile(`(((?P<value>(([^",\\{}\s(NULL)])+|"([^"\\]|\\"|\\\\)*")))(,)?)`)
21 pgValueIdx int 27 pgValueIdx int
@@ -32,6 +38,7 @@ func findIdx() {
32 38
33type GrilsModule struct { 39type GrilsModule struct {
34 g *grilist.Grilist 40 g *grilist.Grilist
41 c *cache.Cache
35} 42}
36 43
37func (m *GrilsModule) Name() string { 44func (m *GrilsModule) Name() string {
@@ -43,6 +50,8 @@ func (m *GrilsModule) Init(g *grilist.Grilist) {
43 m.g = g 50 m.g = g
44 m.g.Router.GET("/gril/:id", m.viewGril) 51 m.g.Router.GET("/gril/:id", m.viewGril)
45 m.g.Router.GET("/gril/:id/*rest", m.viewGril) 52 m.g.Router.GET("/gril/:id/*rest", m.viewGril)
53
54 m.c = cache.New()
46} 55}
47 56
48func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gril, error) { 57func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gril, error) {
@@ -143,6 +152,10 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das
143} 152}
144 153
145func (m *GrilsModule) FromID(id int) (*Gril, error) { 154func (m *GrilsModule) FromID(id int) (*Gril, error) {
155 if g, ok := m.c.Get(id); ok {
156 return g.(*Gril), nil
157 }
158
146 gril := &Gril{ 159 gril := &Gril{
147 ID: id, 160 ID: id,
148 } 161 }
@@ -155,6 +168,7 @@ func (m *GrilsModule) FromID(id int) (*Gril, error) {
155 if err != nil { 168 if err != nil {
156 return nil, err 169 return nil, err
157 } 170 }
171 m.c.Insert(id, gril)
158 return gril, nil 172 return gril, nil
159} 173}
160 174
diff --git a/modules/lists/lists.go b/modules/lists/lists.go
index 836f6af..910162a 100644
--- a/modules/lists/lists.go
+++ b/modules/lists/lists.go
@@ -7,6 +7,7 @@ import (
7 "fagott.pw/charakterin" 7 "fagott.pw/charakterin"
8 "fagott.pw/grilist/frontend" 8 "fagott.pw/grilist/frontend"
9 "fagott.pw/grilist/grilist" 9 "fagott.pw/grilist/grilist"
10 "fagott.pw/grilist/cache"
10 "fagott.pw/grilist/modules/grils" 11 "fagott.pw/grilist/modules/grils"
11 "fmt" 12 "fmt"
12 "github.com/julienschmidt/httprouter" 13 "github.com/julienschmidt/httprouter"
@@ -22,6 +23,7 @@ import (
22// Module und so. 23// Module und so.
23type Module struct { 24type Module struct {
24 g *grilist.Grilist 25 g *grilist.Grilist
26 c *cache.Cache
25 grils *grils.GrilsModule 27 grils *grils.GrilsModule
26} 28}
27 29
@@ -76,6 +78,8 @@ func (m *Module) Init(g *grilist.Grilist) {
76 m.g.Router.POST("/new/list", m.createList) 78 m.g.Router.POST("/new/list", m.createList)
77 79
78 m.g.Router.GET("/api/lists/user", m.APIgetUserLists) 80 m.g.Router.GET("/api/lists/user", m.APIgetUserLists)
81
82 m.c = cache.New()
79} 83}
80 84
81func (m *Module) getListGrils(list *List) error { 85func (m *Module) getListGrils(list *List) error {
@@ -85,6 +89,7 @@ func (m *Module) getListGrils(list *List) error {
85 } 89 }
86 90
87 defer rows.Close() 91 defer rows.Close()
92 list.Grils = list.Grils[:0]
88 for rows.Next() { 93 for rows.Next() {
89 var grilID int 94 var grilID int
90 lg := &ListGril{} 95 lg := &ListGril{}
@@ -123,6 +128,12 @@ func (m *Module) getLists(whereClause string, params ...interface{}) ([]*List, e
123 log.Println("error scanning row in getLists:", err) 128 log.Println("error scanning row in getLists:", err)
124 continue 129 continue
125 } 130 }
131
132 if clist, ok := m.c.Get(list.ID); ok {
133 // weiteres parsen abbrechen
134 lists = append(lists, clist.(*List))
135 continue
136 }
126 137
127 // Owner kriegn 138 // Owner kriegn
128 owner, err := m.g.Charakterin.GetUserByID(ownerID) 139 owner, err := m.g.Charakterin.GetUserByID(ownerID)
@@ -131,7 +142,8 @@ func (m *Module) getLists(whereClause string, params ...interface{}) ([]*List, e
131 continue 142 continue
132 } 143 }
133 list.Owner = owner 144 list.Owner = owner
134 145
146 m.c.Insert(list.ID, list)
135 lists = append(lists, list) 147 lists = append(lists, list)
136 } 148 }
137 149
@@ -157,6 +169,17 @@ func (m *Module) GetUserLists(u *charakterin.User, withGrils bool) []*List {
157 169
158// FromID sucht nach der Liste mit der gegebenen ID und gibt sie, falls sie existiert, zurück. 170// FromID sucht nach der Liste mit der gegebenen ID und gibt sie, falls sie existiert, zurück.
159func (m *Module) FromID(id int, withGrils bool) (*List, error) { 171func (m *Module) FromID(id int, withGrils bool) (*List, error) {
172 if lst, ok := m.c.Get(id); ok {
173 l := lst.(*List)
174 // Potenzieller Optimierungsbedarf: neue query, wenn die Liste leer ist. updateGrilOrder nutzt derzeit aber genau dieses verhalten.
175 if withGrils && len(l.Grils) == 0 {
176 if err := m.getListGrils(l); err != nil {
177 return nil, err
178 }
179 }
180 return l, nil
181 }
182
160 lists, err := m.getLists(`id = $1`, id) 183 lists, err := m.getLists(`id = $1`, id)
161 if err != nil { 184 if err != nil {
162 return nil, err 185 return nil, err
@@ -210,11 +233,12 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar
210 log.Println(err) 233 log.Println(err)
211 return categories 234 return categories
212 } 235 }
213 //for _, list := range lists { 236
214 //if err := m.getListGrils(list); err != nil { 237 for _, list := range lists {
215 // log.Println(err) 238 if err := m.getListGrils(list); err != nil {
216 //} 239 log.Println(err)
217 //} 240 }
241 }
218 242
219 categories = append(categories, grilist.DashboardCategory{ 243 categories = append(categories, grilist.DashboardCategory{
220 Title: "Neueste Listen", 244 Title: "Neueste Listen",
@@ -242,12 +266,14 @@ func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.P
242 266
243 id, err := strconv.Atoi(sid) 267 id, err := strconv.Atoi(sid)
244 if err != nil { 268 if err != nil {
269 log.Println("redir")
245 http.Redirect(w, r, "/", 302) 270 http.Redirect(w, r, "/", 302)
246 return 271 return
247 } 272 }
248 273
249 list, err := m.FromID(id, true) 274 list, err := m.FromID(id, true)
250 if err != nil { 275 if err != nil {
276 log.Println("redir")
251 http.Redirect(w, r, "/", 302) 277 http.Redirect(w, r, "/", 302)
252 return 278 return
253 } 279 }
@@ -406,6 +432,12 @@ func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httpr
406 http.Error(w, "could not update gril order", 500) 432 http.Error(w, "could not update gril order", 500)
407 return 433 return
408 } 434 }
435
436 // wenn die liste im cache ist, die Grils clearen, damit beim naechsten aufruf die Gril-Liste neu geholt wird.
437 if l, ok := m.c.Get(listID); ok {
438 ls := l.(*List)
439 ls.Grils = ls.Grils[:0]
440 }
409 441
410 w.WriteHeader(200) 442 w.WriteHeader(200)
411 w.Write([]byte("ok")) 443 w.Write([]byte("ok"))
diff --git a/modules/user/user.go b/modules/user/user.go
index 06245e2..92a04db 100644
--- a/modules/user/user.go
+++ b/modules/user/user.go
@@ -27,13 +27,13 @@ func (m *Module) Name() string {
27 27
28func (m *Module) Init(g *grilist.Grilist) { 28func (m *Module) Init(g *grilist.Grilist) {
29 m.g = g 29 m.g = g
30 30
31 gm, ok := g.Modules["Lists"] 31 gm, ok := g.Modules["Lists"]
32 if !ok { 32 if !ok {
33 log.Fatal("tags: lists module not found") 33 log.Fatal("tags: lists module not found")
34 } 34 }
35 m.lists = gm.(*lists.Module) 35 m.lists = gm.(*lists.Module)
36 36
37 m.g.Router.GET("/user/:id", m.viewUser) 37 m.g.Router.GET("/user/:id", m.viewUser)
38 m.g.Router.GET("/user/:id/*rest", m.viewUser) 38 m.g.Router.GET("/user/:id/*rest", m.viewUser)
39} 39}
@@ -44,13 +44,13 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar
44 44
45func (m *Module) viewUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 45func (m *Module) viewUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
46 currentUser, _ := m.g.Charakterin.GetUserFromRequest(r) 46 currentUser, _ := m.g.Charakterin.GetUserFromRequest(r)
47 47
48 userID, err := strconv.Atoi(p.ByName("id")) 48 userID, err := strconv.Atoi(p.ByName("id"))
49 if err != nil { 49 if err != nil {
50 http.Redirect(w, r, "/", 302) 50 http.Redirect(w, r, "/", 302)
51 return 51 return
52 } 52 }
53 53
54 user := currentUser 54 user := currentUser
55 if user == nil || currentUser.ID != userID { 55 if user == nil || currentUser.ID != userID {
56 user, err = m.g.Charakterin.GetUserByID(userID) 56 user, err = m.g.Charakterin.GetUserByID(userID)
@@ -59,13 +59,13 @@ func (m *Module) viewUser(w http.ResponseWriter, r *http.Request, p httprouter.P
59 return 59 return
60 } 60 }
61 } 61 }
62 62
63 userLists := m.lists.GetUserLists(user, true) 63 userLists := m.lists.GetUserLists(user, true)
64 64
65 data := m.g.Renderer.DefaultData() 65 data := m.g.Renderer.DefaultData()
66 data["user"] = currentUser 66 data["user"] = currentUser
67 data["DisplayUser"] = user 67 data["DisplayUser"] = user
68 data["Lists"] = lists.ListsToCards(userLists) 68 data["Lists"] = lists.ListsToCards(userLists)
69 69
70 m.g.Renderer.RenderPage("user", w, data) 70 m.g.Renderer.RenderPage("user", w, data)
71} 71}