Skip to content

Commit

Permalink
Merge pull request #83 from mohemohe/PKT-74
Browse files Browse the repository at this point in the history
fix #74
  • Loading branch information
mohemohe authored Dec 28, 2019
2 parents e928db1 + d70430f commit 66fbfc7
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 13 deletions.
2 changes: 1 addition & 1 deletion client/admin/stores/SettingsStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class SettingsStore extends StoreBase {
@observable
public ssrPageCache: boolean;

@observable
@observable.shallow
public cloudflare: ICloudflare;

constructor() {
Expand Down
3 changes: 2 additions & 1 deletion server/controllers/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers
import (
"github.com/labstack/echo/v4"
"github.com/mohemohe/parakeet/server/models"
"github.com/mohemohe/parakeet/server/util"
"net/http"
)

Expand Down Expand Up @@ -187,7 +188,7 @@ func SetCloudflare(c echo.Context) error {
panic("bind error")
}

if err := models.SetKVS(models.KVCloudflare, reqBody); err != nil {
if err := models.SetKVS(models.KVCloudflare, util.StructToJsonMap(reqBody)); err != nil {
panic(err)
}

Expand Down
14 changes: 9 additions & 5 deletions server/models/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ func SetCache(key string, value interface{}) error {
return connection.DsCache().Set(key, v, 365*24*time.Hour)
}

func PurgeInternalCache() {
connection.PurgeDsCache()

if err := pubsub.Publish(purgeCacheEvent, ""); err != nil {
util.Logger().Warn(err)
}
}

func PurgeCache() {
go func() {
connection.PurgeDsCache()

if err := pubsub.Publish(purgeCacheEvent, ""); err != nil {
util.Logger().Warn(err)
}
PurgeInternalCache()

if kv := GetKVS(KVCloudflare); kv != nil {
v := new(Cloudflare)
Expand Down
6 changes: 6 additions & 0 deletions server/models/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func GetEntries(perPage int, page int, includeDraft bool) *Entries {

entries := new(Entries)
if err := GetCache(cacheKey, entries); err == nil {
if len(entries.Entries) == 0 {
entries.Entries = []Entry{}
}
return entries
}

Expand All @@ -86,6 +89,9 @@ func GetEntries(perPage int, page int, includeDraft bool) *Entries {
for i := 0; i < info.RecordsOnPage; i++ {
_ = find.Next(&entryArray[i])
}
if len(entryArray) == 0 {
entryArray = []Entry{}
}

entries = &Entries{
Info: info,
Expand Down
1 change: 1 addition & 0 deletions server/models/kvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func GetKVS(key string) *KV {
func SetKVS(key string, value interface{}) error {
_, err := connection.Mongo().Collection(collections.KVS).Collection().Upsert(bson.M{"key": key}, bson.M{"key": key, "value": value})
if err == nil {
PurgeInternalCache()
_ = SetCache("kvs:"+key, value)
}
return err
Expand Down
12 changes: 6 additions & 6 deletions server/models/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,22 @@ func InitDB() {

setDefaultConfig(KVSiteTitle, "parakeet")
setDefaultConfig(KVSideNavContents, []string{})
setDefaultConfig(KVNotifyMastodon, NotifyMastodon{
setDefaultConfig(KVNotifyMastodon, util.StructToJsonMap(NotifyMastodon{
BaseURL: "",
Token: "",
Template: "ブログを書きました: %ENTRY_TITLE% %ENTRY_URL%",
})
setDefaultConfig(KVServerSideRendering, ServerSideRendering{
}))
setDefaultConfig(KVServerSideRendering, util.StructToJsonMap(ServerSideRendering{
Entries: true,
Entry: true,
})
}))
setDefaultConfig(KVEnableMongoDBQueryCache, true)
setDefaultConfig(KVEnableSSRPageCache, false)
setDefaultConfig(KVCloudflare, Cloudflare{
setDefaultConfig(KVCloudflare, util.StructToJsonMap(Cloudflare{
Enable: false,
ZoneID: "",
APIToken: "",
})
}))

user := GetUserByEmail("root")
if user == nil {
Expand Down
31 changes: 31 additions & 0 deletions server/util/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package util

import (
"reflect"
)

func StructToJsonMap(item interface{}) map[string]interface{} {
m := map[string]interface{}{}

v := reflect.ValueOf(item)
v = reflect.Indirect(v)

t := reflect.TypeOf(item)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}

for i := 0; i < t.NumField(); i++ {
tag := t.Field(i).Tag.Get("json")
f := v.Field(i).Interface()
if tag != "" && tag != "-" {
if t.Field(i).Type.Kind() == reflect.Struct {
m[tag] = StructToJsonMap(f)
} else {
m[tag] = f
}
}
}

return m
}

0 comments on commit 66fbfc7

Please sign in to comment.