Skip to content

Commit

Permalink
Merge pull request beego#2611 from astaxie/develop
Browse files Browse the repository at this point in the history
1.8.2
  • Loading branch information
astaxie authored May 1, 2017
2 parents 522b3a4 + e76423e commit 7452151
Show file tree
Hide file tree
Showing 44 changed files with 370 additions and 252 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ install:
- go get github.com/gogo/protobuf/proto
- go get -u honnef.co/go/tools/cmd/gosimple
- go get -u github.com/mdempsky/unconvert
- go get -u github.com/gordonklaus/ineffassign
- go get -u github.com/golang/lint/golint
before_script:
- psql --version
- sh -c "if [ '$ORM_DRIVER' = 'postgres' ]; then psql -c 'create database orm_test;' -U postgres; fi"
Expand All @@ -51,5 +53,8 @@ script:
- go test -v ./...
- gosimple -ignore "$(cat .gosimpleignore)" $(go list ./... | grep -v /vendor/)
- unconvert $(go list ./... | grep -v /vendor/)
- ineffassign .
- find . ! \( -path './vendor' -prune \) -type f -name '*.go' -print0 | xargs -0 gofmt -l -s
- golint ./...
addons:
postgresql: "9.4"
49 changes: 27 additions & 22 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,29 +105,12 @@ func listConf(rw http.ResponseWriter, r *http.Request) {
tmpl.Execute(rw, data)

case "router":
var (
content = map[string]interface{}{
"Fields": []string{
"Router Pattern",
"Methods",
"Controller",
},
}
methods = []string{}
methodsData = make(map[string]interface{})
)
for method, t := range BeeApp.Handlers.routers {

resultList := new([][]string)

printTree(resultList, t)

methods = append(methods, method)
methodsData[method] = resultList
content := PrintTree()
content["Fields"] = []string{
"Router Pattern",
"Methods",
"Controller",
}

content["Data"] = methodsData
content["Methods"] = methods
data["Content"] = content
data["Title"] = "Routers"
execTpl(rw, data, routerAndFilterTpl, defaultScriptsTpl)
Expand Down Expand Up @@ -200,6 +183,28 @@ func list(root string, p interface{}, m map[string]interface{}) {
}
}

// PrintTree prints all registered routers.
func PrintTree() map[string]interface{} {
var (
content = map[string]interface{}{}
methods = []string{}
methodsData = make(map[string]interface{})
)
for method, t := range BeeApp.Handlers.routers {

resultList := new([][]string)

printTree(resultList, t)

methods = append(methods, method)
methodsData[method] = resultList
}

content["Data"] = methodsData
content["Methods"] = methods
return content
}

func printTree(resultList *[][]string, t *Tree) {
for _, tr := range t.fixrouters {
printTree(resultList, tr)
Expand Down
2 changes: 1 addition & 1 deletion beego.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

const (
// VERSION represent beego web framework version.
VERSION = "1.8.1"
VERSION = "1.8.2"

// DEV is for develop
DEV = "dev"
Expand Down
2 changes: 2 additions & 0 deletions config/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package env is used to parse environment.
package env

import (
Expand Down
5 changes: 4 additions & 1 deletion config/ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,10 @@ func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {

// Get section or key comments. Fixed #1607
getCommentStr := func(section, key string) string {
comment, ok := "", false
var (
comment string
ok bool
)
if len(key) == 0 {
comment, ok = c.sectionComment[section]
} else {
Expand Down
1 change: 1 addition & 0 deletions context/acceptencoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
getMethodOnly bool
)

// InitGzip init the gzipcompress
func InitGzip(minLength, compressLevel int, methods []string) {
if minLength >= 0 {
gzipMinLength = minLength
Expand Down
2 changes: 1 addition & 1 deletion context/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (output *BeegoOutput) Cookie(name string, value string, others ...interface
switch {
case maxAge > 0:
fmt.Fprintf(&b, "; Expires=%s; Max-Age=%d", time.Now().Add(time.Duration(maxAge)*time.Second).UTC().Format(time.RFC1123), maxAge)
case maxAge <= 0:
case maxAge < 0:
fmt.Fprintf(&b, "; Max-Age=0")
}
}
Expand Down
4 changes: 2 additions & 2 deletions grace/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func (srv *Server) fork() (err error) {
// RegisterSignalHook registers a function to be run PreSignal or PostSignal for a given signal.
func (srv *Server) RegisterSignalHook(ppFlag int, sig os.Signal, f func()) (err error) {
if ppFlag != PreSignal && ppFlag != PostSignal {
err = fmt.Errorf("Invalid ppFlag argument. Must be either grace.PreSignal or grace.PostSignal.")
err = fmt.Errorf("Invalid ppFlag argument. Must be either grace.PreSignal or grace.PostSignal")
return
}
for _, s := range hookableSignals {
Expand All @@ -300,6 +300,6 @@ func (srv *Server) RegisterSignalHook(ppFlag int, sig os.Signal, f func()) (err
return
}
}
err = fmt.Errorf("Signal '%v' is not supported.", sig)
err = fmt.Errorf("Signal '%v' is not supported", sig)
return
}
6 changes: 3 additions & 3 deletions hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ func registerSession() error {
conf.ProviderConfig = filepath.ToSlash(BConfig.WebConfig.Session.SessionProviderConfig)
conf.DisableHTTPOnly = BConfig.WebConfig.Session.SessionDisableHTTPOnly
conf.Domain = BConfig.WebConfig.Session.SessionDomain
conf.EnableSidInHttpHeader = BConfig.WebConfig.Session.SessionEnableSidInHTTPHeader
conf.SessionNameInHttpHeader = BConfig.WebConfig.Session.SessionNameInHTTPHeader
conf.EnableSidInUrlQuery = BConfig.WebConfig.Session.SessionEnableSidInURLQuery
conf.EnableSidInHTTPHeader = BConfig.WebConfig.Session.SessionEnableSidInHTTPHeader
conf.SessionNameInHTTPHeader = BConfig.WebConfig.Session.SessionNameInHTTPHeader
conf.EnableSidInURLQuery = BConfig.WebConfig.Session.SessionEnableSidInURLQuery
} else {
if err = json.Unmarshal([]byte(sessionConfig), conf); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion httplib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The default timeout is `60` seconds, function prototype:

SetTimeout(connectTimeout, readWriteTimeout time.Duration)

Exmaple:
Example:

// GET
httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)
Expand Down
4 changes: 2 additions & 2 deletions httplib/httplib.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,9 @@ func (b *BeegoHTTPRequest) Bytes() ([]byte, error) {
return nil, err
}
b.body, err = ioutil.ReadAll(reader)
} else {
b.body, err = ioutil.ReadAll(resp.Body)
return b.body, err
}
b.body, err = ioutil.ReadAll(resp.Body)
return b.body, err
}

Expand Down
46 changes: 20 additions & 26 deletions logs/alils/alils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ package alils

import (
"encoding/json"
"github.com/astaxie/beego/logs"
"github.com/gogo/protobuf/proto"
"strings"
"sync"
"time"

"github.com/astaxie/beego/logs"
"github.com/gogo/protobuf/proto"
)

const (
CacheSize int = 64
// CacheSize set the flush size
CacheSize int = 64
// Delimiter define the topic delimiter
Delimiter string = "##"
)

type AliLSConfig struct {
// Config is the Config for Ali Log
type Config struct {
Project string `json:"project"`
Endpoint string `json:"endpoint"`
KeyID string `json:"key_id"`
Expand All @@ -34,18 +38,17 @@ type aliLSWriter struct {
withMap bool
groupMap map[string]*LogGroup
lock *sync.Mutex
AliLSConfig
Config
}

// 创建提供Logger接口的日志服务
// NewAliLS create a new Logger
func NewAliLS() logs.Logger {
alils := new(aliLSWriter)
alils.Level = logs.LevelTrace
return alils
}

// 读取配置
// 初始化必要的数据结构
// Init parse config and init struct
func (c *aliLSWriter) Init(jsonConfig string) (err error) {

json.Unmarshal([]byte(jsonConfig), c)
Expand All @@ -54,28 +57,26 @@ func (c *aliLSWriter) Init(jsonConfig string) (err error) {
c.FlushWhen = CacheSize
}

// 初始化Project
prj := &LogProject{
Name: c.Project,
Endpoint: c.Endpoint,
AccessKeyId: c.KeyID,
AccessKeyID: c.KeyID,
AccessKeySecret: c.KeySecret,
}

// 获取logstore
c.store, err = prj.GetLogStore(c.LogStore)
if err != nil {
return err
}

// 创建默认Log Group
// Create default Log Group
c.group = append(c.group, &LogGroup{
Topic: proto.String(""),
Source: proto.String(c.Source),
Logs: make([]*Log, 0, c.FlushWhen),
})

// 创建其它Log Group
// Create other Log Group
c.groupMap = make(map[string]*LogGroup)
for _, topic := range c.Topics {

Expand Down Expand Up @@ -113,7 +114,7 @@ func (c *aliLSWriter) WriteMsg(when time.Time, msg string, level int) (err error
var lg *LogGroup
if c.withMap {

// 解析出Topic,并匹配LogGroup
// Topic,LogGroup
strs := strings.SplitN(msg, Delimiter, 2)
if len(strs) == 2 {
pos := strings.LastIndex(strs[0], " ")
Expand All @@ -122,27 +123,24 @@ func (c *aliLSWriter) WriteMsg(when time.Time, msg string, level int) (err error
lg = c.groupMap[topic]
}

// 默认发到空Topic
// send to empty Topic
if lg == nil {
topic = ""
content = msg
lg = c.group[0]
}
} else {
topic = ""
content = msg
lg = c.group[0]
}

// 生成日志
c1 := &Log_Content{
c1 := &LogContent{
Key: proto.String("msg"),
Value: proto.String(content),
}

l := &Log{
Time: proto.Uint32(uint32(when.Unix())), // 填写日志时间
Contents: []*Log_Content{
Time: proto.Uint32(uint32(when.Unix())),
Contents: []*LogContent{
c1,
},
}
Expand All @@ -151,7 +149,6 @@ func (c *aliLSWriter) WriteMsg(when time.Time, msg string, level int) (err error
lg.Logs = append(lg.Logs, l)
c.lock.Unlock()

// 满足条件则Flush
if len(lg.Logs) >= c.FlushWhen {
c.flush(lg)
}
Expand All @@ -162,7 +159,7 @@ func (c *aliLSWriter) WriteMsg(when time.Time, msg string, level int) (err error
// Flush implementing method. empty.
func (c *aliLSWriter) Flush() {

// flush所有group
// flush all group
for _, lg := range c.group {
c.flush(lg)
}
Expand All @@ -176,9 +173,6 @@ func (c *aliLSWriter) flush(lg *LogGroup) {

c.lock.Lock()
defer c.lock.Unlock()

// 把以上的LogGroup推送到SLS服务器,
// SLS服务器会根据该logstore的shard个数自动进行负载均衡。
err := c.store.PutLogs(lg)
if err != nil {
return
Expand Down
Loading

0 comments on commit 7452151

Please sign in to comment.