Skip to content

Commit

Permalink
Reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
yangbor committed Dec 19, 2018
1 parent dbed8c4 commit 2816db5
Show file tree
Hide file tree
Showing 64 changed files with 602 additions and 608 deletions.
7 changes: 4 additions & 3 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"encoding/json"
"encoding/xml"
"errors"
"github.com/devfeel/dotweb/framework/reflects"
"strings"

"github.com/devfeel/dotweb/framework/reflects"
)

const (
defaultTagName = "form"
jsonTagName = "json"
jsonTagName = "json"
)

type (
Expand Down Expand Up @@ -43,7 +44,7 @@ func (b *binder) Bind(i interface{}, ctx Context) (err error) {
default:
//check is use json tag, fixed for issue #91
tagName := defaultTagName
if ctx.HttpServer().ServerConfig().EnabledBindUseJsonTag{
if ctx.HttpServer().ServerConfig().EnabledBindUseJsonTag {
tagName = jsonTagName
}
//no check content type for fixed issue #6
Expand Down
1 change: 0 additions & 1 deletion bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type Person struct {
Legs []string
}


//json
func TestBinder_Bind_json(t *testing.T) {

Expand Down
10 changes: 4 additions & 6 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@ var runtimeCache Cache
var key string
var val []byte

func init(){
func init() {
runtimeCache = NewRuntimeCache()
key = "abc"
val = []byte("def")
}


func DoSet(cache Cache){
func DoSet(cache Cache) {
expire := 60 // expire in 60 seconds
cache.Set(key, val, int64(expire))
}

func DoGet(cache Cache){
func DoGet(cache Cache) {
cache.Get(key)
}


func BenchmarkTestSet(b *testing.B) {
for i := 0; i < b.N; i++ {
DoSet(runtimeCache)
Expand All @@ -34,4 +32,4 @@ func BenchmarkTestGet(b *testing.B) {
for i := 0; i < b.N; i++ {
DoGet(runtimeCache)
}
}
}
7 changes: 4 additions & 3 deletions cache/redis/cache_redis.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package redis

import (
"github.com/devfeel/dotweb/framework/redis"
"strconv"

"github.com/devfeel/dotweb/framework/redis"
)

var (
Expand Down Expand Up @@ -101,9 +102,9 @@ func (ca *RedisCache) GetInt64(key string) (int64, error) {
func (ca *RedisCache) Set(key string, value interface{}, ttl int64) error {
redisClient := redisutil.GetRedisClient(ca.serverURL)
var err error
if ttl <= 0{
if ttl <= 0 {
_, err = redisClient.Set(key, value)
}else{
} else {
_, err = redisClient.SetWithExpire(key, value, ttl)
}
return err
Expand Down
6 changes: 3 additions & 3 deletions cache/runtime/cache_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func (mi *RuntimeItem) isExpire() bool {
type RuntimeCache struct {
sync.RWMutex
gcInterval time.Duration
items *sync.Map
items *sync.Map
//items map[string]*RuntimeItem
}

// NewRuntimeCache returns a new *RuntimeCache.
func NewRuntimeCache() *RuntimeCache {
cache := RuntimeCache{items:new(sync.Map),gcInterval: DefaultGCInterval}
cache := RuntimeCache{items: new(sync.Map), gcInterval: DefaultGCInterval}
go cache.gc()
return &cache
}
Expand Down Expand Up @@ -244,7 +244,7 @@ func (ca *RuntimeCache) gc() {
if ca.items == nil {
return
}
ca.items.Range(func(key interface{}, v interface{}) bool{
ca.items.Range(func(key interface{}, v interface{}) bool {
ca.itemExpired(fmt.Sprint(key))
return true
})
Expand Down
113 changes: 56 additions & 57 deletions cache/runtime/cache_runtime_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package runtime

import (
"sync"
"testing"
"time"

"github.com/devfeel/dotweb/test"
"sync"
)

const (
DefaultTestGCInterval = 2

TEST_CACHE_KEY = "joe"
TEST_CACHE_KEY = "joe"
TEST_CACHE_VALUE = "zou"
//int value
TEST_CACHE_INT_VALUE = 1
Expand All @@ -20,104 +21,102 @@ const (
)

func TestRuntimeCache_Get(t *testing.T) {
cache:=NewRuntimeCache()
cache.Set(TEST_CACHE_KEY,TEST_CACHE_VALUE,5)
cache := NewRuntimeCache()
cache.Set(TEST_CACHE_KEY, TEST_CACHE_VALUE, 5)
//check value
go func(cache *RuntimeCache,t *testing.T) {
time.Sleep(4*time.Second)
value,err:=cache.Get(TEST_CACHE_KEY)
go func(cache *RuntimeCache, t *testing.T) {
time.Sleep(4 * time.Second)
value, err := cache.Get(TEST_CACHE_KEY)

test.Nil(t,err)
test.Equal(t,TEST_CACHE_VALUE,value)
}(cache,t)
test.Nil(t, err)
test.Equal(t, TEST_CACHE_VALUE, value)
}(cache, t)

//check expired
go func(cache *RuntimeCache,t *testing.T) {
time.Sleep(5*time.Second)
value,err:=cache.Exists(TEST_CACHE_KEY)
go func(cache *RuntimeCache, t *testing.T) {
time.Sleep(5 * time.Second)
value, err := cache.Exists(TEST_CACHE_KEY)

test.Nil(t,err)
test.Equal(t,true,value)
}(cache,t)
test.Nil(t, err)
test.Equal(t, true, value)
}(cache, t)

time.Sleep(5*time.Second)
time.Sleep(5 * time.Second)
}


func TestRuntimeCache_GetInt(t *testing.T) {
testRuntimeCache(t,TEST_CACHE_INT_VALUE,func(cache *RuntimeCache,key string)(interface{}, error){
testRuntimeCache(t, TEST_CACHE_INT_VALUE, func(cache *RuntimeCache, key string) (interface{}, error) {
return cache.GetInt(key)
})
}


func TestRuntimeCache_GetInt64(t *testing.T) {
testRuntimeCache(t,TEST_CACHE_INT64_VALUE,func(cache *RuntimeCache,key string)(interface{}, error){
testRuntimeCache(t, TEST_CACHE_INT64_VALUE, func(cache *RuntimeCache, key string) (interface{}, error) {
return cache.GetInt64(key)
})
}

func TestRuntimeCache_GetString(t *testing.T) {
testRuntimeCache(t,TEST_CACHE_VALUE,func(cache *RuntimeCache,key string)(interface{}, error){
testRuntimeCache(t, TEST_CACHE_VALUE, func(cache *RuntimeCache, key string) (interface{}, error) {
return cache.GetString(key)
})
}

func testRuntimeCache(t *testing.T,insertValue interface{},f func(cache *RuntimeCache,key string)(interface{}, error)) {
cache:=NewRuntimeCache()
cache.Set(TEST_CACHE_KEY,insertValue,5)
func testRuntimeCache(t *testing.T, insertValue interface{}, f func(cache *RuntimeCache, key string) (interface{}, error)) {
cache := NewRuntimeCache()
cache.Set(TEST_CACHE_KEY, insertValue, 5)
//check value
go func(cache *RuntimeCache,t *testing.T) {
time.Sleep(4*time.Second)
value,err:=f(cache,TEST_CACHE_KEY)
go func(cache *RuntimeCache, t *testing.T) {
time.Sleep(4 * time.Second)
value, err := f(cache, TEST_CACHE_KEY)

test.Nil(t,err)
test.Equal(t,insertValue,value)
}(cache,t)
test.Nil(t, err)
test.Equal(t, insertValue, value)
}(cache, t)

time.Sleep(5*time.Second)
time.Sleep(5 * time.Second)
}

func TestRuntimeCache_Delete(t *testing.T) {
cache:=NewRuntimeCache()
cache.Set(TEST_CACHE_KEY,TEST_CACHE_VALUE,5)
cache := NewRuntimeCache()
cache.Set(TEST_CACHE_KEY, TEST_CACHE_VALUE, 5)

value,e:=cache.Get(TEST_CACHE_KEY)
value, e := cache.Get(TEST_CACHE_KEY)

test.Nil(t,e)
test.Equal(t,TEST_CACHE_VALUE,value)
test.Nil(t, e)
test.Equal(t, TEST_CACHE_VALUE, value)

cache.Delete(TEST_CACHE_KEY)

value,e=cache.Get(TEST_CACHE_KEY)
test.Nil(t,e)
test.Nil(t,value)
value, e = cache.Get(TEST_CACHE_KEY)
test.Nil(t, e)
test.Nil(t, value)
}

func TestRuntimeCache_ClearAll(t *testing.T) {
cache:=NewRuntimeCache()
cache.Set(TEST_CACHE_KEY,TEST_CACHE_VALUE,5)
cache.Set("2",TEST_CACHE_VALUE,5)
cache.Set("3",TEST_CACHE_VALUE,5)
cache := NewRuntimeCache()
cache.Set(TEST_CACHE_KEY, TEST_CACHE_VALUE, 5)
cache.Set("2", TEST_CACHE_VALUE, 5)
cache.Set("3", TEST_CACHE_VALUE, 5)

val2, err := cache.GetString("2")
if err != nil{
if err != nil {
t.Error(err)
}
test.Equal(t,TEST_CACHE_VALUE, val2)
test.Equal(t, TEST_CACHE_VALUE, val2)

cache.ClearAll()
exists2, err := cache.Exists("2")
if err != nil{
if err != nil {
t.Error(err)
}
if exists2{
if exists2 {
t.Error("exists 2 but need not exists")
}
}

func TestRuntimeCache_Incr(t *testing.T) {
cache:=NewRuntimeCache()
cache := NewRuntimeCache()
var wg sync.WaitGroup
wg.Add(2)

Expand All @@ -138,14 +137,14 @@ func TestRuntimeCache_Incr(t *testing.T) {

wg.Wait()

value,e:=cache.GetInt(TEST_CACHE_KEY)
test.Nil(t,e)
value, e := cache.GetInt(TEST_CACHE_KEY)
test.Nil(t, e)

test.Equal(t,100,value)
test.Equal(t, 100, value)
}

func TestRuntimeCache_Decr(t *testing.T) {
cache:=NewRuntimeCache()
cache := NewRuntimeCache()
var wg sync.WaitGroup
wg.Add(2)

Expand All @@ -166,8 +165,8 @@ func TestRuntimeCache_Decr(t *testing.T) {

wg.Wait()

value,e:=cache.GetInt(TEST_CACHE_KEY)
test.Nil(t,e)
value, e := cache.GetInt(TEST_CACHE_KEY)
test.Nil(t, e)

test.Equal(t,-100,value)
}
test.Equal(t, -100, value)
}
51 changes: 26 additions & 25 deletions config/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package config
import (
"encoding/xml"
"errors"
"io/ioutil"

"github.com/devfeel/dotweb/core"
"github.com/devfeel/dotweb/framework/file"
"io/ioutil"
)

type (
Expand Down Expand Up @@ -42,33 +43,33 @@ type (

// ServerNode dotweb app's httpserver config
ServerNode struct {
EnabledListDir bool `xml:"enabledlistdir,attr"` //设置是否启用目录浏览,仅对Router.ServerFile有效,若设置该项,则可以浏览目录文件,默认不开启
EnabledRequestID bool `xml:"enabledrequestid,attr"` //设置是否启用唯一请求ID,默认不开启,开启后使用32位UUID
EnabledGzip bool `xml:"enabledgzip,attr"` //是否启用gzip
EnabledAutoHEAD bool `xml:"enabledautohead,attr"` //设置是否自动启用Head路由,若设置该项,则会为除Websocket\HEAD外所有路由方式默认添加HEAD路由,默认不开启
EnabledAutoOPTIONS bool //设置是否自动启用Options路由,若设置该项,则会为除Websocket\Options外所有路由方式默认添加Options路由,默认不开启
EnabledAutoCORS bool `xml:"enabledautocors,attr"` //设置是否自动跨域支持,若设置,默认“GET, POST, PUT, DELETE, OPTIONS”全部请求均支持跨域
EnabledIgnoreFavicon bool `xml:"enabledignorefavicon,attr"` //设置是否忽略favicon.ico请求,若设置,网站将把所有favicon.ico请求直接空返回
EnabledBindUseJsonTag bool `xml:"enabledbindusejsontag,attr"` //设置bind是否启用json标签,默认不启用,若设置,bind自动识别json tag,忽略form tag
EnabledStaticFileMiddleware bool //The flag which enabled or disabled middleware for static-file route
Port int `xml:"port,attr"` //端口
EnabledTLS bool `xml:"enabledtls,attr"` //是否启用TLS模式
TLSCertFile string `xml:"tlscertfile,attr"` //TLS模式下Certificate证书文件地址
TLSKeyFile string `xml:"tlskeyfile,attr"` //TLS模式下秘钥文件地址
IndexPage string `xml:"indexpage,attr"` //默认index页面
EnabledDetailRequestData bool `xml:"enableddetailrequestdata,attr"` //设置状态数据是否启用详细页面统计,默认不启用,请特别对待,如果站点url过多,会导致数据量过大
VirtualPath string //virtual path when deploy on no root path
EnabledListDir bool `xml:"enabledlistdir,attr"` //设置是否启用目录浏览,仅对Router.ServerFile有效,若设置该项,则可以浏览目录文件,默认不开启
EnabledRequestID bool `xml:"enabledrequestid,attr"` //设置是否启用唯一请求ID,默认不开启,开启后使用32位UUID
EnabledGzip bool `xml:"enabledgzip,attr"` //是否启用gzip
EnabledAutoHEAD bool `xml:"enabledautohead,attr"` //设置是否自动启用Head路由,若设置该项,则会为除Websocket\HEAD外所有路由方式默认添加HEAD路由,默认不开启
EnabledAutoOPTIONS bool //设置是否自动启用Options路由,若设置该项,则会为除Websocket\Options外所有路由方式默认添加Options路由,默认不开启
EnabledAutoCORS bool `xml:"enabledautocors,attr"` //设置是否自动跨域支持,若设置,默认“GET, POST, PUT, DELETE, OPTIONS”全部请求均支持跨域
EnabledIgnoreFavicon bool `xml:"enabledignorefavicon,attr"` //设置是否忽略favicon.ico请求,若设置,网站将把所有favicon.ico请求直接空返回
EnabledBindUseJsonTag bool `xml:"enabledbindusejsontag,attr"` //设置bind是否启用json标签,默认不启用,若设置,bind自动识别json tag,忽略form tag
EnabledStaticFileMiddleware bool //The flag which enabled or disabled middleware for static-file route
Port int `xml:"port,attr"` //端口
EnabledTLS bool `xml:"enabledtls,attr"` //是否启用TLS模式
TLSCertFile string `xml:"tlscertfile,attr"` //TLS模式下Certificate证书文件地址
TLSKeyFile string `xml:"tlskeyfile,attr"` //TLS模式下秘钥文件地址
IndexPage string `xml:"indexpage,attr"` //默认index页面
EnabledDetailRequestData bool `xml:"enableddetailrequestdata,attr"` //设置状态数据是否启用详细页面统计,默认不启用,请特别对待,如果站点url过多,会导致数据量过大
VirtualPath string //virtual path when deploy on no root path
}

// SessionNode dotweb app's session config
SessionNode struct {
EnabledSession bool `xml:"enabled,attr"` //启用Session
SessionMode string `xml:"mode,attr"` //session mode,now support runtime、redis
CookieName string `xml:"cookiename,attr"` //custom cookie name which sessionid store, default is dotweb_sessionId
Timeout int64 `xml:"timeout,attr"` //session time-out period, with second
ServerIP string `xml:"serverip,attr"` //remote session server url
BackupServerUrl string `xml:"backupserverurl,attr"` //backup remote session server url
StoreKeyPre string `xml:"storekeypre,attr"` //remote session StoreKeyPre
EnabledSession bool `xml:"enabled,attr"` //启用Session
SessionMode string `xml:"mode,attr"` //session mode,now support runtime、redis
CookieName string `xml:"cookiename,attr"` //custom cookie name which sessionid store, default is dotweb_sessionId
Timeout int64 `xml:"timeout,attr"` //session time-out period, with second
ServerIP string `xml:"serverip,attr"` //remote session server url
BackupServerUrl string `xml:"backupserverurl,attr"` //backup remote session server url
StoreKeyPre string `xml:"storekeypre,attr"` //remote session StoreKeyPre
}

// RouterNode dotweb app's router config
Expand Down Expand Up @@ -97,7 +98,7 @@ type (

const (
// ConfigType_XML xml config file
ConfigType_XML = "xml"
ConfigType_XML = "xml"
// ConfigType_JSON json config file
ConfigType_JSON = "json"
// ConfigType_Yaml yaml config file
Expand Down
Loading

0 comments on commit 2816db5

Please sign in to comment.