mirror of
https://github.com/astaxie/beego.git
synced 2025-07-12 22:11:02 +00:00
Compare commits
105 Commits
Author | SHA1 | Date | |
---|---|---|---|
1b6edafc96 | |||
8152ade1b6 | |||
6350f8b904 | |||
0c5398a19c | |||
4b656268d3 | |||
10729a1fc5 | |||
cdb3ef808f | |||
a5a2471f2c | |||
6282747f6d | |||
d5fd5cad38 | |||
fab7c6b6d0 | |||
42ade6aa49 | |||
55d9b69cd9 | |||
2a8d6f943f | |||
6b0155c4fb | |||
e22a5143bc | |||
a17eb54515 | |||
d5cf1050db | |||
b021686521 | |||
b0e2bbce2a | |||
7a50ea7e36 | |||
3070cfc60b | |||
e56d1b718f | |||
c4c3067a31 | |||
81346fe641 | |||
1a66ad56c6 | |||
31f2adb79d | |||
f514ae309b | |||
277d3d98e3 | |||
2c46877b36 | |||
cfe54a02c5 | |||
55f390d08a | |||
cf31222643 | |||
9e036bcab5 | |||
d02170e3cb | |||
2d6f1af1a5 | |||
430457609f | |||
0333e26b3e | |||
d0d28566b9 | |||
872b787e6c | |||
01a99edf80 | |||
6050d37d2a | |||
876dce8e54 | |||
24885c28f2 | |||
5ea04bdfd3 | |||
9fdc1eaf3a | |||
6b5a70d246 | |||
8391d26220 | |||
f193e313a3 | |||
9ac4928113 | |||
9865779f14 | |||
aa6d0f9f0b | |||
68b0bd98fd | |||
3447798494 | |||
ca1b96f986 | |||
771fe35431 | |||
2f00ad1602 | |||
f740b71ded | |||
7aae58a543 | |||
c8da875f83 | |||
501d8a97f6 | |||
736e66fcda | |||
d3ad810f16 | |||
abc8b78065 | |||
f64e6b72e9 | |||
4e83b4400a | |||
f6f61513a1 | |||
8217817a0b | |||
833f54d818 | |||
706c086bc5 | |||
187add9b84 | |||
5b8e468a13 | |||
dff9c8f5fa | |||
21a8623002 | |||
ea9c5822e6 | |||
ad0d166d46 | |||
2c2ace9a60 | |||
8134a89e81 | |||
e342a0099f | |||
c4ed5030da | |||
7b9c24567d | |||
6906c5ce30 | |||
e4605f232b | |||
6092e737a1 | |||
0e4d954fa7 | |||
1cbba4d56f | |||
cf5d1f3f3c | |||
1097ac3682 | |||
755cc98ef7 | |||
5c407ff2e3 | |||
8f455ef199 | |||
7e0649d661 | |||
1a3dcb4f84 | |||
b606f1f73f | |||
8241f219fd | |||
dea45a3d6c | |||
34a812d45f | |||
842336834f | |||
58fe012446 | |||
bf0d40bca6 | |||
48e6658eca | |||
1bd3fb7a33 | |||
d86410a631 | |||
6b62502b99 | |||
7886e69236 |
@ -1,8 +1,8 @@
|
|||||||
language: go
|
language: go
|
||||||
|
|
||||||
go:
|
go:
|
||||||
- "1.9.7"
|
- "1.10.x"
|
||||||
- "1.10.3"
|
- "1.11.x"
|
||||||
services:
|
services:
|
||||||
- redis-server
|
- redis-server
|
||||||
- mysql
|
- mysql
|
||||||
@ -34,6 +34,7 @@ install:
|
|||||||
- go get github.com/gogo/protobuf/proto
|
- go get github.com/gogo/protobuf/proto
|
||||||
- go get github.com/Knetic/govaluate
|
- go get github.com/Knetic/govaluate
|
||||||
- go get github.com/casbin/casbin
|
- go get github.com/casbin/casbin
|
||||||
|
- go get github.com/elazarl/go-bindata-assetfs
|
||||||
- go get -u honnef.co/go/tools/cmd/gosimple
|
- go get -u honnef.co/go/tools/cmd/gosimple
|
||||||
- go get -u github.com/mdempsky/unconvert
|
- go get -u github.com/mdempsky/unconvert
|
||||||
- go get -u github.com/gordonklaus/ineffassign
|
- go get -u github.com/gordonklaus/ineffassign
|
||||||
@ -49,7 +50,7 @@ before_script:
|
|||||||
- mkdir -p res/var
|
- mkdir -p res/var
|
||||||
- ./ssdb/ssdb-server ./ssdb/ssdb.conf -d
|
- ./ssdb/ssdb-server ./ssdb/ssdb.conf -d
|
||||||
after_script:
|
after_script:
|
||||||
-killall -w ssdb-server
|
- killall -w ssdb-server
|
||||||
- rm -rf ./res/var/*
|
- rm -rf ./res/var/*
|
||||||
script:
|
script:
|
||||||
- go test -v ./...
|
- go test -v ./...
|
||||||
|
@ -4,6 +4,8 @@
|
|||||||
beego is used for rapid development of RESTful APIs, web apps and backend services in Go.
|
beego is used for rapid development of RESTful APIs, web apps and backend services in Go.
|
||||||
It is inspired by Tornado, Sinatra and Flask. beego has some Go-specific features such as interfaces and struct embedding.
|
It is inspired by Tornado, Sinatra and Flask. beego has some Go-specific features such as interfaces and struct embedding.
|
||||||
|
|
||||||
|
Response time ranking: [web-frameworks](https://github.com/the-benchmarker/web-frameworks).
|
||||||
|
|
||||||
###### More info at [beego.me](http://beego.me).
|
###### More info at [beego.me](http://beego.me).
|
||||||
|
|
||||||
## Quick Start
|
## Quick Start
|
||||||
|
35
admin.go
35
admin.go
@ -20,11 +20,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"github.com/astaxie/beego/grace"
|
"github.com/astaxie/beego/grace"
|
||||||
"github.com/astaxie/beego/logs"
|
"github.com/astaxie/beego/logs"
|
||||||
"github.com/astaxie/beego/toolbox"
|
"github.com/astaxie/beego/toolbox"
|
||||||
@ -35,7 +34,7 @@ import (
|
|||||||
var beeAdminApp *adminApp
|
var beeAdminApp *adminApp
|
||||||
|
|
||||||
// FilterMonitorFunc is default monitor filter when admin module is enable.
|
// FilterMonitorFunc is default monitor filter when admin module is enable.
|
||||||
// if this func returns, admin module records qbs for this request by condition of this function logic.
|
// if this func returns, admin module records qps for this request by condition of this function logic.
|
||||||
// usage:
|
// usage:
|
||||||
// func MyFilterMonitor(method, requestPath string, t time.Duration, pattern string, statusCode int) bool {
|
// func MyFilterMonitor(method, requestPath string, t time.Duration, pattern string, statusCode int) bool {
|
||||||
// if method == "POST" {
|
// if method == "POST" {
|
||||||
@ -67,18 +66,18 @@ func init() {
|
|||||||
|
|
||||||
// AdminIndex is the default http.Handler for admin module.
|
// AdminIndex is the default http.Handler for admin module.
|
||||||
// it matches url pattern "/".
|
// it matches url pattern "/".
|
||||||
func adminIndex(rw http.ResponseWriter, r *http.Request) {
|
func adminIndex(rw http.ResponseWriter, _ *http.Request) {
|
||||||
execTpl(rw, map[interface{}]interface{}{}, indexTpl, defaultScriptsTpl)
|
execTpl(rw, map[interface{}]interface{}{}, indexTpl, defaultScriptsTpl)
|
||||||
}
|
}
|
||||||
|
|
||||||
// QpsIndex is the http.Handler for writing qbs statistics map result info in http.ResponseWriter.
|
// QpsIndex is the http.Handler for writing qps statistics map result info in http.ResponseWriter.
|
||||||
// it's registered with url pattern "/qbs" in admin module.
|
// it's registered with url pattern "/qps" in admin module.
|
||||||
func qpsIndex(rw http.ResponseWriter, r *http.Request) {
|
func qpsIndex(rw http.ResponseWriter, _ *http.Request) {
|
||||||
data := make(map[interface{}]interface{})
|
data := make(map[interface{}]interface{})
|
||||||
data["Content"] = toolbox.StatisticsMap.GetMap()
|
data["Content"] = toolbox.StatisticsMap.GetMap()
|
||||||
|
|
||||||
// do html escape before display path, avoid xss
|
// do html escape before display path, avoid xss
|
||||||
if content, ok := (data["Content"]).(map[string]interface{}); ok {
|
if content, ok := (data["Content"]).(M); ok {
|
||||||
if resultLists, ok := (content["Data"]).([][]string); ok {
|
if resultLists, ok := (content["Data"]).([][]string); ok {
|
||||||
for i := range resultLists {
|
for i := range resultLists {
|
||||||
if len(resultLists[i]) > 0 {
|
if len(resultLists[i]) > 0 {
|
||||||
@ -104,7 +103,7 @@ func listConf(rw http.ResponseWriter, r *http.Request) {
|
|||||||
data := make(map[interface{}]interface{})
|
data := make(map[interface{}]interface{})
|
||||||
switch command {
|
switch command {
|
||||||
case "conf":
|
case "conf":
|
||||||
m := make(map[string]interface{})
|
m := make(M)
|
||||||
list("BConfig", BConfig, m)
|
list("BConfig", BConfig, m)
|
||||||
m["AppConfigPath"] = appConfigPath
|
m["AppConfigPath"] = appConfigPath
|
||||||
m["AppConfigProvider"] = appConfigProvider
|
m["AppConfigProvider"] = appConfigProvider
|
||||||
@ -128,14 +127,14 @@ func listConf(rw http.ResponseWriter, r *http.Request) {
|
|||||||
execTpl(rw, data, routerAndFilterTpl, defaultScriptsTpl)
|
execTpl(rw, data, routerAndFilterTpl, defaultScriptsTpl)
|
||||||
case "filter":
|
case "filter":
|
||||||
var (
|
var (
|
||||||
content = map[string]interface{}{
|
content = M{
|
||||||
"Fields": []string{
|
"Fields": []string{
|
||||||
"Router Pattern",
|
"Router Pattern",
|
||||||
"Filter Function",
|
"Filter Function",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
filterTypes = []string{}
|
filterTypes = []string{}
|
||||||
filterTypeData = make(map[string]interface{})
|
filterTypeData = make(M)
|
||||||
)
|
)
|
||||||
|
|
||||||
if BeeApp.Handlers.enableFilter {
|
if BeeApp.Handlers.enableFilter {
|
||||||
@ -173,7 +172,7 @@ func listConf(rw http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func list(root string, p interface{}, m map[string]interface{}) {
|
func list(root string, p interface{}, m M) {
|
||||||
pt := reflect.TypeOf(p)
|
pt := reflect.TypeOf(p)
|
||||||
pv := reflect.ValueOf(p)
|
pv := reflect.ValueOf(p)
|
||||||
if pt.Kind() == reflect.Ptr {
|
if pt.Kind() == reflect.Ptr {
|
||||||
@ -196,11 +195,11 @@ func list(root string, p interface{}, m map[string]interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PrintTree prints all registered routers.
|
// PrintTree prints all registered routers.
|
||||||
func PrintTree() map[string]interface{} {
|
func PrintTree() M {
|
||||||
var (
|
var (
|
||||||
content = map[string]interface{}{}
|
content = M{}
|
||||||
methods = []string{}
|
methods = []string{}
|
||||||
methodsData = make(map[string]interface{})
|
methodsData = make(M)
|
||||||
)
|
)
|
||||||
for method, t := range BeeApp.Handlers.routers {
|
for method, t := range BeeApp.Handlers.routers {
|
||||||
|
|
||||||
@ -291,12 +290,12 @@ func profIndex(rw http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// Healthcheck is a http.Handler calling health checking and showing the result.
|
// Healthcheck is a http.Handler calling health checking and showing the result.
|
||||||
// it's in "/healthcheck" pattern in admin module.
|
// it's in "/healthcheck" pattern in admin module.
|
||||||
func healthcheck(rw http.ResponseWriter, req *http.Request) {
|
func healthcheck(rw http.ResponseWriter, _ *http.Request) {
|
||||||
var (
|
var (
|
||||||
result []string
|
result []string
|
||||||
data = make(map[interface{}]interface{})
|
data = make(map[interface{}]interface{})
|
||||||
resultList = new([][]string)
|
resultList = new([][]string)
|
||||||
content = map[string]interface{}{
|
content = M{
|
||||||
"Fields": []string{"Name", "Message", "Status"},
|
"Fields": []string{"Name", "Message", "Status"},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -344,7 +343,7 @@ func taskStatus(rw http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// List Tasks
|
// List Tasks
|
||||||
content := make(map[string]interface{})
|
content := make(M)
|
||||||
resultList := new([][]string)
|
resultList := new([][]string)
|
||||||
var fields = []string{
|
var fields = []string{
|
||||||
"Task Name",
|
"Task Name",
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestList_01(t *testing.T) {
|
func TestList_01(t *testing.T) {
|
||||||
m := make(map[string]interface{})
|
m := make(M)
|
||||||
list("BConfig", BConfig, m)
|
list("BConfig", BConfig, m)
|
||||||
t.Log(m)
|
t.Log(m)
|
||||||
om := oldMap()
|
om := oldMap()
|
||||||
@ -18,8 +18,8 @@ func TestList_01(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func oldMap() map[string]interface{} {
|
func oldMap() M {
|
||||||
m := make(map[string]interface{})
|
m := make(M)
|
||||||
m["BConfig.AppName"] = BConfig.AppName
|
m["BConfig.AppName"] = BConfig.AppName
|
||||||
m["BConfig.RunMode"] = BConfig.RunMode
|
m["BConfig.RunMode"] = BConfig.RunMode
|
||||||
m["BConfig.RouterCaseSensitive"] = BConfig.RouterCaseSensitive
|
m["BConfig.RouterCaseSensitive"] = BConfig.RouterCaseSensitive
|
||||||
|
7
beego.go
7
beego.go
@ -23,7 +23,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
// VERSION represent beego web framework version.
|
// VERSION represent beego web framework version.
|
||||||
VERSION = "1.10.1"
|
VERSION = "1.11.1"
|
||||||
|
|
||||||
// DEV is for develop
|
// DEV is for develop
|
||||||
DEV = "dev"
|
DEV = "dev"
|
||||||
@ -31,7 +31,10 @@ const (
|
|||||||
PROD = "prod"
|
PROD = "prod"
|
||||||
)
|
)
|
||||||
|
|
||||||
//hook function to run
|
// M is Map shortcut
|
||||||
|
type M map[string]interface{}
|
||||||
|
|
||||||
|
// Hook function to run
|
||||||
type hookfunc func() error
|
type hookfunc func() error
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
10
cache/memory.go
vendored
10
cache/memory.go
vendored
@ -203,13 +203,17 @@ func (bc *MemoryCache) StartAndGC(config string) error {
|
|||||||
dur := time.Duration(cf["interval"]) * time.Second
|
dur := time.Duration(cf["interval"]) * time.Second
|
||||||
bc.Every = cf["interval"]
|
bc.Every = cf["interval"]
|
||||||
bc.dur = dur
|
bc.dur = dur
|
||||||
go bc.vaccuum()
|
go bc.vacuum()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// check expiration.
|
// check expiration.
|
||||||
func (bc *MemoryCache) vaccuum() {
|
func (bc *MemoryCache) vacuum() {
|
||||||
if bc.Every < 1 {
|
bc.RLock()
|
||||||
|
every := bc.Every
|
||||||
|
bc.RUnlock()
|
||||||
|
|
||||||
|
if every < 1 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
|
9
cache/redis/redis.go
vendored
9
cache/redis/redis.go
vendored
@ -39,6 +39,7 @@ import (
|
|||||||
"github.com/gomodule/redigo/redis"
|
"github.com/gomodule/redigo/redis"
|
||||||
|
|
||||||
"github.com/astaxie/beego/cache"
|
"github.com/astaxie/beego/cache"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -164,6 +165,14 @@ func (rc *Cache) StartAndGC(config string) error {
|
|||||||
if _, ok := cf["conn"]; !ok {
|
if _, ok := cf["conn"]; !ok {
|
||||||
return errors.New("config has no conn key")
|
return errors.New("config has no conn key")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Format redis://<password>@<host>:<port>
|
||||||
|
cf["conn"] = strings.Replace(cf["conn"], "redis://", "", 1)
|
||||||
|
if i := strings.Index(cf["conn"], "@"); i > -1 {
|
||||||
|
cf["password"] = cf["conn"][0:i]
|
||||||
|
cf["conn"] = cf["conn"][i+1:]
|
||||||
|
}
|
||||||
|
|
||||||
if _, ok := cf["dbNum"]; !ok {
|
if _, ok := cf["dbNum"]; !ok {
|
||||||
cf["dbNum"] = "0"
|
cf["dbNum"] = "0"
|
||||||
}
|
}
|
||||||
|
@ -78,15 +78,37 @@ func (ini *IniConfig) parseData(dir string, data []byte) (*IniConfigContainer, e
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
section := defaultSection
|
section := defaultSection
|
||||||
|
tmpBuf := bytes.NewBuffer(nil)
|
||||||
for {
|
for {
|
||||||
line, _, err := buf.ReadLine()
|
tmpBuf.Reset()
|
||||||
if err == io.EOF {
|
|
||||||
|
shouldBreak := false
|
||||||
|
for {
|
||||||
|
tmp, isPrefix, err := buf.ReadLine()
|
||||||
|
if err == io.EOF {
|
||||||
|
shouldBreak = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
//It might be a good idea to throw a error on all unknonw errors?
|
||||||
|
if _, ok := err.(*os.PathError); ok {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpBuf.Write(tmp)
|
||||||
|
if isPrefix {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isPrefix {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if shouldBreak {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
//It might be a good idea to throw a error on all unknonw errors?
|
|
||||||
if _, ok := err.(*os.PathError); ok {
|
line := tmpBuf.Bytes()
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
line = bytes.TrimSpace(line)
|
line = bytes.TrimSpace(line)
|
||||||
if bytes.Equal(line, bEmpty) {
|
if bytes.Equal(line, bEmpty) {
|
||||||
continue
|
continue
|
||||||
|
@ -48,15 +48,15 @@ func TestAssignConfig_02(t *testing.T) {
|
|||||||
_BConfig := &Config{}
|
_BConfig := &Config{}
|
||||||
bs, _ := json.Marshal(newBConfig())
|
bs, _ := json.Marshal(newBConfig())
|
||||||
|
|
||||||
jsonMap := map[string]interface{}{}
|
jsonMap := M{}
|
||||||
json.Unmarshal(bs, &jsonMap)
|
json.Unmarshal(bs, &jsonMap)
|
||||||
|
|
||||||
configMap := map[string]interface{}{}
|
configMap := M{}
|
||||||
for k, v := range jsonMap {
|
for k, v := range jsonMap {
|
||||||
if reflect.TypeOf(v).Kind() == reflect.Map {
|
if reflect.TypeOf(v).Kind() == reflect.Map {
|
||||||
for k1, v1 := range v.(map[string]interface{}) {
|
for k1, v1 := range v.(M) {
|
||||||
if reflect.TypeOf(v1).Kind() == reflect.Map {
|
if reflect.TypeOf(v1).Kind() == reflect.Map {
|
||||||
for k2, v2 := range v1.(map[string]interface{}) {
|
for k2, v2 := range v1.(M) {
|
||||||
configMap[k2] = v2
|
configMap[k2] = v2
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -38,6 +38,14 @@ import (
|
|||||||
"github.com/astaxie/beego/utils"
|
"github.com/astaxie/beego/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//commonly used mime-types
|
||||||
|
const (
|
||||||
|
ApplicationJSON = "application/json"
|
||||||
|
ApplicationXML = "application/xml"
|
||||||
|
ApplicationYAML = "application/x-yaml"
|
||||||
|
TextXML = "text/xml"
|
||||||
|
)
|
||||||
|
|
||||||
// NewContext return the Context with Input and Output
|
// NewContext return the Context with Input and Output
|
||||||
func NewContext() *Context {
|
func NewContext() *Context {
|
||||||
return &Context{
|
return &Context{
|
||||||
@ -193,6 +201,7 @@ type Response struct {
|
|||||||
http.ResponseWriter
|
http.ResponseWriter
|
||||||
Started bool
|
Started bool
|
||||||
Status int
|
Status int
|
||||||
|
Elapsed time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Response) reset(rw http.ResponseWriter) {
|
func (r *Response) reset(rw http.ResponseWriter) {
|
||||||
@ -244,3 +253,11 @@ func (r *Response) CloseNotify() <-chan bool {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pusher http.Pusher
|
||||||
|
func (r *Response) Pusher() (pusher http.Pusher) {
|
||||||
|
if pusher, ok := r.ResponseWriter.(http.Pusher); ok {
|
||||||
|
return pusher
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -206,7 +206,7 @@ func (output *BeegoOutput) JSON(data interface{}, hasIndent bool, encoding bool)
|
|||||||
|
|
||||||
// YAML writes yaml to response body.
|
// YAML writes yaml to response body.
|
||||||
func (output *BeegoOutput) YAML(data interface{}) error {
|
func (output *BeegoOutput) YAML(data interface{}) error {
|
||||||
output.Header("Content-Type", "application/application/x-yaml; charset=utf-8")
|
output.Header("Content-Type", "application/x-yaml; charset=utf-8")
|
||||||
var content []byte
|
var content []byte
|
||||||
var err error
|
var err error
|
||||||
content, err = yaml.Marshal(data)
|
content, err = yaml.Marshal(data)
|
||||||
@ -260,6 +260,19 @@ func (output *BeegoOutput) XML(data interface{}, hasIndent bool) error {
|
|||||||
return output.Body(content)
|
return output.Body(content)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServeFormatted serve YAML, XML OR JSON, depending on the value of the Accept header
|
||||||
|
func (output *BeegoOutput) ServeFormatted(data interface{}, hasIndent bool, hasEncode ...bool) {
|
||||||
|
accept := output.Context.Input.Header("Accept")
|
||||||
|
switch accept {
|
||||||
|
case ApplicationYAML:
|
||||||
|
output.YAML(data)
|
||||||
|
case ApplicationXML, TextXML:
|
||||||
|
output.XML(data, hasIndent)
|
||||||
|
default:
|
||||||
|
output.JSON(data, hasIndent, len(hasEncode) > 0 && hasEncode[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Download forces response for download file.
|
// Download forces response for download file.
|
||||||
// it prepares the download response header automatically.
|
// it prepares the download response header automatically.
|
||||||
func (output *BeegoOutput) Download(file string, filename ...string) {
|
func (output *BeegoOutput) Download(file string, filename ...string) {
|
||||||
|
@ -32,14 +32,6 @@ import (
|
|||||||
"github.com/astaxie/beego/session"
|
"github.com/astaxie/beego/session"
|
||||||
)
|
)
|
||||||
|
|
||||||
//commonly used mime-types
|
|
||||||
const (
|
|
||||||
applicationJSON = "application/json"
|
|
||||||
applicationXML = "application/xml"
|
|
||||||
applicationYAML = "application/x-yaml"
|
|
||||||
textXML = "text/xml"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ErrAbort custom error when user stop request handler manually.
|
// ErrAbort custom error when user stop request handler manually.
|
||||||
ErrAbort = errors.New("User stop run")
|
ErrAbort = errors.New("User stop run")
|
||||||
@ -47,10 +39,37 @@ var (
|
|||||||
GlobalControllerRouter = make(map[string][]ControllerComments)
|
GlobalControllerRouter = make(map[string][]ControllerComments)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ControllerFilter store the filter for controller
|
||||||
|
type ControllerFilter struct {
|
||||||
|
Pattern string
|
||||||
|
Pos int
|
||||||
|
Filter FilterFunc
|
||||||
|
ReturnOnOutput bool
|
||||||
|
ResetParams bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// ControllerFilterComments store the comment for controller level filter
|
||||||
|
type ControllerFilterComments struct {
|
||||||
|
Pattern string
|
||||||
|
Pos int
|
||||||
|
Filter string // NOQA
|
||||||
|
ReturnOnOutput bool
|
||||||
|
ResetParams bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// ControllerImportComments store the import comment for controller needed
|
||||||
|
type ControllerImportComments struct {
|
||||||
|
ImportPath string
|
||||||
|
ImportAlias string
|
||||||
|
}
|
||||||
|
|
||||||
// ControllerComments store the comment for the controller method
|
// ControllerComments store the comment for the controller method
|
||||||
type ControllerComments struct {
|
type ControllerComments struct {
|
||||||
Method string
|
Method string
|
||||||
Router string
|
Router string
|
||||||
|
Filters []*ControllerFilter
|
||||||
|
ImportComments []*ControllerImportComments
|
||||||
|
FilterComments []*ControllerFilterComments
|
||||||
AllowHTTPMethods []string
|
AllowHTTPMethods []string
|
||||||
Params []map[string]string
|
Params []map[string]string
|
||||||
MethodParams []*param.MethodParam
|
MethodParams []*param.MethodParam
|
||||||
@ -275,16 +294,15 @@ func (c *Controller) viewPath() string {
|
|||||||
func (c *Controller) Redirect(url string, code int) {
|
func (c *Controller) Redirect(url string, code int) {
|
||||||
logAccess(c.Ctx, nil, code)
|
logAccess(c.Ctx, nil, code)
|
||||||
c.Ctx.Redirect(code, url)
|
c.Ctx.Redirect(code, url)
|
||||||
panic(ErrAbort)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the data depending on the accepted
|
// SetData set the data depending on the accepted
|
||||||
func (c *Controller) SetData(data interface{}) {
|
func (c *Controller) SetData(data interface{}) {
|
||||||
accept := c.Ctx.Input.Header("Accept")
|
accept := c.Ctx.Input.Header("Accept")
|
||||||
switch accept {
|
switch accept {
|
||||||
case applicationJSON:
|
case context.ApplicationYAML:
|
||||||
c.Data["json"] = data
|
c.Data["yaml"] = data
|
||||||
case applicationXML, textXML:
|
case context.ApplicationXML, context.TextXML:
|
||||||
c.Data["xml"] = data
|
c.Data["xml"] = data
|
||||||
default:
|
default:
|
||||||
c.Data["json"] = data
|
c.Data["json"] = data
|
||||||
@ -333,54 +351,35 @@ func (c *Controller) URLFor(endpoint string, values ...interface{}) string {
|
|||||||
// ServeJSON sends a json response with encoding charset.
|
// ServeJSON sends a json response with encoding charset.
|
||||||
func (c *Controller) ServeJSON(encoding ...bool) {
|
func (c *Controller) ServeJSON(encoding ...bool) {
|
||||||
var (
|
var (
|
||||||
hasIndent = true
|
hasIndent = BConfig.RunMode != PROD
|
||||||
hasEncoding = false
|
hasEncoding = len(encoding) > 0 && encoding[0]
|
||||||
)
|
)
|
||||||
if BConfig.RunMode == PROD {
|
|
||||||
hasIndent = false
|
|
||||||
}
|
|
||||||
if len(encoding) > 0 && encoding[0] {
|
|
||||||
hasEncoding = true
|
|
||||||
}
|
|
||||||
c.Ctx.Output.JSON(c.Data["json"], hasIndent, hasEncoding)
|
c.Ctx.Output.JSON(c.Data["json"], hasIndent, hasEncoding)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeJSONP sends a jsonp response.
|
// ServeJSONP sends a jsonp response.
|
||||||
func (c *Controller) ServeJSONP() {
|
func (c *Controller) ServeJSONP() {
|
||||||
hasIndent := true
|
hasIndent := BConfig.RunMode != PROD
|
||||||
if BConfig.RunMode == PROD {
|
|
||||||
hasIndent = false
|
|
||||||
}
|
|
||||||
c.Ctx.Output.JSONP(c.Data["jsonp"], hasIndent)
|
c.Ctx.Output.JSONP(c.Data["jsonp"], hasIndent)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeXML sends xml response.
|
// ServeXML sends xml response.
|
||||||
func (c *Controller) ServeXML() {
|
func (c *Controller) ServeXML() {
|
||||||
hasIndent := true
|
hasIndent := BConfig.RunMode != PROD
|
||||||
if BConfig.RunMode == PROD {
|
|
||||||
hasIndent = false
|
|
||||||
}
|
|
||||||
c.Ctx.Output.XML(c.Data["xml"], hasIndent)
|
c.Ctx.Output.XML(c.Data["xml"], hasIndent)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeXML sends xml response.
|
// ServeYAML sends yaml response.
|
||||||
func (c *Controller) ServeYAML() {
|
func (c *Controller) ServeYAML() {
|
||||||
c.Ctx.Output.YAML(c.Data["yaml"])
|
c.Ctx.Output.YAML(c.Data["yaml"])
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeFormatted serve Xml OR Json, depending on the value of the Accept header
|
// ServeFormatted serve YAML, XML OR JSON, depending on the value of the Accept header
|
||||||
func (c *Controller) ServeFormatted() {
|
func (c *Controller) ServeFormatted(encoding ...bool) {
|
||||||
accept := c.Ctx.Input.Header("Accept")
|
hasIndent := BConfig.RunMode != PROD
|
||||||
switch accept {
|
hasEncoding := len(encoding) > 0 && encoding[0]
|
||||||
case applicationJSON:
|
c.Ctx.Output.ServeFormatted(c.Data, hasIndent, hasEncoding)
|
||||||
c.ServeJSON()
|
|
||||||
case applicationXML, textXML:
|
|
||||||
c.ServeXML()
|
|
||||||
case applicationYAML:
|
|
||||||
c.ServeYAML()
|
|
||||||
default:
|
|
||||||
c.ServeJSON()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Input returns the input data map from POST or PUT request body and query string.
|
// Input returns the input data map from POST or PUT request body and query string.
|
||||||
|
2
error.go
2
error.go
@ -361,7 +361,7 @@ func gatewayTimeout(rw http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func responseError(rw http.ResponseWriter, r *http.Request, errCode int, errContent string) {
|
func responseError(rw http.ResponseWriter, r *http.Request, errCode int, errContent string) {
|
||||||
t, _ := template.New("beegoerrortemp").Parse(errtpl)
|
t, _ := template.New("beegoerrortemp").Parse(errtpl)
|
||||||
data := map[string]interface{}{
|
data := M{
|
||||||
"Title": http.StatusText(errCode),
|
"Title": http.StatusText(errCode),
|
||||||
"BeegoVersion": VERSION,
|
"BeegoVersion": VERSION,
|
||||||
"Content": template.HTML(errContent),
|
"Content": template.HTML(errContent),
|
||||||
|
74
fs.go
Normal file
74
fs.go
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
package beego
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FileSystem struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d FileSystem) Open(name string) (http.File, error) {
|
||||||
|
return os.Open(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Walk walks the file tree rooted at root in filesystem, calling walkFn for each file or
|
||||||
|
// directory in the tree, including root. All errors that arise visiting files
|
||||||
|
// and directories are filtered by walkFn.
|
||||||
|
func Walk(fs http.FileSystem, root string, walkFn filepath.WalkFunc) error {
|
||||||
|
|
||||||
|
f, err := fs.Open(root)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
info, err := f.Stat()
|
||||||
|
if err != nil {
|
||||||
|
err = walkFn(root, nil, err)
|
||||||
|
} else {
|
||||||
|
err = walk(fs, root, info, walkFn)
|
||||||
|
}
|
||||||
|
if err == filepath.SkipDir {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// walk recursively descends path, calling walkFn.
|
||||||
|
func walk(fs http.FileSystem, path string, info os.FileInfo, walkFn filepath.WalkFunc) error {
|
||||||
|
var err error
|
||||||
|
if !info.IsDir() {
|
||||||
|
return walkFn(path, info, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
dir, err := fs.Open(path)
|
||||||
|
defer dir.Close()
|
||||||
|
if err != nil {
|
||||||
|
if err1 := walkFn(path, info, err); err1 != nil {
|
||||||
|
return err1
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
dirs, err := dir.Readdir(-1)
|
||||||
|
err1 := walkFn(path, info, err)
|
||||||
|
// If err != nil, walk can't walk into this directory.
|
||||||
|
// err1 != nil means walkFn want walk to skip this directory or stop walking.
|
||||||
|
// Therefore, if one of err and err1 isn't nil, walk will return.
|
||||||
|
if err != nil || err1 != nil {
|
||||||
|
// The caller's behavior is controlled by the return value, which is decided
|
||||||
|
// by walkFn. walkFn may ignore err and return nil.
|
||||||
|
// If walkFn returns SkipDir, it will be handled by the caller.
|
||||||
|
// So walk should return whatever walkFn returns.
|
||||||
|
return err1
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, fileInfo := range dirs {
|
||||||
|
filename := filepath.Join(path, fileInfo.Name())
|
||||||
|
if err = walk(fs, filename, fileInfo, walkFn); err != nil {
|
||||||
|
if !fileInfo.IsDir() || err != filepath.SkipDir {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
39
go.mod
Normal file
39
go.mod
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
module github.com/astaxie/beego
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/Knetic/govaluate v3.0.0+incompatible // indirect
|
||||||
|
github.com/beego/goyaml2 v0.0.0-20130207012346-5545475820dd
|
||||||
|
github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542
|
||||||
|
github.com/belogik/goes v0.0.0-20151229125003-e54d722c3aff
|
||||||
|
github.com/bradfitz/gomemcache v0.0.0-20180710155616-bc664df96737
|
||||||
|
github.com/casbin/casbin v1.7.0
|
||||||
|
github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58
|
||||||
|
github.com/couchbase/go-couchbase v0.0.0-20181122212707-3e9b6e1258bb
|
||||||
|
github.com/couchbase/gomemcached v0.0.0-20181122193126-5125a94a666c // indirect
|
||||||
|
github.com/couchbase/goutils v0.0.0-20180530154633-e865a1461c8a // indirect
|
||||||
|
github.com/cupcake/rdb v0.0.0-20161107195141-43ba34106c76 // indirect
|
||||||
|
github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712 // indirect
|
||||||
|
github.com/elazarl/go-bindata-assetfs v1.0.0
|
||||||
|
github.com/go-redis/redis v6.14.2+incompatible
|
||||||
|
github.com/go-sql-driver/mysql v1.4.1
|
||||||
|
github.com/gogo/protobuf v1.1.1
|
||||||
|
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
|
||||||
|
github.com/gomodule/redigo v2.0.0+incompatible
|
||||||
|
github.com/lib/pq v1.0.0
|
||||||
|
github.com/mattn/go-sqlite3 v1.10.0
|
||||||
|
github.com/pelletier/go-toml v1.2.0 // indirect
|
||||||
|
github.com/pkg/errors v0.8.0 // indirect
|
||||||
|
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726 // indirect
|
||||||
|
github.com/siddontang/ledisdb v0.0.0-20181029004158-becf5f38d373
|
||||||
|
github.com/siddontang/rdb v0.0.0-20150307021120-fc89ed2e418d // indirect
|
||||||
|
github.com/ssdb/gossdb v0.0.0-20180723034631-88f6b59b84ec
|
||||||
|
github.com/syndtr/goleveldb v0.0.0-20181127023241-353a9fca669c // indirect
|
||||||
|
github.com/wendal/errors v0.0.0-20130201093226-f66c77a7882b // indirect
|
||||||
|
golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85
|
||||||
|
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a // indirect
|
||||||
|
gopkg.in/yaml.v2 v2.2.1
|
||||||
|
)
|
||||||
|
|
||||||
|
replace golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85 => github.com/golang/crypto v0.0.0-20181127143415-eb0de9b17e85
|
||||||
|
|
||||||
|
replace gopkg.in/yaml.v2 v2.2.1 => github.com/go-yaml/yaml v0.0.0-20180328195020-5420a8b6744d
|
67
go.sum
Normal file
67
go.sum
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
github.com/Knetic/govaluate v3.0.0+incompatible h1:7o6+MAPhYTCF0+fdvoz1xDedhRb4f6s9Tn1Tt7/WTEg=
|
||||||
|
github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
|
||||||
|
github.com/beego/goyaml2 v0.0.0-20130207012346-5545475820dd h1:jZtX5jh5IOMu0fpOTC3ayh6QGSPJ/KWOv1lgPvbRw1M=
|
||||||
|
github.com/beego/goyaml2 v0.0.0-20130207012346-5545475820dd/go.mod h1:1b+Y/CofkYwXMUU0OhQqGvsY2Bvgr4j6jfT699wyZKQ=
|
||||||
|
github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542 h1:nYXb+3jF6Oq/j8R/y90XrKpreCxIalBWfeyeKymgOPk=
|
||||||
|
github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542/go.mod h1:kSeGC/p1AbBiEp5kat81+DSQrZenVBZXklMLaELspWU=
|
||||||
|
github.com/belogik/goes v0.0.0-20151229125003-e54d722c3aff h1:/kO0p2RTGLB8R5gub7ps0GmYpB2O8LXEoPq8tzFDCUI=
|
||||||
|
github.com/belogik/goes v0.0.0-20151229125003-e54d722c3aff/go.mod h1:PhH1ZhyCzHKt4uAasyx+ljRCgoezetRNf59CUtwUkqY=
|
||||||
|
github.com/bradfitz/gomemcache v0.0.0-20180710155616-bc664df96737 h1:rRISKWyXfVxvoa702s91Zl5oREZTrR3yv+tXrrX7G/g=
|
||||||
|
github.com/bradfitz/gomemcache v0.0.0-20180710155616-bc664df96737/go.mod h1:PmM6Mmwb0LSuEubjR8N7PtNe1KxZLtOUHtbeikc5h60=
|
||||||
|
github.com/casbin/casbin v1.7.0 h1:PuzlE8w0JBg/DhIqnkF1Dewf3z+qmUZMVN07PonvVUQ=
|
||||||
|
github.com/casbin/casbin v1.7.0/go.mod h1:c67qKN6Oum3UF5Q1+BByfFxkwKvhwW57ITjqwtzR1KE=
|
||||||
|
github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58 h1:F1EaeKL/ta07PY/k9Os/UFtwERei2/XzGemhpGnBKNg=
|
||||||
|
github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58/go.mod h1:EOBUe0h4xcZ5GoxqC5SDxFQ8gwyZPKQoEzownBlhI80=
|
||||||
|
github.com/couchbase/go-couchbase v0.0.0-20181122212707-3e9b6e1258bb h1:w3RapLhkA5+km9Z8vUkC6VCaskduJXvXwJg5neKnfDU=
|
||||||
|
github.com/couchbase/go-couchbase v0.0.0-20181122212707-3e9b6e1258bb/go.mod h1:TWI8EKQMs5u5jLKW/tsb9VwauIrMIxQG1r5fMsswK5U=
|
||||||
|
github.com/couchbase/gomemcached v0.0.0-20181122193126-5125a94a666c h1:K4FIibkr4//ziZKOKmt4RL0YImuTjLLBtwElf+F2lSQ=
|
||||||
|
github.com/couchbase/gomemcached v0.0.0-20181122193126-5125a94a666c/go.mod h1:srVSlQLB8iXBVXHgnqemxUXqN6FCvClgCMPCsjBDR7c=
|
||||||
|
github.com/couchbase/goutils v0.0.0-20180530154633-e865a1461c8a h1:Y5XsLCEhtEI8qbD9RP3Qlv5FXdTDHxZM9UPUnMRgBp8=
|
||||||
|
github.com/couchbase/goutils v0.0.0-20180530154633-e865a1461c8a/go.mod h1:BQwMFlJzDjFDG3DJUdU0KORxn88UlsOULuxLExMh3Hs=
|
||||||
|
github.com/cupcake/rdb v0.0.0-20161107195141-43ba34106c76 h1:Lgdd/Qp96Qj8jqLpq2cI1I1X7BJnu06efS+XkhRoLUQ=
|
||||||
|
github.com/cupcake/rdb v0.0.0-20161107195141-43ba34106c76/go.mod h1:vYwsqCOLxGiisLwp9rITslkFNpZD5rz43tf41QFkTWY=
|
||||||
|
github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712 h1:aaQcKT9WumO6JEJcRyTqFVq4XUZiUcKR2/GI31TOcz8=
|
||||||
|
github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
|
||||||
|
github.com/elazarl/go-bindata-assetfs v1.0.0 h1:G/bYguwHIzWq9ZoyUQqrjTmJbbYn3j3CKKpKinvZLFk=
|
||||||
|
github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
|
||||||
|
github.com/go-redis/redis v6.14.2+incompatible h1:UE9pLhzmWf+xHNmZsoccjXosPicuiNaInPgym8nzfg0=
|
||||||
|
github.com/go-redis/redis v6.14.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
|
||||||
|
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
|
||||||
|
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
|
||||||
|
github.com/go-yaml/yaml v0.0.0-20180328195020-5420a8b6744d h1:xy93KVe+KrIIwWDEAfQBdIfsiHJkepbYsDr+VY3g9/o=
|
||||||
|
github.com/go-yaml/yaml v0.0.0-20180328195020-5420a8b6744d/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
github.com/gogo/protobuf v1.1.1 h1:72R+M5VuhED/KujmZVcIquuo8mBgX4oVda//DQb3PXo=
|
||||||
|
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
||||||
|
github.com/golang/crypto v0.0.0-20181127143415-eb0de9b17e85 h1:B7ZbAFz7NOmvpUE5RGtu3u0WIizy5GdvbNpEf4RPnWs=
|
||||||
|
github.com/golang/crypto v0.0.0-20181127143415-eb0de9b17e85/go.mod h1:uZvAcrsnNaCxlh1HorK5dUQHGmEKPh2H/Rl1kehswPo=
|
||||||
|
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w=
|
||||||
|
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||||
|
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
|
||||||
|
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
|
||||||
|
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
|
||||||
|
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||||
|
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
|
||||||
|
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
||||||
|
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
|
||||||
|
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
|
||||||
|
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
|
||||||
|
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726 h1:xT+JlYxNGqyT+XcU8iUrN18JYed2TvG9yN5ULG2jATM=
|
||||||
|
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726/go.mod h1:3yhqj7WBBfRhbBlzyOC3gUxftwsU0u8gqevxwIHQpMw=
|
||||||
|
github.com/siddontang/ledisdb v0.0.0-20181029004158-becf5f38d373 h1:p6IxqQMjab30l4lb9mmkIkkcE1yv6o0SKbPhW5pxqHI=
|
||||||
|
github.com/siddontang/ledisdb v0.0.0-20181029004158-becf5f38d373/go.mod h1:mF1DpOSOUiJRMR+FDqaqu3EBqrybQtrDDszLUZ6oxPg=
|
||||||
|
github.com/siddontang/rdb v0.0.0-20150307021120-fc89ed2e418d h1:NVwnfyR3rENtlz62bcrkXME3INVUa4lcdGt+opvxExs=
|
||||||
|
github.com/siddontang/rdb v0.0.0-20150307021120-fc89ed2e418d/go.mod h1:AMEsy7v5z92TR1JKMkLLoaOQk++LVnOKL3ScbJ8GNGA=
|
||||||
|
github.com/ssdb/gossdb v0.0.0-20180723034631-88f6b59b84ec h1:q6XVwXmKvCRHRqesF3cSv6lNqqHi0QWOvgDlSohg8UA=
|
||||||
|
github.com/ssdb/gossdb v0.0.0-20180723034631-88f6b59b84ec/go.mod h1:QBvMkMya+gXctz3kmljlUCu/yB3GZ6oee+dUozsezQE=
|
||||||
|
github.com/syndtr/goleveldb v0.0.0-20181127023241-353a9fca669c h1:3eGShk3EQf5gJCYW+WzA0TEJQd37HLOmlYF7N0YJwv0=
|
||||||
|
github.com/syndtr/goleveldb v0.0.0-20181127023241-353a9fca669c/go.mod h1:Z4AUp2Km+PwemOoO/VB5AOx9XSsIItzFjoJlOSiYmn0=
|
||||||
|
github.com/wendal/errors v0.0.0-20130201093226-f66c77a7882b h1:0Ve0/CCjiAiyKddUMUn3RwIGlq2iTW4GuVzyoKBYO/8=
|
||||||
|
github.com/wendal/errors v0.0.0-20130201093226-f66c77a7882b/go.mod h1:Q12BUT7DqIlHRmgv3RskH+UCM/4eqVMgI0EMmlSpAXc=
|
||||||
|
golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85 h1:et7+NAX3lLIk5qUCTA9QelBjGE/NkhzYw/mhnr0s7nI=
|
||||||
|
golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||||
|
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a h1:gOpx8G595UYyvj8UK4+OFyY4rx037g3fmfhe5SasG3U=
|
||||||
|
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
|
||||||
|
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
2
hooks.go
2
hooks.go
@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/astaxie/beego/session"
|
"github.com/astaxie/beego/session"
|
||||||
)
|
)
|
||||||
|
|
||||||
//
|
// register MIME type with content type
|
||||||
func registerMime() error {
|
func registerMime() error {
|
||||||
for k, v := range mimemaps {
|
for k, v := range mimemaps {
|
||||||
mime.AddExtensionType(k, v)
|
mime.AddExtensionType(k, v)
|
||||||
|
@ -16,6 +16,8 @@ package httplib
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -161,7 +163,16 @@ func TestWithSetting(t *testing.T) {
|
|||||||
var setting BeegoHTTPSettings
|
var setting BeegoHTTPSettings
|
||||||
setting.EnableCookie = true
|
setting.EnableCookie = true
|
||||||
setting.UserAgent = v
|
setting.UserAgent = v
|
||||||
setting.Transport = nil
|
setting.Transport = &http.Transport{
|
||||||
|
DialContext: (&net.Dialer{
|
||||||
|
Timeout: 30 * time.Second,
|
||||||
|
KeepAlive: 30 * time.Second,
|
||||||
|
DualStack: true,
|
||||||
|
}).DialContext,
|
||||||
|
MaxIdleConns: 50,
|
||||||
|
IdleConnTimeout: 90 * time.Second,
|
||||||
|
ExpectContinueTimeout: 1 * time.Second,
|
||||||
|
}
|
||||||
setting.ReadWriteTimeout = 5 * time.Second
|
setting.ReadWriteTimeout = 5 * time.Second
|
||||||
SetDefaultSetting(setting)
|
SetDefaultSetting(setting)
|
||||||
|
|
||||||
|
44
orm/db.go
44
orm/db.go
@ -762,7 +762,13 @@ func (d *dbBase) UpdateBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con
|
|||||||
}
|
}
|
||||||
|
|
||||||
d.ins.ReplaceMarks(&query)
|
d.ins.ReplaceMarks(&query)
|
||||||
res, err := q.Exec(query, values...)
|
var err error
|
||||||
|
var res sql.Result
|
||||||
|
if qs != nil && qs.forContext {
|
||||||
|
res, err = q.ExecContext(qs.ctx, query, values...)
|
||||||
|
} else {
|
||||||
|
res, err = q.Exec(query, values...)
|
||||||
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return res.RowsAffected()
|
return res.RowsAffected()
|
||||||
}
|
}
|
||||||
@ -851,11 +857,16 @@ func (d *dbBase) DeleteBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con
|
|||||||
for i := range marks {
|
for i := range marks {
|
||||||
marks[i] = "?"
|
marks[i] = "?"
|
||||||
}
|
}
|
||||||
sql := fmt.Sprintf("IN (%s)", strings.Join(marks, ", "))
|
sqlIn := fmt.Sprintf("IN (%s)", strings.Join(marks, ", "))
|
||||||
query = fmt.Sprintf("DELETE FROM %s%s%s WHERE %s%s%s %s", Q, mi.table, Q, Q, mi.fields.pk.column, Q, sql)
|
query = fmt.Sprintf("DELETE FROM %s%s%s WHERE %s%s%s %s", Q, mi.table, Q, Q, mi.fields.pk.column, Q, sqlIn)
|
||||||
|
|
||||||
d.ins.ReplaceMarks(&query)
|
d.ins.ReplaceMarks(&query)
|
||||||
res, err := q.Exec(query, args...)
|
var res sql.Result
|
||||||
|
if qs != nil && qs.forContext {
|
||||||
|
res, err = q.ExecContext(qs.ctx, query, args...)
|
||||||
|
} else {
|
||||||
|
res, err = q.Exec(query, args...)
|
||||||
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
num, err := res.RowsAffected()
|
num, err := res.RowsAffected()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -928,7 +939,7 @@ func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condi
|
|||||||
maps[fi.column] = true
|
maps[fi.column] = true
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
panic(fmt.Errorf("wrong field/column name `%s`", col))
|
return 0, fmt.Errorf("wrong field/column name `%s`", col)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if hasRel {
|
if hasRel {
|
||||||
@ -978,11 +989,18 @@ func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condi
|
|||||||
d.ins.ReplaceMarks(&query)
|
d.ins.ReplaceMarks(&query)
|
||||||
|
|
||||||
var rs *sql.Rows
|
var rs *sql.Rows
|
||||||
r, err := q.Query(query, args...)
|
var err error
|
||||||
if err != nil {
|
if qs != nil && qs.forContext {
|
||||||
return 0, err
|
rs, err = q.QueryContext(qs.ctx, query, args...)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
rs, err = q.Query(query, args...)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
rs = r
|
|
||||||
|
|
||||||
refs := make([]interface{}, colsNum)
|
refs := make([]interface{}, colsNum)
|
||||||
for i := range refs {
|
for i := range refs {
|
||||||
@ -1111,8 +1129,12 @@ func (d *dbBase) Count(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition
|
|||||||
|
|
||||||
d.ins.ReplaceMarks(&query)
|
d.ins.ReplaceMarks(&query)
|
||||||
|
|
||||||
row := q.QueryRow(query, args...)
|
var row *sql.Row
|
||||||
|
if qs != nil && qs.forContext {
|
||||||
|
row = q.QueryRowContext(qs.ctx, query, args...)
|
||||||
|
} else {
|
||||||
|
row = q.QueryRow(query, args...)
|
||||||
|
}
|
||||||
err = row.Scan(&cnt)
|
err = row.Scan(&cnt)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -372,7 +372,13 @@ func (t *dbTables) getCondSQL(cond *Condition, sub bool, tz *time.Location) (whe
|
|||||||
operator = "exact"
|
operator = "exact"
|
||||||
}
|
}
|
||||||
|
|
||||||
operSQL, args := t.base.GenerateOperatorSQL(mi, fi, operator, p.args, tz)
|
var operSQL string
|
||||||
|
var args []interface{}
|
||||||
|
if p.isRaw {
|
||||||
|
operSQL = p.sql
|
||||||
|
} else {
|
||||||
|
operSQL, args = t.base.GenerateOperatorSQL(mi, fi, operator, p.args, tz)
|
||||||
|
}
|
||||||
|
|
||||||
leftCol := fmt.Sprintf("%s.%s%s%s", index, Q, fi.column, Q)
|
leftCol := fmt.Sprintf("%s.%s%s%s", index, Q, fi.column, Q)
|
||||||
t.base.GenerateOperatorLeftCol(fi, operator, &leftCol)
|
t.base.GenerateOperatorLeftCol(fi, operator, &leftCol)
|
||||||
|
@ -109,7 +109,7 @@ func getTableUnique(val reflect.Value) [][]string {
|
|||||||
func getColumnName(ft int, addrField reflect.Value, sf reflect.StructField, col string) string {
|
func getColumnName(ft int, addrField reflect.Value, sf reflect.StructField, col string) string {
|
||||||
column := col
|
column := col
|
||||||
if col == "" {
|
if col == "" {
|
||||||
column = snakeString(sf.Name)
|
column = nameStrategyMap[nameStrategy](sf.Name)
|
||||||
}
|
}
|
||||||
switch ft {
|
switch ft {
|
||||||
case RelForeignKey, RelOneToOne:
|
case RelForeignKey, RelOneToOne:
|
||||||
|
@ -425,7 +425,7 @@ func (o *orm) getRelQs(md interface{}, mi *modelInfo, fi *fieldInfo) *querySet {
|
|||||||
func (o *orm) QueryTable(ptrStructOrTableName interface{}) (qs QuerySeter) {
|
func (o *orm) QueryTable(ptrStructOrTableName interface{}) (qs QuerySeter) {
|
||||||
var name string
|
var name string
|
||||||
if table, ok := ptrStructOrTableName.(string); ok {
|
if table, ok := ptrStructOrTableName.(string); ok {
|
||||||
name = snakeString(table)
|
name = nameStrategyMap[defaultNameStrategy](table)
|
||||||
if mi, ok := modelCache.get(name); ok {
|
if mi, ok := modelCache.get(name); ok {
|
||||||
qs = newQuerySet(o, mi)
|
qs = newQuerySet(o, mi)
|
||||||
}
|
}
|
||||||
@ -548,6 +548,9 @@ func NewOrmWithDB(driverName, aliasName string, db *sql.DB) (Ormer, error) {
|
|||||||
|
|
||||||
al.Name = aliasName
|
al.Name = aliasName
|
||||||
al.DriverName = driverName
|
al.DriverName = driverName
|
||||||
|
al.DB = db
|
||||||
|
|
||||||
|
detectTZ(al)
|
||||||
|
|
||||||
o := new(orm)
|
o := new(orm)
|
||||||
o.alias = al
|
o.alias = al
|
||||||
|
@ -31,6 +31,8 @@ type condValue struct {
|
|||||||
isOr bool
|
isOr bool
|
||||||
isNot bool
|
isNot bool
|
||||||
isCond bool
|
isCond bool
|
||||||
|
isRaw bool
|
||||||
|
sql string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Condition struct.
|
// Condition struct.
|
||||||
@ -45,6 +47,15 @@ func NewCondition() *Condition {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Raw add raw sql to condition
|
||||||
|
func (c Condition) Raw(expr string, sql string) *Condition {
|
||||||
|
if len(sql) == 0 {
|
||||||
|
panic(fmt.Errorf("<Condition.Raw> sql cannot empty"))
|
||||||
|
}
|
||||||
|
c.params = append(c.params, condValue{exprs: strings.Split(expr, ExprSep), sql: sql, isRaw: true})
|
||||||
|
return &c
|
||||||
|
}
|
||||||
|
|
||||||
// And add expression to condition
|
// And add expression to condition
|
||||||
func (c Condition) And(expr string, args ...interface{}) *Condition {
|
func (c Condition) And(expr string, args ...interface{}) *Condition {
|
||||||
if expr == "" || len(args) == 0 {
|
if expr == "" || len(args) == 0 {
|
||||||
|
@ -123,6 +123,13 @@ func (d *dbQueryLog) Prepare(query string) (*sql.Stmt, error) {
|
|||||||
return stmt, err
|
return stmt, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *dbQueryLog) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {
|
||||||
|
a := time.Now()
|
||||||
|
stmt, err := d.db.PrepareContext(ctx, query)
|
||||||
|
debugLogQueies(d.alias, "db.Prepare", query, a, err)
|
||||||
|
return stmt, err
|
||||||
|
}
|
||||||
|
|
||||||
func (d *dbQueryLog) Exec(query string, args ...interface{}) (sql.Result, error) {
|
func (d *dbQueryLog) Exec(query string, args ...interface{}) (sql.Result, error) {
|
||||||
a := time.Now()
|
a := time.Now()
|
||||||
res, err := d.db.Exec(query, args...)
|
res, err := d.db.Exec(query, args...)
|
||||||
@ -130,6 +137,13 @@ func (d *dbQueryLog) Exec(query string, args ...interface{}) (sql.Result, error)
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *dbQueryLog) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
|
||||||
|
a := time.Now()
|
||||||
|
res, err := d.db.ExecContext(ctx, query, args...)
|
||||||
|
debugLogQueies(d.alias, "db.Exec", query, a, err, args...)
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
func (d *dbQueryLog) Query(query string, args ...interface{}) (*sql.Rows, error) {
|
func (d *dbQueryLog) Query(query string, args ...interface{}) (*sql.Rows, error) {
|
||||||
a := time.Now()
|
a := time.Now()
|
||||||
res, err := d.db.Query(query, args...)
|
res, err := d.db.Query(query, args...)
|
||||||
@ -137,6 +151,13 @@ func (d *dbQueryLog) Query(query string, args ...interface{}) (*sql.Rows, error)
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *dbQueryLog) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
|
||||||
|
a := time.Now()
|
||||||
|
res, err := d.db.QueryContext(ctx, query, args...)
|
||||||
|
debugLogQueies(d.alias, "db.Query", query, a, err, args...)
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
func (d *dbQueryLog) QueryRow(query string, args ...interface{}) *sql.Row {
|
func (d *dbQueryLog) QueryRow(query string, args ...interface{}) *sql.Row {
|
||||||
a := time.Now()
|
a := time.Now()
|
||||||
res := d.db.QueryRow(query, args...)
|
res := d.db.QueryRow(query, args...)
|
||||||
@ -144,6 +165,13 @@ func (d *dbQueryLog) QueryRow(query string, args ...interface{}) *sql.Row {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *dbQueryLog) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
|
||||||
|
a := time.Now()
|
||||||
|
res := d.db.QueryRowContext(ctx, query, args...)
|
||||||
|
debugLogQueies(d.alias, "db.QueryRow", query, a, nil, args...)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
func (d *dbQueryLog) Begin() (*sql.Tx, error) {
|
func (d *dbQueryLog) Begin() (*sql.Tx, error) {
|
||||||
a := time.Now()
|
a := time.Now()
|
||||||
tx, err := d.db.(txer).Begin()
|
tx, err := d.db.(txer).Begin()
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
package orm
|
package orm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -55,17 +56,19 @@ func ColValue(opt operator, value interface{}) interface{} {
|
|||||||
|
|
||||||
// real query struct
|
// real query struct
|
||||||
type querySet struct {
|
type querySet struct {
|
||||||
mi *modelInfo
|
mi *modelInfo
|
||||||
cond *Condition
|
cond *Condition
|
||||||
related []string
|
related []string
|
||||||
relDepth int
|
relDepth int
|
||||||
limit int64
|
limit int64
|
||||||
offset int64
|
offset int64
|
||||||
groups []string
|
groups []string
|
||||||
orders []string
|
orders []string
|
||||||
distinct bool
|
distinct bool
|
||||||
forupdate bool
|
forupdate bool
|
||||||
orm *orm
|
orm *orm
|
||||||
|
ctx context.Context
|
||||||
|
forContext bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ QuerySeter = new(querySet)
|
var _ QuerySeter = new(querySet)
|
||||||
@ -79,6 +82,15 @@ func (o querySet) Filter(expr string, args ...interface{}) QuerySeter {
|
|||||||
return &o
|
return &o
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add raw sql to querySeter.
|
||||||
|
func (o querySet) FilterRaw(expr string, sql string) QuerySeter {
|
||||||
|
if o.cond == nil {
|
||||||
|
o.cond = NewCondition()
|
||||||
|
}
|
||||||
|
o.cond = o.cond.Raw(expr, sql)
|
||||||
|
return &o
|
||||||
|
}
|
||||||
|
|
||||||
// add NOT condition to querySeter.
|
// add NOT condition to querySeter.
|
||||||
func (o querySet) Exclude(expr string, args ...interface{}) QuerySeter {
|
func (o querySet) Exclude(expr string, args ...interface{}) QuerySeter {
|
||||||
if o.cond == nil {
|
if o.cond == nil {
|
||||||
@ -198,11 +210,7 @@ func (o *querySet) PrepareInsert() (Inserter, error) {
|
|||||||
// query all data and map to containers.
|
// query all data and map to containers.
|
||||||
// cols means the columns when querying.
|
// cols means the columns when querying.
|
||||||
func (o *querySet) All(container interface{}, cols ...string) (int64, error) {
|
func (o *querySet) All(container interface{}, cols ...string) (int64, error) {
|
||||||
num, err := o.orm.alias.DbBaser.ReadBatch(o.orm.db, o, o.mi, o.cond, container, o.orm.alias.TZ, cols)
|
return o.orm.alias.DbBaser.ReadBatch(o.orm.db, o, o.mi, o.cond, container, o.orm.alias.TZ, cols)
|
||||||
if num == 0 {
|
|
||||||
return 0, ErrNoRows
|
|
||||||
}
|
|
||||||
return num, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// query one row data and map to containers.
|
// query one row data and map to containers.
|
||||||
@ -270,6 +278,13 @@ func (o *querySet) RowsToStruct(ptrStruct interface{}, keyCol, valueCol string)
|
|||||||
panic(ErrNotImplement)
|
panic(ErrNotImplement)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set context to QuerySeter.
|
||||||
|
func (o querySet) WithContext(ctx context.Context) QuerySeter {
|
||||||
|
o.ctx = ctx
|
||||||
|
o.forContext = true
|
||||||
|
return &o
|
||||||
|
}
|
||||||
|
|
||||||
// create new QuerySeter.
|
// create new QuerySeter.
|
||||||
func newQuerySet(orm *orm, mi *modelInfo) QuerySeter {
|
func newQuerySet(orm *orm, mi *modelInfo) QuerySeter {
|
||||||
o := new(querySet)
|
o := new(querySet)
|
||||||
|
@ -358,7 +358,7 @@ func (o *rawSet) QueryRow(containers ...interface{}) error {
|
|||||||
_, tags := parseStructTag(fe.Tag.Get(defaultStructTagName))
|
_, tags := parseStructTag(fe.Tag.Get(defaultStructTagName))
|
||||||
var col string
|
var col string
|
||||||
if col = tags["column"]; col == "" {
|
if col = tags["column"]; col == "" {
|
||||||
col = snakeString(fe.Name)
|
col = nameStrategyMap[nameStrategy](fe.Name)
|
||||||
}
|
}
|
||||||
if v, ok := columnsMp[col]; ok {
|
if v, ok := columnsMp[col]; ok {
|
||||||
value := reflect.ValueOf(v).Elem().Interface()
|
value := reflect.ValueOf(v).Elem().Interface()
|
||||||
@ -509,7 +509,7 @@ func (o *rawSet) QueryRows(containers ...interface{}) (int64, error) {
|
|||||||
_, tags := parseStructTag(fe.Tag.Get(defaultStructTagName))
|
_, tags := parseStructTag(fe.Tag.Get(defaultStructTagName))
|
||||||
var col string
|
var col string
|
||||||
if col = tags["column"]; col == "" {
|
if col = tags["column"]; col == "" {
|
||||||
col = snakeString(fe.Name)
|
col = nameStrategyMap[nameStrategy](fe.Name)
|
||||||
}
|
}
|
||||||
if v, ok := columnsMp[col]; ok {
|
if v, ok := columnsMp[col]; ok {
|
||||||
value := reflect.ValueOf(v).Elem().Interface()
|
value := reflect.ValueOf(v).Elem().Interface()
|
||||||
|
@ -899,6 +899,18 @@ func TestOperators(t *testing.T) {
|
|||||||
num, err = qs.Filter("id__between", []int{2, 3}).Count()
|
num, err = qs.Filter("id__between", []int{2, 3}).Count()
|
||||||
throwFail(t, err)
|
throwFail(t, err)
|
||||||
throwFail(t, AssertIs(num, 2))
|
throwFail(t, AssertIs(num, 2))
|
||||||
|
|
||||||
|
num, err = qs.FilterRaw("user_name", "= 'slene'").Count()
|
||||||
|
throwFail(t, err)
|
||||||
|
throwFail(t, AssertIs(num, 1))
|
||||||
|
|
||||||
|
num, err = qs.FilterRaw("status", "IN (1, 2)").Count()
|
||||||
|
throwFail(t, err)
|
||||||
|
throwFail(t, AssertIs(num, 2))
|
||||||
|
|
||||||
|
num, err = qs.FilterRaw("profile_id", "IN (SELECT id FROM user_profile WHERE age=30)").Count()
|
||||||
|
throwFail(t, err)
|
||||||
|
throwFail(t, AssertIs(num, 1))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetCond(t *testing.T) {
|
func TestSetCond(t *testing.T) {
|
||||||
@ -924,6 +936,11 @@ func TestSetCond(t *testing.T) {
|
|||||||
num, err = qs.SetCond(cond4).Count()
|
num, err = qs.SetCond(cond4).Count()
|
||||||
throwFail(t, err)
|
throwFail(t, err)
|
||||||
throwFail(t, AssertIs(num, 3))
|
throwFail(t, AssertIs(num, 3))
|
||||||
|
|
||||||
|
cond5 := cond.Raw("user_name", "= 'slene'").OrNotCond(cond.And("user_name", "slene"))
|
||||||
|
num, err = qs.SetCond(cond5).Count()
|
||||||
|
throwFail(t, err)
|
||||||
|
throwFail(t, AssertIs(num, 3))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLimit(t *testing.T) {
|
func TestLimit(t *testing.T) {
|
||||||
@ -1011,13 +1028,13 @@ func TestAll(t *testing.T) {
|
|||||||
|
|
||||||
qs = dORM.QueryTable("user")
|
qs = dORM.QueryTable("user")
|
||||||
num, err = qs.Filter("user_name", "nothing").All(&users)
|
num, err = qs.Filter("user_name", "nothing").All(&users)
|
||||||
throwFailNow(t, AssertIs(err, ErrNoRows))
|
throwFailNow(t, err)
|
||||||
throwFailNow(t, AssertIs(num, 0))
|
throwFailNow(t, AssertIs(num, 0))
|
||||||
|
|
||||||
var users3 []*User
|
var users3 []*User
|
||||||
qs = dORM.QueryTable("user")
|
qs = dORM.QueryTable("user")
|
||||||
num, err = qs.Filter("user_name", "nothing").All(&users3)
|
num, err = qs.Filter("user_name", "nothing").All(&users3)
|
||||||
throwFailNow(t, AssertIs(err, ErrNoRows))
|
throwFailNow(t, err)
|
||||||
throwFailNow(t, AssertIs(num, 0))
|
throwFailNow(t, AssertIs(num, 0))
|
||||||
throwFailNow(t, AssertIs(users3 == nil, false))
|
throwFailNow(t, AssertIs(users3 == nil, false))
|
||||||
}
|
}
|
||||||
|
12
orm/types.go
12
orm/types.go
@ -147,6 +147,11 @@ type QuerySeter interface {
|
|||||||
// // time compare
|
// // time compare
|
||||||
// qs.Filter("created", time.Now())
|
// qs.Filter("created", time.Now())
|
||||||
Filter(string, ...interface{}) QuerySeter
|
Filter(string, ...interface{}) QuerySeter
|
||||||
|
// add raw sql to querySeter.
|
||||||
|
// for example:
|
||||||
|
// qs.FilterRaw("user_id IN (SELECT id FROM profile WHERE age>=18)")
|
||||||
|
// //sql-> WHERE user_id IN (SELECT id FROM profile WHERE age>=18)
|
||||||
|
FilterRaw(string, string) QuerySeter
|
||||||
// add NOT condition to querySeter.
|
// add NOT condition to querySeter.
|
||||||
// have the same usage as Filter
|
// have the same usage as Filter
|
||||||
Exclude(string, ...interface{}) QuerySeter
|
Exclude(string, ...interface{}) QuerySeter
|
||||||
@ -390,16 +395,23 @@ type RawSeter interface {
|
|||||||
type stmtQuerier interface {
|
type stmtQuerier interface {
|
||||||
Close() error
|
Close() error
|
||||||
Exec(args ...interface{}) (sql.Result, error)
|
Exec(args ...interface{}) (sql.Result, error)
|
||||||
|
//ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error)
|
||||||
Query(args ...interface{}) (*sql.Rows, error)
|
Query(args ...interface{}) (*sql.Rows, error)
|
||||||
|
//QueryContext(args ...interface{}) (*sql.Rows, error)
|
||||||
QueryRow(args ...interface{}) *sql.Row
|
QueryRow(args ...interface{}) *sql.Row
|
||||||
|
//QueryRowContext(ctx context.Context, args ...interface{}) *sql.Row
|
||||||
}
|
}
|
||||||
|
|
||||||
// db querier
|
// db querier
|
||||||
type dbQuerier interface {
|
type dbQuerier interface {
|
||||||
Prepare(query string) (*sql.Stmt, error)
|
Prepare(query string) (*sql.Stmt, error)
|
||||||
|
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
|
||||||
Exec(query string, args ...interface{}) (sql.Result, error)
|
Exec(query string, args ...interface{}) (sql.Result, error)
|
||||||
|
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
|
||||||
Query(query string, args ...interface{}) (*sql.Rows, error)
|
Query(query string, args ...interface{}) (*sql.Rows, error)
|
||||||
|
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
|
||||||
QueryRow(query string, args ...interface{}) *sql.Row
|
QueryRow(query string, args ...interface{}) *sql.Row
|
||||||
|
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
|
||||||
}
|
}
|
||||||
|
|
||||||
// type DB interface {
|
// type DB interface {
|
||||||
|
43
orm/utils.go
43
orm/utils.go
@ -23,6 +23,18 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type fn func(string) string
|
||||||
|
|
||||||
|
var (
|
||||||
|
nameStrategyMap = map[string]fn{
|
||||||
|
defaultNameStrategy: snakeString,
|
||||||
|
SnakeAcronymNameStrategy: snakeStringWithAcronym,
|
||||||
|
}
|
||||||
|
defaultNameStrategy = "snakeString"
|
||||||
|
SnakeAcronymNameStrategy = "snakeStringWithAcronym"
|
||||||
|
nameStrategy = defaultNameStrategy
|
||||||
|
)
|
||||||
|
|
||||||
// StrTo is the target string
|
// StrTo is the target string
|
||||||
type StrTo string
|
type StrTo string
|
||||||
|
|
||||||
@ -198,7 +210,28 @@ func ToInt64(value interface{}) (d int64) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// snake string, XxYy to xx_yy , XxYY to xx_yy
|
func snakeStringWithAcronym(s string) string {
|
||||||
|
data := make([]byte, 0, len(s)*2)
|
||||||
|
num := len(s)
|
||||||
|
for i := 0; i < num; i++ {
|
||||||
|
d := s[i]
|
||||||
|
before := false
|
||||||
|
after := false
|
||||||
|
if i > 0 {
|
||||||
|
before = s[i-1] >= 'a' && s[i-1] <= 'z'
|
||||||
|
}
|
||||||
|
if i+1 < num {
|
||||||
|
after = s[i+1] >= 'a' && s[i+1] <= 'z'
|
||||||
|
}
|
||||||
|
if i > 0 && d >= 'A' && d <= 'Z' && (before || after) {
|
||||||
|
data = append(data, '_')
|
||||||
|
}
|
||||||
|
data = append(data, d)
|
||||||
|
}
|
||||||
|
return strings.ToLower(string(data[:]))
|
||||||
|
}
|
||||||
|
|
||||||
|
// snake string, XxYy to xx_yy , XxYY to xx_y_y
|
||||||
func snakeString(s string) string {
|
func snakeString(s string) string {
|
||||||
data := make([]byte, 0, len(s)*2)
|
data := make([]byte, 0, len(s)*2)
|
||||||
j := false
|
j := false
|
||||||
@ -216,6 +249,14 @@ func snakeString(s string) string {
|
|||||||
return strings.ToLower(string(data[:]))
|
return strings.ToLower(string(data[:]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetNameStrategy set different name strategy
|
||||||
|
func SetNameStrategy(s string) {
|
||||||
|
if SnakeAcronymNameStrategy != s {
|
||||||
|
nameStrategy = defaultNameStrategy
|
||||||
|
}
|
||||||
|
nameStrategy = s
|
||||||
|
}
|
||||||
|
|
||||||
// camel string, xx_yy to XxYy
|
// camel string, xx_yy to XxYy
|
||||||
func camelString(s string) string {
|
func camelString(s string) string {
|
||||||
data := make([]byte, 0, len(s))
|
data := make([]byte, 0, len(s))
|
||||||
|
@ -34,3 +34,37 @@ func TestCamelString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSnakeString(t *testing.T) {
|
||||||
|
camel := []string{"PicUrl", "HelloWorld", "HelloWorld", "HelLOWord", "PicUrl1", "XyXX"}
|
||||||
|
snake := []string{"pic_url", "hello_world", "hello_world", "hel_l_o_word", "pic_url1", "xy_x_x"}
|
||||||
|
|
||||||
|
answer := make(map[string]string)
|
||||||
|
for i, v := range camel {
|
||||||
|
answer[v] = snake[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range camel {
|
||||||
|
res := snakeString(v)
|
||||||
|
if res != answer[v] {
|
||||||
|
t.Error("Unit Test Fail:", v, res, answer[v])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSnakeStringWithAcronym(t *testing.T) {
|
||||||
|
camel := []string{"ID", "PicURL", "HelloWorld", "HelloWorld", "HelLOWord", "PicUrl1", "XyXX"}
|
||||||
|
snake := []string{"id", "pic_url", "hello_world", "hello_world", "hel_lo_word", "pic_url1", "xy_xx"}
|
||||||
|
|
||||||
|
answer := make(map[string]string)
|
||||||
|
for i, v := range camel {
|
||||||
|
answer[v] = snake[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range camel {
|
||||||
|
res := snakeStringWithAcronym(v)
|
||||||
|
if res != answer[v] {
|
||||||
|
t.Error("Unit Test Fail:", v, res, answer[v])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
203
parser.go
203
parser.go
@ -39,7 +39,7 @@ var globalRouterTemplate = `package routers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
"github.com/astaxie/beego/context/param"
|
"github.com/astaxie/beego/context/param"{{.globalimport}}
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -52,6 +52,22 @@ var (
|
|||||||
commentFilename string
|
commentFilename string
|
||||||
pkgLastupdate map[string]int64
|
pkgLastupdate map[string]int64
|
||||||
genInfoList map[string][]ControllerComments
|
genInfoList map[string][]ControllerComments
|
||||||
|
|
||||||
|
routerHooks = map[string]int{
|
||||||
|
"beego.BeforeStatic": BeforeStatic,
|
||||||
|
"beego.BeforeRouter": BeforeRouter,
|
||||||
|
"beego.BeforeExec": BeforeExec,
|
||||||
|
"beego.AfterExec": AfterExec,
|
||||||
|
"beego.FinishRouter": FinishRouter,
|
||||||
|
}
|
||||||
|
|
||||||
|
routerHooksMapping = map[int]string{
|
||||||
|
BeforeStatic: "beego.BeforeStatic",
|
||||||
|
BeforeRouter: "beego.BeforeRouter",
|
||||||
|
BeforeExec: "beego.BeforeExec",
|
||||||
|
AfterExec: "beego.AfterExec",
|
||||||
|
FinishRouter: "beego.FinishRouter",
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
const commentPrefix = "commentsRouter_"
|
const commentPrefix = "commentsRouter_"
|
||||||
@ -102,6 +118,20 @@ type parsedComment struct {
|
|||||||
routerPath string
|
routerPath string
|
||||||
methods []string
|
methods []string
|
||||||
params map[string]parsedParam
|
params map[string]parsedParam
|
||||||
|
filters []parsedFilter
|
||||||
|
imports []parsedImport
|
||||||
|
}
|
||||||
|
|
||||||
|
type parsedImport struct {
|
||||||
|
importPath string
|
||||||
|
importAlias string
|
||||||
|
}
|
||||||
|
|
||||||
|
type parsedFilter struct {
|
||||||
|
pattern string
|
||||||
|
pos int
|
||||||
|
filter string
|
||||||
|
params []bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type parsedParam struct {
|
type parsedParam struct {
|
||||||
@ -126,6 +156,8 @@ func parserComments(f *ast.FuncDecl, controllerName, pkgpath string) error {
|
|||||||
cc.Router = parsedComment.routerPath
|
cc.Router = parsedComment.routerPath
|
||||||
cc.AllowHTTPMethods = parsedComment.methods
|
cc.AllowHTTPMethods = parsedComment.methods
|
||||||
cc.MethodParams = buildMethodParams(f.Type.Params.List, parsedComment)
|
cc.MethodParams = buildMethodParams(f.Type.Params.List, parsedComment)
|
||||||
|
cc.FilterComments = buildFilters(parsedComment.filters)
|
||||||
|
cc.ImportComments = buildImports(parsedComment.imports)
|
||||||
genInfoList[key] = append(genInfoList[key], cc)
|
genInfoList[key] = append(genInfoList[key], cc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -133,6 +165,48 @@ func parserComments(f *ast.FuncDecl, controllerName, pkgpath string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func buildImports(pis []parsedImport) []*ControllerImportComments {
|
||||||
|
var importComments []*ControllerImportComments
|
||||||
|
|
||||||
|
for _, pi := range pis {
|
||||||
|
importComments = append(importComments, &ControllerImportComments{
|
||||||
|
ImportPath: pi.importPath,
|
||||||
|
ImportAlias: pi.importAlias,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return importComments
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildFilters(pfs []parsedFilter) []*ControllerFilterComments {
|
||||||
|
var filterComments []*ControllerFilterComments
|
||||||
|
|
||||||
|
for _, pf := range pfs {
|
||||||
|
var (
|
||||||
|
returnOnOutput bool
|
||||||
|
resetParams bool
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(pf.params) >= 1 {
|
||||||
|
returnOnOutput = pf.params[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(pf.params) >= 2 {
|
||||||
|
resetParams = pf.params[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
filterComments = append(filterComments, &ControllerFilterComments{
|
||||||
|
Filter: pf.filter,
|
||||||
|
Pattern: pf.pattern,
|
||||||
|
Pos: pf.pos,
|
||||||
|
ReturnOnOutput: returnOnOutput,
|
||||||
|
ResetParams: resetParams,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return filterComments
|
||||||
|
}
|
||||||
|
|
||||||
func buildMethodParams(funcParams []*ast.Field, pc *parsedComment) []*param.MethodParam {
|
func buildMethodParams(funcParams []*ast.Field, pc *parsedComment) []*param.MethodParam {
|
||||||
result := make([]*param.MethodParam, 0, len(funcParams))
|
result := make([]*param.MethodParam, 0, len(funcParams))
|
||||||
for _, fparam := range funcParams {
|
for _, fparam := range funcParams {
|
||||||
@ -181,6 +255,8 @@ var routeRegex = regexp.MustCompile(`@router\s+(\S+)(?:\s+\[(\S+)\])?`)
|
|||||||
func parseComment(lines []*ast.Comment) (pcs []*parsedComment, err error) {
|
func parseComment(lines []*ast.Comment) (pcs []*parsedComment, err error) {
|
||||||
pcs = []*parsedComment{}
|
pcs = []*parsedComment{}
|
||||||
params := map[string]parsedParam{}
|
params := map[string]parsedParam{}
|
||||||
|
filters := []parsedFilter{}
|
||||||
|
imports := []parsedImport{}
|
||||||
|
|
||||||
for _, c := range lines {
|
for _, c := range lines {
|
||||||
t := strings.TrimSpace(strings.TrimLeft(c.Text, "//"))
|
t := strings.TrimSpace(strings.TrimLeft(c.Text, "//"))
|
||||||
@ -209,9 +285,69 @@ func parseComment(lines []*ast.Comment) (pcs []*parsedComment, err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, c := range lines {
|
||||||
|
t := strings.TrimSpace(strings.TrimLeft(c.Text, "//"))
|
||||||
|
if strings.HasPrefix(t, "@Import") {
|
||||||
|
iv := getparams(strings.TrimSpace(strings.TrimLeft(t, "@Import")))
|
||||||
|
if len(iv) == 0 || len(iv) > 2 {
|
||||||
|
logs.Error("Invalid @Import format. Only accepts 1 or 2 parameters")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
p := parsedImport{}
|
||||||
|
p.importPath = iv[0]
|
||||||
|
|
||||||
|
if len(iv) == 2 {
|
||||||
|
p.importAlias = iv[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
imports = append(imports, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
filterLoop:
|
||||||
|
for _, c := range lines {
|
||||||
|
t := strings.TrimSpace(strings.TrimLeft(c.Text, "//"))
|
||||||
|
if strings.HasPrefix(t, "@Filter") {
|
||||||
|
fv := getparams(strings.TrimSpace(strings.TrimLeft(t, "@Filter")))
|
||||||
|
if len(fv) < 3 {
|
||||||
|
logs.Error("Invalid @Filter format. Needs at least 3 parameters")
|
||||||
|
continue filterLoop
|
||||||
|
}
|
||||||
|
|
||||||
|
p := parsedFilter{}
|
||||||
|
p.pattern = fv[0]
|
||||||
|
posName := fv[1]
|
||||||
|
if pos, exists := routerHooks[posName]; exists {
|
||||||
|
p.pos = pos
|
||||||
|
} else {
|
||||||
|
logs.Error("Invalid @Filter pos: ", posName)
|
||||||
|
continue filterLoop
|
||||||
|
}
|
||||||
|
|
||||||
|
p.filter = fv[2]
|
||||||
|
fvParams := fv[3:]
|
||||||
|
for _, fvParam := range fvParams {
|
||||||
|
switch fvParam {
|
||||||
|
case "true":
|
||||||
|
p.params = append(p.params, true)
|
||||||
|
case "false":
|
||||||
|
p.params = append(p.params, false)
|
||||||
|
default:
|
||||||
|
logs.Error("Invalid @Filter param: ", fvParam)
|
||||||
|
continue filterLoop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
filters = append(filters, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, c := range lines {
|
for _, c := range lines {
|
||||||
var pc = &parsedComment{}
|
var pc = &parsedComment{}
|
||||||
pc.params = params
|
pc.params = params
|
||||||
|
pc.filters = filters
|
||||||
|
pc.imports = imports
|
||||||
|
|
||||||
t := strings.TrimSpace(strings.TrimLeft(c.Text, "//"))
|
t := strings.TrimSpace(strings.TrimLeft(c.Text, "//"))
|
||||||
if strings.HasPrefix(t, "@router") {
|
if strings.HasPrefix(t, "@router") {
|
||||||
@ -276,8 +412,9 @@ func genRouterCode(pkgRealpath string) {
|
|||||||
os.Mkdir(getRouterDir(pkgRealpath), 0755)
|
os.Mkdir(getRouterDir(pkgRealpath), 0755)
|
||||||
logs.Info("generate router from comments")
|
logs.Info("generate router from comments")
|
||||||
var (
|
var (
|
||||||
globalinfo string
|
globalinfo string
|
||||||
sortKey []string
|
globalimport string
|
||||||
|
sortKey []string
|
||||||
)
|
)
|
||||||
for k := range genInfoList {
|
for k := range genInfoList {
|
||||||
sortKey = append(sortKey, k)
|
sortKey = append(sortKey, k)
|
||||||
@ -295,6 +432,7 @@ func genRouterCode(pkgRealpath string) {
|
|||||||
}
|
}
|
||||||
allmethod = strings.TrimRight(allmethod, ",") + "}"
|
allmethod = strings.TrimRight(allmethod, ",") + "}"
|
||||||
}
|
}
|
||||||
|
|
||||||
params := "nil"
|
params := "nil"
|
||||||
if len(c.Params) > 0 {
|
if len(c.Params) > 0 {
|
||||||
params = "[]map[string]string{"
|
params = "[]map[string]string{"
|
||||||
@ -305,6 +443,7 @@ func genRouterCode(pkgRealpath string) {
|
|||||||
}
|
}
|
||||||
params = strings.TrimRight(params, ",") + "}"
|
params = strings.TrimRight(params, ",") + "}"
|
||||||
}
|
}
|
||||||
|
|
||||||
methodParams := "param.Make("
|
methodParams := "param.Make("
|
||||||
if len(c.MethodParams) > 0 {
|
if len(c.MethodParams) > 0 {
|
||||||
lines := make([]string, 0, len(c.MethodParams))
|
lines := make([]string, 0, len(c.MethodParams))
|
||||||
@ -316,24 +455,66 @@ func genRouterCode(pkgRealpath string) {
|
|||||||
",\n "
|
",\n "
|
||||||
}
|
}
|
||||||
methodParams += ")"
|
methodParams += ")"
|
||||||
|
|
||||||
|
imports := ""
|
||||||
|
if len(c.ImportComments) > 0 {
|
||||||
|
for _, i := range c.ImportComments {
|
||||||
|
if i.ImportAlias != "" {
|
||||||
|
imports += fmt.Sprintf(`
|
||||||
|
%s "%s"`, i.ImportAlias, i.ImportPath)
|
||||||
|
} else {
|
||||||
|
imports += fmt.Sprintf(`
|
||||||
|
"%s"`, i.ImportPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
filters := ""
|
||||||
|
if len(c.FilterComments) > 0 {
|
||||||
|
for _, f := range c.FilterComments {
|
||||||
|
filters += fmt.Sprintf(` &beego.ControllerFilter{
|
||||||
|
Pattern: "%s",
|
||||||
|
Pos: %s,
|
||||||
|
Filter: %s,
|
||||||
|
ReturnOnOutput: %v,
|
||||||
|
ResetParams: %v,
|
||||||
|
},`, f.Pattern, routerHooksMapping[f.Pos], f.Filter, f.ReturnOnOutput, f.ResetParams)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if filters == "" {
|
||||||
|
filters = "nil"
|
||||||
|
} else {
|
||||||
|
filters = fmt.Sprintf(`[]*beego.ControllerFilter{
|
||||||
|
%s
|
||||||
|
}`, filters)
|
||||||
|
}
|
||||||
|
|
||||||
|
globalimport = imports
|
||||||
|
|
||||||
globalinfo = globalinfo + `
|
globalinfo = globalinfo + `
|
||||||
beego.GlobalControllerRouter["` + k + `"] = append(beego.GlobalControllerRouter["` + k + `"],
|
beego.GlobalControllerRouter["` + k + `"] = append(beego.GlobalControllerRouter["` + k + `"],
|
||||||
beego.ControllerComments{
|
beego.ControllerComments{
|
||||||
Method: "` + strings.TrimSpace(c.Method) + `",
|
Method: "` + strings.TrimSpace(c.Method) + `",
|
||||||
` + "Router: `" + c.Router + "`" + `,
|
` + "Router: `" + c.Router + "`" + `,
|
||||||
AllowHTTPMethods: ` + allmethod + `,
|
AllowHTTPMethods: ` + allmethod + `,
|
||||||
MethodParams: ` + methodParams + `,
|
MethodParams: ` + methodParams + `,
|
||||||
Params: ` + params + `})
|
Filters: ` + filters + `,
|
||||||
|
Params: ` + params + `})
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if globalinfo != "" {
|
if globalinfo != "" {
|
||||||
f, err := os.Create(filepath.Join(getRouterDir(pkgRealpath), commentFilename))
|
f, err := os.Create(filepath.Join(getRouterDir(pkgRealpath), commentFilename))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
f.WriteString(strings.Replace(globalRouterTemplate, "{{.globalinfo}}", globalinfo, -1))
|
|
||||||
|
content := strings.Replace(globalRouterTemplate, "{{.globalinfo}}", globalinfo, -1)
|
||||||
|
content = strings.Replace(content, "{{.globalimport}}", globalimport, -1)
|
||||||
|
f.WriteString(content)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
25
router.go
25
router.go
@ -43,7 +43,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
routerTypeBeego = iota
|
routerTypeBeego = iota
|
||||||
routerTypeRESTFul
|
routerTypeRESTFul
|
||||||
routerTypeHandler
|
routerTypeHandler
|
||||||
)
|
)
|
||||||
@ -133,14 +133,15 @@ type ControllerRegister struct {
|
|||||||
|
|
||||||
// NewControllerRegister returns a new ControllerRegister.
|
// NewControllerRegister returns a new ControllerRegister.
|
||||||
func NewControllerRegister() *ControllerRegister {
|
func NewControllerRegister() *ControllerRegister {
|
||||||
cr := &ControllerRegister{
|
return &ControllerRegister{
|
||||||
routers: make(map[string]*Tree),
|
routers: make(map[string]*Tree),
|
||||||
policies: make(map[string]*Tree),
|
policies: make(map[string]*Tree),
|
||||||
|
pool: sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
return beecontext.NewContext()
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
cr.pool.New = func() interface{} {
|
|
||||||
return beecontext.NewContext()
|
|
||||||
}
|
|
||||||
return cr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add controller handler and pattern rules to ControllerRegister.
|
// Add controller handler and pattern rules to ControllerRegister.
|
||||||
@ -277,6 +278,10 @@ func (p *ControllerRegister) Include(cList ...ControllerInterface) {
|
|||||||
key := t.PkgPath() + ":" + t.Name()
|
key := t.PkgPath() + ":" + t.Name()
|
||||||
if comm, ok := GlobalControllerRouter[key]; ok {
|
if comm, ok := GlobalControllerRouter[key]; ok {
|
||||||
for _, a := range comm {
|
for _, a := range comm {
|
||||||
|
for _, f := range a.Filters {
|
||||||
|
p.InsertFilter(f.Pattern, f.Pos, f.Filter, f.ReturnOnOutput, f.ResetParams)
|
||||||
|
}
|
||||||
|
|
||||||
p.addWithMethodParams(a.Router, c, a.MethodParams, strings.Join(a.AllowHTTPMethods, ",")+":"+a.Method)
|
p.addWithMethodParams(a.Router, c, a.MethodParams, strings.Join(a.AllowHTTPMethods, ",")+":"+a.Method)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -794,7 +799,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
if !isRunnable {
|
if !isRunnable {
|
||||||
//Invoke the request handler
|
//Invoke the request handler
|
||||||
var execController ControllerInterface
|
var execController ControllerInterface
|
||||||
if routerInfo.initialize != nil {
|
if routerInfo != nil && routerInfo.initialize != nil {
|
||||||
execController = routerInfo.initialize()
|
execController = routerInfo.initialize()
|
||||||
} else {
|
} else {
|
||||||
vc := reflect.New(runRouter)
|
vc := reflect.New(runRouter)
|
||||||
@ -877,7 +882,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
}
|
}
|
||||||
|
|
||||||
Admin:
|
Admin:
|
||||||
//admin module record QPS
|
//admin module record QPS
|
||||||
|
|
||||||
statusCode := context.ResponseWriter.Status
|
statusCode := context.ResponseWriter.Status
|
||||||
if statusCode == 0 {
|
if statusCode == 0 {
|
||||||
@ -886,8 +891,9 @@ Admin:
|
|||||||
|
|
||||||
logAccess(context, &startTime, statusCode)
|
logAccess(context, &startTime, statusCode)
|
||||||
|
|
||||||
|
timeDur := time.Since(startTime)
|
||||||
|
context.ResponseWriter.Elapsed = timeDur
|
||||||
if BConfig.Listen.EnableAdmin {
|
if BConfig.Listen.EnableAdmin {
|
||||||
timeDur := time.Since(startTime)
|
|
||||||
pattern := ""
|
pattern := ""
|
||||||
if routerInfo != nil {
|
if routerInfo != nil {
|
||||||
pattern = routerInfo.pattern
|
pattern = routerInfo.pattern
|
||||||
@ -904,7 +910,6 @@ Admin:
|
|||||||
|
|
||||||
if BConfig.RunMode == DEV && !BConfig.Log.AccessLogs {
|
if BConfig.RunMode == DEV && !BConfig.Log.AccessLogs {
|
||||||
var devInfo string
|
var devInfo string
|
||||||
timeDur := time.Since(startTime)
|
|
||||||
iswin := (runtime.GOOS == "windows")
|
iswin := (runtime.GOOS == "windows")
|
||||||
statusColor := logs.ColorByStatus(iswin, statusCode)
|
statusColor := logs.ColorByStatus(iswin, statusCode)
|
||||||
methodColor := logs.ColorByMethod(iswin, r.Method)
|
methodColor := logs.ColorByMethod(iswin, r.Method)
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -127,6 +128,9 @@ func (fp *FileProvider) SessionInit(maxlifetime int64, savePath string) error {
|
|||||||
// if file is not exist, create it.
|
// if file is not exist, create it.
|
||||||
// the file path is generated from sid string.
|
// the file path is generated from sid string.
|
||||||
func (fp *FileProvider) SessionRead(sid string) (Store, error) {
|
func (fp *FileProvider) SessionRead(sid string) (Store, error) {
|
||||||
|
if strings.ContainsAny(sid, "./") {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
filepder.lock.Lock()
|
filepder.lock.Lock()
|
||||||
defer filepder.lock.Unlock()
|
defer filepder.lock.Unlock()
|
||||||
|
|
||||||
|
@ -96,6 +96,7 @@ type ManagerConfig struct {
|
|||||||
EnableSidInHTTPHeader bool `json:"EnableSidInHTTPHeader"`
|
EnableSidInHTTPHeader bool `json:"EnableSidInHTTPHeader"`
|
||||||
SessionNameInHTTPHeader string `json:"SessionNameInHTTPHeader"`
|
SessionNameInHTTPHeader string `json:"SessionNameInHTTPHeader"`
|
||||||
EnableSidInURLQuery bool `json:"EnableSidInURLQuery"`
|
EnableSidInURLQuery bool `json:"EnableSidInURLQuery"`
|
||||||
|
SessionIDPrefix string `json:"sessionIDPrefix"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Manager contains Provider and its configuration.
|
// Manager contains Provider and its configuration.
|
||||||
@ -153,6 +154,11 @@ func NewManager(provideName string, cf *ManagerConfig) (*Manager, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetProvider return current manager's provider
|
||||||
|
func (manager *Manager) GetProvider() Provider {
|
||||||
|
return manager.provider
|
||||||
|
}
|
||||||
|
|
||||||
// getSid retrieves session identifier from HTTP Request.
|
// getSid retrieves session identifier from HTTP Request.
|
||||||
// First try to retrieve id by reading from cookie, session cookie name is configurable,
|
// First try to retrieve id by reading from cookie, session cookie name is configurable,
|
||||||
// if not exist, then retrieve id from querying parameters.
|
// if not exist, then retrieve id from querying parameters.
|
||||||
@ -331,7 +337,7 @@ func (manager *Manager) sessionID() (string, error) {
|
|||||||
if n != len(b) || err != nil {
|
if n != len(b) || err != nil {
|
||||||
return "", fmt.Errorf("Could not successfully read from the system CSPRNG")
|
return "", fmt.Errorf("Could not successfully read from the system CSPRNG")
|
||||||
}
|
}
|
||||||
return hex.EncodeToString(b), nil
|
return manager.config.SessionIDPrefix + hex.EncodeToString(b), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set cookie with https.
|
// Set cookie with https.
|
||||||
|
@ -178,7 +178,7 @@ func searchFile(ctx *context.Context) (string, os.FileInfo, error) {
|
|||||||
if !strings.Contains(requestPath, prefix) {
|
if !strings.Contains(requestPath, prefix) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if len(requestPath) > len(prefix) && requestPath[len(prefix)] != '/' {
|
if prefix != "/" && len(requestPath) > len(prefix) && requestPath[len(prefix)] != '/' {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
filePath := path.Join(staticDir, requestPath[len(prefix):])
|
filePath := path.Join(staticDir, requestPath[len(prefix):])
|
||||||
|
68
template.go
68
template.go
@ -20,6 +20,7 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
@ -40,6 +41,7 @@ var (
|
|||||||
beeTemplateExt = []string{"tpl", "html"}
|
beeTemplateExt = []string{"tpl", "html"}
|
||||||
// beeTemplatePreprocessors stores associations of extension -> preprocessor handler
|
// beeTemplatePreprocessors stores associations of extension -> preprocessor handler
|
||||||
beeTemplateEngines = map[string]templatePreProcessor{}
|
beeTemplateEngines = map[string]templatePreProcessor{}
|
||||||
|
beeTemplateFS = defaultFSFunc
|
||||||
)
|
)
|
||||||
|
|
||||||
// ExecuteTemplate applies the template with name to the specified data object,
|
// ExecuteTemplate applies the template with name to the specified data object,
|
||||||
@ -181,12 +183,17 @@ func lockViewPaths() {
|
|||||||
// BuildTemplate will build all template files in a directory.
|
// BuildTemplate will build all template files in a directory.
|
||||||
// it makes beego can render any template file in view directory.
|
// it makes beego can render any template file in view directory.
|
||||||
func BuildTemplate(dir string, files ...string) error {
|
func BuildTemplate(dir string, files ...string) error {
|
||||||
if _, err := os.Stat(dir); err != nil {
|
var err error
|
||||||
|
fs := beeTemplateFS()
|
||||||
|
f, err := fs.Open(dir)
|
||||||
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return errors.New("dir open err")
|
return errors.New("dir open err")
|
||||||
}
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
beeTemplates, ok := beeViewPathTemplates[dir]
|
beeTemplates, ok := beeViewPathTemplates[dir]
|
||||||
if !ok {
|
if !ok {
|
||||||
panic("Unknown view path: " + dir)
|
panic("Unknown view path: " + dir)
|
||||||
@ -195,11 +202,11 @@ func BuildTemplate(dir string, files ...string) error {
|
|||||||
root: dir,
|
root: dir,
|
||||||
files: make(map[string][]string),
|
files: make(map[string][]string),
|
||||||
}
|
}
|
||||||
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
|
err = Walk(fs, dir, func(path string, f os.FileInfo, err error) error {
|
||||||
return self.visit(path, f, err)
|
return self.visit(path, f, err)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("filepath.Walk() returned %v\n", err)
|
fmt.Printf("Walk() returned %v\n", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
buildAllFiles := len(files) == 0
|
buildAllFiles := len(files) == 0
|
||||||
@ -210,11 +217,11 @@ func BuildTemplate(dir string, files ...string) error {
|
|||||||
ext := filepath.Ext(file)
|
ext := filepath.Ext(file)
|
||||||
var t *template.Template
|
var t *template.Template
|
||||||
if len(ext) == 0 {
|
if len(ext) == 0 {
|
||||||
t, err = getTemplate(self.root, file, v...)
|
t, err = getTemplate(self.root, fs, file, v...)
|
||||||
} else if fn, ok := beeTemplateEngines[ext[1:]]; ok {
|
} else if fn, ok := beeTemplateEngines[ext[1:]]; ok {
|
||||||
t, err = fn(self.root, file, beegoTplFuncMap)
|
t, err = fn(self.root, file, beegoTplFuncMap)
|
||||||
} else {
|
} else {
|
||||||
t, err = getTemplate(self.root, file, v...)
|
t, err = getTemplate(self.root, fs, file, v...)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Error("parse template err:", file, err)
|
logs.Error("parse template err:", file, err)
|
||||||
@ -229,9 +236,10 @@ func BuildTemplate(dir string, files ...string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTplDeep(root, file, parent string, t *template.Template) (*template.Template, [][]string, error) {
|
func getTplDeep(root string, fs http.FileSystem, file string, parent string, t *template.Template) (*template.Template, [][]string, error) {
|
||||||
var fileAbsPath string
|
var fileAbsPath string
|
||||||
var rParent string
|
var rParent string
|
||||||
|
var err error
|
||||||
if filepath.HasPrefix(file, "../") {
|
if filepath.HasPrefix(file, "../") {
|
||||||
rParent = filepath.Join(filepath.Dir(parent), file)
|
rParent = filepath.Join(filepath.Dir(parent), file)
|
||||||
fileAbsPath = filepath.Join(root, filepath.Dir(parent), file)
|
fileAbsPath = filepath.Join(root, filepath.Dir(parent), file)
|
||||||
@ -239,10 +247,12 @@ func getTplDeep(root, file, parent string, t *template.Template) (*template.Temp
|
|||||||
rParent = file
|
rParent = file
|
||||||
fileAbsPath = filepath.Join(root, file)
|
fileAbsPath = filepath.Join(root, file)
|
||||||
}
|
}
|
||||||
if e := utils.FileExists(fileAbsPath); !e {
|
f, err := fs.Open(fileAbsPath)
|
||||||
|
defer f.Close()
|
||||||
|
if err != nil {
|
||||||
panic("can't find template file:" + file)
|
panic("can't find template file:" + file)
|
||||||
}
|
}
|
||||||
data, err := ioutil.ReadFile(fileAbsPath)
|
data, err := ioutil.ReadAll(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, [][]string{}, err
|
return nil, [][]string{}, err
|
||||||
}
|
}
|
||||||
@ -261,7 +271,7 @@ func getTplDeep(root, file, parent string, t *template.Template) (*template.Temp
|
|||||||
if !HasTemplateExt(m[1]) {
|
if !HasTemplateExt(m[1]) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
_, _, err = getTplDeep(root, m[1], rParent, t)
|
_, _, err = getTplDeep(root, fs, m[1], rParent, t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, [][]string{}, err
|
return nil, [][]string{}, err
|
||||||
}
|
}
|
||||||
@ -270,14 +280,14 @@ func getTplDeep(root, file, parent string, t *template.Template) (*template.Temp
|
|||||||
return t, allSub, nil
|
return t, allSub, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTemplate(root, file string, others ...string) (t *template.Template, err error) {
|
func getTemplate(root string, fs http.FileSystem, file string, others ...string) (t *template.Template, err error) {
|
||||||
t = template.New(file).Delims(BConfig.WebConfig.TemplateLeft, BConfig.WebConfig.TemplateRight).Funcs(beegoTplFuncMap)
|
t = template.New(file).Delims(BConfig.WebConfig.TemplateLeft, BConfig.WebConfig.TemplateRight).Funcs(beegoTplFuncMap)
|
||||||
var subMods [][]string
|
var subMods [][]string
|
||||||
t, subMods, err = getTplDeep(root, file, "", t)
|
t, subMods, err = getTplDeep(root, fs, file, "", t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
t, err = _getTemplate(t, root, subMods, others...)
|
t, err = _getTemplate(t, root, fs, subMods, others...)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -285,7 +295,7 @@ func getTemplate(root, file string, others ...string) (t *template.Template, err
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func _getTemplate(t0 *template.Template, root string, subMods [][]string, others ...string) (t *template.Template, err error) {
|
func _getTemplate(t0 *template.Template, root string, fs http.FileSystem, subMods [][]string, others ...string) (t *template.Template, err error) {
|
||||||
t = t0
|
t = t0
|
||||||
for _, m := range subMods {
|
for _, m := range subMods {
|
||||||
if len(m) == 2 {
|
if len(m) == 2 {
|
||||||
@ -297,11 +307,11 @@ func _getTemplate(t0 *template.Template, root string, subMods [][]string, others
|
|||||||
for _, otherFile := range others {
|
for _, otherFile := range others {
|
||||||
if otherFile == m[1] {
|
if otherFile == m[1] {
|
||||||
var subMods1 [][]string
|
var subMods1 [][]string
|
||||||
t, subMods1, err = getTplDeep(root, otherFile, "", t)
|
t, subMods1, err = getTplDeep(root, fs, otherFile, "", t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Trace("template parse file err:", err)
|
logs.Trace("template parse file err:", err)
|
||||||
} else if len(subMods1) > 0 {
|
} else if len(subMods1) > 0 {
|
||||||
t, err = _getTemplate(t, root, subMods1, others...)
|
t, err = _getTemplate(t, root, fs, subMods1, others...)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -310,8 +320,16 @@ func _getTemplate(t0 *template.Template, root string, subMods [][]string, others
|
|||||||
for _, otherFile := range others {
|
for _, otherFile := range others {
|
||||||
var data []byte
|
var data []byte
|
||||||
fileAbsPath := filepath.Join(root, otherFile)
|
fileAbsPath := filepath.Join(root, otherFile)
|
||||||
data, err = ioutil.ReadFile(fileAbsPath)
|
f, err := fs.Open(fileAbsPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
f.Close()
|
||||||
|
logs.Trace("template file parse error, not success open file:", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
data, err = ioutil.ReadAll(f)
|
||||||
|
f.Close()
|
||||||
|
if err != nil {
|
||||||
|
logs.Trace("template file parse error, not success read file:", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
reg := regexp.MustCompile(BConfig.WebConfig.TemplateLeft + "[ ]*define[ ]+\"([^\"]+)\"")
|
reg := regexp.MustCompile(BConfig.WebConfig.TemplateLeft + "[ ]*define[ ]+\"([^\"]+)\"")
|
||||||
@ -319,11 +337,14 @@ func _getTemplate(t0 *template.Template, root string, subMods [][]string, others
|
|||||||
for _, sub := range allSub {
|
for _, sub := range allSub {
|
||||||
if len(sub) == 2 && sub[1] == m[1] {
|
if len(sub) == 2 && sub[1] == m[1] {
|
||||||
var subMods1 [][]string
|
var subMods1 [][]string
|
||||||
t, subMods1, err = getTplDeep(root, otherFile, "", t)
|
t, subMods1, err = getTplDeep(root, fs, otherFile, "", t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Trace("template parse file err:", err)
|
logs.Trace("template parse file err:", err)
|
||||||
} else if len(subMods1) > 0 {
|
} else if len(subMods1) > 0 {
|
||||||
t, err = _getTemplate(t, root, subMods1, others...)
|
t, err = _getTemplate(t, root, fs, subMods1, others...)
|
||||||
|
if err != nil {
|
||||||
|
logs.Trace("template parse file err:", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -335,6 +356,17 @@ func _getTemplate(t0 *template.Template, root string, subMods [][]string, others
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type templateFSFunc func() http.FileSystem
|
||||||
|
|
||||||
|
func defaultFSFunc() http.FileSystem {
|
||||||
|
return FileSystem{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTemplateFSFunc set default filesystem function
|
||||||
|
func SetTemplateFSFunc(fnt templateFSFunc) {
|
||||||
|
beeTemplateFS = fnt
|
||||||
|
}
|
||||||
|
|
||||||
// SetViewsPath sets view directory path in beego application.
|
// SetViewsPath sets view directory path in beego application.
|
||||||
func SetViewsPath(path string) *App {
|
func SetViewsPath(path string) *App {
|
||||||
BConfig.WebConfig.ViewsPath = path
|
BConfig.WebConfig.ViewsPath = path
|
||||||
|
@ -16,6 +16,9 @@ package beego
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"github.com/astaxie/beego/testdata"
|
||||||
|
"github.com/elazarl/go-bindata-assetfs"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
@ -256,3 +259,58 @@ func TestTemplateLayout(t *testing.T) {
|
|||||||
}
|
}
|
||||||
os.RemoveAll(dir)
|
os.RemoveAll(dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TestingFileSystem struct {
|
||||||
|
assetfs *assetfs.AssetFS
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d TestingFileSystem) Open(name string) (http.File, error) {
|
||||||
|
return d.assetfs.Open(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
var outputBinData = `<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>beego welcome template</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
|
||||||
|
<h1>Hello, blocks!</h1>
|
||||||
|
|
||||||
|
|
||||||
|
<h1>Hello, astaxie!</h1>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<h2>Hello</h2>
|
||||||
|
<p> This is SomeVar: val</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`
|
||||||
|
|
||||||
|
func TestFsBinData(t *testing.T) {
|
||||||
|
SetTemplateFSFunc(func() http.FileSystem {
|
||||||
|
return TestingFileSystem{&assetfs.AssetFS{Asset: testdata.Asset, AssetDir: testdata.AssetDir, AssetInfo: testdata.AssetInfo}}
|
||||||
|
})
|
||||||
|
dir := "views"
|
||||||
|
if err := AddViewPath("views"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
beeTemplates := beeViewPathTemplates[dir]
|
||||||
|
if len(beeTemplates) != 3 {
|
||||||
|
t.Fatalf("should be 3 but got %v", len(beeTemplates))
|
||||||
|
}
|
||||||
|
if err := beeTemplates["index.tpl"].ExecuteTemplate(os.Stdout, "index.tpl", map[string]string{"Title": "Hello", "SomeVar": "val"}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
out := bytes.NewBufferString("")
|
||||||
|
if err := beeTemplates["index.tpl"].ExecuteTemplate(out, "index.tpl", map[string]string{"Title": "Hello", "SomeVar": "val"}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if out.String() != outputBinData {
|
||||||
|
t.Log(out.String())
|
||||||
|
t.Fatal("Compare failed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -692,7 +692,7 @@ func ge(arg1, arg2 interface{}) (bool, error) {
|
|||||||
|
|
||||||
// MapGet getting value from map by keys
|
// MapGet getting value from map by keys
|
||||||
// usage:
|
// usage:
|
||||||
// Data["m"] = map[string]interface{} {
|
// Data["m"] = M{
|
||||||
// "a": 1,
|
// "a": 1,
|
||||||
// "1": map[string]float64{
|
// "1": map[string]float64{
|
||||||
// "c": 4,
|
// "c": 4,
|
||||||
|
@ -329,7 +329,7 @@ func TestMapGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// test 2 level map
|
// test 2 level map
|
||||||
m2 := map[string]interface{}{
|
m2 := M{
|
||||||
"1": map[string]float64{
|
"1": map[string]float64{
|
||||||
"2": 3.5,
|
"2": 3.5,
|
||||||
},
|
},
|
||||||
@ -344,11 +344,11 @@ func TestMapGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// test 5 level map
|
// test 5 level map
|
||||||
m5 := map[string]interface{}{
|
m5 := M{
|
||||||
"1": map[string]interface{}{
|
"1": M{
|
||||||
"2": map[string]interface{}{
|
"2": M{
|
||||||
"3": map[string]interface{}{
|
"3": M{
|
||||||
"4": map[string]interface{}{
|
"4": M{
|
||||||
"5": 1.2,
|
"5": 1.2,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
2
testdata/Makefile
vendored
Normal file
2
testdata/Makefile
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
build_view:
|
||||||
|
$(GOPATH)/bin/go-bindata-assetfs -pkg testdata views/...
|
296
testdata/bindata.go
vendored
Normal file
296
testdata/bindata.go
vendored
Normal file
@ -0,0 +1,296 @@
|
|||||||
|
// Code generated by go-bindata.
|
||||||
|
// sources:
|
||||||
|
// views/blocks/block.tpl
|
||||||
|
// views/header.tpl
|
||||||
|
// views/index.tpl
|
||||||
|
// DO NOT EDIT!
|
||||||
|
|
||||||
|
package testdata
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"compress/gzip"
|
||||||
|
"fmt"
|
||||||
|
"github.com/elazarl/go-bindata-assetfs"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func bindataRead(data []byte, name string) ([]byte, error) {
|
||||||
|
gz, err := gzip.NewReader(bytes.NewBuffer(data))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Read %q: %v", name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
_, err = io.Copy(&buf, gz)
|
||||||
|
clErr := gz.Close()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Read %q: %v", name, err)
|
||||||
|
}
|
||||||
|
if clErr != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type asset struct {
|
||||||
|
bytes []byte
|
||||||
|
info os.FileInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
type bindataFileInfo struct {
|
||||||
|
name string
|
||||||
|
size int64
|
||||||
|
mode os.FileMode
|
||||||
|
modTime time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fi bindataFileInfo) Name() string {
|
||||||
|
return fi.name
|
||||||
|
}
|
||||||
|
func (fi bindataFileInfo) Size() int64 {
|
||||||
|
return fi.size
|
||||||
|
}
|
||||||
|
func (fi bindataFileInfo) Mode() os.FileMode {
|
||||||
|
return fi.mode
|
||||||
|
}
|
||||||
|
func (fi bindataFileInfo) ModTime() time.Time {
|
||||||
|
return fi.modTime
|
||||||
|
}
|
||||||
|
func (fi bindataFileInfo) IsDir() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
func (fi bindataFileInfo) Sys() interface{} {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var _viewsBlocksBlockTpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xaa\xae\x4e\x49\x4d\xcb\xcc\x4b\x55\x50\x4a\xca\xc9\x4f\xce\x56\xaa\xad\xe5\xb2\xc9\x30\xb4\xf3\x48\xcd\xc9\xc9\xd7\x51\x00\x8b\x15\x2b\xda\xe8\x67\x18\xda\x71\x55\x57\xa7\xe6\xa5\xd4\xd6\x02\x02\x00\x00\xff\xff\xfd\xa1\x7a\xf6\x32\x00\x00\x00")
|
||||||
|
|
||||||
|
func viewsBlocksBlockTplBytes() ([]byte, error) {
|
||||||
|
return bindataRead(
|
||||||
|
_viewsBlocksBlockTpl,
|
||||||
|
"views/blocks/block.tpl",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func viewsBlocksBlockTpl() (*asset, error) {
|
||||||
|
bytes, err := viewsBlocksBlockTplBytes()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
info := bindataFileInfo{name: "views/blocks/block.tpl", size: 50, mode: os.FileMode(436), modTime: time.Unix(1541431067, 0)}
|
||||||
|
a := &asset{bytes: bytes, info: info}
|
||||||
|
return a, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var _viewsHeaderTpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xaa\xae\x4e\x49\x4d\xcb\xcc\x4b\x55\x50\xca\x48\x4d\x4c\x49\x2d\x52\xaa\xad\xe5\xb2\xc9\x30\xb4\xf3\x48\xcd\xc9\xc9\xd7\x51\x48\x2c\x2e\x49\xac\xc8\x4c\x55\xb4\xd1\xcf\x30\xb4\xe3\xaa\xae\x4e\xcd\x4b\xa9\xad\x05\x04\x00\x00\xff\xff\xe4\x12\x47\x01\x34\x00\x00\x00")
|
||||||
|
|
||||||
|
func viewsHeaderTplBytes() ([]byte, error) {
|
||||||
|
return bindataRead(
|
||||||
|
_viewsHeaderTpl,
|
||||||
|
"views/header.tpl",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func viewsHeaderTpl() (*asset, error) {
|
||||||
|
bytes, err := viewsHeaderTplBytes()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
info := bindataFileInfo{name: "views/header.tpl", size: 52, mode: os.FileMode(436), modTime: time.Unix(1541431067, 0)}
|
||||||
|
a := &asset{bytes: bytes, info: info}
|
||||||
|
return a, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var _viewsIndexTpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x64\x8f\xbd\x8a\xc3\x30\x10\x84\x6b\xeb\x29\xe6\xfc\x00\x16\xb8\x3c\x16\x35\x77\xa9\x13\x88\x09\xa4\xf4\xcf\x12\x99\x48\x48\xd8\x82\x10\x84\xde\x3d\xc8\x8a\x8b\x90\x6a\xa4\xd9\x6f\xd8\x59\xfa\xf9\x3f\xfe\x75\xd7\xd3\x01\x3a\x58\xa3\x04\x15\x01\x48\x73\x3f\xe5\x07\x40\x61\x0e\x86\xd5\xc0\x7c\x73\x78\xb0\x19\x9d\x65\x04\xb6\xde\xf4\x81\x49\x96\x69\x8e\xc8\x3d\x43\x83\x9b\x9e\x4a\x88\x2a\xc6\x9d\x43\x3d\x18\x37\xde\xeb\x94\x3e\xdd\x1c\xe1\xe5\xcb\xde\xe0\x55\x6e\xd2\x04\x6f\x32\x20\x2a\xd2\xad\x8a\x11\x4d\x97\x57\x22\x25\x92\xba\x55\xa2\x22\xaf\xd0\xe9\x79\xc5\xbc\xe2\xec\x2c\x5f\xfa\xe5\x17\x99\x7b\x7f\x36\xd2\x97\x8a\xa5\x19\xc9\x72\xe7\x2b\x00\x00\xff\xff\xb2\x39\xca\x9f\xff\x00\x00\x00")
|
||||||
|
|
||||||
|
func viewsIndexTplBytes() ([]byte, error) {
|
||||||
|
return bindataRead(
|
||||||
|
_viewsIndexTpl,
|
||||||
|
"views/index.tpl",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func viewsIndexTpl() (*asset, error) {
|
||||||
|
bytes, err := viewsIndexTplBytes()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
info := bindataFileInfo{name: "views/index.tpl", size: 255, mode: os.FileMode(436), modTime: time.Unix(1541434906, 0)}
|
||||||
|
a := &asset{bytes: bytes, info: info}
|
||||||
|
return a, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Asset loads and returns the asset for the given name.
|
||||||
|
// It returns an error if the asset could not be found or
|
||||||
|
// could not be loaded.
|
||||||
|
func Asset(name string) ([]byte, error) {
|
||||||
|
cannonicalName := strings.Replace(name, "\\", "/", -1)
|
||||||
|
if f, ok := _bindata[cannonicalName]; ok {
|
||||||
|
a, err := f()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err)
|
||||||
|
}
|
||||||
|
return a.bytes, nil
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("Asset %s not found", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustAsset is like Asset but panics when Asset would return an error.
|
||||||
|
// It simplifies safe initialization of global variables.
|
||||||
|
func MustAsset(name string) []byte {
|
||||||
|
a, err := Asset(name)
|
||||||
|
if err != nil {
|
||||||
|
panic("asset: Asset(" + name + "): " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
// AssetInfo loads and returns the asset info for the given name.
|
||||||
|
// It returns an error if the asset could not be found or
|
||||||
|
// could not be loaded.
|
||||||
|
func AssetInfo(name string) (os.FileInfo, error) {
|
||||||
|
cannonicalName := strings.Replace(name, "\\", "/", -1)
|
||||||
|
if f, ok := _bindata[cannonicalName]; ok {
|
||||||
|
a, err := f()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("AssetInfo %s can't read by error: %v", name, err)
|
||||||
|
}
|
||||||
|
return a.info, nil
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("AssetInfo %s not found", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AssetNames returns the names of the assets.
|
||||||
|
func AssetNames() []string {
|
||||||
|
names := make([]string, 0, len(_bindata))
|
||||||
|
for name := range _bindata {
|
||||||
|
names = append(names, name)
|
||||||
|
}
|
||||||
|
return names
|
||||||
|
}
|
||||||
|
|
||||||
|
// _bindata is a table, holding each asset generator, mapped to its name.
|
||||||
|
var _bindata = map[string]func() (*asset, error){
|
||||||
|
"views/blocks/block.tpl": viewsBlocksBlockTpl,
|
||||||
|
"views/header.tpl": viewsHeaderTpl,
|
||||||
|
"views/index.tpl": viewsIndexTpl,
|
||||||
|
}
|
||||||
|
|
||||||
|
// AssetDir returns the file names below a certain
|
||||||
|
// directory embedded in the file by go-bindata.
|
||||||
|
// For example if you run go-bindata on data/... and data contains the
|
||||||
|
// following hierarchy:
|
||||||
|
// data/
|
||||||
|
// foo.txt
|
||||||
|
// img/
|
||||||
|
// a.png
|
||||||
|
// b.png
|
||||||
|
// then AssetDir("data") would return []string{"foo.txt", "img"}
|
||||||
|
// AssetDir("data/img") would return []string{"a.png", "b.png"}
|
||||||
|
// AssetDir("foo.txt") and AssetDir("notexist") would return an error
|
||||||
|
// AssetDir("") will return []string{"data"}.
|
||||||
|
func AssetDir(name string) ([]string, error) {
|
||||||
|
node := _bintree
|
||||||
|
if len(name) != 0 {
|
||||||
|
cannonicalName := strings.Replace(name, "\\", "/", -1)
|
||||||
|
pathList := strings.Split(cannonicalName, "/")
|
||||||
|
for _, p := range pathList {
|
||||||
|
node = node.Children[p]
|
||||||
|
if node == nil {
|
||||||
|
return nil, fmt.Errorf("Asset %s not found", name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if node.Func != nil {
|
||||||
|
return nil, fmt.Errorf("Asset %s not found", name)
|
||||||
|
}
|
||||||
|
rv := make([]string, 0, len(node.Children))
|
||||||
|
for childName := range node.Children {
|
||||||
|
rv = append(rv, childName)
|
||||||
|
}
|
||||||
|
return rv, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type bintree struct {
|
||||||
|
Func func() (*asset, error)
|
||||||
|
Children map[string]*bintree
|
||||||
|
}
|
||||||
|
|
||||||
|
var _bintree = &bintree{nil, map[string]*bintree{
|
||||||
|
"views": &bintree{nil, map[string]*bintree{
|
||||||
|
"blocks": &bintree{nil, map[string]*bintree{
|
||||||
|
"block.tpl": &bintree{viewsBlocksBlockTpl, map[string]*bintree{}},
|
||||||
|
}},
|
||||||
|
"header.tpl": &bintree{viewsHeaderTpl, map[string]*bintree{}},
|
||||||
|
"index.tpl": &bintree{viewsIndexTpl, map[string]*bintree{}},
|
||||||
|
}},
|
||||||
|
}}
|
||||||
|
|
||||||
|
// RestoreAsset restores an asset under the given directory
|
||||||
|
func RestoreAsset(dir, name string) error {
|
||||||
|
data, err := Asset(name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
info, err := AssetInfo(name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = os.MkdirAll(_filePath(dir, filepath.Dir(name)), os.FileMode(0755))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RestoreAssets restores an asset under the given directory recursively
|
||||||
|
func RestoreAssets(dir, name string) error {
|
||||||
|
children, err := AssetDir(name)
|
||||||
|
// File
|
||||||
|
if err != nil {
|
||||||
|
return RestoreAsset(dir, name)
|
||||||
|
}
|
||||||
|
// Dir
|
||||||
|
for _, child := range children {
|
||||||
|
err = RestoreAssets(dir, filepath.Join(name, child))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func _filePath(dir, name string) string {
|
||||||
|
cannonicalName := strings.Replace(name, "\\", "/", -1)
|
||||||
|
return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func assetFS() *assetfs.AssetFS {
|
||||||
|
assetInfo := func(path string) (os.FileInfo, error) {
|
||||||
|
return os.Stat(path)
|
||||||
|
}
|
||||||
|
for k := range _bintree.Children {
|
||||||
|
return &assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: assetInfo, Prefix: k}
|
||||||
|
}
|
||||||
|
panic("unreachable")
|
||||||
|
}
|
3
testdata/views/blocks/block.tpl
vendored
Normal file
3
testdata/views/blocks/block.tpl
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{{define "block"}}
|
||||||
|
<h1>Hello, blocks!</h1>
|
||||||
|
{{end}}
|
3
testdata/views/header.tpl
vendored
Normal file
3
testdata/views/header.tpl
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{{define "header"}}
|
||||||
|
<h1>Hello, astaxie!</h1>
|
||||||
|
{{end}}
|
15
testdata/views/index.tpl
vendored
Normal file
15
testdata/views/index.tpl
vendored
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>beego welcome template</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
{{template "block"}}
|
||||||
|
{{template "header"}}
|
||||||
|
{{template "blocks/block.tpl"}}
|
||||||
|
|
||||||
|
<h2>{{ .Title }}</h2>
|
||||||
|
<p> This is SomeVar: {{ .SomeVar }}</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -428,6 +428,9 @@ func run() {
|
|||||||
continue
|
continue
|
||||||
case <-changed:
|
case <-changed:
|
||||||
now = time.Now().Local()
|
now = time.Now().Local()
|
||||||
|
for _, t := range AdminTaskList {
|
||||||
|
t.SetNext(now)
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
case <-stop:
|
case <-stop:
|
||||||
return
|
return
|
||||||
@ -446,6 +449,7 @@ func StopTask() {
|
|||||||
|
|
||||||
// AddTask add task with name
|
// AddTask add task with name
|
||||||
func AddTask(taskname string, t Tasker) {
|
func AddTask(taskname string, t Tasker) {
|
||||||
|
t.SetNext(time.Now().Local())
|
||||||
AdminTaskList[taskname] = t
|
AdminTaskList[taskname] = t
|
||||||
if isstart {
|
if isstart {
|
||||||
changed <- true
|
changed <- true
|
||||||
|
@ -26,6 +26,7 @@ func TestNewBeeMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSet(t *testing.T) {
|
func TestSet(t *testing.T) {
|
||||||
|
safeMap = NewBeeMap()
|
||||||
if ok := safeMap.Set("astaxie", 1); !ok {
|
if ok := safeMap.Set("astaxie", 1); !ok {
|
||||||
t.Error("expected", true, "got", false)
|
t.Error("expected", true, "got", false)
|
||||||
}
|
}
|
||||||
|
27
vendor/golang.org/x/net/LICENSE
generated
vendored
27
vendor/golang.org/x/net/LICENSE
generated
vendored
@ -1,27 +0,0 @@
|
|||||||
Copyright (c) 2009 The Go Authors. All rights reserved.
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
|
||||||
modification, are permitted provided that the following conditions are
|
|
||||||
met:
|
|
||||||
|
|
||||||
* Redistributions of source code must retain the above copyright
|
|
||||||
notice, this list of conditions and the following disclaimer.
|
|
||||||
* Redistributions in binary form must reproduce the above
|
|
||||||
copyright notice, this list of conditions and the following disclaimer
|
|
||||||
in the documentation and/or other materials provided with the
|
|
||||||
distribution.
|
|
||||||
* Neither the name of Google Inc. nor the names of its
|
|
||||||
contributors may be used to endorse or promote products derived from
|
|
||||||
this software without specific prior written permission.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
||||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
||||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
||||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
||||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
||||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
||||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
||||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
||||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
||||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
22
vendor/golang.org/x/net/PATENTS
generated
vendored
22
vendor/golang.org/x/net/PATENTS
generated
vendored
@ -1,22 +0,0 @@
|
|||||||
Additional IP Rights Grant (Patents)
|
|
||||||
|
|
||||||
"This implementation" means the copyrightable works distributed by
|
|
||||||
Google as part of the Go project.
|
|
||||||
|
|
||||||
Google hereby grants to You a perpetual, worldwide, non-exclusive,
|
|
||||||
no-charge, royalty-free, irrevocable (except as stated in this section)
|
|
||||||
patent license to make, have made, use, offer to sell, sell, import,
|
|
||||||
transfer and otherwise run, modify and propagate the contents of this
|
|
||||||
implementation of Go, where such license applies only to those patent
|
|
||||||
claims, both currently owned or controlled by Google and acquired in
|
|
||||||
the future, licensable by Google that are necessarily infringed by this
|
|
||||||
implementation of Go. This grant does not include claims that would be
|
|
||||||
infringed only as a consequence of further modification of this
|
|
||||||
implementation. If you or your agent or exclusive licensee institute or
|
|
||||||
order or agree to the institution of patent litigation against any
|
|
||||||
entity (including a cross-claim or counterclaim in a lawsuit) alleging
|
|
||||||
that this implementation of Go or any code incorporated within this
|
|
||||||
implementation of Go constitutes direct or contributory patent
|
|
||||||
infringement, or inducement of patent infringement, then any patent
|
|
||||||
rights granted to you under this License for this implementation of Go
|
|
||||||
shall terminate as of the date such litigation is filed.
|
|
54
vendor/golang.org/x/net/context/context.go
generated
vendored
54
vendor/golang.org/x/net/context/context.go
generated
vendored
@ -1,54 +0,0 @@
|
|||||||
// Copyright 2014 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// Package context defines the Context type, which carries deadlines,
|
|
||||||
// cancelation signals, and other request-scoped values across API boundaries
|
|
||||||
// and between processes.
|
|
||||||
//
|
|
||||||
// Incoming requests to a server should create a Context, and outgoing calls to
|
|
||||||
// servers should accept a Context. The chain of function calls between must
|
|
||||||
// propagate the Context, optionally replacing it with a modified copy created
|
|
||||||
// using WithDeadline, WithTimeout, WithCancel, or WithValue.
|
|
||||||
//
|
|
||||||
// Programs that use Contexts should follow these rules to keep interfaces
|
|
||||||
// consistent across packages and enable static analysis tools to check context
|
|
||||||
// propagation:
|
|
||||||
//
|
|
||||||
// Do not store Contexts inside a struct type; instead, pass a Context
|
|
||||||
// explicitly to each function that needs it. The Context should be the first
|
|
||||||
// parameter, typically named ctx:
|
|
||||||
//
|
|
||||||
// func DoSomething(ctx context.Context, arg Arg) error {
|
|
||||||
// // ... use ctx ...
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// Do not pass a nil Context, even if a function permits it. Pass context.TODO
|
|
||||||
// if you are unsure about which Context to use.
|
|
||||||
//
|
|
||||||
// Use context Values only for request-scoped data that transits processes and
|
|
||||||
// APIs, not for passing optional parameters to functions.
|
|
||||||
//
|
|
||||||
// The same Context may be passed to functions running in different goroutines;
|
|
||||||
// Contexts are safe for simultaneous use by multiple goroutines.
|
|
||||||
//
|
|
||||||
// See http://blog.golang.org/context for example code for a server that uses
|
|
||||||
// Contexts.
|
|
||||||
package context // import "golang.org/x/net/context"
|
|
||||||
|
|
||||||
// Background returns a non-nil, empty Context. It is never canceled, has no
|
|
||||||
// values, and has no deadline. It is typically used by the main function,
|
|
||||||
// initialization, and tests, and as the top-level Context for incoming
|
|
||||||
// requests.
|
|
||||||
func Background() Context {
|
|
||||||
return background
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO returns a non-nil, empty Context. Code should use context.TODO when
|
|
||||||
// it's unclear which Context to use or it is not yet available (because the
|
|
||||||
// surrounding function has not yet been extended to accept a Context
|
|
||||||
// parameter). TODO is recognized by static analysis tools that determine
|
|
||||||
// whether Contexts are propagated correctly in a program.
|
|
||||||
func TODO() Context {
|
|
||||||
return todo
|
|
||||||
}
|
|
72
vendor/golang.org/x/net/context/go17.go
generated
vendored
72
vendor/golang.org/x/net/context/go17.go
generated
vendored
@ -1,72 +0,0 @@
|
|||||||
// Copyright 2016 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// +build go1.7
|
|
||||||
|
|
||||||
package context
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context" // standard library's context, as of Go 1.7
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
todo = context.TODO()
|
|
||||||
background = context.Background()
|
|
||||||
)
|
|
||||||
|
|
||||||
// Canceled is the error returned by Context.Err when the context is canceled.
|
|
||||||
var Canceled = context.Canceled
|
|
||||||
|
|
||||||
// DeadlineExceeded is the error returned by Context.Err when the context's
|
|
||||||
// deadline passes.
|
|
||||||
var DeadlineExceeded = context.DeadlineExceeded
|
|
||||||
|
|
||||||
// WithCancel returns a copy of parent with a new Done channel. The returned
|
|
||||||
// context's Done channel is closed when the returned cancel function is called
|
|
||||||
// or when the parent context's Done channel is closed, whichever happens first.
|
|
||||||
//
|
|
||||||
// Canceling this context releases resources associated with it, so code should
|
|
||||||
// call cancel as soon as the operations running in this Context complete.
|
|
||||||
func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
|
|
||||||
ctx, f := context.WithCancel(parent)
|
|
||||||
return ctx, CancelFunc(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithDeadline returns a copy of the parent context with the deadline adjusted
|
|
||||||
// to be no later than d. If the parent's deadline is already earlier than d,
|
|
||||||
// WithDeadline(parent, d) is semantically equivalent to parent. The returned
|
|
||||||
// context's Done channel is closed when the deadline expires, when the returned
|
|
||||||
// cancel function is called, or when the parent context's Done channel is
|
|
||||||
// closed, whichever happens first.
|
|
||||||
//
|
|
||||||
// Canceling this context releases resources associated with it, so code should
|
|
||||||
// call cancel as soon as the operations running in this Context complete.
|
|
||||||
func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {
|
|
||||||
ctx, f := context.WithDeadline(parent, deadline)
|
|
||||||
return ctx, CancelFunc(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).
|
|
||||||
//
|
|
||||||
// Canceling this context releases resources associated with it, so code should
|
|
||||||
// call cancel as soon as the operations running in this Context complete:
|
|
||||||
//
|
|
||||||
// func slowOperationWithTimeout(ctx context.Context) (Result, error) {
|
|
||||||
// ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
|
|
||||||
// defer cancel() // releases resources if slowOperation completes before timeout elapses
|
|
||||||
// return slowOperation(ctx)
|
|
||||||
// }
|
|
||||||
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
|
|
||||||
return WithDeadline(parent, time.Now().Add(timeout))
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithValue returns a copy of parent in which the value associated with key is
|
|
||||||
// val.
|
|
||||||
//
|
|
||||||
// Use context Values only for request-scoped data that transits processes and
|
|
||||||
// APIs, not for passing optional parameters to functions.
|
|
||||||
func WithValue(parent Context, key interface{}, val interface{}) Context {
|
|
||||||
return context.WithValue(parent, key, val)
|
|
||||||
}
|
|
20
vendor/golang.org/x/net/context/go19.go
generated
vendored
20
vendor/golang.org/x/net/context/go19.go
generated
vendored
@ -1,20 +0,0 @@
|
|||||||
// Copyright 2017 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// +build go1.9
|
|
||||||
|
|
||||||
package context
|
|
||||||
|
|
||||||
import "context" // standard library's context, as of Go 1.7
|
|
||||||
|
|
||||||
// A Context carries a deadline, a cancelation signal, and other values across
|
|
||||||
// API boundaries.
|
|
||||||
//
|
|
||||||
// Context's methods may be called by multiple goroutines simultaneously.
|
|
||||||
type Context = context.Context
|
|
||||||
|
|
||||||
// A CancelFunc tells an operation to abandon its work.
|
|
||||||
// A CancelFunc does not wait for the work to stop.
|
|
||||||
// After the first call, subsequent calls to a CancelFunc do nothing.
|
|
||||||
type CancelFunc = context.CancelFunc
|
|
300
vendor/golang.org/x/net/context/pre_go17.go
generated
vendored
300
vendor/golang.org/x/net/context/pre_go17.go
generated
vendored
@ -1,300 +0,0 @@
|
|||||||
// Copyright 2014 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// +build !go1.7
|
|
||||||
|
|
||||||
package context
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// An emptyCtx is never canceled, has no values, and has no deadline. It is not
|
|
||||||
// struct{}, since vars of this type must have distinct addresses.
|
|
||||||
type emptyCtx int
|
|
||||||
|
|
||||||
func (*emptyCtx) Deadline() (deadline time.Time, ok bool) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*emptyCtx) Done() <-chan struct{} {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*emptyCtx) Err() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*emptyCtx) Value(key interface{}) interface{} {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *emptyCtx) String() string {
|
|
||||||
switch e {
|
|
||||||
case background:
|
|
||||||
return "context.Background"
|
|
||||||
case todo:
|
|
||||||
return "context.TODO"
|
|
||||||
}
|
|
||||||
return "unknown empty Context"
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
background = new(emptyCtx)
|
|
||||||
todo = new(emptyCtx)
|
|
||||||
)
|
|
||||||
|
|
||||||
// Canceled is the error returned by Context.Err when the context is canceled.
|
|
||||||
var Canceled = errors.New("context canceled")
|
|
||||||
|
|
||||||
// DeadlineExceeded is the error returned by Context.Err when the context's
|
|
||||||
// deadline passes.
|
|
||||||
var DeadlineExceeded = errors.New("context deadline exceeded")
|
|
||||||
|
|
||||||
// WithCancel returns a copy of parent with a new Done channel. The returned
|
|
||||||
// context's Done channel is closed when the returned cancel function is called
|
|
||||||
// or when the parent context's Done channel is closed, whichever happens first.
|
|
||||||
//
|
|
||||||
// Canceling this context releases resources associated with it, so code should
|
|
||||||
// call cancel as soon as the operations running in this Context complete.
|
|
||||||
func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
|
|
||||||
c := newCancelCtx(parent)
|
|
||||||
propagateCancel(parent, c)
|
|
||||||
return c, func() { c.cancel(true, Canceled) }
|
|
||||||
}
|
|
||||||
|
|
||||||
// newCancelCtx returns an initialized cancelCtx.
|
|
||||||
func newCancelCtx(parent Context) *cancelCtx {
|
|
||||||
return &cancelCtx{
|
|
||||||
Context: parent,
|
|
||||||
done: make(chan struct{}),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// propagateCancel arranges for child to be canceled when parent is.
|
|
||||||
func propagateCancel(parent Context, child canceler) {
|
|
||||||
if parent.Done() == nil {
|
|
||||||
return // parent is never canceled
|
|
||||||
}
|
|
||||||
if p, ok := parentCancelCtx(parent); ok {
|
|
||||||
p.mu.Lock()
|
|
||||||
if p.err != nil {
|
|
||||||
// parent has already been canceled
|
|
||||||
child.cancel(false, p.err)
|
|
||||||
} else {
|
|
||||||
if p.children == nil {
|
|
||||||
p.children = make(map[canceler]bool)
|
|
||||||
}
|
|
||||||
p.children[child] = true
|
|
||||||
}
|
|
||||||
p.mu.Unlock()
|
|
||||||
} else {
|
|
||||||
go func() {
|
|
||||||
select {
|
|
||||||
case <-parent.Done():
|
|
||||||
child.cancel(false, parent.Err())
|
|
||||||
case <-child.Done():
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// parentCancelCtx follows a chain of parent references until it finds a
|
|
||||||
// *cancelCtx. This function understands how each of the concrete types in this
|
|
||||||
// package represents its parent.
|
|
||||||
func parentCancelCtx(parent Context) (*cancelCtx, bool) {
|
|
||||||
for {
|
|
||||||
switch c := parent.(type) {
|
|
||||||
case *cancelCtx:
|
|
||||||
return c, true
|
|
||||||
case *timerCtx:
|
|
||||||
return c.cancelCtx, true
|
|
||||||
case *valueCtx:
|
|
||||||
parent = c.Context
|
|
||||||
default:
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// removeChild removes a context from its parent.
|
|
||||||
func removeChild(parent Context, child canceler) {
|
|
||||||
p, ok := parentCancelCtx(parent)
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
p.mu.Lock()
|
|
||||||
if p.children != nil {
|
|
||||||
delete(p.children, child)
|
|
||||||
}
|
|
||||||
p.mu.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
// A canceler is a context type that can be canceled directly. The
|
|
||||||
// implementations are *cancelCtx and *timerCtx.
|
|
||||||
type canceler interface {
|
|
||||||
cancel(removeFromParent bool, err error)
|
|
||||||
Done() <-chan struct{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A cancelCtx can be canceled. When canceled, it also cancels any children
|
|
||||||
// that implement canceler.
|
|
||||||
type cancelCtx struct {
|
|
||||||
Context
|
|
||||||
|
|
||||||
done chan struct{} // closed by the first cancel call.
|
|
||||||
|
|
||||||
mu sync.Mutex
|
|
||||||
children map[canceler]bool // set to nil by the first cancel call
|
|
||||||
err error // set to non-nil by the first cancel call
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *cancelCtx) Done() <-chan struct{} {
|
|
||||||
return c.done
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *cancelCtx) Err() error {
|
|
||||||
c.mu.Lock()
|
|
||||||
defer c.mu.Unlock()
|
|
||||||
return c.err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *cancelCtx) String() string {
|
|
||||||
return fmt.Sprintf("%v.WithCancel", c.Context)
|
|
||||||
}
|
|
||||||
|
|
||||||
// cancel closes c.done, cancels each of c's children, and, if
|
|
||||||
// removeFromParent is true, removes c from its parent's children.
|
|
||||||
func (c *cancelCtx) cancel(removeFromParent bool, err error) {
|
|
||||||
if err == nil {
|
|
||||||
panic("context: internal error: missing cancel error")
|
|
||||||
}
|
|
||||||
c.mu.Lock()
|
|
||||||
if c.err != nil {
|
|
||||||
c.mu.Unlock()
|
|
||||||
return // already canceled
|
|
||||||
}
|
|
||||||
c.err = err
|
|
||||||
close(c.done)
|
|
||||||
for child := range c.children {
|
|
||||||
// NOTE: acquiring the child's lock while holding parent's lock.
|
|
||||||
child.cancel(false, err)
|
|
||||||
}
|
|
||||||
c.children = nil
|
|
||||||
c.mu.Unlock()
|
|
||||||
|
|
||||||
if removeFromParent {
|
|
||||||
removeChild(c.Context, c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithDeadline returns a copy of the parent context with the deadline adjusted
|
|
||||||
// to be no later than d. If the parent's deadline is already earlier than d,
|
|
||||||
// WithDeadline(parent, d) is semantically equivalent to parent. The returned
|
|
||||||
// context's Done channel is closed when the deadline expires, when the returned
|
|
||||||
// cancel function is called, or when the parent context's Done channel is
|
|
||||||
// closed, whichever happens first.
|
|
||||||
//
|
|
||||||
// Canceling this context releases resources associated with it, so code should
|
|
||||||
// call cancel as soon as the operations running in this Context complete.
|
|
||||||
func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {
|
|
||||||
if cur, ok := parent.Deadline(); ok && cur.Before(deadline) {
|
|
||||||
// The current deadline is already sooner than the new one.
|
|
||||||
return WithCancel(parent)
|
|
||||||
}
|
|
||||||
c := &timerCtx{
|
|
||||||
cancelCtx: newCancelCtx(parent),
|
|
||||||
deadline: deadline,
|
|
||||||
}
|
|
||||||
propagateCancel(parent, c)
|
|
||||||
d := deadline.Sub(time.Now())
|
|
||||||
if d <= 0 {
|
|
||||||
c.cancel(true, DeadlineExceeded) // deadline has already passed
|
|
||||||
return c, func() { c.cancel(true, Canceled) }
|
|
||||||
}
|
|
||||||
c.mu.Lock()
|
|
||||||
defer c.mu.Unlock()
|
|
||||||
if c.err == nil {
|
|
||||||
c.timer = time.AfterFunc(d, func() {
|
|
||||||
c.cancel(true, DeadlineExceeded)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return c, func() { c.cancel(true, Canceled) }
|
|
||||||
}
|
|
||||||
|
|
||||||
// A timerCtx carries a timer and a deadline. It embeds a cancelCtx to
|
|
||||||
// implement Done and Err. It implements cancel by stopping its timer then
|
|
||||||
// delegating to cancelCtx.cancel.
|
|
||||||
type timerCtx struct {
|
|
||||||
*cancelCtx
|
|
||||||
timer *time.Timer // Under cancelCtx.mu.
|
|
||||||
|
|
||||||
deadline time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *timerCtx) Deadline() (deadline time.Time, ok bool) {
|
|
||||||
return c.deadline, true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *timerCtx) String() string {
|
|
||||||
return fmt.Sprintf("%v.WithDeadline(%s [%s])", c.cancelCtx.Context, c.deadline, c.deadline.Sub(time.Now()))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *timerCtx) cancel(removeFromParent bool, err error) {
|
|
||||||
c.cancelCtx.cancel(false, err)
|
|
||||||
if removeFromParent {
|
|
||||||
// Remove this timerCtx from its parent cancelCtx's children.
|
|
||||||
removeChild(c.cancelCtx.Context, c)
|
|
||||||
}
|
|
||||||
c.mu.Lock()
|
|
||||||
if c.timer != nil {
|
|
||||||
c.timer.Stop()
|
|
||||||
c.timer = nil
|
|
||||||
}
|
|
||||||
c.mu.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).
|
|
||||||
//
|
|
||||||
// Canceling this context releases resources associated with it, so code should
|
|
||||||
// call cancel as soon as the operations running in this Context complete:
|
|
||||||
//
|
|
||||||
// func slowOperationWithTimeout(ctx context.Context) (Result, error) {
|
|
||||||
// ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
|
|
||||||
// defer cancel() // releases resources if slowOperation completes before timeout elapses
|
|
||||||
// return slowOperation(ctx)
|
|
||||||
// }
|
|
||||||
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
|
|
||||||
return WithDeadline(parent, time.Now().Add(timeout))
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithValue returns a copy of parent in which the value associated with key is
|
|
||||||
// val.
|
|
||||||
//
|
|
||||||
// Use context Values only for request-scoped data that transits processes and
|
|
||||||
// APIs, not for passing optional parameters to functions.
|
|
||||||
func WithValue(parent Context, key interface{}, val interface{}) Context {
|
|
||||||
return &valueCtx{parent, key, val}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A valueCtx carries a key-value pair. It implements Value for that key and
|
|
||||||
// delegates all other calls to the embedded Context.
|
|
||||||
type valueCtx struct {
|
|
||||||
Context
|
|
||||||
key, val interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *valueCtx) String() string {
|
|
||||||
return fmt.Sprintf("%v.WithValue(%#v, %#v)", c.Context, c.key, c.val)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *valueCtx) Value(key interface{}) interface{} {
|
|
||||||
if c.key == key {
|
|
||||||
return c.val
|
|
||||||
}
|
|
||||||
return c.Context.Value(key)
|
|
||||||
}
|
|
109
vendor/golang.org/x/net/context/pre_go19.go
generated
vendored
109
vendor/golang.org/x/net/context/pre_go19.go
generated
vendored
@ -1,109 +0,0 @@
|
|||||||
// Copyright 2014 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// +build !go1.9
|
|
||||||
|
|
||||||
package context
|
|
||||||
|
|
||||||
import "time"
|
|
||||||
|
|
||||||
// A Context carries a deadline, a cancelation signal, and other values across
|
|
||||||
// API boundaries.
|
|
||||||
//
|
|
||||||
// Context's methods may be called by multiple goroutines simultaneously.
|
|
||||||
type Context interface {
|
|
||||||
// Deadline returns the time when work done on behalf of this context
|
|
||||||
// should be canceled. Deadline returns ok==false when no deadline is
|
|
||||||
// set. Successive calls to Deadline return the same results.
|
|
||||||
Deadline() (deadline time.Time, ok bool)
|
|
||||||
|
|
||||||
// Done returns a channel that's closed when work done on behalf of this
|
|
||||||
// context should be canceled. Done may return nil if this context can
|
|
||||||
// never be canceled. Successive calls to Done return the same value.
|
|
||||||
//
|
|
||||||
// WithCancel arranges for Done to be closed when cancel is called;
|
|
||||||
// WithDeadline arranges for Done to be closed when the deadline
|
|
||||||
// expires; WithTimeout arranges for Done to be closed when the timeout
|
|
||||||
// elapses.
|
|
||||||
//
|
|
||||||
// Done is provided for use in select statements:
|
|
||||||
//
|
|
||||||
// // Stream generates values with DoSomething and sends them to out
|
|
||||||
// // until DoSomething returns an error or ctx.Done is closed.
|
|
||||||
// func Stream(ctx context.Context, out chan<- Value) error {
|
|
||||||
// for {
|
|
||||||
// v, err := DoSomething(ctx)
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// select {
|
|
||||||
// case <-ctx.Done():
|
|
||||||
// return ctx.Err()
|
|
||||||
// case out <- v:
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// See http://blog.golang.org/pipelines for more examples of how to use
|
|
||||||
// a Done channel for cancelation.
|
|
||||||
Done() <-chan struct{}
|
|
||||||
|
|
||||||
// Err returns a non-nil error value after Done is closed. Err returns
|
|
||||||
// Canceled if the context was canceled or DeadlineExceeded if the
|
|
||||||
// context's deadline passed. No other values for Err are defined.
|
|
||||||
// After Done is closed, successive calls to Err return the same value.
|
|
||||||
Err() error
|
|
||||||
|
|
||||||
// Value returns the value associated with this context for key, or nil
|
|
||||||
// if no value is associated with key. Successive calls to Value with
|
|
||||||
// the same key returns the same result.
|
|
||||||
//
|
|
||||||
// Use context values only for request-scoped data that transits
|
|
||||||
// processes and API boundaries, not for passing optional parameters to
|
|
||||||
// functions.
|
|
||||||
//
|
|
||||||
// A key identifies a specific value in a Context. Functions that wish
|
|
||||||
// to store values in Context typically allocate a key in a global
|
|
||||||
// variable then use that key as the argument to context.WithValue and
|
|
||||||
// Context.Value. A key can be any type that supports equality;
|
|
||||||
// packages should define keys as an unexported type to avoid
|
|
||||||
// collisions.
|
|
||||||
//
|
|
||||||
// Packages that define a Context key should provide type-safe accessors
|
|
||||||
// for the values stores using that key:
|
|
||||||
//
|
|
||||||
// // Package user defines a User type that's stored in Contexts.
|
|
||||||
// package user
|
|
||||||
//
|
|
||||||
// import "golang.org/x/net/context"
|
|
||||||
//
|
|
||||||
// // User is the type of value stored in the Contexts.
|
|
||||||
// type User struct {...}
|
|
||||||
//
|
|
||||||
// // key is an unexported type for keys defined in this package.
|
|
||||||
// // This prevents collisions with keys defined in other packages.
|
|
||||||
// type key int
|
|
||||||
//
|
|
||||||
// // userKey is the key for user.User values in Contexts. It is
|
|
||||||
// // unexported; clients use user.NewContext and user.FromContext
|
|
||||||
// // instead of using this key directly.
|
|
||||||
// var userKey key = 0
|
|
||||||
//
|
|
||||||
// // NewContext returns a new Context that carries value u.
|
|
||||||
// func NewContext(ctx context.Context, u *User) context.Context {
|
|
||||||
// return context.WithValue(ctx, userKey, u)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // FromContext returns the User value stored in ctx, if any.
|
|
||||||
// func FromContext(ctx context.Context) (*User, bool) {
|
|
||||||
// u, ok := ctx.Value(userKey).(*User)
|
|
||||||
// return u, ok
|
|
||||||
// }
|
|
||||||
Value(key interface{}) interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A CancelFunc tells an operation to abandon its work.
|
|
||||||
// A CancelFunc does not wait for the work to stop.
|
|
||||||
// After the first call, subsequent calls to a CancelFunc do nothing.
|
|
||||||
type CancelFunc func()
|
|
202
vendor/google.golang.org/appengine/LICENSE
generated
vendored
202
vendor/google.golang.org/appengine/LICENSE
generated
vendored
@ -1,202 +0,0 @@
|
|||||||
|
|
||||||
Apache License
|
|
||||||
Version 2.0, January 2004
|
|
||||||
http://www.apache.org/licenses/
|
|
||||||
|
|
||||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
||||||
|
|
||||||
1. Definitions.
|
|
||||||
|
|
||||||
"License" shall mean the terms and conditions for use, reproduction,
|
|
||||||
and distribution as defined by Sections 1 through 9 of this document.
|
|
||||||
|
|
||||||
"Licensor" shall mean the copyright owner or entity authorized by
|
|
||||||
the copyright owner that is granting the License.
|
|
||||||
|
|
||||||
"Legal Entity" shall mean the union of the acting entity and all
|
|
||||||
other entities that control, are controlled by, or are under common
|
|
||||||
control with that entity. For the purposes of this definition,
|
|
||||||
"control" means (i) the power, direct or indirect, to cause the
|
|
||||||
direction or management of such entity, whether by contract or
|
|
||||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
||||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
||||||
|
|
||||||
"You" (or "Your") shall mean an individual or Legal Entity
|
|
||||||
exercising permissions granted by this License.
|
|
||||||
|
|
||||||
"Source" form shall mean the preferred form for making modifications,
|
|
||||||
including but not limited to software source code, documentation
|
|
||||||
source, and configuration files.
|
|
||||||
|
|
||||||
"Object" form shall mean any form resulting from mechanical
|
|
||||||
transformation or translation of a Source form, including but
|
|
||||||
not limited to compiled object code, generated documentation,
|
|
||||||
and conversions to other media types.
|
|
||||||
|
|
||||||
"Work" shall mean the work of authorship, whether in Source or
|
|
||||||
Object form, made available under the License, as indicated by a
|
|
||||||
copyright notice that is included in or attached to the work
|
|
||||||
(an example is provided in the Appendix below).
|
|
||||||
|
|
||||||
"Derivative Works" shall mean any work, whether in Source or Object
|
|
||||||
form, that is based on (or derived from) the Work and for which the
|
|
||||||
editorial revisions, annotations, elaborations, or other modifications
|
|
||||||
represent, as a whole, an original work of authorship. For the purposes
|
|
||||||
of this License, Derivative Works shall not include works that remain
|
|
||||||
separable from, or merely link (or bind by name) to the interfaces of,
|
|
||||||
the Work and Derivative Works thereof.
|
|
||||||
|
|
||||||
"Contribution" shall mean any work of authorship, including
|
|
||||||
the original version of the Work and any modifications or additions
|
|
||||||
to that Work or Derivative Works thereof, that is intentionally
|
|
||||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
||||||
or by an individual or Legal Entity authorized to submit on behalf of
|
|
||||||
the copyright owner. For the purposes of this definition, "submitted"
|
|
||||||
means any form of electronic, verbal, or written communication sent
|
|
||||||
to the Licensor or its representatives, including but not limited to
|
|
||||||
communication on electronic mailing lists, source code control systems,
|
|
||||||
and issue tracking systems that are managed by, or on behalf of, the
|
|
||||||
Licensor for the purpose of discussing and improving the Work, but
|
|
||||||
excluding communication that is conspicuously marked or otherwise
|
|
||||||
designated in writing by the copyright owner as "Not a Contribution."
|
|
||||||
|
|
||||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
||||||
on behalf of whom a Contribution has been received by Licensor and
|
|
||||||
subsequently incorporated within the Work.
|
|
||||||
|
|
||||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
||||||
this License, each Contributor hereby grants to You a perpetual,
|
|
||||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
||||||
copyright license to reproduce, prepare Derivative Works of,
|
|
||||||
publicly display, publicly perform, sublicense, and distribute the
|
|
||||||
Work and such Derivative Works in Source or Object form.
|
|
||||||
|
|
||||||
3. Grant of Patent License. Subject to the terms and conditions of
|
|
||||||
this License, each Contributor hereby grants to You a perpetual,
|
|
||||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
||||||
(except as stated in this section) patent license to make, have made,
|
|
||||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
||||||
where such license applies only to those patent claims licensable
|
|
||||||
by such Contributor that are necessarily infringed by their
|
|
||||||
Contribution(s) alone or by combination of their Contribution(s)
|
|
||||||
with the Work to which such Contribution(s) was submitted. If You
|
|
||||||
institute patent litigation against any entity (including a
|
|
||||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
||||||
or a Contribution incorporated within the Work constitutes direct
|
|
||||||
or contributory patent infringement, then any patent licenses
|
|
||||||
granted to You under this License for that Work shall terminate
|
|
||||||
as of the date such litigation is filed.
|
|
||||||
|
|
||||||
4. Redistribution. You may reproduce and distribute copies of the
|
|
||||||
Work or Derivative Works thereof in any medium, with or without
|
|
||||||
modifications, and in Source or Object form, provided that You
|
|
||||||
meet the following conditions:
|
|
||||||
|
|
||||||
(a) You must give any other recipients of the Work or
|
|
||||||
Derivative Works a copy of this License; and
|
|
||||||
|
|
||||||
(b) You must cause any modified files to carry prominent notices
|
|
||||||
stating that You changed the files; and
|
|
||||||
|
|
||||||
(c) You must retain, in the Source form of any Derivative Works
|
|
||||||
that You distribute, all copyright, patent, trademark, and
|
|
||||||
attribution notices from the Source form of the Work,
|
|
||||||
excluding those notices that do not pertain to any part of
|
|
||||||
the Derivative Works; and
|
|
||||||
|
|
||||||
(d) If the Work includes a "NOTICE" text file as part of its
|
|
||||||
distribution, then any Derivative Works that You distribute must
|
|
||||||
include a readable copy of the attribution notices contained
|
|
||||||
within such NOTICE file, excluding those notices that do not
|
|
||||||
pertain to any part of the Derivative Works, in at least one
|
|
||||||
of the following places: within a NOTICE text file distributed
|
|
||||||
as part of the Derivative Works; within the Source form or
|
|
||||||
documentation, if provided along with the Derivative Works; or,
|
|
||||||
within a display generated by the Derivative Works, if and
|
|
||||||
wherever such third-party notices normally appear. The contents
|
|
||||||
of the NOTICE file are for informational purposes only and
|
|
||||||
do not modify the License. You may add Your own attribution
|
|
||||||
notices within Derivative Works that You distribute, alongside
|
|
||||||
or as an addendum to the NOTICE text from the Work, provided
|
|
||||||
that such additional attribution notices cannot be construed
|
|
||||||
as modifying the License.
|
|
||||||
|
|
||||||
You may add Your own copyright statement to Your modifications and
|
|
||||||
may provide additional or different license terms and conditions
|
|
||||||
for use, reproduction, or distribution of Your modifications, or
|
|
||||||
for any such Derivative Works as a whole, provided Your use,
|
|
||||||
reproduction, and distribution of the Work otherwise complies with
|
|
||||||
the conditions stated in this License.
|
|
||||||
|
|
||||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
||||||
any Contribution intentionally submitted for inclusion in the Work
|
|
||||||
by You to the Licensor shall be under the terms and conditions of
|
|
||||||
this License, without any additional terms or conditions.
|
|
||||||
Notwithstanding the above, nothing herein shall supersede or modify
|
|
||||||
the terms of any separate license agreement you may have executed
|
|
||||||
with Licensor regarding such Contributions.
|
|
||||||
|
|
||||||
6. Trademarks. This License does not grant permission to use the trade
|
|
||||||
names, trademarks, service marks, or product names of the Licensor,
|
|
||||||
except as required for reasonable and customary use in describing the
|
|
||||||
origin of the Work and reproducing the content of the NOTICE file.
|
|
||||||
|
|
||||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
||||||
agreed to in writing, Licensor provides the Work (and each
|
|
||||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
||||||
implied, including, without limitation, any warranties or conditions
|
|
||||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
||||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
||||||
appropriateness of using or redistributing the Work and assume any
|
|
||||||
risks associated with Your exercise of permissions under this License.
|
|
||||||
|
|
||||||
8. Limitation of Liability. In no event and under no legal theory,
|
|
||||||
whether in tort (including negligence), contract, or otherwise,
|
|
||||||
unless required by applicable law (such as deliberate and grossly
|
|
||||||
negligent acts) or agreed to in writing, shall any Contributor be
|
|
||||||
liable to You for damages, including any direct, indirect, special,
|
|
||||||
incidental, or consequential damages of any character arising as a
|
|
||||||
result of this License or out of the use or inability to use the
|
|
||||||
Work (including but not limited to damages for loss of goodwill,
|
|
||||||
work stoppage, computer failure or malfunction, or any and all
|
|
||||||
other commercial damages or losses), even if such Contributor
|
|
||||||
has been advised of the possibility of such damages.
|
|
||||||
|
|
||||||
9. Accepting Warranty or Additional Liability. While redistributing
|
|
||||||
the Work or Derivative Works thereof, You may choose to offer,
|
|
||||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
||||||
or other liability obligations and/or rights consistent with this
|
|
||||||
License. However, in accepting such obligations, You may act only
|
|
||||||
on Your own behalf and on Your sole responsibility, not on behalf
|
|
||||||
of any other Contributor, and only if You agree to indemnify,
|
|
||||||
defend, and hold each Contributor harmless for any liability
|
|
||||||
incurred by, or claims asserted against, such Contributor by reason
|
|
||||||
of your accepting any such warranty or additional liability.
|
|
||||||
|
|
||||||
END OF TERMS AND CONDITIONS
|
|
||||||
|
|
||||||
APPENDIX: How to apply the Apache License to your work.
|
|
||||||
|
|
||||||
To apply the Apache License to your work, attach the following
|
|
||||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
||||||
replaced with your own identifying information. (Don't include
|
|
||||||
the brackets!) The text should be enclosed in the appropriate
|
|
||||||
comment syntax for the file format. We also recommend that a
|
|
||||||
file or class name and description of purpose be included on the
|
|
||||||
same "printed page" as the copyright notice for easier
|
|
||||||
identification within third-party archives.
|
|
||||||
|
|
||||||
Copyright [yyyy] [name of copyright owner]
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
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.
|
|
62
vendor/google.golang.org/appengine/cloudsql/cloudsql.go
generated
vendored
62
vendor/google.golang.org/appengine/cloudsql/cloudsql.go
generated
vendored
@ -1,62 +0,0 @@
|
|||||||
// Copyright 2013 Google Inc. All rights reserved.
|
|
||||||
// Use of this source code is governed by the Apache 2.0
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
/*
|
|
||||||
Package cloudsql exposes access to Google Cloud SQL databases.
|
|
||||||
|
|
||||||
This package does not work in App Engine "flexible environment".
|
|
||||||
|
|
||||||
This package is intended for MySQL drivers to make App Engine-specific
|
|
||||||
connections. Applications should use this package through database/sql:
|
|
||||||
Select a pure Go MySQL driver that supports this package, and use sql.Open
|
|
||||||
with protocol "cloudsql" and an address of the Cloud SQL instance.
|
|
||||||
|
|
||||||
A Go MySQL driver that has been tested to work well with Cloud SQL
|
|
||||||
is the go-sql-driver:
|
|
||||||
import "database/sql"
|
|
||||||
import _ "github.com/go-sql-driver/mysql"
|
|
||||||
|
|
||||||
db, err := sql.Open("mysql", "user@cloudsql(project-id:instance-name)/dbname")
|
|
||||||
|
|
||||||
|
|
||||||
Another driver that works well with Cloud SQL is the mymysql driver:
|
|
||||||
import "database/sql"
|
|
||||||
import _ "github.com/ziutek/mymysql/godrv"
|
|
||||||
|
|
||||||
db, err := sql.Open("mymysql", "cloudsql:instance-name*dbname/user/password")
|
|
||||||
|
|
||||||
|
|
||||||
Using either of these drivers, you can perform a standard SQL query.
|
|
||||||
This example assumes there is a table named 'users' with
|
|
||||||
columns 'first_name' and 'last_name':
|
|
||||||
|
|
||||||
rows, err := db.Query("SELECT first_name, last_name FROM users")
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf(ctx, "db.Query: %v", err)
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
var firstName string
|
|
||||||
var lastName string
|
|
||||||
if err := rows.Scan(&firstName, &lastName); err != nil {
|
|
||||||
log.Errorf(ctx, "rows.Scan: %v", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
log.Infof(ctx, "First: %v - Last: %v", firstName, lastName)
|
|
||||||
}
|
|
||||||
if err := rows.Err(); err != nil {
|
|
||||||
log.Errorf(ctx, "Row error: %v", err)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
package cloudsql
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Dial connects to the named Cloud SQL instance.
|
|
||||||
func Dial(instance string) (net.Conn, error) {
|
|
||||||
return connect(instance)
|
|
||||||
}
|
|
17
vendor/google.golang.org/appengine/cloudsql/cloudsql_classic.go
generated
vendored
17
vendor/google.golang.org/appengine/cloudsql/cloudsql_classic.go
generated
vendored
@ -1,17 +0,0 @@
|
|||||||
// Copyright 2013 Google Inc. All rights reserved.
|
|
||||||
// Use of this source code is governed by the Apache 2.0
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// +build appengine
|
|
||||||
|
|
||||||
package cloudsql
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net"
|
|
||||||
|
|
||||||
"appengine/cloudsql"
|
|
||||||
)
|
|
||||||
|
|
||||||
func connect(instance string) (net.Conn, error) {
|
|
||||||
return cloudsql.Dial(instance)
|
|
||||||
}
|
|
16
vendor/google.golang.org/appengine/cloudsql/cloudsql_vm.go
generated
vendored
16
vendor/google.golang.org/appengine/cloudsql/cloudsql_vm.go
generated
vendored
@ -1,16 +0,0 @@
|
|||||||
// Copyright 2013 Google Inc. All rights reserved.
|
|
||||||
// Use of this source code is governed by the Apache 2.0
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// +build !appengine
|
|
||||||
|
|
||||||
package cloudsql
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"net"
|
|
||||||
)
|
|
||||||
|
|
||||||
func connect(instance string) (net.Conn, error) {
|
|
||||||
return nil, errors.New(`cloudsql: not supported in App Engine "flexible environment"`)
|
|
||||||
}
|
|
12
vendor/vendor.json
vendored
12
vendor/vendor.json
vendored
@ -20,18 +20,6 @@
|
|||||||
"revision": "c126467f60eb25f8f27e5a981f32a87e3965053f",
|
"revision": "c126467f60eb25f8f27e5a981f32a87e3965053f",
|
||||||
"revisionTime": "2018-07-23T15:26:11Z"
|
"revisionTime": "2018-07-23T15:26:11Z"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"checksumSHA1": "dr5+PfIRzXeN+l1VG+s0lea9qz8=",
|
|
||||||
"path": "golang.org/x/net/context",
|
|
||||||
"revision": "b60f3a92103dfd93dfcb900ec77c6d0643510868",
|
|
||||||
"revisionTime": "2017-09-18T06:10:02Z"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"checksumSHA1": "LiyXfqOzaeQ8vgYZH3t2hUEdVTw=",
|
|
||||||
"path": "google.golang.org/appengine/cloudsql",
|
|
||||||
"revision": "b1f26356af11148e710935ed1ac8a7f5702c7612",
|
|
||||||
"revisionTime": "2018-05-21T22:34:13Z"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"checksumSHA1": "ZSWoOPUNRr5+3dhkLK3C4cZAQPk=",
|
"checksumSHA1": "ZSWoOPUNRr5+3dhkLK3C4cZAQPk=",
|
||||||
"path": "gopkg.in/yaml.v2",
|
"path": "gopkg.in/yaml.v2",
|
||||||
|
Reference in New Issue
Block a user