1
0
mirror of https://github.com/astaxie/beego.git synced 2025-07-11 18:21:02 +00:00

811 Commits

Author SHA1 Message Date
1aeb3d9051 release 1.6.1 2016-03-11 11:35:24 +08:00
778a5a11ac Merge pull request #1781 from JessonChan/develop
duplicate adapter logger  bug fixed
2016-03-11 11:19:07 +08:00
4801099675 duplicate adapter logger bug fixed 2016-03-11 10:12:17 +08:00
420cd507b2 update output information 2016-03-11 10:07:44 +08:00
22196d7841 add mis function NSHandler 2016-03-11 09:21:13 +08:00
571f9b4b65 Merge pull request #1780 from goodloop/develop
fix static pattern match for leaf
2016-03-11 09:07:53 +08:00
1f0a65f0a2 fix the orm test 2016-03-10 22:21:21 +08:00
90e7d252a7 fix static pattern match for leaf 2016-03-10 22:16:41 +08:00
31f7524dae fix the golint travis 2016-03-10 21:47:50 +08:00
3a12e238cc support oracle 2016-03-10 21:23:13 +08:00
f45b271b96 Merge pull request #1723 from miraclesu/feature/orm_inline_struct
orm: inline struct support
2016-03-10 14:31:44 +08:00
589616b303 Merge pull request #1768 from aolu11/master
fix json extra newline
2016-03-09 21:11:20 +08:00
65b13eddad Merge pull request #1719 from JessonChan/err_ctrler
multiple response.WriteHeader calls
2016-03-09 19:18:09 +08:00
686d2e834e Merge pull request #1765 from saturn4er/fix_layout_rebuild_in_dev
Add layout rebuilding on each request in dev mode
2016-03-09 19:02:46 +08:00
adbae18e8c Fix formatting with gofmt 2016-03-09 10:47:09 +02:00
f21cff0166 some typo fixed 2016-03-09 16:00:52 +08:00
3dd9020249 Merge remote-tracking branch 'remotes/upstream/develop' into err_ctrler 2016-03-09 15:59:13 +08:00
9a2696d216 accept asta's idea see the talk
https://github.com/astaxie/beego/pull/1719
2016-03-09 15:56:18 +08:00
64e0858d44 orm: add inline struct test case 2016-03-08 22:24:38 +08:00
d86ab2ed31 Merge pull request #1721 from JessonChan/log_enhancement
Log enhancement
2016-03-08 21:35:31 +08:00
b2f071395b rename files to mulitfile 2016-03-08 18:44:39 +08:00
54b5120a64 rename files to mulitfile 2016-03-08 18:43:09 +08:00
5e2384e95a fix json extra newline 2016-03-08 17:04:14 +08:00
adb41eb299 Merge pull request #1761 from lcbluestorm/develop
add ssdb cache adapter
2016-03-08 15:47:45 +08:00
bd04be4470 move time to the top 2016-03-08 14:44:37 +08:00
ef59a0ed63 fix travis.yml 2016-03-08 14:03:33 +08:00
14be252d2a fix travis.yml 2016-03-08 14:01:33 +08:00
5698b5dc92 Merge pull request #1709 from mlgd/develop
Fix cookies in accordance with the "net / http" and Flash usage
2016-03-08 13:53:43 +08:00
0a0fc351e7 fix ssdb_test 2016-03-08 12:59:19 +08:00
662bea352f modify travis 2016-03-08 12:45:54 +08:00
2f18b9103b Merge pull request #1679 from ysqi/emptybodyfix
fix #1669 and return IO error
2016-03-08 09:48:17 +08:00
2c5ef8ccc8 Add layout rebuilding on each request in dev mode 2016-03-07 23:24:52 +02:00
1ddb1ce2fe add ssdb travis 2016-03-07 15:50:13 +08:00
9ddc2f5474 fix panic err 2016-03-07 15:00:03 +08:00
e29f4b57a3 fix travis.yml 2016-03-07 14:53:36 +08:00
0caadb9b66 rename vars 2016-03-07 14:45:45 +08:00
22e3900403 add .travis.yml 2016-03-07 14:34:40 +08:00
b39830dff3 add .travis.yml 2016-03-07 10:31:56 +08:00
be23c42674 Merge branches 'master' and 'develop' of lcbluestorm.github.com:lcbluestorm/beego into develop 2016-03-07 10:19:55 +08:00
90344a7b8f fix conflicts 2016-03-06 21:25:43 +08:00
920862884d Merge branch 'astaxie/develop' into emptybodyfix
# Conflicts:
#	config.go
2016-03-06 21:19:04 +08:00
48ec7f736e fix GetMulti bug 2016-03-06 14:46:13 +08:00
292d8f2c00 add ssdb cache adapter 2016-03-06 13:17:16 +08:00
f6f34306ee Merge pull request #1740 from ysqi/configer
Fixed #1735 Return nil if config value does not exist or is empty
2016-03-05 22:05:43 +08:00
a40c0dd156 Merge pull request #1750 from JessonChan/staticfile_map_race
static file map race bug fixed
2016-03-05 20:43:00 +08:00
795092bdd2 Merge pull request #1751 from FlamingTree/develop
Update phone regexp
2016-03-05 20:41:52 +08:00
524446c857 Merge pull request #1752 from JessonChan/ab_lock_race
fix template  read-write lock race
2016-03-05 20:40:14 +08:00
f5adec31c6 improve the template reader function 2016-03-04 14:49:16 +08:00
6747c55a81 remove unused cache 2016-03-04 12:01:04 +08:00
1f46c1d231 add template read lock when dev mode 2016-03-04 12:00:43 +08:00
8bd1be8e29 Update validators.go
增加178号段
2016-03-04 11:16:47 +08:00
226e54e0d8 static file map race bug fixed 2016-03-04 10:54:54 +08:00
3379a2b7ed remove file bug fixed
remove file by filename and file suffix
2016-03-04 10:43:57 +08:00
19d921d3f5 Return nil not empty []string{}
Return nil if config value does not exist or is empty
2016-03-03 20:03:23 +08:00
4b99e41880 Merge pull request #1688 from ysqi/configIssue
fixed handle config issue
2016-03-03 09:50:14 +08:00
8ff74e71cb Fixed #1735 Return empty []string
Need return empty []string  if config value is empty.

split `“”` ==> []string{}, Not []string{“”}
2016-03-02 22:44:20 +08:00
2a148473e9 Merge remote-tracking branch 'remotes/upstream/develop' into log_enhancement 2016-03-02 13:34:37 +08:00
b30ce768f8 Merge remote-tracking branch 'remotes/upstream/develop' into err_ctrler 2016-03-02 13:34:00 +08:00
70e63570f5 Merge pull request #1731 from math345/develop
fix bug: session id undecoded when destroy and sesssion memory provider push wrong
2016-03-02 13:26:57 +08:00
36e3160904 add go1.6.0 and remove 1.3.3 2016-03-01 21:41:44 +08:00
ca3c57fbc6 add a line of comment 2016-03-01 17:13:50 +08:00
387dd6ec0e add a line of comment 2016-03-01 17:12:21 +08:00
26cc040f9a daily log name dot fixed 2016-03-01 17:00:24 +08:00
d81a768802 change couchbase to beego/go-couchbase 2016-03-01 16:54:37 +08:00
f0dcaa7f84 remove couchbase dependence 2016-03-01 16:43:56 +08:00
9da4d1d847 Merge branch 'develop' into log_enhancement 2016-03-01 13:59:27 +08:00
a144f117a3 remove comment 2016-03-01 13:39:36 +08:00
477de9a3f3 fix bug: session id undecoded when destroy and sesssion memory provider push wrong 2016-03-01 10:51:47 +08:00
ffbb45e567 Revert "ignore parse include config file error"
This reverts commit 891016a0a2.
2016-02-27 20:18:59 +08:00
67fbafb380 Merge pull request #1680 from ysqi/fix-router-error
fix #1595
2016-02-26 16:17:53 +08:00
6eaa5537f5 Merge pull request #1652 from JessonChan/develop
colorful console and go fmt
2016-02-26 16:10:11 +08:00
62cc987620 Merge pull request #1639 from youngsterxyf/logger-flush-close
try to fix the little bug when calling Close or Flush in async mode
2016-02-26 16:04:29 +08:00
f0a41f978f Merge pull request #1727 from JessonChan/templates_bug_fix
lock the templates map when goroutie update the map
2016-02-26 15:56:47 +08:00
3da28535fe lock the templates map when goroutie update the map 2016-02-25 17:37:28 +08:00
85f55fcb41 orm: inline struct support 2016-02-24 18:46:14 +08:00
8c37e76503 the net/http should set header first,the set http status code and then write the content 2016-02-24 14:14:16 +08:00
76d69b6e51 prevent auto detect of content-type
https://golang.org/src/net/http/server.go#L1031
2016-02-24 10:34:20 +08:00
20301bc212 multiple response.WriteHeader calls 2016-02-24 10:31:44 +08:00
9119f766d2 Fix cookies in accordance with the "net / http" and Flash usage
Fixed issue of Flash cookies that are deleted before being read
Max-age parameter conform to "net/http" Cookie
2016-02-22 13:35:54 +01:00
891016a0a2 ignore parse include config file error 2016-02-14 18:55:42 +08:00
d5f07d65bb panic parse config error 2016-02-14 18:54:40 +08:00
9411063574 fix #1595 2016-02-12 14:45:45 +08:00
23860e6807 go fmt 2016-02-12 11:36:59 +08:00
d35c50a8e0 return write body error 2016-02-12 11:36:25 +08:00
810f6db8d2 fix #1669 write empty body panic error 2016-02-12 11:27:59 +08:00
1f716dda3e add test files and bug fixed 2016-02-03 17:54:58 +08:00
f8c4b3aa4c add files logger to separate different logs 2016-02-03 17:11:53 +08:00
51b1095e73 add files logger 2016-02-03 16:32:59 +08:00
68cc53e92b when rotate by date ,there's no num after log file 2016-02-03 15:43:15 +08:00
6caa3ecd91 when rotate by date ,there's no num after log file 2016-02-03 15:31:59 +08:00
304a5ccea0 comment fix 2016-02-03 15:06:53 +08:00
9806a43783 make more fast 2016-02-03 15:03:37 +08:00
a1cb000701 remove log package 2016-02-03 14:42:38 +08:00
2b23764ee0 Merge pull request #1657 from lei-cao/slack
Added slack
2016-02-03 08:58:24 +08:00
2301633d42 Added slack 2016-02-02 23:34:32 +08:00
2efe7c4c89 merge multi commit 2016-02-02 17:12:47 +08:00
c71ac7431d Merge branch 'develop' into logger-flush-close 2016-02-02 17:11:41 +08:00
360220161b remove useless var , name style fixed 2016-02-02 16:52:53 +08:00
5dec3d127c colorful console only the terminal supports 2016-02-02 16:37:09 +08:00
16b01c362a Merge branch 'develop' of https://github.com/astaxie/beego into develop 2016-02-02 12:45:01 +08:00
441f795a1a Merge pull request #1651 from thanhtranjs/develop
Add GroupBy to QuerySeter
2016-02-02 12:44:49 +08:00
4906b600e3 fix the static url with / problem 2016-02-02 12:43:58 +08:00
bb50383aa9 Add GroupBy to QuerySeter 2016-02-02 11:28:43 +07:00
631b4d36f9 Merge pull request #1647 from youngsterxyf/fix-issue1641
fix issue #1641
2016-02-02 09:23:12 +08:00
da38cbfe41 fix issue #1641 2016-02-01 22:40:34 +08:00
09193213a0 add travis hooks 2016-01-31 23:28:56 +08:00
0382146c60 fix the go vet for go tip 2016-01-29 13:11:11 +08:00
1bf52e8922 Merge branch 'develop' of https://github.com/astaxie/beego into develop 2016-01-29 10:26:40 +08:00
5cd1ed8106 add test for tip 2016-01-29 00:01:04 +08:00
bd79972d85 Merge pull request #1638 from pjoe/log_console_nocolor
Add noColor option for console logger
2016-01-28 22:39:02 +08:00
ecab397073 try to fix the little bug when calling Close or Flush in async mode 2016-01-28 21:53:44 +08:00
a043432398 Change option name from noColor to color
Still same default behavior (with color)
2016-01-28 07:50:07 +01:00
7f888e3d18 Add noColor option for console logger
- Also added simple test
2016-01-27 19:20:58 +01:00
85c0fcd335 Merge pull request #1637 from pjoe/input_param_fix
Fix Context.Input.SetParam not overwriting existing value
2016-01-27 23:01:10 +08:00
f769016126 Merge pull request #1632 from youngsterxyf/config-logic
Config logic
2016-01-27 22:25:00 +08:00
453d744db9 Fix Context.Input.SetParam not overwriting existing value
- Also added tests for Context.Input.Param handling
2016-01-27 14:58:50 +01:00
31ef4ae507 rename AppConfigPath, AppConfigProvider to appConfigPath, appConfigProvider, make public to private 2016-01-27 16:43:01 +08:00
ccce566ba7 accept @astaxie suggestion: change the sequence adapterName and configPath 2016-01-27 16:19:10 +08:00
e357f6846b accept @ysqi suggestion 2016-01-27 13:30:34 +08:00
321bcc606a fix bug of test case 2016-01-27 12:26:37 +08:00
e549d0fd9c move some code piece 2016-01-27 12:13:26 +08:00
c59a029ce7 Merge branch 'develop' into config-logic 2016-01-27 10:58:38 +08:00
4ce094a29a Merge branch 'master' into develop 2016-01-27 10:14:34 +08:00
fbb98fbe1f Merge pull request #1631 from yunkai/issue1
Fix regression caused by commit ad65479
2016-01-27 10:13:30 +08:00
20efd5236e fix bug 2016-01-27 00:42:07 +08:00
330b3b1931 enhancement code 2016-01-27 00:17:56 +08:00
d9e6250d08 fix config logic 2016-01-27 00:10:21 +08:00
e3810b599d Fix regression caused by commit ad65479
Commit ad65479 will cause "Method Not Allow" in preflight response
when enable CORS plugin.

The root cause is that CORS plugin didn't generate http output after applied
commit ad65479, so the value of `ctx.ResponseWriter.Started` will be keep
`false`, and then later filter chains will be go on to run when CORS filter
finished.

This path will both fix "Method Not Allow" and the original bug
"multiple response.WriteHeader calls".

Signed-off-by: Yunkai Zhang <qiushu.zyk@taobao.com>
2016-01-26 23:27:26 +08:00
e1f9491aed Merge pull request #1608 from ysqi/iniSaveErrorFix
Fixed #1607
2016-01-26 21:46:31 +08:00
6aeff53d8c Merge pull request #1625 from miraclesu/fix/mail_from
Fix utils mail some bugs
2016-01-26 21:42:21 +08:00
bf870eb9a2 mv mime.QEncoding.Encode logic to mail
it is named qEncode
2016-01-26 20:50:03 +08:00
f26d360ec9 Fix vet fail 2016-01-26 14:54:36 +08:00
f73eaf6393 Merge pull request #1626 from JessonChan/develop
log file name bug fixed
2016-01-26 13:42:47 +08:00
e11d150e8b replace \t with space 2016-01-26 09:35:39 +08:00
f2567bc114 some typo fixed 2016-01-26 09:29:04 +08:00
b5a07c6ba8 log file name bug fixed
this bug happens when daily rotate. ex,when it is 2016-01-22 23:59:59 and need a rotate,the file name should named with 2016-01-22 but named with 2016-01-23(next day)
2016-01-26 09:20:49 +08:00
01ccc75d6b Merge pull request #1615 from ysqi/routerErrorFix
Fixed #1586
2016-01-26 00:31:01 +08:00
b19f9bf88c Merge branch 'develop' of https://github.com/astaxie/beego into develop 2016-01-26 00:27:44 +08:00
6cc3d4470a Merge pull request #1616 from coseyo/path_patch
fix path issue #1613
2016-01-26 00:27:34 +08:00
61e9dc74c9 make sure the memcache testing success 2016-01-26 00:27:02 +08:00
8611862fd7 Merge pull request #1609 from youngsterxyf/fix-issue1566
fix issue #1566
2016-01-26 00:07:43 +08:00
d1481ea659 move assignment to init 2016-01-25 23:00:09 +08:00
5930f27da7 Fix mail Chinese subject garbled bug 2016-01-25 22:55:40 +08:00
4de91f675d show from when Config from is empty 2016-01-25 22:29:45 +08:00
15e9ba19c0 fix the range only used in Go 1.4 fix #1623 2016-01-25 21:39:44 +08:00
f8004b69ad fix the go vet 2016-01-25 21:33:57 +08:00
3dac344ff6 fix the vet url 2016-01-25 21:20:10 +08:00
e7d4452af0 add golint and go test 2016-01-25 21:13:56 +08:00
7e3ad5bcb0 fix #1585 2016-01-25 21:08:29 +08:00
87650ce8bc make golint happy 2016-01-25 20:57:41 +08:00
fdce4af9c8 fix #1619 2016-01-25 20:53:52 +08:00
bcac4bb8e3 accept @JessonChan suggestion 2016-01-25 20:53:25 +08:00
0e17e2a3d2 accept @JessonChan suggestion 2016-01-25 20:20:53 +08:00
a80feb00b8 Fix utils mail from field can't including Chinese bug 2016-01-25 18:15:08 +08:00
cf055c9db2 Merge branch 'astaxie/develop' into iniSaveErrorFix
# Conflicts:
#	config/ini_test.go
2016-01-24 11:37:43 +08:00
3d7354b9d2 import reset 2016-01-24 11:10:04 +08:00
09d3d89c6f fix test error again 2016-01-24 00:47:37 +08:00
3031bdd176 fix test error 2016-01-24 00:40:03 +08:00
4c1cfc1386 fix path issue 2016-01-24 00:18:16 +08:00
57d522a96a Merge pull request #1606 from ysqi/configWork
Support Parse Bool with more diffrent values
2016-01-23 23:03:03 +08:00
fd7473466b Merge pull request #1581 from hbejgel/patch-2
Checks if index is greater than the length of the wildcards. #1580
2016-01-23 23:01:22 +08:00
007af6224e Fixed #1586 2016-01-23 19:13:19 +08:00
cbc7f43e88 fix issue #1601 2016-01-23 17:12:46 +08:00
ecf24640fd fix issue #1566 2016-01-23 16:56:54 +08:00
51ae45a799 Fixed #1607 2016-01-23 14:53:52 +08:00
be544f963e Support Parse Bool with more diffrent values
ParseBool returns the boolean value represented by the string.
It accepts 1, 1.0, t, T, TRUE, true, True, YES, yes, Yes,Y, y, ON, on,
On,
 0, 0.0, f, F, FALSE, false, False, NO, no, No, N,n, OFF, off, Off.
Any other value returns an error.
2016-01-23 11:02:40 +08:00
af346e871b Merge pull request #1603 from coseyo/remote_develop
fix conf load bug
2016-01-22 20:34:46 +08:00
cb5fd49612 fix conf load bug 2016-01-22 18:03:02 +08:00
0f85c82a21 Merge pull request #1594 from nemtsevp/patch-2
Exceptions in controller methods
2016-01-21 14:57:02 +08:00
93b04e8a3b Exceptions in controller methods
Exceptions in methods names should be changed according to controller.go
2016-01-21 09:44:17 +03:00
07937dea9a Merge pull request #1588 from Kavin-Cao/master
template.go 的beegoTplFuncMap注释有误
2016-01-20 14:18:35 +08:00
5757e6548e template.go 的urlfor Func注释有误
template.go 的urlfor Func注释有误
2016-01-19 10:14:16 +08:00
b48f251043 Merge remote-tracking branch 'refs/remotes/astaxie/master' 2016-01-19 09:43:43 +08:00
35e340b937 Checks if index is greater than the length of the wildcards. #1580 2016-01-18 21:35:14 -02:00
befeac5b61 Merge branch 'develop' of https://github.com/astaxie/beego into develop 2016-01-18 23:30:12 +08:00
23bb36d35c fix the issue #1573 2016-01-18 23:29:56 +08:00
4b52a38183 Merge pull request #1575 from youngsterxyf/develop
simplify the implementation of splitPath in tree.go
2016-01-18 16:33:23 +08:00
30e5634bdb simplify the implementation of splitPath in tree.go 2016-01-18 16:13:31 +08:00
fa8f6e5a53 session destroy 2016-01-18 16:11:27 +08:00
6e987bfdaf Merge branch 'develop' of https://github.com/astaxie/beego into develop 2016-01-18 15:28:18 +08:00
918b9e510f fix the add tempfunc 2016-01-18 15:27:33 +08:00
12e5584c01 Merge pull request #1574 from youngsterxyf/develop
DRY
2016-01-18 15:24:53 +08:00
ac3b013de7 DRY 2016-01-18 15:17:42 +08:00
089242eda0 memcache test mulit return map has no sequence 2016-01-18 00:36:05 +08:00
92d0b9ae95 golint the config.go 2016-01-18 00:22:31 +08:00
6f786e1dea golint config.go 2016-01-18 00:22:11 +08:00
eb0bc084ec make the code mode readable
golint all the files
2016-01-18 00:18:52 +08:00
f925bb9058 golint all the files 2016-01-18 00:18:21 +08:00
da36d1d0e7 fix the wrong io.Writer 2016-01-18 00:03:39 +08:00
48f19b4191 Merge pull request #1571 from astaxie/develop
beego 1.6.0 released
2016-01-17 23:58:34 +08:00
9adf20d72e gofmt -s 2016-01-17 23:57:07 +08:00
90d1349665 fix typo 2016-01-17 23:48:17 +08:00
9b2597be68 fix the mail send empty subject 2016-01-17 23:48:09 +08:00
895748d632 beego 1.6.0 released 2016-01-17 22:55:09 +08:00
566aab4354 mail send support Address format 2016-01-17 22:49:02 +08:00
a304bb9c25 Revert "add test case for tidb"
This reverts commit f70d2cc373.
2016-01-17 18:35:11 +08:00
f70d2cc373 add test case for tidb 2016-01-17 18:24:29 +08:00
897cf57840 use travis-ci.org as build-status 2016-01-15 14:59:16 +08:00
8e6b0a6d7b Merge pull request #1567 from JessonChan/develop
some typo fixed
2016-01-15 14:40:14 +08:00
797571c85f fix the ORM test case 2016-01-15 14:36:45 +08:00
814e4ac031 rename the docsSepc to docs_spec 2016-01-15 14:14:09 +08:00
52083de720 typo fixed
seperator => separator
2016-01-15 14:07:37 +08:00
a069c73b3a test case bool default value is true 2016-01-15 14:02:08 +08:00
7dbeb2c39a fix the default value 2016-01-15 08:43:02 +08:00
4375ca84d1 fix the sqlite m2m 2016-01-14 23:49:28 +08:00
0eaf923a27 Merge pull request #1560 from JessonChan/log_enhancement
Log enhancement
2016-01-13 22:57:39 +08:00
9c55985915 update readme 2016-01-13 22:20:34 +08:00
6976c0f51d add CONTRIBUTING.md 2016-01-13 22:02:36 +08:00
5befc67389 Merge remote-tracking branch 'remotes/upstream/develop' into log_enhancement 2016-01-13 10:15:00 +08:00
fb5b04506a code refactor and format 2016-01-13 09:24:27 +08:00
7663d50c97 remove the lock writer 2016-01-13 09:21:55 +08:00
58730e3528 test file modify 2016-01-13 09:21:32 +08:00
e1b73b33d0 improve code 2016-01-13 08:21:44 +08:00
c535dc386e fast format 2016-01-12 22:54:39 +08:00
69804afc1b use pool to logMsg 2016-01-12 22:39:40 +08:00
164366ae0d return error 2016-01-12 22:33:52 +08:00
2b9d7ff714 remove log package 2016-01-12 22:32:36 +08:00
bd0f3c29fa decr malloc new object 2016-01-12 22:32:20 +08:00
6660720ce6 update some config name 2016-01-12 21:55:02 +08:00
12e7f0f94a extract a func to write to every logger 2016-01-12 21:15:25 +08:00
482b7a62bd use array not map 2016-01-12 21:05:06 +08:00
b90a28bafb Merge pull request #1546 from youngsterxyf/develop
fix #1530
2016-01-12 20:01:51 +08:00
baa2e9d64a code format 2016-01-12 19:44:28 +08:00
5511e03b52 embedding file writer 2016-01-12 19:25:33 +08:00
9507e59c2f camel name fixed 2016-01-12 19:10:08 +08:00
2479e61db9 add asynchronous and call depth benchmark 2016-01-12 19:09:00 +08:00
c9b890b10e add asynchronous benchmark 2016-01-12 17:59:23 +08:00
e32e3a759c console type no need to bench 2016-01-12 17:46:10 +08:00
391f897eb1 simplify sessionID 2016-01-11 16:49:56 +08:00
dc278da17c fix the sqlite3 & cache sleep 11 second 2016-01-08 23:30:19 +08:00
c7146d22f4 add all dependence 2016-01-08 23:20:25 +08:00
1aff26cc31 add dependence database 2016-01-08 23:10:50 +08:00
c59bc431e3 mulit variable 2016-01-08 23:00:22 +08:00
d0dd68351a update travis 2016-01-08 22:34:23 +08:00
0c48738841 for issue #1530, fix incompatible bug 2016-01-08 21:29:12 +08:00
0b0904db13 for issue #1530, accept @JessonChan's suggestion 2016-01-08 20:16:58 +08:00
fd608d2bf6 disable tidb testing 2016-01-08 19:59:20 +08:00
5b028796b8 fix the test case for input 2016-01-08 16:24:59 +08:00
d2de71d8ab update the dependence 2016-01-08 16:01:07 +08:00
302b1ef7df fix the data 2016-01-08 15:52:57 +08:00
77fa891499 update dependence 2016-01-08 15:47:13 +08:00
69bcbcdb31 add travis 2016-01-08 15:34:02 +08:00
bb43d3a78c fix #1530 2016-01-08 13:47:14 +08:00
01012fa898 admin configure 2016-01-08 01:40:19 +08:00
9167587929 add Params for input 2016-01-08 01:20:34 +08:00
c68505e451 read config from app.conf for session 2016-01-07 23:55:55 +08:00
98f3fecc03 fix the typo 2016-01-07 23:35:01 +08:00
c6141f5d94 add httpsaddr 2016-01-07 23:31:33 +08:00
43ca13b516 Merge pull request #1543 from miraclesu/validation
Add validation custom function
2016-01-07 23:24:31 +08:00
687266fb64 Add 179 to valid Phone number 2016-01-07 23:03:32 +08:00
21f767784b Add custom validation function doc 2016-01-07 22:55:12 +08:00
103ac3ee5b Add custom validation function 2016-01-07 22:42:04 +08:00
db2918b0aa Merge pull request #1542 from ysqi/develop
fix issues  #1473, #1502
2016-01-07 21:02:42 +08:00
6eff2e433f fix #1502,Notes error repair 2016-01-07 20:55:28 +08:00
58e2a7c099 fix #1473,Only update redis session if it already exist 2016-01-07 20:42:26 +08:00
434544060a Merge pull request #1540 from ysqi/develop
TplNames renamed TplName ,fix #1229,Remember modify bee tool.
2016-01-07 17:03:22 +08:00
4c0c0ec2a7 TplNames renamed TplName ,fix #1229,Remember modify bee tool. 2016-01-07 16:16:39 +08:00
ecc6bcba3f Merge pull request #1539 from ysqi/develop
change get sessionID logic from cookie
2016-01-07 13:49:33 +08:00
80912b6210 change get sessionID logic from cookie 2016-01-07 13:15:40 +08:00
3fdf72f14c Merge pull request #1532 from JessonChan/develop
mem cache put function fixed
2016-01-07 12:56:47 +08:00
3821b2cb26 createdTime typo fixed 2016-01-07 09:37:50 +08:00
8aed4c13d7 isExist func will check if the value is expired 2016-01-07 09:36:23 +08:00
6465dbd703 no more goroutine ,i will be GCed at a gc goroutine 2016-01-07 09:28:40 +08:00
98e0626f0c rename vals 2016-01-07 09:25:06 +08:00
b0b9812de6 extract a expire fun 2016-01-07 09:13:47 +08:00
eff200e014 modify as xuxiaohei suggest
https://github.com/astaxie/beego/issues/1259
2016-01-07 09:08:00 +08:00
8832334d6a tiny fix for error description and comment 2016-01-06 15:12:25 +08:00
0b39091292 mem cache put function fixed
when a expire duration==0,it means forever
https://github.com/astaxie/beego/issues/1260
2016-01-06 15:05:29 +08:00
a411042416 fix the init parse for config 2016-01-06 14:55:18 +08:00
8929814126 fix the log carsh before init 2016-01-06 11:48:23 +08:00
bef6bca397 Add function to set validation default messages 2016-01-05 21:14:35 +08:00
f7ef4aa7e5 recover for conn.Close fix #1333 2016-01-04 23:34:45 +08:00
5c18d02b17 fix #1268 2016-01-04 22:41:25 +08:00
3bb22d149e add the comments 2016-01-04 22:22:42 +08:00
f0be45dfff fix the comments and json tag 2016-01-04 22:18:59 +08:00
7fbaf82897 fix #1424 2016-01-04 22:10:18 +08:00
73168d2f7d Merge pull request #1527 from JessonChan/develop
reuse compress writer
2016-01-04 14:36:40 +08:00
a03fa0fb73 improve cache modules. support mulit instances 2016-01-04 10:50:04 +08:00
fd2ded190b EnableGzip bug fixed 2016-01-04 09:27:58 +08:00
d23291ccc7 remove a dump err 2016-01-04 08:50:59 +08:00
92d157736b add testing to test #1511 2016-01-03 21:06:35 +08:00
6585e66f97 all the browser should support delete and put now 2016-01-03 20:36:16 +08:00
3ebf275157 fixed camel style name 2016-01-03 15:40:44 +08:00
ee2322e83b add any level compress 2016-01-03 15:35:32 +08:00
59fa248292 use sync.Pool to decrease new compression writer 2015-12-31 18:50:52 +08:00
9519fc6c96 Merge pull request #1519 from vvelikodny/develop
Refactoring: Move dev & prod runmodes to const
2015-12-30 21:03:54 +08:00
4b368d9f5e Refactoring: keep config package beego independent 2015-12-30 11:22:09 +03:00
48fd9675ad Refactoring: Move dev & prod runmodes to const 2015-12-29 21:32:37 +03:00
ac3a447479 fix the session update issues 2015-12-27 14:09:20 +08:00
37dff6be28 Merge pull request #1507 from yydzero/develop
Retrieve session identifier from cookie and query parameters
2015-12-27 11:01:04 +08:00
cdde5bdc62 Merge pull request #1516 from johndoejdg/patch-2
Add link to russian
2015-12-27 11:00:24 +08:00
2d4cc6e33d Add link to russian
Add link to russian
2015-12-26 16:43:49 +03:00
5336e83469 Merge pull request #1506 from fuxiaohei/develop
clean code in docs.go, error.go, filter.go and hooks.go
2015-12-23 12:12:08 +08:00
da39082d4f Retrieve session identifier from cookie and query parameters 2015-12-22 10:30:44 +08:00
fud
cd514803a4 simplify filter.go hooks.go 2015-12-22 10:02:59 +08:00
fud
2ddda59605 Merge branch 'develop' of https://github.com/astaxie/beego into develop 2015-12-22 09:57:47 +08:00
25337aec27 Merge branch 'develop' of https://github.com/astaxie/beego into develop 2015-12-21 22:54:22 +08:00
351dfac653 context.Response should implement Hijack/Flush/CloseNotify 2015-12-21 22:51:18 +08:00
9105fee453 Merge pull request #1503 from fuxiaohei/develop
simplfy config.go and controller.go
2015-12-21 19:49:22 +08:00
fud
bbd42ce152 simplify docs.go and error.go, use http.StatusText instead of string codes 2015-12-21 17:16:58 +08:00
fud
92711e80a3 refactor controller.go 2015-12-21 16:23:31 +08:00
fud
c43e3d6684 fix type mismatch error 2015-12-21 16:05:26 +08:00
fud
f6c508f138 Merge branch 'develop' of https://github.com/astaxie/beego into develop 2015-12-21 15:49:23 +08:00
130ce7eb1f Merge pull request #1501 from nkbai/develop
cors test benchmark doesn't use b.N
2015-12-19 13:45:04 +08:00
083c155079 cors test benchmark doesn't use b.N 2015-12-19 10:29:12 +08:00
a71c4283e8 Merge pull request #1498 from JessonChan/develop
compress level test fixed
2015-12-18 10:18:42 +08:00
80ac8aa40e compress level test fixed 2015-12-18 09:28:40 +08:00
5e249772d5 reduce loop 2015-12-18 00:14:28 +08:00
a4f674e7f4 Merge pull request #1483 from nkbai/develop
为orm接口添加注释
2015-12-17 22:23:32 +08:00
ae52d4aa18 improve the splitPath 2015-12-17 21:31:44 +08:00
f9138c5a99 simplify config.go 2015-12-17 20:05:00 +08:00
e5096be32b Merge pull request #1490 from pjoe/orm_pk_rel_many
Fix joins for reverse(many) with custom pk
2015-12-17 14:56:06 +08:00
9b87f528ec Merge pull request #1494 from astaxie/revert-1489-develop
Revert "go test fixed"
2015-12-17 14:45:17 +08:00
c3d1e4d088 Revert "go test fixed" 2015-12-17 14:45:10 +08:00
77113e843c Merge pull request #1489 from JessonChan/develop
go test fixed
2015-12-17 14:44:29 +08:00
46aa340b1d Merge pull request #1478 from fuxiaohei/develop
clean compliated codes, refactor if sections in app.go
2015-12-17 14:44:14 +08:00
8771634fe4 Merge remote-tracking branch 'remotes/upstream/develop' into develop 2015-12-17 09:25:15 +08:00
2aa50c240f Merge pull request #1486 from KilledKenny/oomDos
Added MaxMemory limit to CopyBody() Supersedes #1484
2015-12-16 23:44:42 +08:00
dbc4ac6945 reduce the slicegrow 2015-12-16 23:43:32 +08:00
29752e2575 refactor router 2015-12-16 23:11:03 +08:00
52c4c1fb98 Added MaxMemory limit to CopyBody()
Beego only uses the MaxMemory flag when using go's built in functions
for parsing forms. However the CopyBody() function have no limit an will
coppy anny amount of data into memory using ioutil.ReedAll() on the
request body whitout anny size validation or limit.

This fix wrapps input.Requst.Body in a LimitedReader using the same
memory limit as ParseFormOrMulitForm()
2015-12-16 10:37:21 +01:00
906637ae8b Fix issue with reverse(many) for models with custom pk
- Also add test covering the issue
2015-12-15 17:39:08 +01:00
3daaaeb32b add commit for orm/types.go 2015-12-15 19:48:28 +08:00
ccc008c257 compress fixed 2015-12-15 14:29:07 +08:00
fd9a6ff7bb Merge remote-tracking branch 'remotes/upstream/develop' into develop 2015-12-15 14:27:43 +08:00
e63d24637d go test fixed 2015-12-15 14:08:58 +08:00
7dcbcf0748 Merge branch 'develop' of https://github.com/astaxie/beego into develop 2015-12-15 14:05:58 +08:00
1576add9a2 Merge pull request #1488 from astaxie/revert-1487-develop
Revert "compress method fixed"
2015-12-15 14:05:43 +08:00
58aa0545b6 Revert "compress method fixed" 2015-12-15 14:05:33 +08:00
10cbe7a867 Merge branch 'develop' of https://github.com/astaxie/beego into develop 2015-12-15 13:18:09 +08:00
d6f23afdbb Merge pull request #1487 from JessonChan/develop
compress method fixed
2015-12-15 11:49:19 +08:00
499e2b59e4 compress method fixed
in http,the deflate is zlib compress method accoding to the sec
http://tools.ietf.org/html/rfc2616#section-3.5
The "zlib" format defined in RFC 1950 [31] in combination with
        the "deflate" compression mechanism described in RFC 1951 [29].
2015-12-15 11:34:26 +08:00
d99c62df1f Merge remote-tracking branch 'upstream/develop' into develop 2015-12-14 15:09:06 +08:00
b079456fcf add comments for orm/types.go 2015-12-14 15:04:17 +08:00
a06022f75c clean compliated codes, refactor if sections in app.go 2015-12-12 14:10:02 +08:00
2b651fbae2 reuse map in tree.Match 2015-12-11 13:51:01 +08:00
80bc372f17 pool.Put 2015-12-11 00:20:17 +08:00
f70f338025 use sync.Pool to reuse Context 2015-12-10 21:59:54 +08:00
f2edfbe7ae make the other three beauty 2015-12-10 00:12:52 +08:00
4f829af7aa gofmt the config default 2015-12-10 00:11:24 +08:00
9f2a2507fd fmt it and remove the init var 2015-12-09 23:47:44 +08:00
be60f47488 Merge pull request #1455 from nkbai/develop
windows下静态文件映射找不到问题以及 grace init延后
2015-12-09 23:44:45 +08:00
d1bba02958 refact beego config 2015-12-09 23:35:04 +08:00
e8fe859a58 格式调整 2015-12-09 09:03:10 +08:00
82e0105d22 Revert "windows下静态文件映射找不到问题,"
windows下路径需要filepath.ToSlash

This reverts commit 2c8cb5693e.
2015-12-04 23:15:06 +08:00
ff0762cc19 Merge remote-tracking branch 'upstream/develop' into develop 2015-12-03 15:50:21 +08:00
3d4ad560f8 Merge pull request #1423 from JessonChan/fargo
fix the  accept encoding
2015-12-02 21:08:34 +08:00
36664634f9 Merge pull request #1467 from astaxie/revert-1464-master
Revert "Update orm_querym2m.go"
2015-12-02 16:35:44 +08:00
ed99c013a6 Revert "Update orm_querym2m.go" 2015-12-02 16:35:36 +08:00
a1d5f958c5 Merge pull request #1464 from gobenon/master
Update orm_querym2m.go
2015-12-02 16:35:05 +08:00
5d902264e5 Merge pull request #1459 from ryanchapman/develop
Log config parsing errors
2015-12-02 16:34:35 +08:00
50f8f3bd20 Update orm_querym2m.go 2015-11-30 18:23:58 +02:00
74ebcd28b2 grace init中的工作可以延后进行
实际上如果没有使用graceful启动,这些init的工作完全没用
当然其他模块也应该存在这样的工作,比如session中的sess_utils.go中的init工作
2015-11-30 16:15:35 +08:00
7151b96465 Log config parsing errors
Beego currently handles the case of "conf/app.conf"
not existing, but all other errors are not logged.
This fixes that.

I ran into an issue where beego was not listening on the
correct port, and it turned out that the conf/app.conf file
had a colon ":" instead of an equal sign "=" (confusion of
INI vs YAML formats).  When there was a parsing error, beego
would give up on parsing app.conf and not log anything to the
console.
2015-11-29 15:09:28 -07:00
2c8cb5693e windows下静态文件映射找不到问题,
path.Clean和filepath.Clean是有区别的
2015-11-27 12:10:39 +08:00
d693ecf046 Revert "这种if型的初始化是有问题的"
This reverts commit 50132df809.
2015-11-26 20:37:36 +08:00
6aaca2eca8 router.go Header 毫无用处
context/output.go 简化一下代码,更清晰
2015-11-26 14:56:39 +08:00
f0474214fe GetInt等函数稍微简化一下,这样看上去更合理一些 2015-11-25 23:39:26 +08:00
2c94d9eab2 Merge pull request #1451 from gobenon/master
fix issue 1438 opened by Ayelet Regev
2015-11-25 09:29:28 +08:00
40700a8532 Merge remote-tracking branch 'remotes/upstream/develop' into fargo
# Conflicts:
#	memzipfile.go
2015-11-25 09:11:32 +08:00
fb7314f8ac Merge pull request #1450 from astaxie/revert-1439-master
Revert "Adding support for junction tables with other fields which arent FK and PK."
2015-11-24 23:00:15 +08:00
b7fcd4f0b9 Revert "Adding support for junction tables with other fields which arent FK and PK." 2015-11-24 23:00:07 +08:00
e48e09a4ab Merge pull request #1439 from gobenon/master
Adding support for junction tables with other fields which arent FK and PK.
2015-11-24 23:00:04 +08:00
235b58504a Merge pull request #1449 from nkbai/master
controller_test.go 既然叫test,那就按照go的规则进行test吧
2015-11-24 15:54:46 +08:00
5e915cb614 controller_test.go 既然叫test,那就按照go的规则进行test吧 2015-11-24 14:55:59 +08:00
9170b91075 go style format (remove the blank after comments) 2015-11-21 08:46:19 +08:00
25320bf86a Merge pull request #1441 from nkbai/master
fix #1433
2015-11-20 21:50:55 +08:00
50132df809 这种if型的初始化是有问题的 2015-11-20 21:34:01 +08:00
83696a40f6 fix #1433 2015-11-20 11:18:45 +08:00
efd30bdba7 Update orm_querym2m.go 2015-11-19 16:46:14 +02:00
01aa1085e0 Merge pull request #1 from gobenon/gobenon-m2mpatch-1
Update orm_querym2m.go
2015-11-19 14:32:24 +02:00
ca37557a26 Update orm_querym2m.go 2015-11-19 14:30:14 +02:00
5a1e821a42 Merge pull request #1434 from WnP/pg-fix
fix postgres syntax error during migration
2015-11-19 12:25:21 +08:00
29ac961c10 fix postgres syntax error during migration 2015-11-18 06:46:44 -06:00
5d01afe3a6 isOk to check whether the file is latest 2015-11-16 14:05:05 +08:00
d963bb79bd avoid map-lock delete 2015-11-16 10:31:27 +08:00
bc2195b07f code simplify 2015-11-12 16:59:07 +08:00
46fbeaadad refactor accept encoder ,simplify the struct 2015-11-12 12:03:53 +08:00
214030fad4 bytes reader replace string reader 2015-11-12 11:44:29 +08:00
f8db8ae9c3 add some comments 2015-11-12 10:48:36 +08:00
a9881388f7 accept encoder header setting fixed 2015-11-12 10:08:57 +08:00
1200b7c347 method refactor 2015-11-11 18:06:18 +08:00
b25a1355f9 remove old code 2015-11-11 16:27:41 +08:00
d2c60619fa new static file support code 2015-11-11 16:22:40 +08:00
82c50b972d new test file 2015-11-11 16:22:05 +08:00
15b0b9b66d delete old static file code 2015-11-11 16:21:04 +08:00
e4c6e5d2e1 change package 2015-11-11 13:47:47 +08:00
f457ea0fe9 refactor encoder package 2015-11-11 13:47:36 +08:00
7ef9b3d55b runnable typo fixed 2015-11-10 14:07:26 +08:00
39e29caf9b refactor to fix encoder type bug 2015-11-10 13:42:10 +08:00
7ccf049a50 bug fixed 2015-11-10 13:27:33 +08:00
7964f7f163 test bug fixed 2015-11-10 13:16:16 +08:00
8603127c81 beego package file path rewrite 2015-11-10 13:10:42 +08:00
07c93cd32c mem zip file test ,add license 2015-11-10 11:59:32 +08:00
83ec39d02e refactor max age cookies setting 2015-11-10 11:47:10 +08:00
8f42193610 better compress func design 2015-11-10 11:32:18 +08:00
891be34fc6 encoding should be specify 2015-11-10 11:20:13 +08:00
0bc70e88f0 ignore the other compress method 2015-11-10 11:00:29 +08:00
3872c48349 accept encoding refactor and bug fixed 2015-11-10 10:55:47 +08:00
dc28e37606 Merge branch 'develop' into fargo 2015-11-09 17:18:00 +08:00
821b2f832e fix the type assert 2015-11-09 11:03:57 +08:00
9b725c73c3 Merge pull request #1376 from JessonChan/develop
static file code refactor and bug fixed
2015-11-08 23:21:16 +08:00
860568cb6c modified as astaxie reviews
https://github.com/astaxie/beego/pull/1376
2015-11-06 18:51:53 +08:00
dc3e324f38 Merge pull request #1418 from ElvizLai/patch-1
Update context.go
2015-11-05 22:39:34 +08:00
b8fc42d38d Update context.go
all this status was setting in error.go, this line will cause multi-resp
2015-11-05 21:20:57 +08:00
a26dee556d fix #1335 2015-11-05 00:19:09 +08:00
fd4630c6dd impove the ResponseWriter. fix #1410 2015-11-04 23:52:42 +08:00
e3120226fa Merge pull request #1414 from FlamingTree/develop
bugfix: graceful failed when both enable http and https
2015-11-04 23:27:27 +08:00
25bec8bbe9 Merge pull request #1381 from ADone/m2m_reverse_bug
fix #671
2015-11-04 23:20:22 +08:00
a257a924a1 Merge pull request #1379 from pjoe/non_int_fk
orm: Fix handling of rel(fk) to model with string pk
2015-11-04 22:26:33 +08:00
c7e9a86b00 Merge pull request #1415 from johndeng/develop
Fixed typos
2015-11-04 13:08:37 +08:00
205de8418d Fixed typos 2015-11-03 23:43:34 +08:00
f81929c28c bugfix: graceful failed when both enable http and https 2015-11-03 14:53:26 +08:00
58ed1436cc Merge pull request #1409 from superhacker777/patch-2
XSRFFormHtml() should also generate XSRF token.
2015-10-28 15:54:07 +08:00
5d18a7466c XSRFFormHtml() should also generate XSRF token. 2015-10-27 20:16:47 +03:00
912abe3272 fix #1388 2015-10-12 21:26:18 +08:00
4ba50e5df5 fix #1385 2015-10-12 20:50:58 +08:00
332fa44231 Merge pull request #1384 from pjoe/update_err_fix
Fix dbBase.Update not returning error on failure
2015-10-12 20:43:22 +08:00
174e758d19 Fix dbBase.Update not returning error 2015-09-28 14:07:35 +02:00
1f2f0b30f4 mem zip file refactor and test 2015-09-22 22:02:56 +08:00
cfcce4f5dc Fix handling of rel(fk) to model with string pk 2015-09-22 12:23:51 +02:00
1abf85ed2a simplify the switch code 2015-09-22 15:18:24 +08:00
d4f3dfd527 return when find static path 2015-09-22 14:15:41 +08:00
6ad215a9bb mem zip file var refactor 2015-09-22 14:11:02 +08:00
9c17f73489 code refactor 2015-09-22 13:48:34 +08:00
4995f91547 code refactor 2015-09-22 13:46:20 +08:00
936cb735e1 file extensions bug fixed 2015-09-22 13:27:35 +08:00
f708ce0299 当有设置的压缩类型时,丢弃默认类型(css,js) 2015-09-22 12:24:52 +08:00
dc38b324e0 code bug fixed 2015-09-22 12:19:31 +08:00
b9fb3a62f5 static file name default lower case 2015-09-22 11:59:48 +08:00
95ef4c7136 server index.html in beego with ServeContent 2015-09-21 23:56:24 +08:00
eb85e8e328 path.Clean can't clean window separate .."
"
2015-09-20 19:59:30 +08:00
e26720496f remove the dupl 2015-09-19 20:05:57 +08:00
8af8936ee0 Merge pull request #1368 from JessonChan/fargo
error bug fixed and clean code
2015-09-19 15:10:22 +08:00
07a424581d // beego.Run("localhost") 2015-09-19 05:53:28 +08:00
69bee9ef3c // beego.Run("localhost") 2015-09-19 05:52:52 +08:00
caf3714495 revert exceptMethod 2015-09-19 05:41:10 +08:00
983bac986a runFunction camel name 2015-09-18 18:34:07 +08:00
56032c67af runFunction camel name 2015-09-18 18:31:06 +08:00
40cb8e0cf1 use reflect to ensure all methods been except 2015-09-18 18:18:12 +08:00
0ac690d2c8 method name refactor 2015-09-18 17:59:28 +08:00
cc5abc6b30 default atoi func to handle exception 2015-09-18 17:03:00 +08:00
cb0400dcd4 file add the config for Perm 2015-09-18 12:12:02 +08:00
fda28fa2ff fix the conv test case 2015-09-18 12:11:48 +08:00
2a96f33543 more clean code 2015-09-18 10:36:16 +08:00
8df2cca627 add comment 2015-09-18 10:32:21 +08:00
ead635e62f default exception handler 2015-09-18 10:31:10 +08:00
4823a0f114 remove the dead code 2015-09-17 23:47:26 +08:00
e665a7dd32 Merge pull request #1367 from dvwallin/develop
added a check to parser to not panic (in develop)
2015-09-17 23:41:59 +08:00
6e24b78b62 fix the wrong response 2015-09-17 23:27:34 +08:00
edbad60782 Merge branch 'develop' of github.com:dvwallin/beego into develop 2015-09-17 17:07:06 +02:00
bb6062857b fix the error refactor 2015-09-17 23:05:45 +08:00
0d100fef7d Merge pull request #1364 from JessonChan/fargo
error and hook refactor
2015-09-17 23:02:32 +08:00
eac09e6fb6 Merge pull request #1349 from ElvizLai/patch-4
Update tree.go
2015-09-17 23:01:32 +08:00
3df0fa462d golint tidb 2015-09-17 23:00:05 +08:00
dfbb1b5ee5 Merge pull request #1366 from ngaut/master
Add support for TiDB
2015-09-17 21:25:12 +08:00
09b7457ac6 orm_test: Skip relation test 2015-09-17 17:05:40 +08:00
c841a77ad6 Orm: Add tidb for query builder 2015-09-17 17:04:23 +08:00
c73e0395ed Orm: Support TiDB 2015-09-17 17:04:23 +08:00
de20960458 error map refactor 2015-09-17 10:36:29 +08:00
bb776cc4cb error map refactor 2015-09-17 10:33:12 +08:00
cce8d1e934 refactor hooks function code 2015-09-17 10:31:53 +08:00
c6448727de golint utils 2015-09-14 23:35:13 +08:00
5015614fdc golint pagination 2015-09-14 23:17:33 +08:00
7b81617a95 golint captcha 2015-09-14 23:13:51 +08:00
2389bc72f9 golint validation 2015-09-13 00:13:19 +08:00
1d200da472 golint toolbox 2015-09-12 23:28:24 +08:00
be7accc94c golint testing 2015-09-12 23:19:18 +08:00
a289b08e64 golint swagger 2015-09-12 23:15:23 +08:00
172894efe8 golint session 2015-09-12 22:53:55 +08:00
ea2039c1dc golint plugins 2015-09-12 22:03:45 +08:00
68ec133aa8 golint orm 2015-09-12 21:46:43 +08:00
542e143e55 golint migration 2015-09-11 23:16:05 +08:00
0a5fa04062 remove i18n.go 2015-09-11 23:09:37 +08:00
34877c52a9 golint logs 2015-09-11 23:08:24 +08:00
657995092a golint httplib 2015-09-11 22:28:28 +08:00
f6d4629103 added a check to parser to not panic 2015-09-10 11:35:57 +02:00
65fb7ce391 golint grace 2015-09-10 16:35:40 +08:00
01a5e54264 delete example from the source code 2015-09-10 15:40:46 +08:00
ff5b09fc19 golint context 2015-09-10 15:31:09 +08:00
bdd6a6ae40 golint config 2015-09-10 14:53:19 +08:00
d7aaf2ebeb golint cache package 2015-09-09 00:15:03 +08:00
62e528ca4c golint tree.go 2015-09-08 23:49:24 +08:00
bcb1db256d golint templatefunc 2015-09-08 23:41:41 +08:00
44bd3beb5e golint happy with template 2015-09-08 23:29:58 +08:00
8615f875f8 make golint happy staticfile.go 2015-09-08 22:07:44 +08:00
b2048e8653 make router test passed 2015-09-08 22:05:38 +08:00
c11740b647 make golint happy router.go 2015-09-08 22:01:13 +08:00
21fffc446b make golint happy parser.go 2015-09-08 21:45:45 +08:00
67b36d7c48 make golint happy 2015-09-08 21:41:38 +08:00
61570ac2f7 make golint happy with controller.go 2015-09-08 10:43:42 +08:00
f28a941e26 make golint happy and also make the config readable 2015-09-07 23:19:42 +08:00
152127c2af make golint happy 2015-09-07 21:38:53 +08:00
919675e793 update the comments 2015-09-07 19:29:52 +08:00
fe9c52fb69 optimize init admin 2015-09-07 19:27:53 +08:00
284dfc0843 move the template related fun to template.go 2015-09-07 19:21:55 +08:00
85d8ec5ca6 optimize the beego structure 2015-09-07 19:18:04 +08:00
eb3479b753 optimize the app structure 2015-09-06 23:00:42 +08:00
a2a6ec954b Update tree.go
go fmt
2015-09-06 22:13:58 +08:00
45b72b0674 Merge pull request #1334 from ElvizLai/patch-2
Update beego.go
2015-09-06 22:12:13 +08:00
9e969957de Merge pull request #1348 from sidbusy/develop
allows custom the TableName of Session
2015-09-06 21:22:05 +08:00
7b0f3a83dc Merge pull request #1351 from leekchan/cache
Fix a wrong test name & update a outdated information in README
2015-09-06 18:46:59 +08:00
fe1ec1675f Fix a wrong url (http 404). 2015-09-06 17:22:33 +09:00
4ad743fc8b Update a outdated information in README. 2015-09-06 17:20:18 +09:00
06ec3a931d Fix a wrong test name. 2015-09-06 17:13:55 +09:00
1377d16559 Update tree_test.go 2015-09-06 12:17:16 +08:00
ddd9bf1305 Update tree.go 2015-09-06 12:16:05 +08:00
508a57be1e Update tree_test.go 2015-09-06 12:07:12 +08:00
5ad999a3d1 Update tree.go
fix routers for:
```
/topic/:id/?:auth
/topic/:id/?:auth:int
```
2015-09-06 12:01:50 +08:00
f55bbbdff4 allows custom the TableName of Session 2015-09-05 10:31:31 +08:00
34aa9002bb fix the httplib test case timeout 2015-09-04 23:03:10 +08:00
f9fe89fff0 fix the file rotate test case issues 2015-09-04 22:33:03 +08:00
9ab7466d5c fix the cappital 2015-09-04 22:20:55 +08:00
2e75c04ffb Merge pull request #1345 from f0r/develop
为querySeter添加GroupBy方法
2015-09-04 21:48:47 +08:00
9038cdfaae Merge pull request #1343 from onealtang/oneal-dev
always use server's locale to parse date
2015-09-04 21:32:18 +08:00
f0r
a074df9c2e 为querySeter添加GroupBy方法 2015-09-03 00:45:09 +08:00
adca455804 always use server's locale to parse date
When parsing the date without time, it's always using UTC date, which is
unexpected. If we want to use UTC date, it's recommend to set the
server's timezone as UTC, and keep the code flexible.
2015-09-02 15:50:40 +08:00
9fd571830d Update beego.go
Maybe the `Hard Coding` should have a higher priority
2015-09-01 17:52:44 +08:00
dd4cbdda66 update the gitignore 2015-08-31 11:58:11 +08:00
ad6547936e fix the http: multiple response.WriteHeader calls 2015-08-28 23:08:00 +08:00
306effa300 Merge pull request #1329 from ElvizLai/patch-1
Update error.go
2015-08-28 22:33:20 +08:00
c516819c56 Update error.go
this caused `http: multiple response.WriteHeader calls` when using method `CustomAbort` or `Abort` when status is already in errMap like 404.
2015-08-28 16:54:49 +08:00
4202fe8fe0 Merge pull request #941 from lei-cao/develop
Added JWT plugin
2015-08-27 22:57:04 +08:00
0c5f4b48d4 Merge pull request #1276 from pjoe/orm_distinct
Fix issue #1274: Add QuerySeter.Distinct()
2015-08-27 22:54:39 +08:00
7aa893612e Merge pull request #1308 from zhangshuai/master
httplib请求参数支持[]string
2015-08-27 22:50:44 +08:00
cfaae5ab8c Merge pull request #1326 from hvnsweeting/patch-1
fix typo
2015-08-27 22:49:38 +08:00
cbb6591bdb fix typo 2015-08-26 15:57:28 +07:00
0c33673197 Revert spaces > tabs change 2015-08-24 09:41:10 +02:00
862ea226e5 优化设置参数 2015-08-24 00:16:56 +08:00
437349f776 Merge pull request #1247 from Skycrab/develop
fix example/chat i/o timeout
2015-08-23 22:18:38 +08:00
bc1f0ac6fd Merge pull request #1298 from wulove/develop
全局变量AutoRender为false时,run时不再编译模版;针对开发模式下,每个请求渲染模版时支持单独编译当前请求相关模版
2015-08-23 22:13:15 +08:00
db2b1ee54f Merge pull request #1318 from tabalt/patch-1
fixed mux1 to mux
2015-08-21 10:57:42 +08:00
99b1c5c54b fixed mux1 to mux 2015-08-21 10:53:59 +08:00
38ddb61199 files is []string, use len(files)==0 2015-08-21 10:32:53 +08:00
d9e4836715 Merge pull request #1305 from Hepri/develop
Added MapGet template func
2015-08-20 22:33:07 +08:00
0e3fe64c69 Added TestMapGet 2015-08-20 19:04:43 +05:00
4081311a37 Merge pull request #1309 from smallfish/develop
Update router.go, add Flush for responseWriter
2015-08-19 15:25:24 +08:00
506f54a080 Update router.go, add Flush for responseWriter 2015-08-19 15:23:50 +08:00
ff92f22d84 删除Param中的断言 2015-08-18 23:19:24 +08:00
860006bfda httplib请求参数支持[]string 2015-08-18 19:28:17 +08:00
9107fd8898 remove the default timeout setting 2015-08-17 22:33:28 +08:00
d91840779a Update templatefunc.go 2015-08-17 01:18:29 +05:00
d4e15c0bd0 Added MapGet template func 2015-08-17 00:08:02 +05:00
877b5c233e 增加编译模版函数BuildTemplate可变参数,使之支持单个或多个模版的编译,同时针对开发模式,每个请求只编译当前请求相关模版
增加编译模版函数BuildTemplate可变参数,使之支持单个或多个模版的编译,同时针对开发模式,每个请求只编译当前请求相关模版,不再每次请求都编译全部模版
2015-08-06 10:09:34 +08:00
57fdc308e3 AutoRender为空时,不再编译模版
AutoRender为空,Controller.Render()不再执行,故无需编译模版
2015-08-06 09:36:43 +08:00
4857e38471 Merge pull request #1284 from wallclockbuilder/develop
Fix #1269 extract godoc to own file.
2015-07-28 11:57:27 +08:00
42fab96cd4 Merge pull request #1285 from JessonChan/beego_develop
typo fixed
2015-07-28 11:56:45 +08:00
b26ef5b2e5 typo fixed
registor==>register
innner ==> inner
2015-07-27 08:44:58 +08:00
b622d5d369 Fix #1269 extract documentation
Fix #1271 Add description from docs on beego.me to README
and also add same description to godoc
2015-07-26 17:06:55 +00:00
19d82ab62c Fix #1274: Add QuerySeter.Distinct() 2015-07-22 18:12:57 +02:00
9775e3e3a4 Merge pull request #1265 from fugr/develop
set DoRotate fname like xx.2013-01-01.2.log
2015-07-17 01:56:48 +08:00
160d82d1d2 Merge pull request #1267 from wallclockbuilder/develop
Fix for #1256. Indent the code sample lines properly.
2015-07-17 01:50:09 +08:00
45d693e6d6 Add quick start example from website to README 2015-07-16 04:32:07 +00:00
0564956fd6 Fix for #1256. Indent the code sample lines properly. 2015-07-15 12:04:18 +00:00
59b903d557 set DoRotate fname like xx.2013-01-01.2.log
fix fname,by extension to identify the file type on mac and windows.
2015-07-15 17:00:48 +08:00
5612f61a93 fix #671 2015-07-08 17:42:00 +03:00
3becd2e0d8 Merge pull request #1249 from wallclockbuilder/patch-1
Fix #1237
2015-07-08 12:38:48 +08:00
8c0ad5ef88 fix example/chat i/o timeout 2015-07-06 21:12:03 +08:00
079993b9f7 fix #1245 2015-07-06 13:54:14 +08:00
c15aaad85b Merge pull request #1244 from simman/develop
Update validators.go
2015-07-04 20:48:47 +08:00
06b25deab2 Update validators.go
Support virtual operators paragraph 170!
2015-07-04 17:55:01 +08:00
002302818d Fix #1237
Package description uses same text as the README.
2015-07-01 04:17:53 +00:00
a89f14d80d Merge pull request #1227 from oiooj/develop
fix FilterHandler crash issue
2015-06-19 11:43:15 +08:00
87e8bcc9be fix FilterHandler crash issue
Filter Handler will crash with error assignment to entry in nil map , params from function Tree.Match() maybe nil.
2015-06-19 11:19:35 +08:00
31e5edbdcf Merge pull request #2 from astaxie/develop
pull from stable
2015-06-19 11:09:11 +08:00
f7f390dfec fix #1221 2015-06-16 14:53:38 +08:00
8f7246e17b change to version 1.5.0 2015-06-15 23:49:13 +08:00
c8f6e0f156 remove the hardcode in runtime.Caller 2015-06-15 20:53:49 +08:00
0207caab6f keep the shortname for logs info/warn/debug 2015-06-15 20:44:14 +08:00
d629c1d3d0 change the comments 2015-06-15 20:22:05 +08:00
817650aa33 keep the short name for logs 2015-06-15 20:20:37 +08:00
ba1232dfaf filter should be always the same 2015-06-14 18:35:46 +08:00
64d4f6518b fix #1213 2015-06-14 18:10:10 +08:00
9f05db8475 Merge pull request #1212 from astaxie/revert-1211-revert-1210-develop
Revert "Revert "fix multiple filters execute issue""
2015-06-14 01:14:42 +08:00
b275d7c6f5 Revert "Revert "fix multiple filters execute issue"" 2015-06-14 01:14:33 +08:00
73770fbe22 Merge pull request #1211 from astaxie/revert-1210-develop
Revert "fix multiple filters execute issue"
2015-06-14 01:13:42 +08:00
fc11169ee3 Revert "fix multiple filters execute issue" 2015-06-14 01:13:34 +08:00
b54589fa9d Merge pull request #1210 from oiooj/develop
fix multiple filters execute issue
2015-06-14 01:08:51 +08:00
2af0c569a5 The last filterFunc with returnOnOutput=ture won't be executed
ex:
	beego.InsertFilter("/*", beego.BeforeExec, FilterLoginCheck1,false)
	beego.InsertFilter("/*", beego.BeforeExec, FilterLoginCheck2)

In function  FilterLoginCheck1 , I'll write data via ResponseWriter, and w.started = true
FilterLoginCheck2 won't be executed, it should be.
2015-06-14 01:02:41 +08:00
27b7a8f743 Merge pull request #1 from astaxie/develop
Develop
2015-06-14 00:35:38 +08:00
c143a6ec19 fix #1090 add Getfiles to support mulit file upload 2015-06-13 16:20:26 +08:00
e619d83990 fix the filter router issues 2015-06-13 12:47:01 +08:00
27b452cd95 Merge branch 'develop' of https://github.com/astaxie/beego into develop 2015-06-13 11:15:44 +08:00
b776e43962 merge bat/httplib to httplib 2015-06-13 11:15:13 +08:00
cb89cd577d Merge pull request #1201 from kongjian/develop
support eq&ne for orm
2015-06-13 10:54:47 +08:00
6b777f0c5e Merge pull request #1207 from oiooj/develop
Don't overwrite the params from function  ValidRouter
2015-06-13 09:16:07 +08:00
491238ce7d Don't overwrite the params from function ValidRouter
just add new params to context.Input.Params
2015-06-13 01:04:46 +08:00
d9bb1a3592 logs support elasticsearch adapter 2015-06-13 00:25:48 +08:00
9c6775c22c log default use synchro, and support async 2015-06-13 00:25:21 +08:00
4d70b22f96 Merge pull request #1157 from ziyel/master
Let filter function get more params info from ctx.Input.Params
2015-06-11 14:38:19 +08:00
d943d16d52 gofmt 2015-06-10 21:26:04 +08:00
bbb6f31f16 support eq&ne for orm 2015-06-09 10:18:21 +08:00
364cacf659 record the critical logs in Prod 2015-06-08 22:00:28 +08:00
21586586ba Merge pull request #1198 from kongjian/develop
remove space after int()& add sort for commentsRouter file
2015-06-08 20:20:40 +08:00
e1d7bc8826 remove space after int()& add sort for commentsRouter file 2015-06-08 17:25:46 +08:00
499ee09d4b Merge pull request #1194 from zieckey/GetMulti
Add GetMulti method for Cache interface
2015-06-08 08:36:20 +08:00
970f0b460c Add GetMulti method for Cache interface 2015-06-07 21:33:01 +08:00
9280683935 Merge pull request #1193 from zieckey/auth
Execute AUTH command when the "password" is configured
2015-06-07 20:34:32 +08:00
a58c8180e8 Execute AUTH command when the "password" is configured 2015-06-07 16:26:23 +08:00
b9852df51c Merge pull request #1190 from xboston/patch-1
fix session table
2015-06-04 22:09:26 +08:00
8e71d31dbe fix session table 2015-06-04 18:40:10 +05:00
db06e954b5 fix the session memcache bug 2015-05-28 12:04:19 +08:00
3abd01799d split into small files 2015-05-27 23:46:45 +08:00
ae37689314 fix #1176 grace support windows 2015-05-27 23:22:05 +08:00
40974365e6 Merge branch 'develop' of https://github.com/astaxie/beego into develop 2015-05-25 09:10:59 +08:00
edbaa080f1 update the string 2015-05-25 09:10:38 +08:00
d56491ab3a sync beeApp.Server to graceful 2015-05-25 09:10:38 +08:00
9fd7acf663 fix #1152 2015-05-25 09:10:37 +08:00
2dca48f26e fix sesseion redis db error 2015-05-25 09:10:37 +08:00
4138fe0217 beego suppot graceful restart application 2015-05-25 09:10:37 +08:00
245762f7d9 add apk mime 2015-05-25 09:10:37 +08:00
18bdec951d fix #1143 2015-05-25 09:10:37 +08:00
9252301fa0 Fix save config ini file 2015-05-25 09:10:37 +08:00
1053b63bbc Improve documentation of filter.go. 2015-05-25 09:10:37 +08:00
3bd6caae0a set default timeout 2015-05-25 09:10:37 +08:00
d8734cf58d set default timeout 2015-05-25 09:10:37 +08:00
51161361db more fixed 2015-05-25 09:10:37 +08:00
6da0cdb9e2 no need lock here 2015-05-25 09:10:37 +08:00
9f070c622b no need defer here 2015-05-25 09:10:37 +08:00
738e22e389 zero timeout means wait until resp 2015-05-25 09:10:37 +08:00
69fc22f0df typo fixed 2015-05-25 09:10:37 +08:00
4cd7177ece typo fixed 2015-05-25 09:10:37 +08:00
8e618192c2 better code and fixed 2015-05-25 09:10:37 +08:00
3415a5b091 better go style 2015-05-25 09:10:37 +08:00
dfe055c47c remove useless comments 2015-05-25 09:10:36 +08:00
74b22649c8 remove the double isStruct/isStructPtr check 2015-05-25 09:10:36 +08:00
4255630564 add Recursively validation 2015-05-25 09:10:36 +08:00
7e3b5e5307 remove unreached code 2015-05-25 09:10:36 +08:00
0222b8d693 fixed: when RelatedSel have multi string/relation, it only get last string 2015-05-25 09:10:36 +08:00
23268b788a *feature) 增加logcalldepth接口,使得能简单再次封装log系列函数。 2015-05-25 09:10:36 +08:00
ee4fd60e4d no output the dump 2015-05-25 09:10:36 +08:00
4414659df4 fix the init struct 2015-05-25 09:10:36 +08:00
68ccd8e5a4 fix the dump has no body 2015-05-25 09:10:36 +08:00
6f802b0a05 fix the params 2015-05-25 09:10:36 +08:00
743628a946 add DumpRequest 2015-05-25 09:10:36 +08:00
d90ce15707 httplib support gzip 2015-05-25 09:10:36 +08:00
23457ed2a0 add sethost 2015-05-25 09:10:36 +08:00
d7791ba837 Update validators.go
//176 中国联通
//177 中国电信
//145 中国联通
//147 中国移动
//149 中国电信
2015-05-25 09:10:36 +08:00
676595213f fix a comment error. 2015-05-25 09:10:36 +08:00
d446b5b011 improve the defaultval 2015-05-25 09:10:36 +08:00
2ba12ad1e1 config read and set support Runmode 2015-05-25 09:10:35 +08:00
8cc57e2fc8 fix #1112 2015-05-25 09:10:35 +08:00
322b208566 Update session.go
remove = in if statement
2015-05-25 09:10:35 +08:00
fd610d6777 Update session.go
move expire in line 154 to 247, because it will cause session_cookie not writen to explorer
2015-05-25 09:10:35 +08:00
1148359570 session cookie support IE 2015-05-25 09:10:35 +08:00
55b1d6a897 Update validators.go
edit mobile regular expression ; add 184 Section No.
2015-05-25 09:10:35 +08:00
c4c9a50c42 fix #1081 2015-05-25 09:10:35 +08:00
a311d712a5 Update output.go 2015-05-25 09:10:35 +08:00
bb5351bb9f Update output.go
fix cookie not work in IE
2015-05-25 09:10:35 +08:00
26130a5df6 fix #1073 2015-05-25 09:10:35 +08:00
2c9363d29b add tests to ensure bool value require test always return true. 2015-05-25 09:10:35 +08:00
91886a4547 bugfix: if a form field type is bool, valid Required should always return true instead of return its value. 2015-05-25 09:10:35 +08:00
a7e60c93dc Set ErrorsMap to nil in Validation.Clear function 2015-05-25 09:10:35 +08:00
5b1705b2d6 Do not check log level in writerMsg() because the check is already done outside. 2015-05-25 09:10:35 +08:00
34940d00c0 Remove unnecessary optional group flag '?' since has to match one of comma or end of string 2015-05-25 09:10:35 +08:00
1a6ea693b5 Added to input.go: AcceptHtml, AcceptsXml and AcceptsJson functions which check the header agains a regex for simpler mult-content-type handling. 2015-05-25 09:10:35 +08:00
2e51c124f1 For enhancing performance, check log level before fmt.Sprintf() 2015-05-25 09:10:35 +08:00
1d8afdc9c9 gofmt -s & go_vet 2015-05-25 09:10:34 +08:00
1592e9c04d 验证码reload问题
当页面放置一段时间,验证码将从缓存中失效。当用户再来刷新验证码将出现验证码404。对于reload操作应该直接生成验证码。
2015-05-25 09:10:34 +08:00
bf6b0d3e1f add JsonBody 2015-05-25 09:10:34 +08:00
af71289c25 Merge pull request #1171 from astaxie/add-license
add missed LICENSE of captcha
2015-05-25 09:09:21 +08:00
b1efae6ff8 add missed LICENSE of captcha 2015-05-24 23:59:39 +08:00
92f3de4027 update the string 2015-05-20 11:09:30 +08:00
185089299c sync beeApp.Server to graceful 2015-05-20 11:07:23 +08:00
59c1e74e13 Merge pull request #1158 from mlgd/master
Fix save config ini file
2015-05-18 22:38:59 +08:00
9bb9855153 Let filter function get more params info from ctx.Input.Params 2015-05-18 14:42:18 +08:00
519602a553 fix #1152 2015-05-15 15:04:08 +08:00
740a526105 fix sesseion redis db error 2015-05-13 21:20:50 +08:00
98289cd8de beego suppot graceful restart application 2015-05-13 21:17:47 +08:00
56e2143630 add apk mime 2015-05-09 14:54:30 +08:00
c0cfb5277c Merge pull request #1127 from vanthanh2305/RelatedSel-multi-string/relation
RelatedSel multi string/relation
2015-05-07 23:12:29 +08:00
b29700c3c3 Merge pull request #1131 from JessonChan/develop
httplib more fixed
2015-05-07 22:59:41 +08:00
47be2fadb5 fix #1143 2015-05-05 21:36:31 +08:00
1d72629334 Fix save config ini file 2015-05-04 15:54:03 +02:00
b4880c5e1d Merge pull request #1140 from cr7pt0gr4ph7/doc-pull-request
Improve documentation of filter.go.
2015-05-04 09:47:25 +08:00
c0bb5b9237 Improve documentation of filter.go. 2015-05-03 23:21:32 +02:00
1e1068e81c Merge pull request #1132 from dafang/master
Recursively validations
2015-04-28 11:53:45 +08:00
6c3e274b6e set default timeout 2015-04-26 16:10:18 +08:00
f56bdb6284 set default timeout 2015-04-26 16:08:25 +08:00
d8fa118727 more fixed 2015-04-26 15:42:10 +08:00
0afd04ec6f no need lock here 2015-04-26 15:24:04 +08:00
973306b28d no need defer here 2015-04-26 02:19:38 +08:00
da8c3c3910 zero timeout means wait until resp 2015-04-26 02:17:46 +08:00
0c1bb6409a typo fixed 2015-04-26 02:06:19 +08:00
cddb4fdb60 typo fixed 2015-04-26 02:05:50 +08:00
e1d20aea5d better code and fixed 2015-04-26 02:04:34 +08:00
4498a02c15 better go style 2015-04-26 01:23:18 +08:00
5534258e22 remove useless comments 2015-04-24 11:17:12 +08:00
73650e1f2b remove the double isStruct/isStructPtr check 2015-04-24 11:14:49 +08:00
d0e7dd686b add Recursively validation 2015-04-24 10:58:46 +08:00
dc58ec8316 remove unreached code 2015-04-19 15:40:23 +08:00
6d72fc63ab fixed: when RelatedSel have multi string/relation, it only get last string 2015-04-15 17:41:41 +07:00
dd4afac5dc Merge pull request #1125 from wuranbo/dev_track
*feature) add BeeLogger.GetLogFuncCallDepth make simple wrapper log method aviable.
2015-04-13 12:49:51 +08:00
e229a4762f *feature) 增加logcalldepth接口,使得能简单再次封装log系列函数。 2015-04-13 12:17:44 +08:00
ecb27f34e6 no output the dump 2015-04-09 00:18:02 +08:00
d55997e520 fix the init struct 2015-04-09 00:12:41 +08:00
44c8534477 fix the dump has no body 2015-04-09 00:11:25 +08:00
44a39a6b3e fix the params 2015-04-08 23:00:08 +08:00
cc5ca458ab add DumpRequest 2015-04-08 22:58:37 +08:00
6f6b412709 httplib support gzip 2015-04-08 21:45:00 +08:00
642a69de02 add sethost 2015-04-08 20:12:10 +08:00
466f3c49c1 Merge pull request #1120 from peeped/develop
Update validators.go
2015-04-07 18:48:44 +08:00
d1c9cb2281 Update validators.go
//176 中国联通
//177 中国电信
//145 中国联通
//147 中国移动
//149 中国电信
2015-04-07 17:48:51 +08:00
9c9ffa202a Merge pull request #1119 from lionel0806/develop
fix a comment error.
2015-04-07 11:12:54 +08:00
56dfe418dd fix a comment error. 2015-04-07 10:35:18 +08:00
7caeb91f9b improve the defaultval 2015-04-05 23:23:35 +08:00
47848fa77b config read and set support Runmode 2015-04-05 23:21:13 +08:00
e0e8b98622 fix #1112 2015-04-05 23:12:29 +08:00
5c84ada389 Merge pull request #1106 from peeped/develop
Update validators.go
2015-04-04 20:46:43 +08:00
ac6203b81b Merge pull request #1114 from ElvizLai/patch-2
Patch 2
2015-04-04 13:03:44 +08:00
5e1e618d0f Update session.go
remove = in if statement
2015-04-04 00:44:22 +08:00
a5124a1d45 Update session.go
move expire in line 154 to 247, because it will cause session_cookie not writen to explorer
2015-04-03 17:41:09 +08:00
e675594e46 session cookie support IE 2015-04-02 14:02:39 +08:00
d02e32fa51 Merge pull request #1103 from ElvizLai/patch-1
Update output.go
2015-04-02 13:35:08 +08:00
8a82e25e85 Update validators.go
edit mobile regular expression ; add 184 Section No.
2015-04-02 12:07:12 +08:00
49c0f8906f fix #1081 2015-04-01 23:31:40 +08:00
9261c80509 Update output.go 2015-03-31 12:36:39 +08:00
217e24815b Update output.go
fix cookie not work in IE
2015-03-31 12:30:47 +08:00
840fd3b64f Merge pull request #1083 from supiyun/patch-1
验证码reload问题
2015-03-30 21:40:57 +08:00
eedaea2fea fix #1073 2015-03-30 20:35:57 +08:00
a002f78443 Merge pull request #1097 from pylemon/develop
form required validate for bool field bugfix
2015-03-27 23:04:07 +08:00
cdf9ff401f Merge pull request #1096 from byrnedo/develop
Set ErrorsMap to nil in Validation.Clear function
2015-03-27 14:46:45 +08:00
caa260f053 add tests to ensure bool value require test always return true. 2015-03-27 13:43:20 +08:00
5fa55ca2d3 bugfix: if a form field type is bool, valid Required should always return true instead of return its value. 2015-03-27 13:30:59 +08:00
260b5b1951 Set ErrorsMap to nil in Validation.Clear function 2015-03-26 20:23:00 +01:00
54ae4bc25b Merge pull request #1094 from toalexjin/check_log_level
For enhancing performance, check log level before fmt.Sprintf()
2015-03-26 21:51:23 +08:00
162bee1b43 Merge pull request #1095 from byrnedo/develop
Added AcceptHtml/Json/Xml function to input
2015-03-26 21:50:41 +08:00
533b00ae56 Merge branch 'master' into develop 2015-03-26 08:40:28 +01:00
d3cdebbee2 Do not check log level in writerMsg() because the check is already done outside. 2015-03-26 14:40:12 +08:00
06b5c7f644 Remove unnecessary optional group flag '?' since has to match one of comma or end of string 2015-03-25 14:54:39 +01:00
185ee872c4 Added to input.go: AcceptHtml, AcceptsXml and AcceptsJson functions which check the header agains a regex for simpler mult-content-type handling. 2015-03-25 14:47:20 +01:00
74e0af4a9a For enhancing performance, check log level before fmt.Sprintf() 2015-03-25 15:14:57 +08:00
8aa9455900 gofmt -s & go_vet 2015-03-19 22:29:01 -07:00
2d26f7df2f 验证码reload问题
当页面放置一段时间,验证码将从缓存中失效。当用户再来刷新验证码将出现验证码404。对于reload操作应该直接生成验证码。
2015-03-16 17:40:55 +08:00
3d6408cfc2 Merge pull request #1070 from fugr/patch-5
add JsonBody
2015-03-13 23:16:18 +08:00
223f57bb4c add JsonBody 2015-03-06 14:12:24 +08:00
c4aa33fb1b Merge pull request #1052 from dockercn/develop
Fix the wrong parameter bug in ledis session support cause build failure
2015-03-01 13:58:21 +08:00
20cc5b261e Reform the ledis_session.go 2015-03-01 12:59:34 +08:00
7ec2a077d9 Fix the wrong parameter bug in ledis session. 2015-03-01 12:03:03 +08:00
9f70561f21 Merge remote-tracking branch 'upstream/master' into develop 2015-03-01 11:47:02 +08:00
1c9898dee5 Merge branch 'develop' 2015-02-27 22:52:42 +08:00
2cf7c6a58a change the jQuery URL 2015-02-27 22:51:20 +08:00
2cee46ab2b change the jQuery URL 2015-02-27 22:50:25 +08:00
020bfbcc9c Merge branch 'develop' 2015-02-27 22:47:59 +08:00
3f8252bffd change the version from 1.4.2 to 1.4.3 2015-02-27 22:47:21 +08:00
6d313aa15f fix #985 2015-02-27 22:37:41 +08:00
2a4e2d4a71 delete the group route, because we already has namespace 2015-02-27 22:37:07 +08:00
f96a6285bf fix #978 2015-02-27 22:21:58 +08:00
e938876c4a fix the cycle import 2015-02-27 00:12:10 +08:00
6e9d2dc965 add more error functions 2015-02-26 23:49:24 +08:00
3aceaf8838 error support controller 2015-02-26 23:34:43 +08:00
71b9854f48 Merge pull request #1044 from fuxiaohei/develop
code style simplify
2015-02-23 22:25:34 +08:00
f59ccd3a35 Merge pull request #1043 from Hepri/develop
Added support to parse slices of ints and strings in ParseForm func
2015-02-23 21:58:32 +08:00
181a7c35fe code simplify for package middleware 2015-02-23 11:50:45 +08:00
2ed272aeb2 code simplify for package middleware 2015-02-23 11:50:13 +08:00
77c1109134 code simplify for package logs 2015-02-23 11:42:46 +08:00
29d4823866 code simplify for package httplib 2015-02-23 11:30:59 +08:00
24cf06d288 code style simplify for context package 2015-02-23 11:15:55 +08:00
0c31c2d689 Added support to parse slices of ints and strings in ParseForm func 2015-02-22 22:13:06 +05:00
f988f035e5 redis provider for session and cache support select db 2015-02-16 21:56:32 +08:00
1b4158c15b Merge pull request #1039 from astaxie/revert-1000-group_by_queryseter
Revert "Add GroupBy to QuerySeter"
2015-02-14 20:40:52 +08:00
433e8f2ce3 Revert "Add GroupBy to QuerySeter" 2015-02-14 20:40:43 +08:00
22ba7fdce4 Merge pull request #1000 from pdf/group_by_queryseter
Add GroupBy to QuerySeter
2015-02-14 20:33:06 +08:00
2a0f87e810 Merge pull request #1010 from BlackLee/master
add compare_not/not_nil methods for template
2015-02-14 20:31:18 +08:00
19db4b67f6 Merge pull request #1025 from kongjian/develop
Update task tpl
2015-02-14 20:29:28 +08:00
b1baf4503d beego task list update for task spec list and task run url error 2015-02-04 18:07:31 +08:00
d536f5b8dc Merge pull request #1021 from kmulvey/readme-typo
typos in the readme
2015-02-03 09:07:00 +08:00
1cc1d57f55 development 2015-02-02 09:33:59 -05:00
73370ade90 modular 2015-02-02 09:33:27 -05:00
0d3b7dcd07 Merge pull request #911 from supar/add-column-default-attribute
Add attribute default to the column on create or alter commands. Skip co...
2015-01-16 10:40:11 -08:00
d7fe5ef435 Merge pull request #1004 from fugr/patch-2
Transaction
2015-01-13 11:19:03 -08:00
8bd902814f Transaction
err处理写反了
2015-01-13 11:09:43 +08:00
378356a65e Merge pull request #1001 from johndeng/develop
Fixed the status code issue at error handler.
2015-01-12 09:54:26 -08:00
30871e2617 Fixed the status issue at error handler. 2015-01-10 17:35:35 +08:00
3731088b4a Add GroupBy to QuerySeter
Adds support for GroupBy to QuerySeter SELECT operations.
2015-01-10 15:26:41 +11:00
d46833c6d8 Merge pull request #997 from dockercn/master
增加session模块中的ledisdb的动态配置
2015-01-09 13:58:13 +08:00
18659e16ba add compare_not/not_nil methods for template 2015-01-05 16:38:57 +08:00
5d8187d005 Merge pull request #977 from athurg/patch-1
Fix RequestURI nil caused template parse failed
2014-12-25 11:40:26 +08:00
d961ae4cd8 Fix RequestURI nil caused template parse failed
Sometime RequestURI is not set, e.g. running after a front proxy server.

We should always follow the document's directive, to use Request.URL instead of RequestURI.

Refer: http://golang.org/pkg/net/http/#Request
2014-12-25 11:23:04 +08:00
0e1a0049d1 Merge pull request #971 from athurg/get_request_params_with_default
Support default value for controller’s params get
2014-12-19 16:03:10 +08:00
0c933643e2 improve the empty router 2014-12-19 15:33:51 +08:00
d2c5daa5ee Update comments for controller's GetXXX functions 2014-12-19 15:28:18 +08:00
d3ab157915 fix the cache test 2014-12-19 14:40:16 +08:00
75d28d49c5 Merge pull request #965 from shuoli84/develop
Fix subdomain, add test, space and comment fix
2014-12-19 13:22:36 +08:00
76bb4827d0 Merge pull request #953 from kristen1980/patch-2
Allow absolute path for filesystem cache
2014-12-18 21:24:17 +08:00
3caba06189 Merge pull request #967 from athurg/support_all_type_on_urlfor
Add all type support for UrlFor’s params
2014-12-18 21:14:44 +08:00
572508ddd8 Clean json config. Fix DefaultStrings 2014-12-17 17:02:46 +08:00
e34f8479bb Add all type support for UrlFor’s params 2014-12-17 15:52:48 +08:00
daf85f06f8 Support default value for controller’s params get 2014-12-17 15:23:11 +08:00
22671c524e Fix subdomain, add test, space and comment fix 2014-12-17 12:06:53 +08:00
ab99d5f1e2 Merge pull request #957 from athurg/patch-2
Fix paginator attributes cannot be modified bug
2014-12-11 19:59:50 +08:00
c52f634d9c Fix paginator attributes cannot be modified bug
We can only use SetPaginator to create a pagination.

After that, we always need to modify something, like the totalNum, perPageNum.

These change should be seen in the view.

So we should give the view a pointer than a object.
2014-12-11 16:42:50 +08:00
9c665afc04 improve the error tips 2014-12-08 14:57:45 +08:00
77ed151243 Allow absolute path for filesystem cache
Gives more flexibility by making it an absolute path. A relative path can easily be created by the user.
2014-12-07 10:00:35 -07:00
29d98731c6 add sess_ledis select db config 2014-11-25 14:41:51 -08:00
934dd2e8d2 Merge branch 'master' of https://github.com/astaxie/beego 2014-11-25 14:27:13 -08:00
e65d87974a Merge pull request #940 from hilyjiang/develop
make Content-Type header more human-readable
2014-11-24 23:26:15 +08:00
647e6ae1c4 Added JWT plugin 2014-11-24 23:21:03 +08:00
db04c3cbb4 make Content-Type header more human-readable 2014-11-24 23:12:09 +08:00
802aa16136 Merge pull request #935 from mnhkahn/master
beego1.4.2,beego.AppConfig.Strings与老版本代码不兼容问题
2014-11-24 21:47:12 +08:00
f2df07f630 Merge pull request #933 from rbastic/develop
Reword message about reloading packages..
2014-11-24 13:18:05 +08:00
dc89f844f3 Reword message about reloading packages.. 2014-11-23 18:22:45 +01:00
b80cdef20f Merge pull request #932 from DeanThompson/master
count log file lines
2014-11-23 23:57:27 +08:00
98dcee0643 Merge pull request #926 from xuewuhen/master
SubDomains function bugfixed
2014-11-23 22:57:40 +08:00
0ad75cb5fa Merge pull request #928 from lei-cao/develop
Return the response directly if it's a options PreflightHeader request
2014-11-21 23:22:59 +08:00
6a9d04c269 count log file lines 2014-11-21 18:12:39 +08:00
93ca11f83d Return the response directly if it's a options PreflightHeader request 2014-11-21 01:35:30 +08:00
git
d0b43ef4f5 beego.AppConfig.Strings bug 2014-11-20 16:35:04 +08:00
c9bb9d6a09 SubDomains function bugfixed 2014-11-18 22:54:48 +08:00
f96245786a fix #912 2014-11-08 15:10:47 +08:00
1a1b0c14b9 Add attribute default to the column on create or alter commands. Skip columns which are keys and date or time data type 2014-11-06 17:43:53 +03:00
168 changed files with 9941 additions and 6183 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
.DS_Store .DS_Store
*.swp *.swp
*.swo *.swo
beego.iml

51
.travis.yml Normal file
View File

@ -0,0 +1,51 @@
language: go
go:
- tip
- 1.6.0
- 1.5.3
- 1.4.3
services:
- redis-server
- mysql
- postgresql
- memcached
env:
- ORM_DRIVER=sqlite3 ORM_SOURCE=$TRAVIS_BUILD_DIR/orm_test.db
- ORM_DRIVER=mysql ORM_SOURCE="root:@/orm_test?charset=utf8"
- ORM_DRIVER=postgres ORM_SOURCE="user=postgres dbname=orm_test sslmode=disable"
before_install:
- git clone git://github.com/ideawu/ssdb.git
- cd ssdb
- make
- cd ..
install:
- go get github.com/lib/pq
- go get github.com/go-sql-driver/mysql
- go get github.com/mattn/go-sqlite3
- go get github.com/bradfitz/gomemcache/memcache
- go get github.com/garyburd/redigo/redis
- go get github.com/beego/x2j
- go get github.com/couchbase/go-couchbase
- go get github.com/beego/goyaml2
- go get github.com/belogik/goes
- go get github.com/siddontang/ledisdb/config
- go get github.com/siddontang/ledisdb/ledis
- go get golang.org/x/tools/cmd/vet
- go get github.com/golang/lint/golint
- go get github.com/ssdb/gossdb/ssdb
before_script:
- sh -c "if [ '$ORM_DRIVER' = 'postgres' ]; then psql -c 'create database orm_test;' -U postgres; fi"
- sh -c "if [ '$ORM_DRIVER' = 'mysql' ]; then mysql -u root -e 'create database orm_test;'; fi"
- sh -c "if [ '$ORM_DRIVER' = 'sqlite' ]; then touch $TRAVIS_BUILD_DIR/orm_test.db; fi"
- mkdir -p res/var
- ./ssdb/ssdb-server ./ssdb/ssdb.conf -d
after_script:
-killall -w ssdb-server
- rm -rf ./res/var/*
script:
- go vet -x ./...
- $HOME/gopath/bin/golint ./...
- go test -v ./...
notifications:
webhooks: https://hooks.pubu.im/services/z7m9bvybl3rgtg9

52
CONTRIBUTING.md Normal file
View File

@ -0,0 +1,52 @@
# Contributing to beego
beego is an open source project.
It is the work of hundreds of contributors. We appreciate your help!
Here are instructions to get you started. They are probably not perfect,
please let us know if anything feels wrong or incomplete.
## Contribution guidelines
### Pull requests
First of all. beego follow the gitflow. So please send you pull request
to **develop** branch. We will close the pull request to master branch.
We are always happy to receive pull requests, and do our best to
review them as fast as possible. Not sure if that typo is worth a pull
request? Do it! We will appreciate it.
If your pull request is not accepted on the first try, don't be
discouraged! Sometimes we can make a mistake, please do more explaining
for us. We will appreciate it.
We're trying very hard to keep beego simple and fast. We don't want it
to do everything for everybody. This means that we might decide against
incorporating a new feature. But we will give you some advice on how to
do it in other way.
### Create issues
Any significant improvement should be documented as [a GitHub
issue](https://github.com/astaxie/beego/issues) before anybody
starts working on it.
Also when filing an issue, make sure to answer these five questions:
- What version of beego are you using (bee version)?
- What operating system and processor architecture are you using?
- What did you do?
- What did you expect to see?
- What did you see instead?
### but check existing issues and docs first!
Please take a moment to check that an issue doesn't already exist
documenting your bug report or improvement proposal. If it does, it
never hurts to add a quick "+1" or "I have this problem too". This will
help prioritize the most common problems and requests.
Also if you don't know how to use it. please make sure you have read though
the docs in http://beego.me/docs

View File

@ -1,16 +1,38 @@
## Beego ## Beego
[![Build Status](https://drone.io/github.com/astaxie/beego/status.png)](https://drone.io/github.com/astaxie/beego/latest) [![Build Status](https://travis-ci.org/astaxie/beego.svg?branch=master)](https://travis-ci.org/astaxie/beego)
[![GoDoc](http://godoc.org/github.com/astaxie/beego?status.svg)](http://godoc.org/github.com/astaxie/beego) [![GoDoc](http://godoc.org/github.com/astaxie/beego?status.svg)](http://godoc.org/github.com/astaxie/beego)
beego is an open-source, high-performance, modularity, full-stack web framework. 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.
More info [beego.me](http://beego.me) More info [beego.me](http://beego.me)
## Installation ##Quick Start
######Download and install
go get github.com/astaxie/beego go get github.com/astaxie/beego
######Create file `hello.go`
```go
package main
import "github.com/astaxie/beego"
func main(){
beego.Run()
}
```
######Build and run
```bash
go build hello.go
./hello
```
######Congratulations!
You just built your first beego app.
Open your browser and visit `http://localhost:8000`.
Please see [Documentation](http://beego.me/docs) for more.
## Features ## Features
* RESTful support * RESTful support
@ -19,19 +41,21 @@ More info [beego.me](http://beego.me)
* Auto API documents * Auto API documents
* Annotation router * Annotation router
* Namespace * Namespace
* Powerful develop tools * Powerful development tools
* Full stack for Web & API * Full stack for Web & API
## Documentation ## Documentation
* [English](http://beego.me/docs/intro/) * [English](http://beego.me/docs/intro/)
* [中文文档](http://beego.me/docs/intro/) * [中文文档](http://beego.me/docs/intro/)
* [Русский](http://beego.me/docs/intro/)
## Community ## Community
* [http://beego.me/community](http://beego.me/community) * [http://beego.me/community](http://beego.me/community)
* Welcome to join us in Slack: [https://beego.slack.com](https://beego.slack.com), you can get invited from [here](https://github.com/beego/beedoc/issues/232)
## LICENSE ## LICENSE
beego is licensed under the Apache Licence, Version 2.0 beego source code is licensed under the Apache Licence, Version 2.0
(http://www.apache.org/licenses/LICENSE-2.0.html). (http://www.apache.org/licenses/LICENSE-2.0.html).

442
admin.go
View File

@ -19,9 +19,11 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"os"
"text/template" "text/template"
"time" "time"
"github.com/astaxie/beego/grace"
"github.com/astaxie/beego/toolbox" "github.com/astaxie/beego/toolbox"
"github.com/astaxie/beego/utils" "github.com/astaxie/beego/utils"
) )
@ -63,24 +65,15 @@ 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, r *http.Request) {
tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl)) execTpl(rw, map[interface{}]interface{}{}, indexTpl, defaultScriptsTpl)
tmpl = template.Must(tmpl.Parse(indexTpl))
tmpl = template.Must(tmpl.Parse(defaultScriptsTpl))
data := make(map[interface{}]interface{})
tmpl.Execute(rw, data)
} }
// QpsIndex is the http.Handler for writing qbs statistics map result info in http.ResponseWriter. // QpsIndex is the http.Handler for writing qbs statistics map result info in http.ResponseWriter.
// it's registered with url pattern "/qbs" in admin module. // it's registered with url pattern "/qbs" in admin module.
func qpsIndex(rw http.ResponseWriter, r *http.Request) { func qpsIndex(rw http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl))
tmpl = template.Must(tmpl.Parse(qpsTpl))
tmpl = template.Must(tmpl.Parse(defaultScriptsTpl))
data := make(map[interface{}]interface{}) data := make(map[interface{}]interface{})
data["Content"] = toolbox.StatisticsMap.GetMap() data["Content"] = toolbox.StatisticsMap.GetMap()
execTpl(rw, data, qpsTpl, defaultScriptsTpl)
tmpl.Execute(rw, data)
} }
// ListConf is the http.Handler of displaying all beego configuration values as key/value pair. // ListConf is the http.Handler of displaying all beego configuration values as key/value pair.
@ -88,178 +81,145 @@ func qpsIndex(rw http.ResponseWriter, r *http.Request) {
func listConf(rw http.ResponseWriter, r *http.Request) { func listConf(rw http.ResponseWriter, r *http.Request) {
r.ParseForm() r.ParseForm()
command := r.Form.Get("command") command := r.Form.Get("command")
if command != "" { if command == "" {
data := make(map[interface{}]interface{}) rw.Write([]byte("command not support"))
switch command { return
case "conf": }
m := make(map[string]interface{})
m["AppName"] = AppName data := make(map[interface{}]interface{})
m["AppPath"] = AppPath switch command {
m["AppConfigPath"] = AppConfigPath case "conf":
m["StaticDir"] = StaticDir m := make(map[string]interface{})
m["StaticExtensionsToGzip"] = StaticExtensionsToGzip m["AppConfigPath"] = appConfigPath
m["HttpAddr"] = HttpAddr m["AppConfigProvider"] = appConfigProvider
m["HttpPort"] = HttpPort m["BConfig.AppName"] = BConfig.AppName
m["HttpTLS"] = EnableHttpTLS m["BConfig.RunMode"] = BConfig.RunMode
m["HttpCertFile"] = HttpCertFile m["BConfig.RouterCaseSensitive"] = BConfig.RouterCaseSensitive
m["HttpKeyFile"] = HttpKeyFile m["BConfig.ServerName"] = BConfig.ServerName
m["RecoverPanic"] = RecoverPanic m["BConfig.RecoverPanic"] = BConfig.RecoverPanic
m["AutoRender"] = AutoRender m["BConfig.CopyRequestBody"] = BConfig.CopyRequestBody
m["ViewsPath"] = ViewsPath m["BConfig.EnableGzip"] = BConfig.EnableGzip
m["RunMode"] = RunMode m["BConfig.MaxMemory"] = BConfig.MaxMemory
m["SessionOn"] = SessionOn m["BConfig.EnableErrorsShow"] = BConfig.EnableErrorsShow
m["SessionProvider"] = SessionProvider m["BConfig.Listen.Graceful"] = BConfig.Listen.Graceful
m["SessionName"] = SessionName m["BConfig.Listen.ServerTimeOut"] = BConfig.Listen.ServerTimeOut
m["SessionGCMaxLifetime"] = SessionGCMaxLifetime m["BConfig.Listen.ListenTCP4"] = BConfig.Listen.ListenTCP4
m["SessionSavePath"] = SessionSavePath m["BConfig.Listen.EnableHTTP"] = BConfig.Listen.EnableHTTP
m["SessionCookieLifeTime"] = SessionCookieLifeTime m["BConfig.Listen.HTTPAddr"] = BConfig.Listen.HTTPAddr
m["UseFcgi"] = UseFcgi m["BConfig.Listen.HTTPPort"] = BConfig.Listen.HTTPPort
m["MaxMemory"] = MaxMemory m["BConfig.Listen.EnableHTTPS"] = BConfig.Listen.EnableHTTPS
m["EnableGzip"] = EnableGzip m["BConfig.Listen.HTTPSAddr"] = BConfig.Listen.HTTPSAddr
m["DirectoryIndex"] = DirectoryIndex m["BConfig.Listen.HTTPSPort"] = BConfig.Listen.HTTPSPort
m["HttpServerTimeOut"] = HttpServerTimeOut m["BConfig.Listen.HTTPSCertFile"] = BConfig.Listen.HTTPSCertFile
m["ErrorsShow"] = ErrorsShow m["BConfig.Listen.HTTPSKeyFile"] = BConfig.Listen.HTTPSKeyFile
m["XSRFKEY"] = XSRFKEY m["BConfig.Listen.EnableAdmin"] = BConfig.Listen.EnableAdmin
m["EnableXSRF"] = EnableXSRF m["BConfig.Listen.AdminAddr"] = BConfig.Listen.AdminAddr
m["XSRFExpire"] = XSRFExpire m["BConfig.Listen.AdminPort"] = BConfig.Listen.AdminPort
m["CopyRequestBody"] = CopyRequestBody m["BConfig.Listen.EnableFcgi"] = BConfig.Listen.EnableFcgi
m["TemplateLeft"] = TemplateLeft m["BConfig.Listen.EnableStdIo"] = BConfig.Listen.EnableStdIo
m["TemplateRight"] = TemplateRight m["BConfig.WebConfig.AutoRender"] = BConfig.WebConfig.AutoRender
m["BeegoServerName"] = BeegoServerName m["BConfig.WebConfig.EnableDocs"] = BConfig.WebConfig.EnableDocs
m["EnableAdmin"] = EnableAdmin m["BConfig.WebConfig.FlashName"] = BConfig.WebConfig.FlashName
m["AdminHttpAddr"] = AdminHttpAddr m["BConfig.WebConfig.FlashSeparator"] = BConfig.WebConfig.FlashSeparator
m["AdminHttpPort"] = AdminHttpPort m["BConfig.WebConfig.DirectoryIndex"] = BConfig.WebConfig.DirectoryIndex
m["BConfig.WebConfig.StaticDir"] = BConfig.WebConfig.StaticDir
m["BConfig.WebConfig.StaticExtensionsToGzip"] = BConfig.WebConfig.StaticExtensionsToGzip
m["BConfig.WebConfig.TemplateLeft"] = BConfig.WebConfig.TemplateLeft
m["BConfig.WebConfig.TemplateRight"] = BConfig.WebConfig.TemplateRight
m["BConfig.WebConfig.ViewsPath"] = BConfig.WebConfig.ViewsPath
m["BConfig.WebConfig.EnableXSRF"] = BConfig.WebConfig.EnableXSRF
m["BConfig.WebConfig.XSRFKEY"] = BConfig.WebConfig.XSRFKey
m["BConfig.WebConfig.XSRFExpire"] = BConfig.WebConfig.XSRFExpire
m["BConfig.WebConfig.Session.SessionOn"] = BConfig.WebConfig.Session.SessionOn
m["BConfig.WebConfig.Session.SessionProvider"] = BConfig.WebConfig.Session.SessionProvider
m["BConfig.WebConfig.Session.SessionName"] = BConfig.WebConfig.Session.SessionName
m["BConfig.WebConfig.Session.SessionGCMaxLifetime"] = BConfig.WebConfig.Session.SessionGCMaxLifetime
m["BConfig.WebConfig.Session.SessionProviderConfig"] = BConfig.WebConfig.Session.SessionProviderConfig
m["BConfig.WebConfig.Session.SessionCookieLifeTime"] = BConfig.WebConfig.Session.SessionCookieLifeTime
m["BConfig.WebConfig.Session.SessionAutoSetCookie"] = BConfig.WebConfig.Session.SessionAutoSetCookie
m["BConfig.WebConfig.Session.SessionDomain"] = BConfig.WebConfig.Session.SessionDomain
m["BConfig.Log.AccessLogs"] = BConfig.Log.AccessLogs
m["BConfig.Log.FileLineNum"] = BConfig.Log.FileLineNum
m["BConfig.Log.Outputs"] = BConfig.Log.Outputs
tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl))
tmpl = template.Must(tmpl.Parse(configTpl))
tmpl = template.Must(tmpl.Parse(defaultScriptsTpl))
tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl)) data["Content"] = m
tmpl = template.Must(tmpl.Parse(configTpl))
tmpl = template.Must(tmpl.Parse(defaultScriptsTpl))
data["Content"] = m tmpl.Execute(rw, data)
tmpl.Execute(rw, data) case "router":
var (
case "router": content = map[string]interface{}{
content := make(map[string]interface{}) "Fields": []string{
"Router Pattern",
var fields = []string{ "Methods",
fmt.Sprintf("Router Pattern"), "Controller",
fmt.Sprintf("Methods"), },
fmt.Sprintf("Controller"),
} }
content["Fields"] = fields methods = []string{}
methodsData = make(map[string]interface{})
)
for method, t := range BeeApp.Handlers.routers {
methods := []string{} resultList := new([][]string)
methodsData := make(map[string]interface{})
for method, t := range BeeApp.Handlers.routers {
resultList := new([][]string) printTree(resultList, t)
printTree(resultList, t) methods = append(methods, method)
methodsData[method] = resultList
methods = append(methods, method)
methodsData[method] = resultList
}
content["Data"] = methodsData
content["Methods"] = methods
data["Content"] = content
data["Title"] = "Routers"
tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl))
tmpl = template.Must(tmpl.Parse(routerAndFilterTpl))
tmpl = template.Must(tmpl.Parse(defaultScriptsTpl))
tmpl.Execute(rw, data)
case "filter":
content := make(map[string]interface{})
var fields = []string{
fmt.Sprintf("Router Pattern"),
fmt.Sprintf("Filter Function"),
}
content["Fields"] = fields
filterTypes := []string{}
filterTypeData := make(map[string]interface{})
if BeeApp.Handlers.enableFilter {
var filterType string
if bf, ok := BeeApp.Handlers.filters[BeforeRouter]; ok {
filterType = "Before Router"
filterTypes = append(filterTypes, filterType)
resultList := new([][]string)
for _, f := range bf {
var result = []string{
fmt.Sprintf("%s", f.pattern),
fmt.Sprintf("%s", utils.GetFuncName(f.filterFunc)),
}
*resultList = append(*resultList, result)
}
filterTypeData[filterType] = resultList
}
if bf, ok := BeeApp.Handlers.filters[BeforeExec]; ok {
filterType = "Before Exec"
filterTypes = append(filterTypes, filterType)
resultList := new([][]string)
for _, f := range bf {
var result = []string{
fmt.Sprintf("%s", f.pattern),
fmt.Sprintf("%s", utils.GetFuncName(f.filterFunc)),
}
*resultList = append(*resultList, result)
}
filterTypeData[filterType] = resultList
}
if bf, ok := BeeApp.Handlers.filters[AfterExec]; ok {
filterType = "After Exec"
filterTypes = append(filterTypes, filterType)
resultList := new([][]string)
for _, f := range bf {
var result = []string{
fmt.Sprintf("%s", f.pattern),
fmt.Sprintf("%s", utils.GetFuncName(f.filterFunc)),
}
*resultList = append(*resultList, result)
}
filterTypeData[filterType] = resultList
}
if bf, ok := BeeApp.Handlers.filters[FinishRouter]; ok {
filterType = "Finish Router"
filterTypes = append(filterTypes, filterType)
resultList := new([][]string)
for _, f := range bf {
var result = []string{
fmt.Sprintf("%s", f.pattern),
fmt.Sprintf("%s", utils.GetFuncName(f.filterFunc)),
}
*resultList = append(*resultList, result)
}
filterTypeData[filterType] = resultList
}
}
content["Data"] = filterTypeData
content["Methods"] = filterTypes
data["Content"] = content
data["Title"] = "Filters"
tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl))
tmpl = template.Must(tmpl.Parse(routerAndFilterTpl))
tmpl = template.Must(tmpl.Parse(defaultScriptsTpl))
tmpl.Execute(rw, data)
default:
rw.Write([]byte("command not support"))
} }
} else {
content["Data"] = methodsData
content["Methods"] = methods
data["Content"] = content
data["Title"] = "Routers"
execTpl(rw, data, routerAndFilterTpl, defaultScriptsTpl)
case "filter":
var (
content = map[string]interface{}{
"Fields": []string{
"Router Pattern",
"Filter Function",
},
}
filterTypes = []string{}
filterTypeData = make(map[string]interface{})
)
if BeeApp.Handlers.enableFilter {
var filterType string
for k, fr := range map[int]string{
BeforeStatic: "Before Static",
BeforeRouter: "Before Router",
BeforeExec: "Before Exec",
AfterExec: "After Exec",
FinishRouter: "Finish Router"} {
if bf, ok := BeeApp.Handlers.filters[k]; ok {
filterType = fr
filterTypes = append(filterTypes, filterType)
resultList := new([][]string)
for _, f := range bf {
var result = []string{
fmt.Sprintf("%s", f.pattern),
fmt.Sprintf("%s", utils.GetFuncName(f.filterFunc)),
}
*resultList = append(*resultList, result)
}
filterTypeData[filterType] = resultList
}
}
}
content["Data"] = filterTypeData
content["Methods"] = filterTypes
data["Content"] = content
data["Title"] = "Filters"
execTpl(rw, data, routerAndFilterTpl, defaultScriptsTpl)
default:
rw.Write([]byte("command not support"))
} }
} }
@ -274,23 +234,23 @@ func printTree(resultList *[][]string, t *Tree) {
if v, ok := l.runObject.(*controllerInfo); ok { if v, ok := l.runObject.(*controllerInfo); ok {
if v.routerType == routerTypeBeego { if v.routerType == routerTypeBeego {
var result = []string{ var result = []string{
fmt.Sprintf("%s", v.pattern), v.pattern,
fmt.Sprintf("%s", v.methods), fmt.Sprintf("%s", v.methods),
fmt.Sprintf("%s", v.controllerType), fmt.Sprintf("%s", v.controllerType),
} }
*resultList = append(*resultList, result) *resultList = append(*resultList, result)
} else if v.routerType == routerTypeRESTFul { } else if v.routerType == routerTypeRESTFul {
var result = []string{ var result = []string{
fmt.Sprintf("%s", v.pattern), v.pattern,
fmt.Sprintf("%s", v.methods), fmt.Sprintf("%s", v.methods),
fmt.Sprintf(""), "",
} }
*resultList = append(*resultList, result) *resultList = append(*resultList, result)
} else if v.routerType == routerTypeHandler { } else if v.routerType == routerTypeHandler {
var result = []string{ var result = []string{
fmt.Sprintf("%s", v.pattern), v.pattern,
fmt.Sprintf(""), "",
fmt.Sprintf(""), "",
} }
*resultList = append(*resultList, result) *resultList = append(*resultList, result)
} }
@ -303,54 +263,49 @@ func printTree(resultList *[][]string, t *Tree) {
func profIndex(rw http.ResponseWriter, r *http.Request) { func profIndex(rw http.ResponseWriter, r *http.Request) {
r.ParseForm() r.ParseForm()
command := r.Form.Get("command") command := r.Form.Get("command")
format := r.Form.Get("format") if command == "" {
data := make(map[string]interface{}) return
}
var result bytes.Buffer var (
if command != "" { format = r.Form.Get("format")
toolbox.ProcessInput(command, &result) data = make(map[interface{}]interface{})
data["Content"] = result.String() result bytes.Buffer
)
toolbox.ProcessInput(command, &result)
data["Content"] = result.String()
if format == "json" && command == "gc summary" { if format == "json" && command == "gc summary" {
dataJson, err := json.Marshal(data) dataJSON, err := json.Marshal(data)
if err != nil { if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
rw.Header().Set("Content-Type", "application/json")
rw.Write(dataJson)
return return
} }
data["Title"] = command rw.Header().Set("Content-Type", "application/json")
tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl)) rw.Write(dataJSON)
tmpl = template.Must(tmpl.Parse(profillingTpl)) return
if command == "gc summary" {
tmpl = template.Must(tmpl.Parse(gcAjaxTpl))
} else {
tmpl = template.Must(tmpl.Parse(defaultScriptsTpl))
}
tmpl.Execute(rw, data)
} else {
} }
data["Title"] = command
defaultTpl := defaultScriptsTpl
if command == "gc summary" {
defaultTpl = gcAjaxTpl
}
execTpl(rw, data, profillingTpl, defaultTpl)
} }
// 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, req *http.Request) {
data := make(map[interface{}]interface{}) var (
data = make(map[interface{}]interface{})
var result = []string{} result = []string{}
fields := []string{ resultList = new([][]string)
fmt.Sprintf("Name"), content = map[string]interface{}{
fmt.Sprintf("Message"), "Fields": []string{"Name", "Message", "Status"},
fmt.Sprintf("Status"), }
} )
resultList := new([][]string)
content := make(map[string]interface{})
for name, h := range toolbox.AdminCheckList { for name, h := range toolbox.AdminCheckList {
if err := h.Check(); err != nil { if err := h.Check(); err != nil {
@ -370,16 +325,10 @@ func healthcheck(rw http.ResponseWriter, req *http.Request) {
} }
*resultList = append(*resultList, result) *resultList = append(*resultList, result)
} }
content["Fields"] = fields
content["Data"] = resultList content["Data"] = resultList
data["Content"] = content data["Content"] = content
data["Title"] = "Health Check" data["Title"] = "Health Check"
tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl)) execTpl(rw, data, healthCheckTpl, defaultScriptsTpl)
tmpl = template.Must(tmpl.Parse(healthCheckTpl))
tmpl = template.Must(tmpl.Parse(defaultScriptsTpl))
tmpl.Execute(rw, data)
} }
// TaskStatus is a http.Handler with running task status (task name, status and the last execution). // TaskStatus is a http.Handler with running task status (task name, status and the last execution).
@ -391,13 +340,11 @@ func taskStatus(rw http.ResponseWriter, req *http.Request) {
req.ParseForm() req.ParseForm()
taskname := req.Form.Get("taskname") taskname := req.Form.Get("taskname")
if taskname != "" { if taskname != "" {
if t, ok := toolbox.AdminTaskList[taskname]; ok { if t, ok := toolbox.AdminTaskList[taskname]; ok {
err := t.Run() if err := t.Run(); err != nil {
if err != nil {
data["Message"] = []string{"error", fmt.Sprintf("%s", err)} data["Message"] = []string{"error", fmt.Sprintf("%s", err)}
} }
data["Message"] = []string{"success", fmt.Sprintf("%s run success,Now the Status is %s", taskname, t.GetStatus())} data["Message"] = []string{"success", fmt.Sprintf("%s run success,Now the Status is <br>%s", taskname, t.GetStatus())}
} else { } else {
data["Message"] = []string{"warning", fmt.Sprintf("there's no task which named: %s", taskname)} data["Message"] = []string{"warning", fmt.Sprintf("there's no task which named: %s", taskname)}
} }
@ -408,16 +355,18 @@ func taskStatus(rw http.ResponseWriter, req *http.Request) {
resultList := new([][]string) resultList := new([][]string)
var result = []string{} var result = []string{}
var fields = []string{ var fields = []string{
fmt.Sprintf("Task Name"), "Task Name",
fmt.Sprintf("Task Spec"), "Task Spec",
fmt.Sprintf("Task Function"), "Task Status",
fmt.Sprintf(""), "Last Time",
"",
} }
for tname, tk := range toolbox.AdminTaskList { for tname, tk := range toolbox.AdminTaskList {
result = []string{ result = []string{
fmt.Sprintf("%s", tname), tname,
fmt.Sprintf("%s", tk.GetSpec()),
fmt.Sprintf("%s", tk.GetStatus()), fmt.Sprintf("%s", tk.GetStatus()),
fmt.Sprintf("%s", tk.GetPrev().String()), tk.GetPrev().String(),
} }
*resultList = append(*resultList, result) *resultList = append(*resultList, result)
} }
@ -426,9 +375,14 @@ func taskStatus(rw http.ResponseWriter, req *http.Request) {
content["Data"] = resultList content["Data"] = resultList
data["Content"] = content data["Content"] = content
data["Title"] = "Tasks" data["Title"] = "Tasks"
execTpl(rw, data, tasksTpl, defaultScriptsTpl)
}
func execTpl(rw http.ResponseWriter, data map[interface{}]interface{}, tpls ...string) {
tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl)) tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl))
tmpl = template.Must(tmpl.Parse(tasksTpl)) for _, tpl := range tpls {
tmpl = template.Must(tmpl.Parse(defaultScriptsTpl)) tmpl = template.Must(tmpl.Parse(tpl))
}
tmpl.Execute(rw, data) tmpl.Execute(rw, data)
} }
@ -448,17 +402,23 @@ func (admin *adminApp) Run() {
if len(toolbox.AdminTaskList) > 0 { if len(toolbox.AdminTaskList) > 0 {
toolbox.StartTask() toolbox.StartTask()
} }
addr := AdminHttpAddr addr := BConfig.Listen.AdminAddr
if AdminHttpPort != 0 { if BConfig.Listen.AdminPort != 0 {
addr = fmt.Sprintf("%s:%d", AdminHttpAddr, AdminHttpPort) addr = fmt.Sprintf("%s:%d", BConfig.Listen.AdminAddr, BConfig.Listen.AdminPort)
} }
for p, f := range admin.routers { for p, f := range admin.routers {
http.Handle(p, f) http.Handle(p, f)
} }
BeeLogger.Info("Admin server Running on %s", addr) BeeLogger.Info("Admin server Running on %s", addr)
err := http.ListenAndServe(addr, nil)
var err error
if BConfig.Listen.Graceful {
err = grace.ListenAndServe(addr, nil)
} else {
err = http.ListenAndServe(addr, nil)
}
if err != nil { if err != nil {
BeeLogger.Critical("Admin ListenAndServe: ", err) BeeLogger.Critical("Admin ListenAndServe: ", err, fmt.Sprintf("%d", os.Getpid()))
} }
} }

View File

@ -186,7 +186,7 @@ bg-warning
</td> </td>
{{end}} {{end}}
<td> <td>
<a class="btn btn-primary btn-sm" href="/task?taskname={{index $slice 1}}">Run</a> <a class="btn btn-primary btn-sm" href="/task?taskname={{index $slice 0}}">Run</a>
</td> </td>
</tr> </tr>
{{end}} {{end}}

357
app.go
View File

@ -19,12 +19,27 @@ import (
"net" "net"
"net/http" "net/http"
"net/http/fcgi" "net/http/fcgi"
"os"
"path"
"time" "time"
"github.com/astaxie/beego/grace"
"github.com/astaxie/beego/utils"
) )
var (
// BeeApp is an application instance
BeeApp *App
)
func init() {
// create beego application
BeeApp = NewApp()
}
// App defines beego application with a new PatternServeMux. // App defines beego application with a new PatternServeMux.
type App struct { type App struct {
Handlers *ControllerRegistor Handlers *ControllerRegister
Server *http.Server Server *http.Server
} }
@ -37,89 +52,311 @@ func NewApp() *App {
// Run beego application. // Run beego application.
func (app *App) Run() { func (app *App) Run() {
addr := HttpAddr addr := BConfig.Listen.HTTPAddr
if HttpPort != 0 { if BConfig.Listen.HTTPPort != 0 {
addr = fmt.Sprintf("%s:%d", HttpAddr, HttpPort) addr = fmt.Sprintf("%s:%d", BConfig.Listen.HTTPAddr, BConfig.Listen.HTTPPort)
} }
var ( var (
err error err error
l net.Listener l net.Listener
endRunning = make(chan bool, 1)
) )
endRunning := make(chan bool, 1)
if UseFcgi { // run cgi server
if UseStdIo { if BConfig.Listen.EnableFcgi {
err = fcgi.Serve(nil, app.Handlers) // standard I/O if BConfig.Listen.EnableStdIo {
if err == nil { if err = fcgi.Serve(nil, app.Handlers); err == nil { // standard I/O
BeeLogger.Info("Use FCGI via standard I/O") BeeLogger.Info("Use FCGI via standard I/O")
} else { } else {
BeeLogger.Info("Cannot use FCGI via standard I/O", err) BeeLogger.Critical("Cannot use FCGI via standard I/O", err)
} }
} else { return
if HttpPort == 0 {
l, err = net.Listen("unix", addr)
} else {
l, err = net.Listen("tcp", addr)
}
if err != nil {
BeeLogger.Critical("Listen: ", err)
}
err = fcgi.Serve(l, app.Handlers)
} }
} else { if BConfig.Listen.HTTPPort == 0 {
app.Server.Addr = addr // remove the Socket file before start
app.Server.Handler = app.Handlers if utils.FileExists(addr) {
app.Server.ReadTimeout = time.Duration(HttpServerTimeOut) * time.Second os.Remove(addr)
app.Server.WriteTimeout = time.Duration(HttpServerTimeOut) * time.Second }
l, err = net.Listen("unix", addr)
} else {
l, err = net.Listen("tcp", addr)
}
if err != nil {
BeeLogger.Critical("Listen: ", err)
}
if err = fcgi.Serve(l, app.Handlers); err != nil {
BeeLogger.Critical("fcgi.Serve: ", err)
}
return
}
if EnableHttpTLS { app.Server.Handler = app.Handlers
app.Server.ReadTimeout = time.Duration(BConfig.Listen.ServerTimeOut) * time.Second
app.Server.WriteTimeout = time.Duration(BConfig.Listen.ServerTimeOut) * time.Second
// run graceful mode
if BConfig.Listen.Graceful {
httpsAddr := BConfig.Listen.HTTPSAddr
app.Server.Addr = httpsAddr
if BConfig.Listen.EnableHTTPS {
go func() { go func() {
time.Sleep(20 * time.Microsecond) time.Sleep(20 * time.Microsecond)
if HttpsPort != 0 { if BConfig.Listen.HTTPSPort != 0 {
app.Server.Addr = fmt.Sprintf("%s:%d", HttpAddr, HttpsPort) httpsAddr = fmt.Sprintf("%s:%d", BConfig.Listen.HTTPSAddr, BConfig.Listen.HTTPSPort)
app.Server.Addr = httpsAddr
} }
BeeLogger.Info("https server Running on %s", app.Server.Addr) server := grace.NewServer(httpsAddr, app.Handlers)
err := app.Server.ListenAndServeTLS(HttpCertFile, HttpKeyFile) server.Server.ReadTimeout = app.Server.ReadTimeout
if err != nil { server.Server.WriteTimeout = app.Server.WriteTimeout
BeeLogger.Critical("ListenAndServeTLS: ", err) if err := server.ListenAndServeTLS(BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile); err != nil {
BeeLogger.Critical("ListenAndServeTLS: ", err, fmt.Sprintf("%d", os.Getpid()))
time.Sleep(100 * time.Microsecond) time.Sleep(100 * time.Microsecond)
endRunning <- true endRunning <- true
} }
}() }()
} }
if BConfig.Listen.EnableHTTP {
if EnableHttpListen {
go func() { go func() {
app.Server.Addr = addr server := grace.NewServer(addr, app.Handlers)
BeeLogger.Info("http server Running on %s", app.Server.Addr) server.Server.ReadTimeout = app.Server.ReadTimeout
if ListenTCP4 && HttpAddr == "" { server.Server.WriteTimeout = app.Server.WriteTimeout
ln, err := net.Listen("tcp4", app.Server.Addr) if BConfig.Listen.ListenTCP4 {
if err != nil { server.Network = "tcp4"
BeeLogger.Critical("ListenAndServe: ", err) }
time.Sleep(100 * time.Microsecond) if err := server.ListenAndServe(); err != nil {
endRunning <- true BeeLogger.Critical("ListenAndServe: ", err, fmt.Sprintf("%d", os.Getpid()))
return time.Sleep(100 * time.Microsecond)
} endRunning <- true
err = app.Server.Serve(ln)
if err != nil {
BeeLogger.Critical("ListenAndServe: ", err)
time.Sleep(100 * time.Microsecond)
endRunning <- true
return
}
} else {
err := app.Server.ListenAndServe()
if err != nil {
BeeLogger.Critical("ListenAndServe: ", err)
time.Sleep(100 * time.Microsecond)
endRunning <- true
}
} }
}() }()
} }
<-endRunning
return
} }
// run normal mode
app.Server.Addr = addr
if BConfig.Listen.EnableHTTPS {
go func() {
time.Sleep(20 * time.Microsecond)
if BConfig.Listen.HTTPSPort != 0 {
app.Server.Addr = fmt.Sprintf("%s:%d", BConfig.Listen.HTTPSAddr, BConfig.Listen.HTTPSPort)
}
BeeLogger.Info("https server Running on %s", app.Server.Addr)
if err := app.Server.ListenAndServeTLS(BConfig.Listen.HTTPSCertFile, BConfig.Listen.HTTPSKeyFile); err != nil {
BeeLogger.Critical("ListenAndServeTLS: ", err)
time.Sleep(100 * time.Microsecond)
endRunning <- true
}
}()
}
if BConfig.Listen.EnableHTTP {
go func() {
app.Server.Addr = addr
BeeLogger.Info("http server Running on %s", app.Server.Addr)
if BConfig.Listen.ListenTCP4 {
ln, err := net.Listen("tcp4", app.Server.Addr)
if err != nil {
BeeLogger.Critical("ListenAndServe: ", err)
time.Sleep(100 * time.Microsecond)
endRunning <- true
return
}
if err = app.Server.Serve(ln); err != nil {
BeeLogger.Critical("ListenAndServe: ", err)
time.Sleep(100 * time.Microsecond)
endRunning <- true
return
}
} else {
if err := app.Server.ListenAndServe(); err != nil {
BeeLogger.Critical("ListenAndServe: ", err)
time.Sleep(100 * time.Microsecond)
endRunning <- true
}
}
}()
}
<-endRunning <-endRunning
} }
// Router adds a patterned controller handler to BeeApp.
// it's an alias method of App.Router.
// usage:
// simple router
// beego.Router("/admin", &admin.UserController{})
// beego.Router("/admin/index", &admin.ArticleController{})
//
// regex router
//
// beego.Router("/api/:id([0-9]+)", &controllers.RController{})
//
// custom rules
// beego.Router("/api/list",&RestController{},"*:ListFood")
// beego.Router("/api/create",&RestController{},"post:CreateFood")
// beego.Router("/api/update",&RestController{},"put:UpdateFood")
// beego.Router("/api/delete",&RestController{},"delete:DeleteFood")
func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *App {
BeeApp.Handlers.Add(rootpath, c, mappingMethods...)
return BeeApp
}
// Include will generate router file in the router/xxx.go from the controller's comments
// usage:
// beego.Include(&BankAccount{}, &OrderController{},&RefundController{},&ReceiptController{})
// type BankAccount struct{
// beego.Controller
// }
//
// register the function
// func (b *BankAccount)Mapping(){
// b.Mapping("ShowAccount" , b.ShowAccount)
// b.Mapping("ModifyAccount", b.ModifyAccount)
//}
//
// //@router /account/:id [get]
// func (b *BankAccount) ShowAccount(){
// //logic
// }
//
//
// //@router /account/:id [post]
// func (b *BankAccount) ModifyAccount(){
// //logic
// }
//
// the comments @router url methodlist
// url support all the function Router's pattern
// methodlist [get post head put delete options *]
func Include(cList ...ControllerInterface) *App {
BeeApp.Handlers.Include(cList...)
return BeeApp
}
// RESTRouter adds a restful controller handler to BeeApp.
// its' controller implements beego.ControllerInterface and
// defines a param "pattern/:objectId" to visit each resource.
func RESTRouter(rootpath string, c ControllerInterface) *App {
Router(rootpath, c)
Router(path.Join(rootpath, ":objectId"), c)
return BeeApp
}
// AutoRouter adds defined controller handler to BeeApp.
// it's same to App.AutoRouter.
// if beego.AddAuto(&MainContorlller{}) and MainController has methods List and Page,
// visit the url /main/list to exec List function or /main/page to exec Page function.
func AutoRouter(c ControllerInterface) *App {
BeeApp.Handlers.AddAuto(c)
return BeeApp
}
// AutoPrefix adds controller handler to BeeApp with prefix.
// it's same to App.AutoRouterWithPrefix.
// if beego.AutoPrefix("/admin",&MainContorlller{}) and MainController has methods List and Page,
// visit the url /admin/main/list to exec List function or /admin/main/page to exec Page function.
func AutoPrefix(prefix string, c ControllerInterface) *App {
BeeApp.Handlers.AddAutoPrefix(prefix, c)
return BeeApp
}
// Get used to register router for Get method
// usage:
// beego.Get("/", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Get(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Get(rootpath, f)
return BeeApp
}
// Post used to register router for Post method
// usage:
// beego.Post("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Post(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Post(rootpath, f)
return BeeApp
}
// Delete used to register router for Delete method
// usage:
// beego.Delete("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Delete(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Delete(rootpath, f)
return BeeApp
}
// Put used to register router for Put method
// usage:
// beego.Put("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Put(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Put(rootpath, f)
return BeeApp
}
// Head used to register router for Head method
// usage:
// beego.Head("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Head(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Head(rootpath, f)
return BeeApp
}
// Options used to register router for Options method
// usage:
// beego.Options("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Options(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Options(rootpath, f)
return BeeApp
}
// Patch used to register router for Patch method
// usage:
// beego.Patch("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Patch(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Patch(rootpath, f)
return BeeApp
}
// Any used to register router for all methods
// usage:
// beego.Any("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Any(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Any(rootpath, f)
return BeeApp
}
// Handler used to register a Handler router
// usage:
// beego.Handler("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Handler(rootpath string, h http.Handler, options ...interface{}) *App {
BeeApp.Handlers.Handler(rootpath, h, options...)
return BeeApp
}
// InsertFilter adds a FilterFunc with pattern condition and action constant.
// The pos means action constant including
// beego.BeforeStatic, beego.BeforeRouter, beego.BeforeExec, beego.AfterExec and beego.FinishRouter.
// The bool params is for setting the returnOnOutput value (false allows multiple filters to execute)
func InsertFilter(pattern string, pos int, filter FilterFunc, params ...bool) *App {
BeeApp.Handlers.InsertFilter(pattern, pos, filter, params...)
return BeeApp
}

410
beego.go
View File

@ -12,320 +12,34 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// beego is an open-source, high-performance, modularity, full-stack web framework
//
// package main
//
// import "github.com/astaxie/beego"
//
// func main() {
// beego.Run()
// }
//
// more infomation: http://beego.me
package beego package beego
import ( import (
"net/http"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"github.com/astaxie/beego/middleware"
"github.com/astaxie/beego/session"
) )
// beego web framework version. const (
const VERSION = "1.4.2" // VERSION represent beego web framework version.
VERSION = "1.6.1"
type hookfunc func() error //hook function to run // DEV is for develop
var hooks []hookfunc //hook function slice to store the hookfunc DEV = "dev"
// PROD is for production
PROD = "prod"
)
type groupRouter struct { //hook function to run
pattern string type hookfunc func() error
controller ControllerInterface
mappingMethods string
}
// RouterGroups which will store routers var (
type GroupRouters []groupRouter hooks = make([]hookfunc, 0) //hook function slice to store the hookfunc
)
// Get a new GroupRouters // AddAPPStartHook is used to register the hookfunc
func NewGroupRouters() GroupRouters { // The hookfuncs will run in beego.Run()
return make(GroupRouters, 0)
}
// Add Router in the GroupRouters
// it is for plugin or module to register router
func (gr *GroupRouters) AddRouter(pattern string, c ControllerInterface, mappingMethod ...string) {
var newRG groupRouter
if len(mappingMethod) > 0 {
newRG = groupRouter{
pattern,
c,
mappingMethod[0],
}
} else {
newRG = groupRouter{
pattern,
c,
"",
}
}
*gr = append(*gr, newRG)
}
func (gr *GroupRouters) AddAuto(c ControllerInterface) {
newRG := groupRouter{
"",
c,
"",
}
*gr = append(*gr, newRG)
}
// AddGroupRouter with the prefix
// it will register the router in BeeApp
// the follow code is write in modules:
// GR:=NewGroupRouters()
// GR.AddRouter("/login",&UserController,"get:Login")
// GR.AddRouter("/logout",&UserController,"get:Logout")
// GR.AddRouter("/register",&UserController,"get:Reg")
// the follow code is write in app:
// import "github.com/beego/modules/auth"
// AddRouterGroup("/admin", auth.GR)
func AddGroupRouter(prefix string, groups GroupRouters) *App {
for _, v := range groups {
if v.pattern == "" {
BeeApp.Handlers.AddAutoPrefix(prefix, v.controller)
} else if v.mappingMethods != "" {
BeeApp.Handlers.Add(prefix+v.pattern, v.controller, v.mappingMethods)
} else {
BeeApp.Handlers.Add(prefix+v.pattern, v.controller)
}
}
return BeeApp
}
// Router adds a patterned controller handler to BeeApp.
// it's an alias method of App.Router.
// usage:
// simple router
// beego.Router("/admin", &admin.UserController{})
// beego.Router("/admin/index", &admin.ArticleController{})
//
// regex router
//
// beego.Router("/api/:id([0-9]+)", &controllers.RController{})
//
// custom rules
// beego.Router("/api/list",&RestController{},"*:ListFood")
// beego.Router("/api/create",&RestController{},"post:CreateFood")
// beego.Router("/api/update",&RestController{},"put:UpdateFood")
// beego.Router("/api/delete",&RestController{},"delete:DeleteFood")
func Router(rootpath string, c ControllerInterface, mappingMethods ...string) *App {
BeeApp.Handlers.Add(rootpath, c, mappingMethods...)
return BeeApp
}
// Router add list from
// usage:
// beego.Include(&BankAccount{}, &OrderController{},&RefundController{},&ReceiptController{})
// type BankAccount struct{
// beego.Controller
// }
//
// register the function
// func (b *BankAccount)Mapping(){
// b.Mapping("ShowAccount" , b.ShowAccount)
// b.Mapping("ModifyAccount", b.ModifyAccount)
//}
//
// //@router /account/:id [get]
// func (b *BankAccount) ShowAccount(){
// //logic
// }
//
//
// //@router /account/:id [post]
// func (b *BankAccount) ModifyAccount(){
// //logic
// }
//
// the comments @router url methodlist
// url support all the function Router's pattern
// methodlist [get post head put delete options *]
func Include(cList ...ControllerInterface) *App {
BeeApp.Handlers.Include(cList...)
return BeeApp
}
// RESTRouter adds a restful controller handler to BeeApp.
// its' controller implements beego.ControllerInterface and
// defines a param "pattern/:objectId" to visit each resource.
func RESTRouter(rootpath string, c ControllerInterface) *App {
Router(rootpath, c)
Router(path.Join(rootpath, ":objectId"), c)
return BeeApp
}
// AutoRouter adds defined controller handler to BeeApp.
// it's same to App.AutoRouter.
// if beego.AddAuto(&MainContorlller{}) and MainController has methods List and Page,
// visit the url /main/list to exec List function or /main/page to exec Page function.
func AutoRouter(c ControllerInterface) *App {
BeeApp.Handlers.AddAuto(c)
return BeeApp
}
// AutoPrefix adds controller handler to BeeApp with prefix.
// it's same to App.AutoRouterWithPrefix.
// if beego.AutoPrefix("/admin",&MainContorlller{}) and MainController has methods List and Page,
// visit the url /admin/main/list to exec List function or /admin/main/page to exec Page function.
func AutoPrefix(prefix string, c ControllerInterface) *App {
BeeApp.Handlers.AddAutoPrefix(prefix, c)
return BeeApp
}
// register router for Get method
// usage:
// beego.Get("/", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Get(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Get(rootpath, f)
return BeeApp
}
// register router for Post method
// usage:
// beego.Post("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Post(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Post(rootpath, f)
return BeeApp
}
// register router for Delete method
// usage:
// beego.Delete("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Delete(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Delete(rootpath, f)
return BeeApp
}
// register router for Put method
// usage:
// beego.Put("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Put(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Put(rootpath, f)
return BeeApp
}
// register router for Head method
// usage:
// beego.Head("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Head(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Head(rootpath, f)
return BeeApp
}
// register router for Options method
// usage:
// beego.Options("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Options(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Options(rootpath, f)
return BeeApp
}
// register router for Patch method
// usage:
// beego.Patch("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Patch(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Patch(rootpath, f)
return BeeApp
}
// register router for all method
// usage:
// beego.Any("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Any(rootpath string, f FilterFunc) *App {
BeeApp.Handlers.Any(rootpath, f)
return BeeApp
}
// register router for own Handler
// usage:
// beego.Handler("/api", func(ctx *context.Context){
// ctx.Output.Body("hello world")
// })
func Handler(rootpath string, h http.Handler, options ...interface{}) *App {
BeeApp.Handlers.Handler(rootpath, h, options...)
return BeeApp
}
// ErrorHandler registers http.HandlerFunc to each http err code string.
// usage:
// beego.ErrorHandler("404",NotFound)
// beego.ErrorHandler("500",InternalServerError)
func Errorhandler(err string, h http.HandlerFunc) *App {
middleware.Errorhandler(err, h)
return BeeApp
}
// SetViewsPath sets view directory path in beego application.
func SetViewsPath(path string) *App {
ViewsPath = path
return BeeApp
}
// SetStaticPath sets static directory path and proper url pattern in beego application.
// if beego.SetStaticPath("static","public"), visit /static/* to load static file in folder "public".
func SetStaticPath(url string, path string) *App {
if !strings.HasPrefix(url, "/") {
url = "/" + url
}
url = strings.TrimRight(url, "/")
StaticDir[url] = path
return BeeApp
}
// DelStaticPath removes the static folder setting in this url pattern in beego application.
func DelStaticPath(url string) *App {
if !strings.HasPrefix(url, "/") {
url = "/" + url
}
url = strings.TrimRight(url, "/")
delete(StaticDir, url)
return BeeApp
}
// InsertFilter adds a FilterFunc with pattern condition and action constant.
// The pos means action constant including
// beego.BeforeStatic, beego.BeforeRouter, beego.BeforeExec, beego.AfterExec and beego.FinishRouter.
// The bool params is for setting the returnOnOutput value (false allows multiple filters to execute)
func InsertFilter(pattern string, pos int, filter FilterFunc, params ...bool) *App {
BeeApp.Handlers.InsertFilter(pattern, pos, filter, params...)
return BeeApp
}
// The hookfunc will run in beego.Run()
// such as sessionInit, middlerware start, buildtemplate, admin start // such as sessionInit, middlerware start, buildtemplate, admin start
func AddAPPStartHook(hf hookfunc) { func AddAPPStartHook(hf hookfunc) {
hooks = append(hooks, hf) hooks = append(hooks, hf)
@ -333,99 +47,45 @@ func AddAPPStartHook(hf hookfunc) {
// Run beego application. // Run beego application.
// beego.Run() default run on HttpPort // beego.Run() default run on HttpPort
// beego.Run("localhost")
// beego.Run(":8089") // beego.Run(":8089")
// beego.Run("127.0.0.1:8089") // beego.Run("127.0.0.1:8089")
func Run(params ...string) { func Run(params ...string) {
initBeforeHTTPRun()
if len(params) > 0 && params[0] != "" { if len(params) > 0 && params[0] != "" {
strs := strings.Split(params[0], ":") strs := strings.Split(params[0], ":")
if len(strs) > 0 && strs[0] != "" { if len(strs) > 0 && strs[0] != "" {
HttpAddr = strs[0] BConfig.Listen.HTTPAddr = strs[0]
} }
if len(strs) > 1 && strs[1] != "" { if len(strs) > 1 && strs[1] != "" {
HttpPort, _ = strconv.Atoi(strs[1]) BConfig.Listen.HTTPPort, _ = strconv.Atoi(strs[1])
} }
} }
initBeforeHttpRun()
if EnableAdmin {
go beeAdminApp.Run()
}
BeeApp.Run() BeeApp.Run()
} }
func initBeforeHttpRun() { func initBeforeHTTPRun() {
// if AppConfigPath not In the conf/app.conf reParse config //init hooks
if AppConfigPath != filepath.Join(AppPath, "conf", "app.conf") { AddAPPStartHook(registerMime)
err := ParseConfig() AddAPPStartHook(registerDefaultErrorHandler)
if err != nil && AppConfigPath != filepath.Join(workPath, "conf", "app.conf") { AddAPPStartHook(registerSession)
// configuration is critical to app, panic here if parse failed AddAPPStartHook(registerDocs)
panic(err) AddAPPStartHook(registerTemplate)
} AddAPPStartHook(registerAdmin)
}
//init mime
AddAPPStartHook(initMime)
// do hooks function
for _, hk := range hooks { for _, hk := range hooks {
err := hk() if err := hk(); err != nil {
if err != nil {
panic(err) panic(err)
} }
} }
if SessionOn {
var err error
sessionConfig := AppConfig.String("sessionConfig")
if sessionConfig == "" {
sessionConfig = `{"cookieName":"` + SessionName + `",` +
`"gclifetime":` + strconv.FormatInt(SessionGCMaxLifetime, 10) + `,` +
`"providerConfig":"` + filepath.ToSlash(SessionSavePath) + `",` +
`"secure":` + strconv.FormatBool(EnableHttpTLS) + `,` +
`"enableSetCookie":` + strconv.FormatBool(SessionAutoSetCookie) + `,` +
`"domain":"` + SessionDomain + `",` +
`"cookieLifeTime":` + strconv.Itoa(SessionCookieLifeTime) + `}`
}
GlobalSessions, err = session.NewManager(SessionProvider,
sessionConfig)
if err != nil {
panic(err)
}
go GlobalSessions.GC()
}
err := BuildTemplate(ViewsPath)
if err != nil {
if RunMode == "dev" {
Warn(err)
}
}
middleware.VERSION = VERSION
middleware.AppName = AppName
middleware.RegisterErrorHandler()
if EnableDocs {
Get("/docs", serverDocs)
Get("/docs/*", serverDocs)
}
} }
// this function is for test package init // TestBeegoInit is for test package init
func TestBeegoInit(apppath string) { func TestBeegoInit(ap string) {
AppPath = apppath os.Setenv("BEEGO_RUNMODE", "test")
RunMode = "test" appConfigPath = filepath.Join(ap, "conf", "app.conf")
AppConfigPath = filepath.Join(AppPath, "conf", "app.conf") os.Chdir(ap)
err := ParseConfig() initBeforeHTTPRun()
if err != nil && !os.IsNotExist(err) {
// for init if doesn't have app.conf will not panic
Info(err)
}
os.Chdir(AppPath)
initBeforeHttpRun()
}
func init() {
hooks = make([]hookfunc, 0)
} }

6
cache/README.md vendored
View File

@ -26,7 +26,7 @@ Then init a Cache (example with memory adapter)
Use it like this: Use it like this:
bm.Put("astaxie", 1, 10) bm.Put("astaxie", 1, 10 * time.Second)
bm.Get("astaxie") bm.Get("astaxie")
bm.IsExist("astaxie") bm.IsExist("astaxie")
bm.Delete("astaxie") bm.Delete("astaxie")
@ -43,7 +43,7 @@ interval means the gc time. The cache will check at each time interval, whether
## Memcache adapter ## Memcache adapter
Memcache adapter use the vitess's [Memcache](http://code.google.com/p/vitess/go/memcache) client. Memcache adapter use the [gomemcache](http://github.com/bradfitz/gomemcache) client.
Configure like this: Configure like this:
@ -52,7 +52,7 @@ Configure like this:
## Redis adapter ## Redis adapter
Redis adapter use the [redigo](http://github.com/garyburd/redigo/redis) client. Redis adapter use the [redigo](http://github.com/garyburd/redigo) client.
Configure like this: Configure like this:

24
cache/cache.go vendored
View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// Package cache provide a Cache interface and some implemetn engine
// Usage: // Usage:
// //
// import( // import(
@ -22,7 +23,7 @@
// //
// Use it like this: // Use it like this:
// //
// bm.Put("astaxie", 1, 10) // bm.Put("astaxie", 1, 10 * time.Second)
// bm.Get("astaxie") // bm.Get("astaxie")
// bm.IsExist("astaxie") // bm.IsExist("astaxie")
// bm.Delete("astaxie") // bm.Delete("astaxie")
@ -32,13 +33,14 @@ package cache
import ( import (
"fmt" "fmt"
"time"
) )
// Cache interface contains all behaviors for cache adapter. // Cache interface contains all behaviors for cache adapter.
// usage: // usage:
// cache.Register("file",cache.NewFileCache()) // this operation is run in init method of file.go. // cache.Register("file",cache.NewFileCache) // this operation is run in init method of file.go.
// c,err := cache.NewCache("file","{....}") // c,err := cache.NewCache("file","{....}")
// c.Put("key",value,3600) // c.Put("key",value, 3600 * time.Second)
// v := c.Get("key") // v := c.Get("key")
// //
// c.Incr("counter") // now is 1 // c.Incr("counter") // now is 1
@ -47,8 +49,10 @@ import (
type Cache interface { type Cache interface {
// get cached value by key. // get cached value by key.
Get(key string) interface{} Get(key string) interface{}
// GetMulti is a batch version of Get.
GetMulti(keys []string) []interface{}
// set cached value with key and expire time. // set cached value with key and expire time.
Put(key string, val interface{}, timeout int64) error Put(key string, val interface{}, timeout time.Duration) error
// delete cached value by key. // delete cached value by key.
Delete(key string) error Delete(key string) error
// increase cached int value by key, as a counter. // increase cached int value by key, as a counter.
@ -63,12 +67,15 @@ type Cache interface {
StartAndGC(config string) error StartAndGC(config string) error
} }
var adapters = make(map[string]Cache) // Instance is a function create a new Cache Instance
type Instance func() Cache
var adapters = make(map[string]Instance)
// Register makes a cache adapter available by the adapter name. // Register makes a cache adapter available by the adapter name.
// If Register is called twice with the same name or if driver is nil, // If Register is called twice with the same name or if driver is nil,
// it panics. // it panics.
func Register(name string, adapter Cache) { func Register(name string, adapter Instance) {
if adapter == nil { if adapter == nil {
panic("cache: Register adapter is nil") panic("cache: Register adapter is nil")
} }
@ -78,15 +85,16 @@ func Register(name string, adapter Cache) {
adapters[name] = adapter adapters[name] = adapter
} }
// Create a new cache driver by adapter name and config string. // NewCache Create a new cache driver by adapter name and config string.
// config need to be correct JSON as string: {"interval":360}. // config need to be correct JSON as string: {"interval":360}.
// it will start gc automatically. // it will start gc automatically.
func NewCache(adapterName, config string) (adapter Cache, err error) { func NewCache(adapterName, config string) (adapter Cache, err error) {
adapter, ok := adapters[adapterName] instanceFunc, ok := adapters[adapterName]
if !ok { if !ok {
err = fmt.Errorf("cache: unknown adapter name %q (forgot to import?)", adapterName) err = fmt.Errorf("cache: unknown adapter name %q (forgot to import?)", adapterName)
return return
} }
adapter = instanceFunc()
err = adapter.StartAndGC(config) err = adapter.StartAndGC(config)
if err != nil { if err != nil {
adapter = nil adapter = nil

65
cache/cache_test.go vendored
View File

@ -15,6 +15,7 @@
package cache package cache
import ( import (
"os"
"testing" "testing"
"time" "time"
) )
@ -24,7 +25,8 @@ func TestCache(t *testing.T) {
if err != nil { if err != nil {
t.Error("init err") t.Error("init err")
} }
if err = bm.Put("astaxie", 1, 10); err != nil { timeoutDuration := 10 * time.Second
if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
t.Error("set Error", err) t.Error("set Error", err)
} }
if !bm.IsExist("astaxie") { if !bm.IsExist("astaxie") {
@ -41,7 +43,7 @@ func TestCache(t *testing.T) {
t.Error("check err") t.Error("check err")
} }
if err = bm.Put("astaxie", 1, 10); err != nil { if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
t.Error("set Error", err) t.Error("set Error", err)
} }
@ -64,14 +66,44 @@ func TestCache(t *testing.T) {
if bm.IsExist("astaxie") { if bm.IsExist("astaxie") {
t.Error("delete err") t.Error("delete err")
} }
//test GetMulti
if err = bm.Put("astaxie", "author", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if !bm.IsExist("astaxie") {
t.Error("check err")
}
if v := bm.Get("astaxie"); v.(string) != "author" {
t.Error("get err")
}
if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if !bm.IsExist("astaxie1") {
t.Error("check err")
}
vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
if len(vv) != 2 {
t.Error("GetMulti ERROR")
}
if vv[0].(string) != "author" {
t.Error("GetMulti ERROR")
}
if vv[1].(string) != "author1" {
t.Error("GetMulti ERROR")
}
} }
func TestFileCache(t *testing.T) { func TestFileCache(t *testing.T) {
bm, err := NewCache("file", `{"CachePath":"/cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0}`) bm, err := NewCache("file", `{"CachePath":"cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0}`)
if err != nil { if err != nil {
t.Error("init err") t.Error("init err")
} }
if err = bm.Put("astaxie", 1, 10); err != nil { timeoutDuration := 10 * time.Second
if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
t.Error("set Error", err) t.Error("set Error", err)
} }
if !bm.IsExist("astaxie") { if !bm.IsExist("astaxie") {
@ -101,15 +133,36 @@ func TestFileCache(t *testing.T) {
if bm.IsExist("astaxie") { if bm.IsExist("astaxie") {
t.Error("delete err") t.Error("delete err")
} }
//test string //test string
if err = bm.Put("astaxie", "author", 10); err != nil { if err = bm.Put("astaxie", "author", timeoutDuration); err != nil {
t.Error("set Error", err) t.Error("set Error", err)
} }
if !bm.IsExist("astaxie") { if !bm.IsExist("astaxie") {
t.Error("check err") t.Error("check err")
} }
if v := bm.Get("astaxie"); v.(string) != "author" { if v := bm.Get("astaxie"); v.(string) != "author" {
t.Error("get err") t.Error("get err")
} }
//test GetMulti
if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if !bm.IsExist("astaxie1") {
t.Error("check err")
}
vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
if len(vv) != 2 {
t.Error("GetMulti ERROR")
}
if vv[0].(string) != "author" {
t.Error("GetMulti ERROR")
}
if vv[1].(string) != "author1" {
t.Error("GetMulti ERROR")
}
os.RemoveAll("cache")
} }

22
cache/conv.go vendored
View File

@ -19,7 +19,7 @@ import (
"strconv" "strconv"
) )
// convert interface to string. // GetString convert interface to string.
func GetString(v interface{}) string { func GetString(v interface{}) string {
switch result := v.(type) { switch result := v.(type) {
case string: case string:
@ -34,7 +34,7 @@ func GetString(v interface{}) string {
return "" return ""
} }
// convert interface to int. // GetInt convert interface to int.
func GetInt(v interface{}) int { func GetInt(v interface{}) int {
switch result := v.(type) { switch result := v.(type) {
case int: case int:
@ -52,7 +52,7 @@ func GetInt(v interface{}) int {
return 0 return 0
} }
// convert interface to int64. // GetInt64 convert interface to int64.
func GetInt64(v interface{}) int64 { func GetInt64(v interface{}) int64 {
switch result := v.(type) { switch result := v.(type) {
case int: case int:
@ -71,7 +71,7 @@ func GetInt64(v interface{}) int64 {
return 0 return 0
} }
// convert interface to float64. // GetFloat64 convert interface to float64.
func GetFloat64(v interface{}) float64 { func GetFloat64(v interface{}) float64 {
switch result := v.(type) { switch result := v.(type) {
case float64: case float64:
@ -85,7 +85,7 @@ func GetFloat64(v interface{}) float64 {
return 0 return 0
} }
// convert interface to bool. // GetBool convert interface to bool.
func GetBool(v interface{}) bool { func GetBool(v interface{}) bool {
switch result := v.(type) { switch result := v.(type) {
case bool: case bool:
@ -98,15 +98,3 @@ func GetBool(v interface{}) bool {
} }
return false return false
} }
// convert interface to byte slice.
func getByteArray(v interface{}) []byte {
switch result := v.(type) {
case []byte:
return result
case string:
return []byte(result)
default:
return nil
}
}

29
cache/conv_test.go vendored
View File

@ -27,7 +27,7 @@ func TestGetString(t *testing.T) {
if "test2" != GetString(t2) { if "test2" != GetString(t2) {
t.Error("get string from byte array error") t.Error("get string from byte array error")
} }
var t3 int = 1 var t3 = 1
if "1" != GetString(t3) { if "1" != GetString(t3) {
t.Error("get string from int error") t.Error("get string from int error")
} }
@ -35,7 +35,7 @@ func TestGetString(t *testing.T) {
if "1" != GetString(t4) { if "1" != GetString(t4) {
t.Error("get string from int64 error") t.Error("get string from int64 error")
} }
var t5 float64 = 1.1 var t5 = 1.1
if "1.1" != GetString(t5) { if "1.1" != GetString(t5) {
t.Error("get string from float64 error") t.Error("get string from float64 error")
} }
@ -46,7 +46,7 @@ func TestGetString(t *testing.T) {
} }
func TestGetInt(t *testing.T) { func TestGetInt(t *testing.T) {
var t1 int = 1 var t1 = 1
if 1 != GetInt(t1) { if 1 != GetInt(t1) {
t.Error("get int from int error") t.Error("get int from int error")
} }
@ -69,7 +69,7 @@ func TestGetInt(t *testing.T) {
func TestGetInt64(t *testing.T) { func TestGetInt64(t *testing.T) {
var i int64 = 1 var i int64 = 1
var t1 int = 1 var t1 = 1
if i != GetInt64(t1) { if i != GetInt64(t1) {
t.Error("get int64 from int error") t.Error("get int64 from int error")
} }
@ -91,12 +91,12 @@ func TestGetInt64(t *testing.T) {
} }
func TestGetFloat64(t *testing.T) { func TestGetFloat64(t *testing.T) {
var f float64 = 1.11 var f = 1.11
var t1 float32 = 1.11 var t1 float32 = 1.11
if f != GetFloat64(t1) { if f != GetFloat64(t1) {
t.Error("get float64 from float32 error") t.Error("get float64 from float32 error")
} }
var t2 float64 = 1.11 var t2 = 1.11
if f != GetFloat64(t2) { if f != GetFloat64(t2) {
t.Error("get float64 from float64 error") t.Error("get float64 from float64 error")
} }
@ -106,7 +106,7 @@ func TestGetFloat64(t *testing.T) {
} }
var f2 float64 = 1 var f2 float64 = 1
var t4 int = 1 var t4 = 1
if f2 != GetFloat64(t4) { if f2 != GetFloat64(t4) {
t.Error("get float64 from int error") t.Error("get float64 from int error")
} }
@ -130,21 +130,6 @@ func TestGetBool(t *testing.T) {
} }
} }
func TestGetByteArray(t *testing.T) {
var b = []byte("test")
var t1 = []byte("test")
if !byteArrayEquals(b, getByteArray(t1)) {
t.Error("get byte array from byte array error")
}
var t2 = "test"
if !byteArrayEquals(b, getByteArray(t2)) {
t.Error("get byte array from string error")
}
if nil != getByteArray(nil) {
t.Error("get byte array from nil error")
}
}
func byteArrayEquals(a []byte, b []byte) bool { func byteArrayEquals(a []byte, b []byte) bool {
if len(a) != len(b) { if len(a) != len(b) {
return false return false

83
cache/file.go vendored
View File

@ -29,23 +29,20 @@ import (
"time" "time"
) )
func init() {
Register("file", NewFileCache())
}
// FileCacheItem is basic unit of file cache adapter. // FileCacheItem is basic unit of file cache adapter.
// it contains data and expire time. // it contains data and expire time.
type FileCacheItem struct { type FileCacheItem struct {
Data interface{} Data interface{}
Lastaccess int64 Lastaccess time.Time
Expired int64 Expired time.Time
} }
// FileCache Config
var ( var (
FileCachePath string = "cache" // cache directory FileCachePath = "cache" // cache directory
FileCacheFileSuffix string = ".bin" // cache file suffix FileCacheFileSuffix = ".bin" // cache file suffix
FileCacheDirectoryLevel int = 2 // cache file deep level if auto generated cache files. FileCacheDirectoryLevel = 2 // cache file deep level if auto generated cache files.
FileCacheEmbedExpiry int64 = 0 // cache expire time, default is no expire forever. FileCacheEmbedExpiry time.Duration // cache expire time, default is no expire forever.
) )
// FileCache is cache adapter for file storage. // FileCache is cache adapter for file storage.
@ -56,14 +53,14 @@ type FileCache struct {
EmbedExpiry int EmbedExpiry int
} }
// Create new file cache with no config. // NewFileCache Create new file cache with no config.
// the level and expiry need set in method StartAndGC as config string. // the level and expiry need set in method StartAndGC as config string.
func NewFileCache() *FileCache { func NewFileCache() Cache {
// return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix} // return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix}
return &FileCache{} return &FileCache{}
} }
// Start and begin gc for file cache. // StartAndGC will start and begin gc for file cache.
// the config need to be like {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0} // the config need to be like {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0}
func (fc *FileCache) StartAndGC(config string) error { func (fc *FileCache) StartAndGC(config string) error {
@ -79,7 +76,7 @@ func (fc *FileCache) StartAndGC(config string) error {
cfg["DirectoryLevel"] = strconv.Itoa(FileCacheDirectoryLevel) cfg["DirectoryLevel"] = strconv.Itoa(FileCacheDirectoryLevel)
} }
if _, ok := cfg["EmbedExpiry"]; !ok { if _, ok := cfg["EmbedExpiry"]; !ok {
cfg["EmbedExpiry"] = strconv.FormatInt(FileCacheEmbedExpiry, 10) cfg["EmbedExpiry"] = strconv.FormatInt(int64(FileCacheEmbedExpiry.Seconds()), 10)
} }
fc.CachePath = cfg["CachePath"] fc.CachePath = cfg["CachePath"]
fc.FileSuffix = cfg["FileSuffix"] fc.FileSuffix = cfg["FileSuffix"]
@ -92,8 +89,6 @@ func (fc *FileCache) StartAndGC(config string) error {
// Init will make new dir for file cache if not exist. // Init will make new dir for file cache if not exist.
func (fc *FileCache) Init() { func (fc *FileCache) Init() {
app := filepath.Dir(os.Args[0])
fc.CachePath = filepath.Join(app, fc.CachePath)
if ok, _ := exists(fc.CachePath); !ok { // todo : error handle if ok, _ := exists(fc.CachePath); !ok { // todo : error handle
_ = os.MkdirAll(fc.CachePath, os.ModePerm) // todo : error handle _ = os.MkdirAll(fc.CachePath, os.ModePerm) // todo : error handle
} }
@ -122,36 +117,46 @@ func (fc *FileCache) getCacheFileName(key string) string {
// Get value from file cache. // Get value from file cache.
// if non-exist or expired, return empty string. // if non-exist or expired, return empty string.
func (fc *FileCache) Get(key string) interface{} { func (fc *FileCache) Get(key string) interface{} {
fileData, err := File_get_contents(fc.getCacheFileName(key)) fileData, err := FileGetContents(fc.getCacheFileName(key))
if err != nil { if err != nil {
return "" return ""
} }
var to FileCacheItem var to FileCacheItem
Gob_decode(fileData, &to) GobDecode(fileData, &to)
if to.Expired < time.Now().Unix() { if to.Expired.Before(time.Now()) {
return "" return ""
} }
return to.Data return to.Data
} }
// GetMulti gets values from file cache.
// if non-exist or expired, return empty string.
func (fc *FileCache) GetMulti(keys []string) []interface{} {
var rc []interface{}
for _, key := range keys {
rc = append(rc, fc.Get(key))
}
return rc
}
// Put value into file cache. // Put value into file cache.
// timeout means how long to keep this file, unit of ms. // timeout means how long to keep this file, unit of ms.
// if timeout equals FileCacheEmbedExpiry(default is 0), cache this item forever. // if timeout equals FileCacheEmbedExpiry(default is 0), cache this item forever.
func (fc *FileCache) Put(key string, val interface{}, timeout int64) error { func (fc *FileCache) Put(key string, val interface{}, timeout time.Duration) error {
gob.Register(val) gob.Register(val)
item := FileCacheItem{Data: val} item := FileCacheItem{Data: val}
if timeout == FileCacheEmbedExpiry { if timeout == FileCacheEmbedExpiry {
item.Expired = time.Now().Unix() + (86400 * 365 * 10) // ten years item.Expired = time.Now().Add((86400 * 365 * 10) * time.Second) // ten years
} else { } else {
item.Expired = time.Now().Unix() + timeout item.Expired = time.Now().Add(timeout)
} }
item.Lastaccess = time.Now().Unix() item.Lastaccess = time.Now()
data, err := Gob_encode(item) data, err := GobEncode(item)
if err != nil { if err != nil {
return err return err
} }
return File_put_contents(fc.getCacheFileName(key), data) return FilePutContents(fc.getCacheFileName(key), data)
} }
// Delete file cache value. // Delete file cache value.
@ -163,7 +168,7 @@ func (fc *FileCache) Delete(key string) error {
return nil return nil
} }
// Increase cached int value. // Incr will increase cached int value.
// fc value is saving forever unless Delete. // fc value is saving forever unless Delete.
func (fc *FileCache) Incr(key string) error { func (fc *FileCache) Incr(key string) error {
data := fc.Get(key) data := fc.Get(key)
@ -177,7 +182,7 @@ func (fc *FileCache) Incr(key string) error {
return nil return nil
} }
// Decrease cached int value. // Decr will decrease cached int value.
func (fc *FileCache) Decr(key string) error { func (fc *FileCache) Decr(key string) error {
data := fc.Get(key) data := fc.Get(key)
var decr int var decr int
@ -190,13 +195,13 @@ func (fc *FileCache) Decr(key string) error {
return nil return nil
} }
// Check value is exist. // IsExist check value is exist.
func (fc *FileCache) IsExist(key string) bool { func (fc *FileCache) IsExist(key string) bool {
ret, _ := exists(fc.getCacheFileName(key)) ret, _ := exists(fc.getCacheFileName(key))
return ret return ret
} }
// Clean cached files. // ClearAll will clean cached files.
// not implemented. // not implemented.
func (fc *FileCache) ClearAll() error { func (fc *FileCache) ClearAll() error {
return nil return nil
@ -214,9 +219,9 @@ func exists(path string) (bool, error) {
return false, err return false, err
} }
// Get bytes to file. // FileGetContents Get bytes to file.
// if non-exist, create this file. // if non-exist, create this file.
func File_get_contents(filename string) (data []byte, e error) { func FileGetContents(filename string) (data []byte, e error) {
f, e := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm) f, e := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm)
if e != nil { if e != nil {
return return
@ -234,9 +239,9 @@ func File_get_contents(filename string) (data []byte, e error) {
return return
} }
// Put bytes to file. // FilePutContents Put bytes to file.
// if non-exist, create this file. // if non-exist, create this file.
func File_put_contents(filename string, content []byte) error { func FilePutContents(filename string, content []byte) error {
fp, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm) fp, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm)
if err != nil { if err != nil {
return err return err
@ -246,8 +251,8 @@ func File_put_contents(filename string, content []byte) error {
return err return err
} }
// Gob encodes file cache item. // GobEncode Gob encodes file cache item.
func Gob_encode(data interface{}) ([]byte, error) { func GobEncode(data interface{}) ([]byte, error) {
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
enc := gob.NewEncoder(buf) enc := gob.NewEncoder(buf)
err := enc.Encode(data) err := enc.Encode(data)
@ -257,9 +262,13 @@ func Gob_encode(data interface{}) ([]byte, error) {
return buf.Bytes(), err return buf.Bytes(), err
} }
// Gob decodes file cache item. // GobDecode Gob decodes file cache item.
func Gob_decode(data []byte, to *FileCacheItem) error { func GobDecode(data []byte, to *FileCacheItem) error {
buf := bytes.NewBuffer(data) buf := bytes.NewBuffer(data)
dec := gob.NewDecoder(buf) dec := gob.NewDecoder(buf)
return dec.Decode(&to) return dec.Decode(&to)
} }
func init() {
Register("file", NewFileCache)
}

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// package memcahe for cache provider // Package memcache for cache provider
// //
// depend on github.com/bradfitz/gomemcache/memcache // depend on github.com/bradfitz/gomemcache/memcache
// //
@ -36,22 +36,24 @@ import (
"github.com/bradfitz/gomemcache/memcache" "github.com/bradfitz/gomemcache/memcache"
"time"
"github.com/astaxie/beego/cache" "github.com/astaxie/beego/cache"
) )
// Memcache adapter. // Cache Memcache adapter.
type MemcacheCache struct { type Cache struct {
conn *memcache.Client conn *memcache.Client
conninfo []string conninfo []string
} }
// create new memcache adapter. // NewMemCache create new memcache adapter.
func NewMemCache() *MemcacheCache { func NewMemCache() cache.Cache {
return &MemcacheCache{} return &Cache{}
} }
// get value from memcache. // Get get value from memcache.
func (rc *MemcacheCache) Get(key string) interface{} { func (rc *Cache) Get(key string) interface{} {
if rc.conn == nil { if rc.conn == nil {
if err := rc.connectInit(); err != nil { if err := rc.connectInit(); err != nil {
return err return err
@ -63,8 +65,33 @@ func (rc *MemcacheCache) Get(key string) interface{} {
return nil return nil
} }
// put value to memcache. only support string. // GetMulti get value from memcache.
func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { func (rc *Cache) GetMulti(keys []string) []interface{} {
size := len(keys)
var rv []interface{}
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
for i := 0; i < size; i++ {
rv = append(rv, err)
}
return rv
}
}
mv, err := rc.conn.GetMulti(keys)
if err == nil {
for _, v := range mv {
rv = append(rv, string(v.Value))
}
return rv
}
for i := 0; i < size; i++ {
rv = append(rv, err)
}
return rv
}
// Put put value to memcache. only support string.
func (rc *Cache) Put(key string, val interface{}, timeout time.Duration) error {
if rc.conn == nil { if rc.conn == nil {
if err := rc.connectInit(); err != nil { if err := rc.connectInit(); err != nil {
return err return err
@ -74,12 +101,12 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error {
if !ok { if !ok {
return errors.New("val must string") return errors.New("val must string")
} }
item := memcache.Item{Key: key, Value: []byte(v), Expiration: int32(timeout)} item := memcache.Item{Key: key, Value: []byte(v), Expiration: int32(timeout / time.Second)}
return rc.conn.Set(&item) return rc.conn.Set(&item)
} }
// delete value in memcache. // Delete delete value in memcache.
func (rc *MemcacheCache) Delete(key string) error { func (rc *Cache) Delete(key string) error {
if rc.conn == nil { if rc.conn == nil {
if err := rc.connectInit(); err != nil { if err := rc.connectInit(); err != nil {
return err return err
@ -88,8 +115,8 @@ func (rc *MemcacheCache) Delete(key string) error {
return rc.conn.Delete(key) return rc.conn.Delete(key)
} }
// increase counter. // Incr increase counter.
func (rc *MemcacheCache) Incr(key string) error { func (rc *Cache) Incr(key string) error {
if rc.conn == nil { if rc.conn == nil {
if err := rc.connectInit(); err != nil { if err := rc.connectInit(); err != nil {
return err return err
@ -99,8 +126,8 @@ func (rc *MemcacheCache) Incr(key string) error {
return err return err
} }
// decrease counter. // Decr decrease counter.
func (rc *MemcacheCache) Decr(key string) error { func (rc *Cache) Decr(key string) error {
if rc.conn == nil { if rc.conn == nil {
if err := rc.connectInit(); err != nil { if err := rc.connectInit(); err != nil {
return err return err
@ -110,8 +137,8 @@ func (rc *MemcacheCache) Decr(key string) error {
return err return err
} }
// check value exists in memcache. // IsExist check value exists in memcache.
func (rc *MemcacheCache) IsExist(key string) bool { func (rc *Cache) IsExist(key string) bool {
if rc.conn == nil { if rc.conn == nil {
if err := rc.connectInit(); err != nil { if err := rc.connectInit(); err != nil {
return false return false
@ -124,8 +151,8 @@ func (rc *MemcacheCache) IsExist(key string) bool {
return true return true
} }
// clear all cached in memcache. // ClearAll clear all cached in memcache.
func (rc *MemcacheCache) ClearAll() error { func (rc *Cache) ClearAll() error {
if rc.conn == nil { if rc.conn == nil {
if err := rc.connectInit(); err != nil { if err := rc.connectInit(); err != nil {
return err return err
@ -134,10 +161,10 @@ func (rc *MemcacheCache) ClearAll() error {
return rc.conn.FlushAll() return rc.conn.FlushAll()
} }
// start memcache adapter. // StartAndGC start memcache adapter.
// config string is like {"conn":"connection info"}. // config string is like {"conn":"connection info"}.
// if connecting error, return. // if connecting error, return.
func (rc *MemcacheCache) StartAndGC(config string) error { func (rc *Cache) StartAndGC(config string) error {
var cf map[string]string var cf map[string]string
json.Unmarshal([]byte(config), &cf) json.Unmarshal([]byte(config), &cf)
if _, ok := cf["conn"]; !ok { if _, ok := cf["conn"]; !ok {
@ -153,11 +180,11 @@ func (rc *MemcacheCache) StartAndGC(config string) error {
} }
// connect to memcache and keep the connection. // connect to memcache and keep the connection.
func (rc *MemcacheCache) connectInit() error { func (rc *Cache) connectInit() error {
rc.conn = memcache.New(rc.conninfo...) rc.conn = memcache.New(rc.conninfo...)
return nil return nil
} }
func init() { func init() {
cache.Register("memcache", NewMemCache()) cache.Register("memcache", NewMemCache)
} }

108
cache/memcache/memcache_test.go vendored Normal file
View File

@ -0,0 +1,108 @@
// Copyright 2014 beego Author. All Rights Reserved.
//
// 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.
package memcache
import (
_ "github.com/bradfitz/gomemcache/memcache"
"strconv"
"testing"
"time"
"github.com/astaxie/beego/cache"
)
func TestMemcacheCache(t *testing.T) {
bm, err := cache.NewCache("memcache", `{"conn": "127.0.0.1:11211"}`)
if err != nil {
t.Error("init err")
}
timeoutDuration := 10 * time.Second
if err = bm.Put("astaxie", "1", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if !bm.IsExist("astaxie") {
t.Error("check err")
}
time.Sleep(11 * time.Second)
if bm.IsExist("astaxie") {
t.Error("check err")
}
if err = bm.Put("astaxie", "1", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if v, err := strconv.Atoi(bm.Get("astaxie").(string)); err != nil || v != 1 {
t.Error("get err")
}
if err = bm.Incr("astaxie"); err != nil {
t.Error("Incr Error", err)
}
if v, err := strconv.Atoi(bm.Get("astaxie").(string)); err != nil || v != 2 {
t.Error("get err")
}
if err = bm.Decr("astaxie"); err != nil {
t.Error("Decr Error", err)
}
if v, err := strconv.Atoi(bm.Get("astaxie").(string)); err != nil || v != 1 {
t.Error("get err")
}
bm.Delete("astaxie")
if bm.IsExist("astaxie") {
t.Error("delete err")
}
//test string
if err = bm.Put("astaxie", "author", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if !bm.IsExist("astaxie") {
t.Error("check err")
}
if v := bm.Get("astaxie").(string); v != "author" {
t.Error("get err")
}
//test GetMulti
if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if !bm.IsExist("astaxie1") {
t.Error("check err")
}
vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
if len(vv) != 2 {
t.Error("GetMulti ERROR")
}
if vv[0].(string) != "author" && vv[0].(string) != "author1" {
t.Error("GetMulti ERROR")
}
if vv[1].(string) != "author1" && vv[1].(string) != "author" {
t.Error("GetMulti ERROR")
}
// test clear all
if err = bm.ClearAll(); err != nil {
t.Error("clear all err")
}
}

126
cache/memory.go vendored
View File

@ -17,34 +17,41 @@ package cache
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"sync" "sync"
"time" "time"
) )
var ( var (
// clock time of recycling the expired cache items in memory. // DefaultEvery means the clock time of recycling the expired cache items in memory.
DefaultEvery int = 60 // 1 minute DefaultEvery = 60 // 1 minute
) )
// Memory cache item. // MemoryItem store memory cache item.
type MemoryItem struct { type MemoryItem struct {
val interface{} val interface{}
Lastaccess time.Time createdTime time.Time
expired int64 lifespan time.Duration
} }
// Memory cache adapter. func (mi *MemoryItem) isExpire() bool {
// 0 means forever
if mi.lifespan == 0 {
return false
}
return time.Now().Sub(mi.createdTime) > mi.lifespan
}
// MemoryCache is Memory cache adapter.
// it contains a RW locker for safe map storage. // it contains a RW locker for safe map storage.
type MemoryCache struct { type MemoryCache struct {
lock sync.RWMutex sync.RWMutex
dur time.Duration dur time.Duration
items map[string]*MemoryItem items map[string]*MemoryItem
Every int // run an expiration check Every clock time Every int // run an expiration check Every clock time
} }
// NewMemoryCache returns a new MemoryCache. // NewMemoryCache returns a new MemoryCache.
func NewMemoryCache() *MemoryCache { func NewMemoryCache() Cache {
cache := MemoryCache{items: make(map[string]*MemoryItem)} cache := MemoryCache{items: make(map[string]*MemoryItem)}
return &cache return &cache
} }
@ -52,11 +59,10 @@ func NewMemoryCache() *MemoryCache {
// Get cache from memory. // Get cache from memory.
// if non-existed or expired, return nil. // if non-existed or expired, return nil.
func (bc *MemoryCache) Get(name string) interface{} { func (bc *MemoryCache) Get(name string) interface{} {
bc.lock.RLock() bc.RLock()
defer bc.lock.RUnlock() defer bc.RUnlock()
if itm, ok := bc.items[name]; ok { if itm, ok := bc.items[name]; ok {
if (time.Now().Unix() - itm.Lastaccess.Unix()) > itm.expired { if itm.isExpire() {
go bc.Delete(name)
return nil return nil
} }
return itm.val return itm.val
@ -64,23 +70,33 @@ func (bc *MemoryCache) Get(name string) interface{} {
return nil return nil
} }
// GetMulti gets caches from memory.
// if non-existed or expired, return nil.
func (bc *MemoryCache) GetMulti(names []string) []interface{} {
var rc []interface{}
for _, name := range names {
rc = append(rc, bc.Get(name))
}
return rc
}
// Put cache to memory. // Put cache to memory.
// if expired is 0, it will be cleaned by next gc operation ( default gc clock is 1 minute). // if lifespan is 0, it will be forever till restart.
func (bc *MemoryCache) Put(name string, value interface{}, expired int64) error { func (bc *MemoryCache) Put(name string, value interface{}, lifespan time.Duration) error {
bc.lock.Lock() bc.Lock()
defer bc.lock.Unlock() defer bc.Unlock()
bc.items[name] = &MemoryItem{ bc.items[name] = &MemoryItem{
val: value, val: value,
Lastaccess: time.Now(), createdTime: time.Now(),
expired: expired, lifespan: lifespan,
} }
return nil return nil
} }
/// Delete cache in memory. // Delete cache in memory.
func (bc *MemoryCache) Delete(name string) error { func (bc *MemoryCache) Delete(name string) error {
bc.lock.Lock() bc.Lock()
defer bc.lock.Unlock() defer bc.Unlock()
if _, ok := bc.items[name]; !ok { if _, ok := bc.items[name]; !ok {
return errors.New("key not exist") return errors.New("key not exist")
} }
@ -91,11 +107,11 @@ func (bc *MemoryCache) Delete(name string) error {
return nil return nil
} }
// Increase cache counter in memory. // Incr increase cache counter in memory.
// it supports int,int64,int32,uint,uint64,uint32. // it supports int,int32,int64,uint,uint32,uint64.
func (bc *MemoryCache) Incr(key string) error { func (bc *MemoryCache) Incr(key string) error {
bc.lock.RLock() bc.RLock()
defer bc.lock.RUnlock() defer bc.RUnlock()
itm, ok := bc.items[key] itm, ok := bc.items[key]
if !ok { if !ok {
return errors.New("key not exist") return errors.New("key not exist")
@ -103,10 +119,10 @@ func (bc *MemoryCache) Incr(key string) error {
switch itm.val.(type) { switch itm.val.(type) {
case int: case int:
itm.val = itm.val.(int) + 1 itm.val = itm.val.(int) + 1
case int64:
itm.val = itm.val.(int64) + 1
case int32: case int32:
itm.val = itm.val.(int32) + 1 itm.val = itm.val.(int32) + 1
case int64:
itm.val = itm.val.(int64) + 1
case uint: case uint:
itm.val = itm.val.(uint) + 1 itm.val = itm.val.(uint) + 1
case uint32: case uint32:
@ -114,15 +130,15 @@ func (bc *MemoryCache) Incr(key string) error {
case uint64: case uint64:
itm.val = itm.val.(uint64) + 1 itm.val = itm.val.(uint64) + 1
default: default:
return errors.New("item val is not int int64 int32") return errors.New("item val is not (u)int (u)int32 (u)int64")
} }
return nil return nil
} }
// Decrease counter in memory. // Decr decrease counter in memory.
func (bc *MemoryCache) Decr(key string) error { func (bc *MemoryCache) Decr(key string) error {
bc.lock.RLock() bc.RLock()
defer bc.lock.RUnlock() defer bc.RUnlock()
itm, ok := bc.items[key] itm, ok := bc.items[key]
if !ok { if !ok {
return errors.New("key not exist") return errors.New("key not exist")
@ -158,23 +174,25 @@ func (bc *MemoryCache) Decr(key string) error {
return nil return nil
} }
// check cache exist in memory. // IsExist check cache exist in memory.
func (bc *MemoryCache) IsExist(name string) bool { func (bc *MemoryCache) IsExist(name string) bool {
bc.lock.RLock() bc.RLock()
defer bc.lock.RUnlock() defer bc.RUnlock()
_, ok := bc.items[name] if v, ok := bc.items[name]; ok {
return ok return !v.isExpire()
}
return false
} }
// delete all cache in memory. // ClearAll will delete all cache in memory.
func (bc *MemoryCache) ClearAll() error { func (bc *MemoryCache) ClearAll() error {
bc.lock.Lock() bc.Lock()
defer bc.lock.Unlock() defer bc.Unlock()
bc.items = make(map[string]*MemoryItem) bc.items = make(map[string]*MemoryItem)
return nil return nil
} }
// start memory cache. it will check expiration in every clock time. // StartAndGC start memory cache. it will check expiration in every clock time.
func (bc *MemoryCache) StartAndGC(config string) error { func (bc *MemoryCache) StartAndGC(config string) error {
var cf map[string]int var cf map[string]int
json.Unmarshal([]byte(config), &cf) json.Unmarshal([]byte(config), &cf)
@ -182,10 +200,7 @@ func (bc *MemoryCache) StartAndGC(config string) error {
cf = make(map[string]int) cf = make(map[string]int)
cf["interval"] = DefaultEvery cf["interval"] = DefaultEvery
} }
dur, err := time.ParseDuration(fmt.Sprintf("%ds", cf["interval"])) dur := time.Duration(cf["interval"]) * time.Second
if err != nil {
return err
}
bc.Every = cf["interval"] bc.Every = cf["interval"]
bc.dur = dur bc.dur = dur
go bc.vaccuum() go bc.vaccuum()
@ -202,21 +217,22 @@ func (bc *MemoryCache) vaccuum() {
if bc.items == nil { if bc.items == nil {
return return
} }
for name, _ := range bc.items { for name := range bc.items {
bc.item_expired(name) bc.itemExpired(name)
} }
} }
} }
// item_expired returns true if an item is expired. // itemExpired returns true if an item is expired.
func (bc *MemoryCache) item_expired(name string) bool { func (bc *MemoryCache) itemExpired(name string) bool {
bc.lock.Lock() bc.Lock()
defer bc.lock.Unlock() defer bc.Unlock()
itm, ok := bc.items[name] itm, ok := bc.items[name]
if !ok { if !ok {
return true return true
} }
if time.Now().Unix()-itm.Lastaccess.Unix() >= itm.expired { if itm.isExpire() {
delete(bc.items, name) delete(bc.items, name)
return true return true
} }
@ -224,5 +240,5 @@ func (bc *MemoryCache) item_expired(name string) bool {
} }
func init() { func init() {
Register("memory", NewMemoryCache()) Register("memory", NewMemoryCache)
} }

119
cache/redis/redis.go vendored
View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// package redis for cache provider // Package redis for cache provider
// //
// depend on github.com/garyburd/redigo/redis // depend on github.com/garyburd/redigo/redis
// //
@ -32,6 +32,7 @@ package redis
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"strconv"
"time" "time"
"github.com/garyburd/redigo/redis" "github.com/garyburd/redigo/redis"
@ -40,24 +41,26 @@ import (
) )
var ( var (
// the collection name of redis for cache adapter. // DefaultKey the collection name of redis for cache adapter.
DefaultKey string = "beecacheRedis" DefaultKey = "beecacheRedis"
) )
// Redis cache adapter. // Cache is Redis cache adapter.
type RedisCache struct { type Cache struct {
p *redis.Pool // redis connection pool p *redis.Pool // redis connection pool
conninfo string conninfo string
dbNum int
key string key string
password string
} }
// create new redis cache with default collection name. // NewRedisCache create new redis cache with default collection name.
func NewRedisCache() *RedisCache { func NewRedisCache() cache.Cache {
return &RedisCache{key: DefaultKey} return &Cache{key: DefaultKey}
} }
// actually do the redis cmds // actually do the redis cmds
func (rc *RedisCache) do(commandName string, args ...interface{}) (reply interface{}, err error) { func (rc *Cache) do(commandName string, args ...interface{}) (reply interface{}, err error) {
c := rc.p.Get() c := rc.p.Get()
defer c.Close() defer c.Close()
@ -65,17 +68,50 @@ func (rc *RedisCache) do(commandName string, args ...interface{}) (reply interfa
} }
// Get cache from redis. // Get cache from redis.
func (rc *RedisCache) Get(key string) interface{} { func (rc *Cache) Get(key string) interface{} {
if v, err := rc.do("GET", key); err == nil { if v, err := rc.do("GET", key); err == nil {
return v return v
} }
return nil return nil
} }
// put cache to redis. // GetMulti get cache from redis.
func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error { func (rc *Cache) GetMulti(keys []string) []interface{} {
size := len(keys)
var rv []interface{}
c := rc.p.Get()
defer c.Close()
var err error var err error
if _, err = rc.do("SETEX", key, timeout, val); err != nil { for _, key := range keys {
err = c.Send("GET", key)
if err != nil {
goto ERROR
}
}
if err = c.Flush(); err != nil {
goto ERROR
}
for i := 0; i < size; i++ {
if v, err := c.Receive(); err == nil {
rv = append(rv, v.([]byte))
} else {
rv = append(rv, err)
}
}
return rv
ERROR:
rv = rv[0:0]
for i := 0; i < size; i++ {
rv = append(rv, nil)
}
return rv
}
// Put put cache to redis.
func (rc *Cache) Put(key string, val interface{}, timeout time.Duration) error {
var err error
if _, err = rc.do("SETEX", key, int64(timeout/time.Second), val); err != nil {
return err return err
} }
@ -85,8 +121,8 @@ func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
return err return err
} }
// delete cache in redis. // Delete delete cache in redis.
func (rc *RedisCache) Delete(key string) error { func (rc *Cache) Delete(key string) error {
var err error var err error
if _, err = rc.do("DEL", key); err != nil { if _, err = rc.do("DEL", key); err != nil {
return err return err
@ -95,8 +131,8 @@ func (rc *RedisCache) Delete(key string) error {
return err return err
} }
// check cache's existence in redis. // IsExist check cache's existence in redis.
func (rc *RedisCache) IsExist(key string) bool { func (rc *Cache) IsExist(key string) bool {
v, err := redis.Bool(rc.do("EXISTS", key)) v, err := redis.Bool(rc.do("EXISTS", key))
if err != nil { if err != nil {
return false return false
@ -109,20 +145,20 @@ func (rc *RedisCache) IsExist(key string) bool {
return v return v
} }
// increase counter in redis. // Incr increase counter in redis.
func (rc *RedisCache) Incr(key string) error { func (rc *Cache) Incr(key string) error {
_, err := redis.Bool(rc.do("INCRBY", key, 1)) _, err := redis.Bool(rc.do("INCRBY", key, 1))
return err return err
} }
// decrease counter in redis. // Decr decrease counter in redis.
func (rc *RedisCache) Decr(key string) error { func (rc *Cache) Decr(key string) error {
_, err := redis.Bool(rc.do("INCRBY", key, -1)) _, err := redis.Bool(rc.do("INCRBY", key, -1))
return err return err
} }
// clean all cache in redis. delete this redis collection. // ClearAll clean all cache in redis. delete this redis collection.
func (rc *RedisCache) ClearAll() error { func (rc *Cache) ClearAll() error {
cachedKeys, err := redis.Strings(rc.do("HKEYS", rc.key)) cachedKeys, err := redis.Strings(rc.do("HKEYS", rc.key))
if err != nil { if err != nil {
return err return err
@ -136,24 +172,31 @@ func (rc *RedisCache) ClearAll() error {
return err return err
} }
// start redis cache adapter. // StartAndGC start redis cache adapter.
// config is like {"key":"collection key","conn":"connection info"} // config is like {"key":"collection key","conn":"connection info","dbNum":"0"}
// the cache item in redis are stored forever, // the cache item in redis are stored forever,
// so no gc operation. // so no gc operation.
func (rc *RedisCache) StartAndGC(config string) error { func (rc *Cache) StartAndGC(config string) error {
var cf map[string]string var cf map[string]string
json.Unmarshal([]byte(config), &cf) json.Unmarshal([]byte(config), &cf)
if _, ok := cf["key"]; !ok { if _, ok := cf["key"]; !ok {
cf["key"] = DefaultKey cf["key"] = DefaultKey
} }
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")
} }
if _, ok := cf["dbNum"]; !ok {
cf["dbNum"] = "0"
}
if _, ok := cf["password"]; !ok {
cf["password"] = ""
}
rc.key = cf["key"] rc.key = cf["key"]
rc.conninfo = cf["conn"] rc.conninfo = cf["conn"]
rc.dbNum, _ = strconv.Atoi(cf["dbNum"])
rc.password = cf["password"]
rc.connectInit() rc.connectInit()
c := rc.p.Get() c := rc.p.Get()
@ -163,9 +206,25 @@ func (rc *RedisCache) StartAndGC(config string) error {
} }
// connect to redis. // connect to redis.
func (rc *RedisCache) connectInit() { func (rc *Cache) connectInit() {
dialFunc := func() (c redis.Conn, err error) { dialFunc := func() (c redis.Conn, err error) {
c, err = redis.Dial("tcp", rc.conninfo) c, err = redis.Dial("tcp", rc.conninfo)
if err != nil {
return nil, err
}
if rc.password != "" {
if _, err := c.Do("AUTH", rc.password); err != nil {
c.Close()
return nil, err
}
}
_, selecterr := c.Do("SELECT", rc.dbNum)
if selecterr != nil {
c.Close()
return nil, selecterr
}
return return
} }
// initialize a new pool // initialize a new pool
@ -177,5 +236,5 @@ func (rc *RedisCache) connectInit() {
} }
func init() { func init() {
cache.Register("redis", NewRedisCache()) cache.Register("redis", NewRedisCache)
} }

View File

@ -28,19 +28,20 @@ func TestRedisCache(t *testing.T) {
if err != nil { if err != nil {
t.Error("init err") t.Error("init err")
} }
if err = bm.Put("astaxie", 1, 10); err != nil { timeoutDuration := 10 * time.Second
if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
t.Error("set Error", err) t.Error("set Error", err)
} }
if !bm.IsExist("astaxie") { if !bm.IsExist("astaxie") {
t.Error("check err") t.Error("check err")
} }
time.Sleep(10 * time.Second) time.Sleep(11 * time.Second)
if bm.IsExist("astaxie") { if bm.IsExist("astaxie") {
t.Error("check err") t.Error("check err")
} }
if err = bm.Put("astaxie", 1, 10); err != nil { if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
t.Error("set Error", err) t.Error("set Error", err)
} }
@ -67,8 +68,9 @@ func TestRedisCache(t *testing.T) {
if bm.IsExist("astaxie") { if bm.IsExist("astaxie") {
t.Error("delete err") t.Error("delete err")
} }
//test string //test string
if err = bm.Put("astaxie", "author", 10); err != nil { if err = bm.Put("astaxie", "author", timeoutDuration); err != nil {
t.Error("set Error", err) t.Error("set Error", err)
} }
if !bm.IsExist("astaxie") { if !bm.IsExist("astaxie") {
@ -78,6 +80,26 @@ func TestRedisCache(t *testing.T) {
if v, _ := redis.String(bm.Get("astaxie"), err); v != "author" { if v, _ := redis.String(bm.Get("astaxie"), err); v != "author" {
t.Error("get err") t.Error("get err")
} }
//test GetMulti
if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if !bm.IsExist("astaxie1") {
t.Error("check err")
}
vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
if len(vv) != 2 {
t.Error("GetMulti ERROR")
}
if v, _ := redis.String(vv[0], nil); v != "author" {
t.Error("GetMulti ERROR")
}
if v, _ := redis.String(vv[1], nil); v != "author1" {
t.Error("GetMulti ERROR")
}
// test clear all // test clear all
if err = bm.ClearAll(); err != nil { if err = bm.ClearAll(); err != nil {
t.Error("clear all err") t.Error("clear all err")

240
cache/ssdb/ssdb.go vendored Normal file
View File

@ -0,0 +1,240 @@
package ssdb
import (
"encoding/json"
"errors"
"strconv"
"strings"
"time"
"github.com/ssdb/gossdb/ssdb"
"github.com/astaxie/beego/cache"
)
// Cache SSDB adapter
type Cache struct {
conn *ssdb.Client
conninfo []string
}
//NewSsdbCache create new ssdb adapter.
func NewSsdbCache() cache.Cache {
return &Cache{}
}
// Get get value from memcache.
func (rc *Cache) Get(key string) interface{} {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return nil
}
}
value, err := rc.conn.Get(key)
if err == nil {
return value
}
return nil
}
// GetMulti get value from memcache.
func (rc *Cache) GetMulti(keys []string) []interface{} {
size := len(keys)
var values []interface{}
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
for i := 0; i < size; i++ {
values = append(values, err)
}
return values
}
}
res, err := rc.conn.Do("multi_get", keys)
resSize := len(res)
if err == nil {
for i := 1; i < resSize; i += 2 {
values = append(values, string(res[i+1]))
}
return values
}
for i := 0; i < size; i++ {
values = append(values, err)
}
return values
}
// DelMulti get value from memcache.
func (rc *Cache) DelMulti(keys []string) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
_, err := rc.conn.Do("multi_del", keys)
if err != nil {
return err
}
return nil
}
// Put put value to memcache. only support string.
func (rc *Cache) Put(key string, value interface{}, timeout time.Duration) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
v, ok := value.(string)
if !ok {
return errors.New("value must string")
}
var resp []string
var err error
ttl := int(timeout / time.Second)
if ttl < 0 {
resp, err = rc.conn.Do("set", key, v)
} else {
resp, err = rc.conn.Do("setx", key, v, ttl)
}
if err != nil {
return err
}
if len(resp) == 2 && resp[0] == "ok" {
return nil
}
return errors.New("bad response")
}
// Delete delete value in memcache.
func (rc *Cache) Delete(key string) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
_, err := rc.conn.Del(key)
if err != nil {
return err
}
return nil
}
// Incr increase counter.
func (rc *Cache) Incr(key string) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
_, err := rc.conn.Do("incr", key, 1)
return err
}
// Decr decrease counter.
func (rc *Cache) Decr(key string) error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
_, err := rc.conn.Do("incr", key, -1)
return err
}
// IsExist check value exists in memcache.
func (rc *Cache) IsExist(key string) bool {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return false
}
}
resp, err := rc.conn.Do("exists", key)
if err != nil {
return false
}
if resp[1] == "1" {
return true
}
return false
}
// ClearAll clear all cached in memcache.
func (rc *Cache) ClearAll() error {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
keyStart, keyEnd, limit := "", "", 50
resp, err := rc.Scan(keyStart, keyEnd, limit)
for err == nil {
size := len(resp)
if size == 1 {
return nil
}
keys := []string{}
for i := 1; i < size; i += 2 {
keys = append(keys, string(resp[i]))
}
_, e := rc.conn.Do("multi_del", keys)
if e != nil {
return e
}
keyStart = resp[size-2]
resp, err = rc.Scan(keyStart, keyEnd, limit)
}
return err
}
// Scan key all cached in ssdb.
func (rc *Cache) Scan(keyStart string, keyEnd string, limit int) ([]string, error) {
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return nil, err
}
}
resp, err := rc.conn.Do("scan", keyStart, keyEnd, limit)
if err != nil {
return nil, err
}
return resp, nil
}
// StartAndGC start memcache adapter.
// config string is like {"conn":"connection info"}.
// if connecting error, return.
func (rc *Cache) StartAndGC(config string) error {
var cf map[string]string
json.Unmarshal([]byte(config), &cf)
if _, ok := cf["conn"]; !ok {
return errors.New("config has no conn key")
}
rc.conninfo = strings.Split(cf["conn"], ";")
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
return nil
}
// connect to memcache and keep the connection.
func (rc *Cache) connectInit() error {
conninfoArray := strings.Split(rc.conninfo[0], ":")
host := conninfoArray[0]
port, e := strconv.Atoi(conninfoArray[1])
if e != nil {
return e
}
var err error
rc.conn, err = ssdb.Connect(host, port)
if err != nil {
return err
}
return nil
}
func init() {
cache.Register("ssdb", NewSsdbCache)
}

103
cache/ssdb/ssdb_test.go vendored Normal file
View File

@ -0,0 +1,103 @@
package ssdb
import (
"github.com/astaxie/beego/cache"
"strconv"
"testing"
"time"
)
func TestSsdbcacheCache(t *testing.T) {
ssdb, err := cache.NewCache("ssdb", `{"conn": "127.0.0.1:8888"}`)
if err != nil {
t.Error("init err")
}
// test put and exist
if ssdb.IsExist("ssdb") {
t.Error("check err")
}
timeoutDuration := 10 * time.Second
//timeoutDuration := -10*time.Second if timeoutDuration is negtive,it means permanent
if err = ssdb.Put("ssdb", "ssdb", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if !ssdb.IsExist("ssdb") {
t.Error("check err")
}
// Get test done
if err = ssdb.Put("ssdb", "ssdb", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if v := ssdb.Get("ssdb"); v != "ssdb" {
t.Error("get Error")
}
//inc/dec test done
if err = ssdb.Put("ssdb", "2", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if err = ssdb.Incr("ssdb"); err != nil {
t.Error("incr Error", err)
}
if v, err := strconv.Atoi(ssdb.Get("ssdb").(string)); err != nil || v != 3 {
t.Error("get err")
}
if err = ssdb.Decr("ssdb"); err != nil {
t.Error("decr error")
}
// test del
if err = ssdb.Put("ssdb", "3", timeoutDuration); err != nil {
t.Error("set Error", err)
}
if v, err := strconv.Atoi(ssdb.Get("ssdb").(string)); err != nil || v != 3 {
t.Error("get err")
}
if err := ssdb.Delete("ssdb"); err == nil {
if ssdb.IsExist("ssdb") {
t.Error("delete err")
}
}
//test string
if err = ssdb.Put("ssdb", "ssdb", -10*time.Second); err != nil {
t.Error("set Error", err)
}
if !ssdb.IsExist("ssdb") {
t.Error("check err")
}
if v := ssdb.Get("ssdb").(string); v != "ssdb" {
t.Error("get err")
}
//test GetMulti done
if err = ssdb.Put("ssdb1", "ssdb1", -10*time.Second); err != nil {
t.Error("set Error", err)
}
if !ssdb.IsExist("ssdb1") {
t.Error("check err")
}
vv := ssdb.GetMulti([]string{"ssdb", "ssdb1"})
if len(vv) != 2 {
t.Error("getmulti error")
}
if vv[0].(string) != "ssdb" {
t.Error("getmulti error")
}
if vv[1].(string) != "ssdb1" {
t.Error("getmulti error")
}
// test clear all done
if err = ssdb.ClearAll(); err != nil {
t.Error("clear all err")
}
if ssdb.IsExist("ssdb") || ssdb.IsExist("ssdb1") {
t.Error("check err")
}
}

756
config.go
View File

@ -16,160 +16,417 @@ package beego
import ( import (
"fmt" "fmt"
"html/template"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"github.com/astaxie/beego/config" "github.com/astaxie/beego/config"
"github.com/astaxie/beego/logs"
"github.com/astaxie/beego/session" "github.com/astaxie/beego/session"
"github.com/astaxie/beego/utils" "github.com/astaxie/beego/utils"
) )
var ( // Config is the main struct for BConfig
BeeApp *App // beego application type Config struct {
AppName string AppName string //Application name
AppPath string RunMode string //Running Mode: dev | prod
workPath string RouterCaseSensitive bool
AppConfigPath string ServerName string
StaticDir map[string]string RecoverPanic bool
TemplateCache map[string]*template.Template // template caching map CopyRequestBody bool
StaticExtensionsToGzip []string // files with should be compressed with gzip (.js,.css,etc) EnableGzip bool
EnableHttpListen bool MaxMemory int64
HttpAddr string EnableErrorsShow bool
HttpPort int Listen Listen
ListenTCP4 bool WebConfig WebConfig
EnableHttpTLS bool Log LogConfig
HttpsPort int
HttpCertFile string
HttpKeyFile string
RecoverPanic bool // flag of auto recover panic
AutoRender bool // flag of render template automatically
ViewsPath string
AppConfig *beegoAppConfig
RunMode string // run mode, "dev" or "prod"
GlobalSessions *session.Manager // global session mananger
SessionOn bool // flag of starting session auto. default is false.
SessionProvider string // default session provider, memory, mysql , redis ,etc.
SessionName string // the cookie name when saving session id into cookie.
SessionGCMaxLifetime int64 // session gc time for auto cleaning expired session.
SessionSavePath string // if use mysql/redis/file provider, define save path to connection info.
SessionCookieLifeTime int // the life time of session id in cookie.
SessionAutoSetCookie bool // auto setcookie
SessionDomain string // the cookie domain default is empty
UseFcgi bool
UseStdIo bool
MaxMemory int64
EnableGzip bool // flag of enable gzip
DirectoryIndex bool // flag of display directory index. default is false.
HttpServerTimeOut int64
ErrorsShow bool // flag of show errors in page. if true, show error and trace info in page rendered with error template.
XSRFKEY string // xsrf hash salt string.
EnableXSRF bool // flag of enable xsrf.
XSRFExpire int // the expiry of xsrf value.
CopyRequestBody bool // flag of copy raw request body in context.
TemplateLeft string
TemplateRight string
BeegoServerName string // beego server name exported in response header.
EnableAdmin bool // flag of enable admin module to log every request info.
AdminHttpAddr string // http server configurations for admin module.
AdminHttpPort int
FlashName string // name of the flash variable found in response header and cookie
FlashSeperator string // used to seperate flash key:value
AppConfigProvider string // config provider
EnableDocs bool // enable generate docs & server docs API Swagger
RouterCaseSensitive bool // router case sensitive default is true
)
type beegoAppConfig struct {
innerConfig config.ConfigContainer
} }
func newAppConfig(AppConfigProvider, AppConfigPath string) (*beegoAppConfig, error) { // Listen holds for http and https related config
ac, err := config.NewConfig(AppConfigProvider, AppConfigPath) type Listen struct {
Graceful bool // Graceful means use graceful module to start the server
ServerTimeOut int64
ListenTCP4 bool
EnableHTTP bool
HTTPAddr string
HTTPPort int
EnableHTTPS bool
HTTPSAddr string
HTTPSPort int
HTTPSCertFile string
HTTPSKeyFile string
EnableAdmin bool
AdminAddr string
AdminPort int
EnableFcgi bool
EnableStdIo bool // EnableStdIo works with EnableFcgi Use FCGI via standard I/O
}
// WebConfig holds web related config
type WebConfig struct {
AutoRender bool
EnableDocs bool
FlashName string
FlashSeparator string
DirectoryIndex bool
StaticDir map[string]string
StaticExtensionsToGzip []string
TemplateLeft string
TemplateRight string
ViewsPath string
EnableXSRF bool
XSRFKey string
XSRFExpire int
Session SessionConfig
}
// SessionConfig holds session related config
type SessionConfig struct {
SessionOn bool
SessionProvider string
SessionName string
SessionGCMaxLifetime int64
SessionProviderConfig string
SessionCookieLifeTime int
SessionAutoSetCookie bool
SessionDomain string
}
// LogConfig holds Log related config
type LogConfig struct {
AccessLogs bool
FileLineNum bool
Outputs map[string]string // Store Adaptor : config
}
var (
// BConfig is the default config for Application
BConfig *Config
// AppConfig is the instance of Config, store the config information from file
AppConfig *beegoAppConfig
// AppPath is the absolute path to the app
AppPath string
// GlobalSessions is the instance for the session manager
GlobalSessions *session.Manager
// appConfigPath is the path to the config files
appConfigPath string
// appConfigProvider is the provider for the config, default is ini
appConfigProvider = "ini"
)
func init() {
AppPath, _ = filepath.Abs(filepath.Dir(os.Args[0]))
os.Chdir(AppPath)
BConfig = &Config{
AppName: "beego",
RunMode: DEV,
RouterCaseSensitive: true,
ServerName: "beegoServer:" + VERSION,
RecoverPanic: true,
CopyRequestBody: false,
EnableGzip: false,
MaxMemory: 1 << 26, //64MB
EnableErrorsShow: true,
Listen: Listen{
Graceful: false,
ServerTimeOut: 0,
ListenTCP4: false,
EnableHTTP: true,
HTTPAddr: "",
HTTPPort: 8080,
EnableHTTPS: false,
HTTPSAddr: "",
HTTPSPort: 10443,
HTTPSCertFile: "",
HTTPSKeyFile: "",
EnableAdmin: false,
AdminAddr: "",
AdminPort: 8088,
EnableFcgi: false,
EnableStdIo: false,
},
WebConfig: WebConfig{
AutoRender: true,
EnableDocs: false,
FlashName: "BEEGO_FLASH",
FlashSeparator: "BEEGOFLASH",
DirectoryIndex: false,
StaticDir: map[string]string{"/static": "static"},
StaticExtensionsToGzip: []string{".css", ".js"},
TemplateLeft: "{{",
TemplateRight: "}}",
ViewsPath: "views",
EnableXSRF: false,
XSRFKey: "beegoxsrf",
XSRFExpire: 0,
Session: SessionConfig{
SessionOn: false,
SessionProvider: "memory",
SessionName: "beegosessionID",
SessionGCMaxLifetime: 3600,
SessionProviderConfig: "",
SessionCookieLifeTime: 0, //set cookie default is the browser life
SessionAutoSetCookie: true,
SessionDomain: "",
},
},
Log: LogConfig{
AccessLogs: false,
FileLineNum: true,
Outputs: map[string]string{"console": ""},
},
}
appConfigPath = filepath.Join(AppPath, "conf", "app.conf")
if !utils.FileExists(appConfigPath) {
AppConfig = &beegoAppConfig{innerConfig: config.NewFakeConfig()}
return
}
if err := parseConfig(appConfigPath); err != nil {
panic(err)
}
}
// now only support ini, next will support json.
func parseConfig(appConfigPath string) (err error) {
AppConfig, err = newAppConfig(appConfigProvider, appConfigPath)
if err != nil {
return err
}
// set the run mode first
if envRunMode := os.Getenv("BEEGO_RUNMODE"); envRunMode != "" {
BConfig.RunMode = envRunMode
} else if runMode := AppConfig.String("RunMode"); runMode != "" {
BConfig.RunMode = runMode
}
BConfig.AppName = AppConfig.DefaultString("AppName", BConfig.AppName)
BConfig.RecoverPanic = AppConfig.DefaultBool("RecoverPanic", BConfig.RecoverPanic)
BConfig.RouterCaseSensitive = AppConfig.DefaultBool("RouterCaseSensitive", BConfig.RouterCaseSensitive)
BConfig.ServerName = AppConfig.DefaultString("ServerName", BConfig.ServerName)
BConfig.EnableGzip = AppConfig.DefaultBool("EnableGzip", BConfig.EnableGzip)
BConfig.EnableErrorsShow = AppConfig.DefaultBool("EnableErrorsShow", BConfig.EnableErrorsShow)
BConfig.CopyRequestBody = AppConfig.DefaultBool("CopyRequestBody", BConfig.CopyRequestBody)
BConfig.MaxMemory = AppConfig.DefaultInt64("MaxMemory", BConfig.MaxMemory)
BConfig.Listen.Graceful = AppConfig.DefaultBool("Graceful", BConfig.Listen.Graceful)
BConfig.Listen.HTTPAddr = AppConfig.String("HTTPAddr")
BConfig.Listen.HTTPPort = AppConfig.DefaultInt("HTTPPort", BConfig.Listen.HTTPPort)
BConfig.Listen.ListenTCP4 = AppConfig.DefaultBool("ListenTCP4", BConfig.Listen.ListenTCP4)
BConfig.Listen.EnableHTTP = AppConfig.DefaultBool("EnableHTTP", BConfig.Listen.EnableHTTP)
BConfig.Listen.EnableHTTPS = AppConfig.DefaultBool("EnableHTTPS", BConfig.Listen.EnableHTTPS)
BConfig.Listen.HTTPSAddr = AppConfig.DefaultString("HTTPSAddr", BConfig.Listen.HTTPSAddr)
BConfig.Listen.HTTPSPort = AppConfig.DefaultInt("HTTPSPort", BConfig.Listen.HTTPSPort)
BConfig.Listen.HTTPSCertFile = AppConfig.DefaultString("HTTPSCertFile", BConfig.Listen.HTTPSCertFile)
BConfig.Listen.HTTPSKeyFile = AppConfig.DefaultString("HTTPSKeyFile", BConfig.Listen.HTTPSKeyFile)
BConfig.Listen.EnableAdmin = AppConfig.DefaultBool("EnableAdmin", BConfig.Listen.EnableAdmin)
BConfig.Listen.AdminAddr = AppConfig.DefaultString("AdminAddr", BConfig.Listen.AdminAddr)
BConfig.Listen.AdminPort = AppConfig.DefaultInt("AdminPort", BConfig.Listen.AdminPort)
BConfig.Listen.EnableFcgi = AppConfig.DefaultBool("EnableFcgi", BConfig.Listen.EnableFcgi)
BConfig.Listen.EnableStdIo = AppConfig.DefaultBool("EnableStdIo", BConfig.Listen.EnableStdIo)
BConfig.Listen.ServerTimeOut = AppConfig.DefaultInt64("ServerTimeOut", BConfig.Listen.ServerTimeOut)
BConfig.WebConfig.AutoRender = AppConfig.DefaultBool("AutoRender", BConfig.WebConfig.AutoRender)
BConfig.WebConfig.ViewsPath = AppConfig.DefaultString("ViewsPath", BConfig.WebConfig.ViewsPath)
BConfig.WebConfig.DirectoryIndex = AppConfig.DefaultBool("DirectoryIndex", BConfig.WebConfig.DirectoryIndex)
BConfig.WebConfig.FlashName = AppConfig.DefaultString("FlashName", BConfig.WebConfig.FlashName)
BConfig.WebConfig.FlashSeparator = AppConfig.DefaultString("FlashSeparator", BConfig.WebConfig.FlashSeparator)
BConfig.WebConfig.EnableDocs = AppConfig.DefaultBool("EnableDocs", BConfig.WebConfig.EnableDocs)
BConfig.WebConfig.XSRFKey = AppConfig.DefaultString("XSRFKEY", BConfig.WebConfig.XSRFKey)
BConfig.WebConfig.EnableXSRF = AppConfig.DefaultBool("EnableXSRF", BConfig.WebConfig.EnableXSRF)
BConfig.WebConfig.XSRFExpire = AppConfig.DefaultInt("XSRFExpire", BConfig.WebConfig.XSRFExpire)
BConfig.WebConfig.TemplateLeft = AppConfig.DefaultString("TemplateLeft", BConfig.WebConfig.TemplateLeft)
BConfig.WebConfig.TemplateRight = AppConfig.DefaultString("TemplateRight", BConfig.WebConfig.TemplateRight)
BConfig.WebConfig.Session.SessionOn = AppConfig.DefaultBool("SessionOn", BConfig.WebConfig.Session.SessionOn)
BConfig.WebConfig.Session.SessionProvider = AppConfig.DefaultString("SessionProvider", BConfig.WebConfig.Session.SessionProvider)
BConfig.WebConfig.Session.SessionName = AppConfig.DefaultString("SessionName", BConfig.WebConfig.Session.SessionName)
BConfig.WebConfig.Session.SessionProviderConfig = AppConfig.DefaultString("SessionProviderConfig", BConfig.WebConfig.Session.SessionProviderConfig)
BConfig.WebConfig.Session.SessionGCMaxLifetime = AppConfig.DefaultInt64("SessionGCMaxLifetime", BConfig.WebConfig.Session.SessionGCMaxLifetime)
BConfig.WebConfig.Session.SessionCookieLifeTime = AppConfig.DefaultInt("SessionCookieLifeTime", BConfig.WebConfig.Session.SessionCookieLifeTime)
BConfig.WebConfig.Session.SessionAutoSetCookie = AppConfig.DefaultBool("SessionAutoSetCookie", BConfig.WebConfig.Session.SessionAutoSetCookie)
BConfig.WebConfig.Session.SessionDomain = AppConfig.DefaultString("SessionDomain", BConfig.WebConfig.Session.SessionDomain)
BConfig.Log.AccessLogs = AppConfig.DefaultBool("LogAccessLogs", BConfig.Log.AccessLogs)
BConfig.Log.FileLineNum = AppConfig.DefaultBool("LogFileLineNum", BConfig.Log.FileLineNum)
if sd := AppConfig.String("StaticDir"); sd != "" {
for k := range BConfig.WebConfig.StaticDir {
delete(BConfig.WebConfig.StaticDir, k)
}
sds := strings.Fields(sd)
for _, v := range sds {
if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 {
BConfig.WebConfig.StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[1]
} else {
BConfig.WebConfig.StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[0]
}
}
}
if sgz := AppConfig.String("StaticExtensionsToGzip"); sgz != "" {
extensions := strings.Split(sgz, ",")
fileExts := []string{}
for _, ext := range extensions {
ext = strings.TrimSpace(ext)
if ext == "" {
continue
}
if !strings.HasPrefix(ext, ".") {
ext = "." + ext
}
fileExts = append(fileExts, ext)
}
if len(fileExts) > 0 {
BConfig.WebConfig.StaticExtensionsToGzip = fileExts
}
}
if lo := AppConfig.String("LogOutputs"); lo != "" {
los := strings.Split(lo, ";")
for _, v := range los {
if logType2Config := strings.SplitN(v, ",", 2); len(logType2Config) == 2 {
BConfig.Log.Outputs[logType2Config[0]] = logType2Config[1]
} else {
continue
}
}
}
//init log
BeeLogger.Reset()
for adaptor, config := range BConfig.Log.Outputs {
err = BeeLogger.SetLogger(adaptor, config)
if err != nil {
fmt.Printf("%s with the config `%s` got err:%s\n", adaptor, config, err)
}
}
SetLogFuncCall(BConfig.Log.FileLineNum)
return nil
}
// LoadAppConfig allow developer to apply a config file
func LoadAppConfig(adapterName, configPath string) error {
absConfigPath, err := filepath.Abs(configPath)
if err != nil {
return err
}
if !utils.FileExists(absConfigPath) {
return fmt.Errorf("the target config file: %s don't exist", configPath)
}
if absConfigPath == appConfigPath {
return nil
}
appConfigPath = absConfigPath
appConfigProvider = adapterName
return parseConfig(appConfigPath)
}
type beegoAppConfig struct {
innerConfig config.Configer
}
func newAppConfig(appConfigProvider, appConfigPath string) (*beegoAppConfig, error) {
ac, err := config.NewConfig(appConfigProvider, appConfigPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
rac := &beegoAppConfig{ac} return &beegoAppConfig{ac}, nil
return rac, nil
} }
func (b *beegoAppConfig) Set(key, val string) error { func (b *beegoAppConfig) Set(key, val string) error {
if err := b.innerConfig.Set(BConfig.RunMode+"::"+key, val); err != nil {
return err
}
return b.innerConfig.Set(key, val) return b.innerConfig.Set(key, val)
} }
func (b *beegoAppConfig) String(key string) string { func (b *beegoAppConfig) String(key string) string {
v := b.innerConfig.String(RunMode + "::" + key) if v := b.innerConfig.String(BConfig.RunMode + "::" + key); v != "" {
if v == "" { return v
return b.innerConfig.String(key)
} }
return v return b.innerConfig.String(key)
} }
func (b *beegoAppConfig) Strings(key string) []string { func (b *beegoAppConfig) Strings(key string) []string {
v := b.innerConfig.Strings(RunMode + "::" + key) if v := b.innerConfig.Strings(BConfig.RunMode + "::" + key); v[0] != "" {
if len(v) == 0 { return v
return b.innerConfig.Strings(key)
} }
return v return b.innerConfig.Strings(key)
} }
func (b *beegoAppConfig) Int(key string) (int, error) { func (b *beegoAppConfig) Int(key string) (int, error) {
v, err := b.innerConfig.Int(RunMode + "::" + key) if v, err := b.innerConfig.Int(BConfig.RunMode + "::" + key); err == nil {
if err != nil { return v, nil
return b.innerConfig.Int(key)
} }
return v, nil return b.innerConfig.Int(key)
} }
func (b *beegoAppConfig) Int64(key string) (int64, error) { func (b *beegoAppConfig) Int64(key string) (int64, error) {
v, err := b.innerConfig.Int64(RunMode + "::" + key) if v, err := b.innerConfig.Int64(BConfig.RunMode + "::" + key); err == nil {
if err != nil { return v, nil
return b.innerConfig.Int64(key)
} }
return v, nil return b.innerConfig.Int64(key)
} }
func (b *beegoAppConfig) Bool(key string) (bool, error) { func (b *beegoAppConfig) Bool(key string) (bool, error) {
v, err := b.innerConfig.Bool(RunMode + "::" + key) if v, err := b.innerConfig.Bool(BConfig.RunMode + "::" + key); err == nil {
if err != nil { return v, nil
return b.innerConfig.Bool(key)
} }
return v, nil return b.innerConfig.Bool(key)
} }
func (b *beegoAppConfig) Float(key string) (float64, error) { func (b *beegoAppConfig) Float(key string) (float64, error) {
v, err := b.innerConfig.Float(RunMode + "::" + key) if v, err := b.innerConfig.Float(BConfig.RunMode + "::" + key); err == nil {
if err != nil { return v, nil
return b.innerConfig.Float(key)
} }
return v, nil return b.innerConfig.Float(key)
} }
func (b *beegoAppConfig) DefaultString(key string, defaultval string) string { func (b *beegoAppConfig) DefaultString(key string, defaultVal string) string {
return b.innerConfig.DefaultString(key, defaultval) if v := b.String(key); v != "" {
return v
}
return defaultVal
} }
func (b *beegoAppConfig) DefaultStrings(key string, defaultval []string) []string { func (b *beegoAppConfig) DefaultStrings(key string, defaultVal []string) []string {
return b.innerConfig.DefaultStrings(key, defaultval) if v := b.Strings(key); len(v) != 0 {
return v
}
return defaultVal
} }
func (b *beegoAppConfig) DefaultInt(key string, defaultval int) int { func (b *beegoAppConfig) DefaultInt(key string, defaultVal int) int {
return b.innerConfig.DefaultInt(key, defaultval) if v, err := b.Int(key); err == nil {
return v
}
return defaultVal
} }
func (b *beegoAppConfig) DefaultInt64(key string, defaultval int64) int64 { func (b *beegoAppConfig) DefaultInt64(key string, defaultVal int64) int64 {
return b.innerConfig.DefaultInt64(key, defaultval) if v, err := b.Int64(key); err == nil {
return v
}
return defaultVal
} }
func (b *beegoAppConfig) DefaultBool(key string, defaultval bool) bool { func (b *beegoAppConfig) DefaultBool(key string, defaultVal bool) bool {
return b.innerConfig.DefaultBool(key, defaultval) if v, err := b.Bool(key); err == nil {
return v
}
return defaultVal
} }
func (b *beegoAppConfig) DefaultFloat(key string, defaultval float64) float64 { func (b *beegoAppConfig) DefaultFloat(key string, defaultVal float64) float64 {
return b.innerConfig.DefaultFloat(key, defaultval) if v, err := b.Float(key); err == nil {
return v
}
return defaultVal
} }
func (b *beegoAppConfig) DIY(key string) (interface{}, error) { func (b *beegoAppConfig) DIY(key string) (interface{}, error) {
@ -183,302 +440,3 @@ func (b *beegoAppConfig) GetSection(section string) (map[string]string, error) {
func (b *beegoAppConfig) SaveConfigFile(filename string) error { func (b *beegoAppConfig) SaveConfigFile(filename string) error {
return b.innerConfig.SaveConfigFile(filename) return b.innerConfig.SaveConfigFile(filename)
} }
func init() {
// create beego application
BeeApp = NewApp()
workPath, _ = os.Getwd()
workPath, _ = filepath.Abs(workPath)
// initialize default configurations
AppPath, _ = filepath.Abs(filepath.Dir(os.Args[0]))
AppConfigPath = filepath.Join(AppPath, "conf", "app.conf")
if workPath != AppPath {
if utils.FileExists(AppConfigPath) {
os.Chdir(AppPath)
} else {
AppConfigPath = filepath.Join(workPath, "conf", "app.conf")
}
}
AppConfigProvider = "ini"
StaticDir = make(map[string]string)
StaticDir["/static"] = "static"
StaticExtensionsToGzip = []string{".css", ".js"}
TemplateCache = make(map[string]*template.Template)
// set this to 0.0.0.0 to make this app available to externally
EnableHttpListen = true //default enable http Listen
HttpAddr = ""
HttpPort = 8080
HttpsPort = 10443
AppName = "beego"
RunMode = "dev" //default runmod
AutoRender = true
RecoverPanic = true
ViewsPath = "views"
SessionOn = false
SessionProvider = "memory"
SessionName = "beegosessionID"
SessionGCMaxLifetime = 3600
SessionSavePath = ""
SessionCookieLifeTime = 0 //set cookie default is the brower life
SessionAutoSetCookie = true
UseFcgi = false
UseStdIo = false
MaxMemory = 1 << 26 //64MB
EnableGzip = false
HttpServerTimeOut = 0
ErrorsShow = true
XSRFKEY = "beegoxsrf"
XSRFExpire = 0
TemplateLeft = "{{"
TemplateRight = "}}"
BeegoServerName = "beegoServer:" + VERSION
EnableAdmin = false
AdminHttpAddr = "127.0.0.1"
AdminHttpPort = 8088
FlashName = "BEEGO_FLASH"
FlashSeperator = "BEEGOFLASH"
RouterCaseSensitive = true
runtime.GOMAXPROCS(runtime.NumCPU())
// init BeeLogger
BeeLogger = logs.NewLogger(10000)
err := BeeLogger.SetLogger("console", "")
if err != nil {
fmt.Println("init console log error:", err)
}
SetLogFuncCall(true)
err = ParseConfig()
if err != nil && os.IsNotExist(err) {
// for init if doesn't have app.conf will not panic
ac := config.NewFakeConfig()
AppConfig = &beegoAppConfig{ac}
Warning(err)
}
}
// ParseConfig parsed default config file.
// now only support ini, next will support json.
func ParseConfig() (err error) {
AppConfig, err = newAppConfig(AppConfigProvider, AppConfigPath)
if err != nil {
return err
}
envRunMode := os.Getenv("BEEGO_RUNMODE")
// set the runmode first
if envRunMode != "" {
RunMode = envRunMode
} else if runmode := AppConfig.String("RunMode"); runmode != "" {
RunMode = runmode
}
HttpAddr = AppConfig.String("HttpAddr")
if v, err := AppConfig.Int("HttpPort"); err == nil {
HttpPort = v
}
if v, err := AppConfig.Bool("ListenTCP4"); err == nil {
ListenTCP4 = v
}
if v, err := AppConfig.Bool("EnableHttpListen"); err == nil {
EnableHttpListen = v
}
if maxmemory, err := AppConfig.Int64("MaxMemory"); err == nil {
MaxMemory = maxmemory
}
if appname := AppConfig.String("AppName"); appname != "" {
AppName = appname
}
if autorender, err := AppConfig.Bool("AutoRender"); err == nil {
AutoRender = autorender
}
if autorecover, err := AppConfig.Bool("RecoverPanic"); err == nil {
RecoverPanic = autorecover
}
if views := AppConfig.String("ViewsPath"); views != "" {
ViewsPath = views
}
if sessionon, err := AppConfig.Bool("SessionOn"); err == nil {
SessionOn = sessionon
}
if sessProvider := AppConfig.String("SessionProvider"); sessProvider != "" {
SessionProvider = sessProvider
}
if sessName := AppConfig.String("SessionName"); sessName != "" {
SessionName = sessName
}
if sesssavepath := AppConfig.String("SessionSavePath"); sesssavepath != "" {
SessionSavePath = sesssavepath
}
if sessMaxLifeTime, err := AppConfig.Int64("SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 {
SessionGCMaxLifetime = sessMaxLifeTime
}
if sesscookielifetime, err := AppConfig.Int("SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 {
SessionCookieLifeTime = sesscookielifetime
}
if usefcgi, err := AppConfig.Bool("UseFcgi"); err == nil {
UseFcgi = usefcgi
}
if enablegzip, err := AppConfig.Bool("EnableGzip"); err == nil {
EnableGzip = enablegzip
}
if directoryindex, err := AppConfig.Bool("DirectoryIndex"); err == nil {
DirectoryIndex = directoryindex
}
if timeout, err := AppConfig.Int64("HttpServerTimeOut"); err == nil {
HttpServerTimeOut = timeout
}
if errorsshow, err := AppConfig.Bool("ErrorsShow"); err == nil {
ErrorsShow = errorsshow
}
if copyrequestbody, err := AppConfig.Bool("CopyRequestBody"); err == nil {
CopyRequestBody = copyrequestbody
}
if xsrfkey := AppConfig.String("XSRFKEY"); xsrfkey != "" {
XSRFKEY = xsrfkey
}
if enablexsrf, err := AppConfig.Bool("EnableXSRF"); err == nil {
EnableXSRF = enablexsrf
}
if expire, err := AppConfig.Int("XSRFExpire"); err == nil {
XSRFExpire = expire
}
if tplleft := AppConfig.String("TemplateLeft"); tplleft != "" {
TemplateLeft = tplleft
}
if tplright := AppConfig.String("TemplateRight"); tplright != "" {
TemplateRight = tplright
}
if httptls, err := AppConfig.Bool("EnableHttpTLS"); err == nil {
EnableHttpTLS = httptls
}
if httpsport, err := AppConfig.Int("HttpsPort"); err == nil {
HttpsPort = httpsport
}
if certfile := AppConfig.String("HttpCertFile"); certfile != "" {
HttpCertFile = certfile
}
if keyfile := AppConfig.String("HttpKeyFile"); keyfile != "" {
HttpKeyFile = keyfile
}
if serverName := AppConfig.String("BeegoServerName"); serverName != "" {
BeegoServerName = serverName
}
if flashname := AppConfig.String("FlashName"); flashname != "" {
FlashName = flashname
}
if flashseperator := AppConfig.String("FlashSeperator"); flashseperator != "" {
FlashSeperator = flashseperator
}
if sd := AppConfig.String("StaticDir"); sd != "" {
for k := range StaticDir {
delete(StaticDir, k)
}
sds := strings.Fields(sd)
for _, v := range sds {
if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 {
StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[1]
} else {
StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[0]
}
}
}
if sgz := AppConfig.String("StaticExtensionsToGzip"); sgz != "" {
extensions := strings.Split(sgz, ",")
if len(extensions) > 0 {
StaticExtensionsToGzip = []string{}
for _, ext := range extensions {
if len(ext) == 0 {
continue
}
extWithDot := ext
if extWithDot[:1] != "." {
extWithDot = "." + extWithDot
}
StaticExtensionsToGzip = append(StaticExtensionsToGzip, extWithDot)
}
}
}
if enableadmin, err := AppConfig.Bool("EnableAdmin"); err == nil {
EnableAdmin = enableadmin
}
if adminhttpaddr := AppConfig.String("AdminHttpAddr"); adminhttpaddr != "" {
AdminHttpAddr = adminhttpaddr
}
if adminhttpport, err := AppConfig.Int("AdminHttpPort"); err == nil {
AdminHttpPort = adminhttpport
}
if enabledocs, err := AppConfig.Bool("EnableDocs"); err == nil {
EnableDocs = enabledocs
}
if casesensitive, err := AppConfig.Bool("RouterCaseSensitive"); err == nil {
RouterCaseSensitive = casesensitive
}
return nil
}

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// Package config is used to parse config
// Usage: // Usage:
// import( // import(
// "github.com/astaxie/beego/config" // "github.com/astaxie/beego/config"
@ -28,12 +29,12 @@
// cnf.Int64(key string) (int64, error) // cnf.Int64(key string) (int64, error)
// cnf.Bool(key string) (bool, error) // cnf.Bool(key string) (bool, error)
// cnf.Float(key string) (float64, error) // cnf.Float(key string) (float64, error)
// cnf.DefaultString(key string, defaultval string) string // cnf.DefaultString(key string, defaultVal string) string
// cnf.DefaultStrings(key string, defaultval []string) []string // cnf.DefaultStrings(key string, defaultVal []string) []string
// cnf.DefaultInt(key string, defaultval int) int // cnf.DefaultInt(key string, defaultVal int) int
// cnf.DefaultInt64(key string, defaultval int64) int64 // cnf.DefaultInt64(key string, defaultVal int64) int64
// cnf.DefaultBool(key string, defaultval bool) bool // cnf.DefaultBool(key string, defaultVal bool) bool
// cnf.DefaultFloat(key string, defaultval float64) float64 // cnf.DefaultFloat(key string, defaultVal float64) float64
// cnf.DIY(key string) (interface{}, error) // cnf.DIY(key string) (interface{}, error)
// cnf.GetSection(section string) (map[string]string, error) // cnf.GetSection(section string) (map[string]string, error)
// cnf.SaveConfigFile(filename string) error // cnf.SaveConfigFile(filename string) error
@ -45,30 +46,30 @@ import (
"fmt" "fmt"
) )
// ConfigContainer defines how to get and set value from configuration raw data. // Configer defines how to get and set value from configuration raw data.
type ConfigContainer interface { type Configer interface {
Set(key, val string) error // support section::key type in given key when using ini type. Set(key, val string) error //support section::key type in given key when using ini type.
String(key string) string // support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same. String(key string) string //support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same.
Strings(key string) []string //get string slice Strings(key string) []string //get string slice
Int(key string) (int, error) Int(key string) (int, error)
Int64(key string) (int64, error) Int64(key string) (int64, error)
Bool(key string) (bool, error) Bool(key string) (bool, error)
Float(key string) (float64, error) Float(key string) (float64, error)
DefaultString(key string, defaultval string) string // support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same. DefaultString(key string, defaultVal string) string // support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same.
DefaultStrings(key string, defaultval []string) []string //get string slice DefaultStrings(key string, defaultVal []string) []string //get string slice
DefaultInt(key string, defaultval int) int DefaultInt(key string, defaultVal int) int
DefaultInt64(key string, defaultval int64) int64 DefaultInt64(key string, defaultVal int64) int64
DefaultBool(key string, defaultval bool) bool DefaultBool(key string, defaultVal bool) bool
DefaultFloat(key string, defaultval float64) float64 DefaultFloat(key string, defaultVal float64) float64
DIY(key string) (interface{}, error) DIY(key string) (interface{}, error)
GetSection(section string) (map[string]string, error) GetSection(section string) (map[string]string, error)
SaveConfigFile(filename string) error SaveConfigFile(filename string) error
} }
// Config is the adapter interface for parsing config file to get raw data to ConfigContainer. // Config is the adapter interface for parsing config file to get raw data to Configer.
type Config interface { type Config interface {
Parse(key string) (ConfigContainer, error) Parse(key string) (Configer, error)
ParseData(data []byte) (ConfigContainer, error) ParseData(data []byte) (Configer, error)
} }
var adapters = make(map[string]Config) var adapters = make(map[string]Config)
@ -86,22 +87,58 @@ func Register(name string, adapter Config) {
adapters[name] = adapter adapters[name] = adapter
} }
// adapterName is ini/json/xml/yaml. // NewConfig adapterName is ini/json/xml/yaml.
// filename is the config file path. // filename is the config file path.
func NewConfig(adapterName, fileaname string) (ConfigContainer, error) { func NewConfig(adapterName, filename string) (Configer, error) {
adapter, ok := adapters[adapterName] adapter, ok := adapters[adapterName]
if !ok { if !ok {
return nil, fmt.Errorf("config: unknown adaptername %q (forgotten import?)", adapterName) return nil, fmt.Errorf("config: unknown adaptername %q (forgotten import?)", adapterName)
} }
return adapter.Parse(fileaname) return adapter.Parse(filename)
} }
// adapterName is ini/json/xml/yaml. // NewConfigData adapterName is ini/json/xml/yaml.
// data is the config data. // data is the config data.
func NewConfigData(adapterName string, data []byte) (ConfigContainer, error) { func NewConfigData(adapterName string, data []byte) (Configer, error) {
adapter, ok := adapters[adapterName] adapter, ok := adapters[adapterName]
if !ok { if !ok {
return nil, fmt.Errorf("config: unknown adaptername %q (forgotten import?)", adapterName) return nil, fmt.Errorf("config: unknown adaptername %q (forgotten import?)", adapterName)
} }
return adapter.ParseData(data) return adapter.ParseData(data)
} }
// ParseBool returns the boolean value represented by the string.
//
// It accepts 1, 1.0, t, T, TRUE, true, True, YES, yes, Yes,Y, y, ON, on, On,
// 0, 0.0, f, F, FALSE, false, False, NO, no, No, N,n, OFF, off, Off.
// Any other value returns an error.
func ParseBool(val interface{}) (value bool, err error) {
if val != nil {
switch v := val.(type) {
case bool:
return v, nil
case string:
switch v {
case "1", "t", "T", "true", "TRUE", "True", "YES", "yes", "Yes", "Y", "y", "ON", "on", "On":
return true, nil
case "0", "f", "F", "false", "FALSE", "False", "NO", "no", "No", "N", "n", "OFF", "off", "Off":
return false, nil
}
case int8, int32, int64:
strV := fmt.Sprintf("%s", v)
if strV == "1" {
return true, nil
} else if strV == "0" {
return false, nil
}
case float64:
if v == 1 {
return true, nil
} else if v == 0 {
return false, nil
}
}
return false, fmt.Errorf("parsing %q: invalid syntax", val)
}
return false, fmt.Errorf("parsing <nil>: invalid syntax")
}

View File

@ -38,23 +38,27 @@ func (c *fakeConfigContainer) String(key string) string {
} }
func (c *fakeConfigContainer) DefaultString(key string, defaultval string) string { func (c *fakeConfigContainer) DefaultString(key string, defaultval string) string {
if v := c.getData(key); v == "" { v := c.getData(key)
if v == "" {
return defaultval return defaultval
} else {
return v
} }
return v
} }
func (c *fakeConfigContainer) Strings(key string) []string { func (c *fakeConfigContainer) Strings(key string) []string {
return strings.Split(c.getData(key), ";") v := c.getData(key)
if v == "" {
return nil
}
return strings.Split(v, ";")
} }
func (c *fakeConfigContainer) DefaultStrings(key string, defaultval []string) []string { func (c *fakeConfigContainer) DefaultStrings(key string, defaultval []string) []string {
if v := c.Strings(key); len(v) == 0 { v := c.Strings(key)
if v == nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
func (c *fakeConfigContainer) Int(key string) (int, error) { func (c *fakeConfigContainer) Int(key string) (int, error) {
@ -62,11 +66,11 @@ func (c *fakeConfigContainer) Int(key string) (int, error) {
} }
func (c *fakeConfigContainer) DefaultInt(key string, defaultval int) int { func (c *fakeConfigContainer) DefaultInt(key string, defaultval int) int {
if v, err := c.Int(key); err != nil { v, err := c.Int(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
func (c *fakeConfigContainer) Int64(key string) (int64, error) { func (c *fakeConfigContainer) Int64(key string) (int64, error) {
@ -74,23 +78,23 @@ func (c *fakeConfigContainer) Int64(key string) (int64, error) {
} }
func (c *fakeConfigContainer) DefaultInt64(key string, defaultval int64) int64 { func (c *fakeConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
if v, err := c.Int64(key); err != nil { v, err := c.Int64(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
func (c *fakeConfigContainer) Bool(key string) (bool, error) { func (c *fakeConfigContainer) Bool(key string) (bool, error) {
return strconv.ParseBool(c.getData(key)) return ParseBool(c.getData(key))
} }
func (c *fakeConfigContainer) DefaultBool(key string, defaultval bool) bool { func (c *fakeConfigContainer) DefaultBool(key string, defaultval bool) bool {
if v, err := c.Bool(key); err != nil { v, err := c.Bool(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
func (c *fakeConfigContainer) Float(key string) (float64, error) { func (c *fakeConfigContainer) Float(key string) (float64, error) {
@ -98,11 +102,11 @@ func (c *fakeConfigContainer) Float(key string) (float64, error) {
} }
func (c *fakeConfigContainer) DefaultFloat(key string, defaultval float64) float64 { func (c *fakeConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
if v, err := c.Float(key); err != nil { v, err := c.Float(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
func (c *fakeConfigContainer) DIY(key string) (interface{}, error) { func (c *fakeConfigContainer) DIY(key string) (interface{}, error) {
@ -120,9 +124,10 @@ func (c *fakeConfigContainer) SaveConfigFile(filename string) error {
return errors.New("not implement in the fakeConfigContainer") return errors.New("not implement in the fakeConfigContainer")
} }
var _ ConfigContainer = new(fakeConfigContainer) var _ Configer = new(fakeConfigContainer)
func NewFakeConfig() ConfigContainer { // NewFakeConfig return a fake Congiger
func NewFakeConfig() Configer {
return &fakeConfigContainer{ return &fakeConfigContainer{
data: make(map[string]string), data: make(map[string]string),
} }

View File

@ -27,27 +27,26 @@ import (
"strings" "strings"
"sync" "sync"
"time" "time"
"unicode"
) )
var ( var (
DEFAULT_SECTION = "default" // default section means if some ini items not in a section, make them in default section, defaultSection = "default" // default section means if some ini items not in a section, make them in default section,
bNumComment = []byte{'#'} // number signal bNumComment = []byte{'#'} // number signal
bSemComment = []byte{';'} // semicolon signal bSemComment = []byte{';'} // semicolon signal
bEmpty = []byte{} bEmpty = []byte{}
bEqual = []byte{'='} // equal signal bEqual = []byte{'='} // equal signal
bDQuote = []byte{'"'} // quote signal bDQuote = []byte{'"'} // quote signal
sectionStart = []byte{'['} // section start signal sectionStart = []byte{'['} // section start signal
sectionEnd = []byte{']'} // section end signal sectionEnd = []byte{']'} // section end signal
lineBreak = "\n" lineBreak = "\n"
) )
// IniConfig implements Config to parse ini file. // IniConfig implements Config to parse ini file.
type IniConfig struct { type IniConfig struct {
} }
// ParseFile creates a new Config and parses the file configuration from the named file. // Parse creates a new Config and parses the file configuration from the named file.
func (ini *IniConfig) Parse(name string) (ConfigContainer, error) { func (ini *IniConfig) Parse(name string) (Configer, error) {
return ini.parseFile(name) return ini.parseFile(name)
} }
@ -77,7 +76,7 @@ func (ini *IniConfig) parseFile(name string) (*IniConfigContainer, error) {
buf.ReadByte() buf.ReadByte()
} }
} }
section := DEFAULT_SECTION section := defaultSection
for { for {
line, _, err := buf.ReadLine() line, _, err := buf.ReadLine()
if err == io.EOF { if err == io.EOF {
@ -97,9 +96,11 @@ func (ini *IniConfig) parseFile(name string) (*IniConfigContainer, error) {
} }
if bComment != nil { if bComment != nil {
line = bytes.TrimLeft(line, string(bComment)) line = bytes.TrimLeft(line, string(bComment))
line = bytes.TrimLeftFunc(line, unicode.IsSpace) // Need append to a new line if multi-line comments.
if comment.Len() > 0 {
comment.WriteByte('\n')
}
comment.Write(line) comment.Write(line)
comment.WriteByte('\n')
continue continue
} }
@ -171,7 +172,8 @@ func (ini *IniConfig) parseFile(name string) (*IniConfigContainer, error) {
return cfg, nil return cfg, nil
} }
func (ini *IniConfig) ParseData(data []byte) (ConfigContainer, error) { // ParseData parse ini the data
func (ini *IniConfig) ParseData(data []byte) (Configer, error) {
// Save memory data to temporary file // Save memory data to temporary file
tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond())) tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond()))
os.MkdirAll(path.Dir(tmpName), os.ModePerm) os.MkdirAll(path.Dir(tmpName), os.ModePerm)
@ -181,7 +183,7 @@ func (ini *IniConfig) ParseData(data []byte) (ConfigContainer, error) {
return ini.Parse(tmpName) return ini.Parse(tmpName)
} }
// A Config represents the ini configuration. // IniConfigContainer A Config represents the ini configuration.
// When set and get value, support key as section:name type. // When set and get value, support key as section:name type.
type IniConfigContainer struct { type IniConfigContainer struct {
filename string filename string
@ -193,17 +195,17 @@ type IniConfigContainer struct {
// Bool returns the boolean value for a given key. // Bool returns the boolean value for a given key.
func (c *IniConfigContainer) Bool(key string) (bool, error) { func (c *IniConfigContainer) Bool(key string) (bool, error) {
return strconv.ParseBool(c.getdata(key)) return ParseBool(c.getdata(key))
} }
// DefaultBool returns the boolean value for a given key. // DefaultBool returns the boolean value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *IniConfigContainer) DefaultBool(key string, defaultval bool) bool { func (c *IniConfigContainer) DefaultBool(key string, defaultval bool) bool {
if v, err := c.Bool(key); err != nil { v, err := c.Bool(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Int returns the integer value for a given key. // Int returns the integer value for a given key.
@ -214,11 +216,11 @@ func (c *IniConfigContainer) Int(key string) (int, error) {
// DefaultInt returns the integer value for a given key. // DefaultInt returns the integer value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *IniConfigContainer) DefaultInt(key string, defaultval int) int { func (c *IniConfigContainer) DefaultInt(key string, defaultval int) int {
if v, err := c.Int(key); err != nil { v, err := c.Int(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Int64 returns the int64 value for a given key. // Int64 returns the int64 value for a given key.
@ -229,11 +231,11 @@ func (c *IniConfigContainer) Int64(key string) (int64, error) {
// DefaultInt64 returns the int64 value for a given key. // DefaultInt64 returns the int64 value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *IniConfigContainer) DefaultInt64(key string, defaultval int64) int64 { func (c *IniConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
if v, err := c.Int64(key); err != nil { v, err := c.Int64(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Float returns the float value for a given key. // Float returns the float value for a given key.
@ -244,11 +246,11 @@ func (c *IniConfigContainer) Float(key string) (float64, error) {
// DefaultFloat returns the float64 value for a given key. // DefaultFloat returns the float64 value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *IniConfigContainer) DefaultFloat(key string, defaultval float64) float64 { func (c *IniConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
if v, err := c.Float(key); err != nil { v, err := c.Float(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// String returns the string value for a given key. // String returns the string value for a given key.
@ -259,35 +261,39 @@ func (c *IniConfigContainer) String(key string) string {
// DefaultString returns the string value for a given key. // DefaultString returns the string value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *IniConfigContainer) DefaultString(key string, defaultval string) string { func (c *IniConfigContainer) DefaultString(key string, defaultval string) string {
if v := c.String(key); v == "" { v := c.String(key)
if v == "" {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Strings returns the []string value for a given key. // Strings returns the []string value for a given key.
// Return nil if config value does not exist or is empty.
func (c *IniConfigContainer) Strings(key string) []string { func (c *IniConfigContainer) Strings(key string) []string {
return strings.Split(c.String(key), ";") v := c.String(key)
if v == "" {
return nil
}
return strings.Split(v, ";")
} }
// DefaultStrings returns the []string value for a given key. // DefaultStrings returns the []string value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *IniConfigContainer) DefaultStrings(key string, defaultval []string) []string { func (c *IniConfigContainer) DefaultStrings(key string, defaultval []string) []string {
if v := c.Strings(key); len(v) == 0 { v := c.Strings(key)
if v == nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// GetSection returns map for the given section // GetSection returns map for the given section
func (c *IniConfigContainer) GetSection(section string) (map[string]string, error) { func (c *IniConfigContainer) GetSection(section string) (map[string]string, error) {
if v, ok := c.data[section]; ok { if v, ok := c.data[section]; ok {
return v, nil return v, nil
} else {
return nil, errors.New("not exist setction")
} }
return nil, errors.New("not exist setction")
} }
// SaveConfigFile save the config into file // SaveConfigFile save the config into file
@ -299,27 +305,35 @@ func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
} }
defer f.Close() defer f.Close()
// Get section or key comments. Fixed #1607
getCommentStr := func(section, key string) string {
comment, ok := "", false
if len(key) == 0 {
comment, ok = c.sectionComment[section]
} else {
comment, ok = c.keyComment[section+"."+key]
}
if ok {
// Empty comment
if len(comment) == 0 || len(strings.TrimSpace(comment)) == 0 {
return string(bNumComment)
}
prefix := string(bNumComment)
// Add the line head character "#"
return prefix + strings.Replace(comment, lineBreak, lineBreak+prefix, -1)
}
return ""
}
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
for section, dt := range c.data { // Save default section at first place
// Write section comments. if dt, ok := c.data[defaultSection]; ok {
if v, ok := c.sectionComment[section]; ok {
if _, err = buf.WriteString(string(bNumComment) + v + lineBreak); err != nil {
return err
}
}
if section != DEFAULT_SECTION {
// Write section name.
if _, err = buf.WriteString(string(sectionStart) + section + string(sectionEnd) + lineBreak); err != nil {
return err
}
}
for key, val := range dt { for key, val := range dt {
if key != " " { if key != " " {
// Write key comments. // Write key comments.
if v, ok := c.keyComment[key]; ok { if v := getCommentStr(defaultSection, key); len(v) > 0 {
if _, err = buf.WriteString(string(bNumComment) + v + lineBreak); err != nil { if _, err = buf.WriteString(v + lineBreak); err != nil {
return err return err
} }
} }
@ -336,6 +350,43 @@ func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
return err return err
} }
} }
// Save named sections
for section, dt := range c.data {
if section != defaultSection {
// Write section comments.
if v := getCommentStr(section, ""); len(v) > 0 {
if _, err = buf.WriteString(v + lineBreak); err != nil {
return err
}
}
// Write section name.
if _, err = buf.WriteString(string(sectionStart) + section + string(sectionEnd) + lineBreak); err != nil {
return err
}
for key, val := range dt {
if key != " " {
// Write key comments.
if v := getCommentStr(section, key); len(v) > 0 {
if _, err = buf.WriteString(v + lineBreak); err != nil {
return err
}
}
// Write key and value.
if _, err = buf.WriteString(key + string(bEqual) + val + lineBreak); err != nil {
return err
}
}
}
// Put a line between sections.
if _, err = buf.WriteString(lineBreak); err != nil {
return err
}
}
}
if _, err = buf.WriteTo(f); err != nil { if _, err = buf.WriteTo(f); err != nil {
return err return err
@ -343,7 +394,7 @@ func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
return nil return nil
} }
// WriteValue writes a new value for key. // Set writes a new value for key.
// if write to one section, the key need be "section::key". // if write to one section, the key need be "section::key".
// if the section is not existed, it panics. // if the section is not existed, it panics.
func (c *IniConfigContainer) Set(key, value string) error { func (c *IniConfigContainer) Set(key, value string) error {
@ -355,14 +406,14 @@ func (c *IniConfigContainer) Set(key, value string) error {
var ( var (
section, k string section, k string
sectionKey []string = strings.Split(key, "::") sectionKey = strings.Split(key, "::")
) )
if len(sectionKey) >= 2 { if len(sectionKey) >= 2 {
section = sectionKey[0] section = sectionKey[0]
k = sectionKey[1] k = sectionKey[1]
} else { } else {
section = DEFAULT_SECTION section = defaultSection
k = sectionKey[0] k = sectionKey[0]
} }
@ -391,13 +442,13 @@ func (c *IniConfigContainer) getdata(key string) string {
var ( var (
section, k string section, k string
sectionKey []string = strings.Split(strings.ToLower(key), "::") sectionKey = strings.Split(strings.ToLower(key), "::")
) )
if len(sectionKey) >= 2 { if len(sectionKey) >= 2 {
section = sectionKey[0] section = sectionKey[0]
k = sectionKey[1] k = sectionKey[1]
} else { } else {
section = DEFAULT_SECTION section = defaultSection
k = sectionKey[0] k = sectionKey[0]
} }
if v, ok := c.data[section]; ok { if v, ok := c.data[section]; ok {

View File

@ -15,11 +15,17 @@
package config package config
import ( import (
"fmt"
"io/ioutil"
"os" "os"
"strings"
"testing" "testing"
) )
var inicontext = ` func TestIni(t *testing.T) {
var (
inicontext = `
;comment one ;comment one
#comment two #comment two
appname = beeapi appname = beeapi
@ -29,6 +35,13 @@ PI = 3.1415976
runmode = "dev" runmode = "dev"
autorender = false autorender = false
copyrequestbody = true copyrequestbody = true
session= on
cookieon= off
newreg = OFF
needlogin = ON
enableSession = Y
enableCookie = N
flag = 1
[demo] [demo]
key1="asta" key1="asta"
key2 = "xie" key2 = "xie"
@ -36,7 +49,32 @@ CaseInsensitive = true
peers = one;two;three peers = one;two;three
` `
func TestIni(t *testing.T) { keyValue = map[string]interface{}{
"appname": "beeapi",
"httpport": 8080,
"mysqlport": int64(3600),
"pi": 3.1415976,
"runmode": "dev",
"autorender": false,
"copyrequestbody": true,
"session": true,
"cookieon": false,
"newreg": false,
"needlogin": true,
"enableSession": true,
"enableCookie": false,
"flag": true,
"demo::key1": "asta",
"demo::key2": "xie",
"demo::CaseInsensitive": true,
"demo::peers": []string{"one", "two", "three"},
"null": "",
"demo2::key1": "",
"error": "",
"emptystrings": []string{},
}
)
f, err := os.Create("testini.conf") f, err := os.Create("testini.conf")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -52,31 +90,31 @@ func TestIni(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if iniconf.String("appname") != "beeapi" { for k, v := range keyValue {
t.Fatal("appname not equal to beeapi") var err error
} var value interface{}
if port, err := iniconf.Int("httpport"); err != nil || port != 8080 { switch v.(type) {
t.Error(port) case int:
t.Fatal(err) value, err = iniconf.Int(k)
} case int64:
if port, err := iniconf.Int64("mysqlport"); err != nil || port != 3600 { value, err = iniconf.Int64(k)
t.Error(port) case float64:
t.Fatal(err) value, err = iniconf.Float(k)
} case bool:
if pi, err := iniconf.Float("PI"); err != nil || pi != 3.1415976 { value, err = iniconf.Bool(k)
t.Error(pi) case []string:
t.Fatal(err) value = iniconf.Strings(k)
} case string:
if iniconf.String("runmode") != "dev" { value = iniconf.String(k)
t.Fatal("runmode not equal to dev") default:
} value, err = iniconf.DIY(k)
if v, err := iniconf.Bool("autorender"); err != nil || v != false { }
t.Error(v) if err != nil {
t.Fatal(err) t.Fatalf("get key %q value fail,err %s", k, err)
} } else if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", value) {
if v, err := iniconf.Bool("copyrequestbody"); err != nil || v != true { t.Fatalf("get key %q value, want %v got %v .", k, v, value)
t.Error(v) }
t.Fatal(err)
} }
if err = iniconf.Set("name", "astaxie"); err != nil { if err = iniconf.Set("name", "astaxie"); err != nil {
t.Fatal(err) t.Fatal(err)
@ -84,20 +122,63 @@ func TestIni(t *testing.T) {
if iniconf.String("name") != "astaxie" { if iniconf.String("name") != "astaxie" {
t.Fatal("get name error") t.Fatal("get name error")
} }
if iniconf.String("demo::key1") != "asta" {
t.Fatal("get demo.key1 error")
}
if iniconf.String("demo::key2") != "xie" {
t.Fatal("get demo.key2 error")
}
if v, err := iniconf.Bool("demo::caseinsensitive"); err != nil || v != true {
t.Fatal("get demo.caseinsensitive error")
}
if data := iniconf.Strings("demo::peers"); len(data) != 3 {
t.Fatal("get strings error", data)
} else if data[0] != "one" {
t.Fatal("get first params error not equat to one")
}
} }
func TestIniSave(t *testing.T) {
const (
inicontext = `
app = app
;comment one
#comment two
# comment three
appname = beeapi
httpport = 8080
# DB Info
# enable db
[dbinfo]
# db type name
# suport mysql,sqlserver
name = mysql
`
saveResult = `
app=app
#comment one
#comment two
# comment three
appname=beeapi
httpport=8080
# DB Info
# enable db
[dbinfo]
# db type name
# suport mysql,sqlserver
name=mysql
`
)
cfg, err := NewConfigData("ini", []byte(inicontext))
if err != nil {
t.Fatal(err)
}
name := "newIniConfig.ini"
if err := cfg.SaveConfigFile(name); err != nil {
t.Fatal(err)
}
defer os.Remove(name)
if data, err := ioutil.ReadFile(name); err != nil {
t.Fatal(err)
} else {
cfgData := string(data)
datas := strings.Split(saveResult, "\n")
for _, line := range datas {
if strings.Contains(cfgData, line+"\n") == false {
t.Fatalf("different after save ini config file. need contains %q", line)
}
}
}
}

View File

@ -20,18 +20,16 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path"
"strings" "strings"
"sync" "sync"
"time"
) )
// JsonConfig is a json config parser and implements Config interface. // JSONConfig is a json config parser and implements Config interface.
type JsonConfig struct { type JSONConfig struct {
} }
// Parse returns a ConfigContainer with parsed json config map. // Parse returns a ConfigContainer with parsed json config map.
func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) { func (js *JSONConfig) Parse(filename string) (Configer, error) {
file, err := os.Open(filename) file, err := os.Open(filename)
if err != nil { if err != nil {
return nil, err return nil, err
@ -41,13 +39,19 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
x := &JsonConfigContainer{
return js.ParseData(content)
}
// ParseData returns a ConfigContainer with json string
func (js *JSONConfig) ParseData(data []byte) (Configer, error) {
x := &JSONConfigContainer{
data: make(map[string]interface{}), data: make(map[string]interface{}),
} }
err = json.Unmarshal(content, &x.data) err := json.Unmarshal(data, &x.data)
if err != nil { if err != nil {
var wrappingArray []interface{} var wrappingArray []interface{}
err2 := json.Unmarshal(content, &wrappingArray) err2 := json.Unmarshal(data, &wrappingArray)
if err2 != nil { if err2 != nil {
return nil, err return nil, err
} }
@ -56,47 +60,33 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) {
return x, nil return x, nil
} }
func (js *JsonConfig) ParseData(data []byte) (ConfigContainer, error) { // JSONConfigContainer A Config represents the json configuration.
// Save memory data to temporary file
tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond()))
os.MkdirAll(path.Dir(tmpName), os.ModePerm)
if err := ioutil.WriteFile(tmpName, data, 0655); err != nil {
return nil, err
}
return js.Parse(tmpName)
}
// A Config represents the json configuration.
// Only when get value, support key as section:name type. // Only when get value, support key as section:name type.
type JsonConfigContainer struct { type JSONConfigContainer struct {
data map[string]interface{} data map[string]interface{}
sync.RWMutex sync.RWMutex
} }
// Bool returns the boolean value for a given key. // Bool returns the boolean value for a given key.
func (c *JsonConfigContainer) Bool(key string) (bool, error) { func (c *JSONConfigContainer) Bool(key string) (bool, error) {
val := c.getData(key) val := c.getData(key)
if val != nil { if val != nil {
if v, ok := val.(bool); ok { return ParseBool(val)
return v, nil
}
return false, errors.New("not bool value")
} }
return false, errors.New("not exist key:" + key) return false, fmt.Errorf("not exist key: %q", key)
} }
// DefaultBool return the bool value if has no error // DefaultBool return the bool value if has no error
// otherwise return the defaultval // otherwise return the defaultval
func (c *JsonConfigContainer) DefaultBool(key string, defaultval bool) bool { func (c *JSONConfigContainer) DefaultBool(key string, defaultval bool) bool {
if v, err := c.Bool(key); err != nil { if v, err := c.Bool(key); err == nil {
return defaultval
} else {
return v return v
} }
return defaultval
} }
// Int returns the integer value for a given key. // Int returns the integer value for a given key.
func (c *JsonConfigContainer) Int(key string) (int, error) { func (c *JSONConfigContainer) Int(key string) (int, error) {
val := c.getData(key) val := c.getData(key)
if val != nil { if val != nil {
if v, ok := val.(float64); ok { if v, ok := val.(float64); ok {
@ -109,16 +99,15 @@ func (c *JsonConfigContainer) Int(key string) (int, error) {
// DefaultInt returns the integer value for a given key. // DefaultInt returns the integer value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *JsonConfigContainer) DefaultInt(key string, defaultval int) int { func (c *JSONConfigContainer) DefaultInt(key string, defaultval int) int {
if v, err := c.Int(key); err != nil { if v, err := c.Int(key); err == nil {
return defaultval
} else {
return v return v
} }
return defaultval
} }
// Int64 returns the int64 value for a given key. // Int64 returns the int64 value for a given key.
func (c *JsonConfigContainer) Int64(key string) (int64, error) { func (c *JSONConfigContainer) Int64(key string) (int64, error) {
val := c.getData(key) val := c.getData(key)
if val != nil { if val != nil {
if v, ok := val.(float64); ok { if v, ok := val.(float64); ok {
@ -131,16 +120,15 @@ func (c *JsonConfigContainer) Int64(key string) (int64, error) {
// DefaultInt64 returns the int64 value for a given key. // DefaultInt64 returns the int64 value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *JsonConfigContainer) DefaultInt64(key string, defaultval int64) int64 { func (c *JSONConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
if v, err := c.Int64(key); err != nil { if v, err := c.Int64(key); err == nil {
return defaultval
} else {
return v return v
} }
return defaultval
} }
// Float returns the float value for a given key. // Float returns the float value for a given key.
func (c *JsonConfigContainer) Float(key string) (float64, error) { func (c *JSONConfigContainer) Float(key string) (float64, error) {
val := c.getData(key) val := c.getData(key)
if val != nil { if val != nil {
if v, ok := val.(float64); ok { if v, ok := val.(float64); ok {
@ -153,16 +141,15 @@ func (c *JsonConfigContainer) Float(key string) (float64, error) {
// DefaultFloat returns the float64 value for a given key. // DefaultFloat returns the float64 value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *JsonConfigContainer) DefaultFloat(key string, defaultval float64) float64 { func (c *JSONConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
if v, err := c.Float(key); err != nil { if v, err := c.Float(key); err == nil {
return defaultval
} else {
return v return v
} }
return defaultval
} }
// String returns the string value for a given key. // String returns the string value for a given key.
func (c *JsonConfigContainer) String(key string) string { func (c *JSONConfigContainer) String(key string) string {
val := c.getData(key) val := c.getData(key)
if val != nil { if val != nil {
if v, ok := val.(string); ok { if v, ok := val.(string); ok {
@ -174,40 +161,42 @@ func (c *JsonConfigContainer) String(key string) string {
// DefaultString returns the string value for a given key. // DefaultString returns the string value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *JsonConfigContainer) DefaultString(key string, defaultval string) string { func (c *JSONConfigContainer) DefaultString(key string, defaultval string) string {
if v := c.String(key); v == "" { // TODO FIXME should not use "" to replace non existence
return defaultval if v := c.String(key); v != "" {
} else {
return v return v
} }
return defaultval
} }
// Strings returns the []string value for a given key. // Strings returns the []string value for a given key.
func (c *JsonConfigContainer) Strings(key string) []string { func (c *JSONConfigContainer) Strings(key string) []string {
stringVal := c.String(key)
if stringVal == "" {
return nil
}
return strings.Split(c.String(key), ";") return strings.Split(c.String(key), ";")
} }
// DefaultStrings returns the []string value for a given key. // DefaultStrings returns the []string value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *JsonConfigContainer) DefaultStrings(key string, defaultval []string) []string { func (c *JSONConfigContainer) DefaultStrings(key string, defaultval []string) []string {
if v := c.Strings(key); len(v) == 0 { if v := c.Strings(key); v != nil {
return defaultval
} else {
return v return v
} }
return defaultval
} }
// GetSection returns map for the given section // GetSection returns map for the given section
func (c *JsonConfigContainer) GetSection(section string) (map[string]string, error) { func (c *JSONConfigContainer) GetSection(section string) (map[string]string, error) {
if v, ok := c.data[section]; ok { if v, ok := c.data[section]; ok {
return v.(map[string]string), nil return v.(map[string]string), nil
} else {
return nil, errors.New("not exist setction")
} }
return nil, errors.New("nonexist section " + section)
} }
// SaveConfigFile save the config into file // SaveConfigFile save the config into file
func (c *JsonConfigContainer) SaveConfigFile(filename string) (err error) { func (c *JSONConfigContainer) SaveConfigFile(filename string) (err error) {
// Write configuration file by filename. // Write configuration file by filename.
f, err := os.Create(filename) f, err := os.Create(filename)
if err != nil { if err != nil {
@ -222,8 +211,8 @@ func (c *JsonConfigContainer) SaveConfigFile(filename string) (err error) {
return err return err
} }
// WriteValue writes a new value for key. // Set writes a new value for key.
func (c *JsonConfigContainer) Set(key, val string) error { func (c *JSONConfigContainer) Set(key, val string) error {
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
c.data[key] = val c.data[key] = val
@ -231,7 +220,7 @@ func (c *JsonConfigContainer) Set(key, val string) error {
} }
// DIY returns the raw value by a given key. // DIY returns the raw value by a given key.
func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) { func (c *JSONConfigContainer) DIY(key string) (v interface{}, err error) {
val := c.getData(key) val := c.getData(key)
if val != nil { if val != nil {
return val, nil return val, nil
@ -240,19 +229,21 @@ func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) {
} }
// section.key or key // section.key or key
func (c *JsonConfigContainer) getData(key string) interface{} { func (c *JSONConfigContainer) getData(key string) interface{} {
c.RLock()
defer c.RUnlock()
if len(key) == 0 { if len(key) == 0 {
return nil return nil
} }
sectionKey := strings.Split(key, "::")
if len(sectionKey) >= 2 { c.RLock()
curValue, ok := c.data[sectionKey[0]] defer c.RUnlock()
sectionKeys := strings.Split(key, "::")
if len(sectionKeys) >= 2 {
curValue, ok := c.data[sectionKeys[0]]
if !ok { if !ok {
return nil return nil
} }
for _, key := range sectionKey[1:] { for _, key := range sectionKeys[1:] {
if v, ok := curValue.(map[string]interface{}); ok { if v, ok := curValue.(map[string]interface{}); ok {
if curValue, ok = v[key]; !ok { if curValue, ok = v[key]; !ok {
return nil return nil
@ -268,5 +259,5 @@ func (c *JsonConfigContainer) getData(key string) interface{} {
} }
func init() { func init() {
Register("json", &JsonConfig{}) Register("json", &JSONConfig{})
} }

View File

@ -15,33 +15,14 @@
package config package config
import ( import (
"fmt"
"os" "os"
"testing" "testing"
) )
var jsoncontext = `{ func TestJsonStartsWithArray(t *testing.T) {
"appname": "beeapi",
"httpport": 8080,
"mysqlport": 3600,
"PI": 3.1415976,
"runmode": "dev",
"autorender": false,
"copyrequestbody": true,
"database": {
"host": "host",
"port": "port",
"database": "database",
"username": "username",
"password": "password",
"conns":{
"maxconnection":12,
"autoconnect":true,
"connectioninfo":"info"
}
}
}`
var jsoncontextwitharray = `[ const jsoncontextwitharray = `[
{ {
"url": "user", "url": "user",
"serviceAPI": "http://www.test.com/user" "serviceAPI": "http://www.test.com/user"
@ -51,8 +32,6 @@ var jsoncontextwitharray = `[
"serviceAPI": "http://www.test.com/employee" "serviceAPI": "http://www.test.com/employee"
} }
]` ]`
func TestJsonStartsWithArray(t *testing.T) {
f, err := os.Create("testjsonWithArray.conf") f, err := os.Create("testjsonWithArray.conf")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -89,6 +68,64 @@ func TestJsonStartsWithArray(t *testing.T) {
} }
func TestJson(t *testing.T) { func TestJson(t *testing.T) {
var (
jsoncontext = `{
"appname": "beeapi",
"testnames": "foo;bar",
"httpport": 8080,
"mysqlport": 3600,
"PI": 3.1415976,
"runmode": "dev",
"autorender": false,
"copyrequestbody": true,
"session": "on",
"cookieon": "off",
"newreg": "OFF",
"needlogin": "ON",
"enableSession": "Y",
"enableCookie": "N",
"flag": 1,
"database": {
"host": "host",
"port": "port",
"database": "database",
"username": "username",
"password": "password",
"conns":{
"maxconnection":12,
"autoconnect":true,
"connectioninfo":"info"
}
}
}`
keyValue = map[string]interface{}{
"appname": "beeapi",
"testnames": []string{"foo", "bar"},
"httpport": 8080,
"mysqlport": int64(3600),
"PI": 3.1415976,
"runmode": "dev",
"autorender": false,
"copyrequestbody": true,
"session": true,
"cookieon": false,
"newreg": false,
"needlogin": true,
"enableSession": true,
"enableCookie": false,
"flag": true,
"database::host": "host",
"database::port": "port",
"database::database": "database",
"database::password": "password",
"database::conns::maxconnection": 12,
"database::conns::autoconnect": true,
"database::conns::connectioninfo": "info",
"unknown": "",
}
)
f, err := os.Create("testjson.conf") f, err := os.Create("testjson.conf")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -104,31 +141,32 @@ func TestJson(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if jsonconf.String("appname") != "beeapi" {
t.Fatal("appname not equal to beeapi") for k, v := range keyValue {
} var err error
if port, err := jsonconf.Int("httpport"); err != nil || port != 8080 { var value interface{}
t.Error(port) switch v.(type) {
t.Fatal(err) case int:
} value, err = jsonconf.Int(k)
if port, err := jsonconf.Int64("mysqlport"); err != nil || port != 3600 { case int64:
t.Error(port) value, err = jsonconf.Int64(k)
t.Fatal(err) case float64:
} value, err = jsonconf.Float(k)
if pi, err := jsonconf.Float("PI"); err != nil || pi != 3.1415976 { case bool:
t.Error(pi) value, err = jsonconf.Bool(k)
t.Fatal(err) case []string:
} value = jsonconf.Strings(k)
if jsonconf.String("runmode") != "dev" { case string:
t.Fatal("runmode not equal to dev") value = jsonconf.String(k)
} default:
if v, err := jsonconf.Bool("autorender"); err != nil || v != false { value, err = jsonconf.DIY(k)
t.Error(v) }
t.Fatal(err) if err != nil {
} t.Fatalf("get key %q value fatal,%v err %s", k, v, err)
if v, err := jsonconf.Bool("copyrequestbody"); err != nil || v != true { } else if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", value) {
t.Error(v) t.Fatalf("get key %q value, want %v got %v .", k, v, value)
t.Fatal(err) }
} }
if err = jsonconf.Set("name", "astaxie"); err != nil { if err = jsonconf.Set("name", "astaxie"); err != nil {
t.Fatal(err) t.Fatal(err)
@ -136,15 +174,7 @@ func TestJson(t *testing.T) {
if jsonconf.String("name") != "astaxie" { if jsonconf.String("name") != "astaxie" {
t.Fatal("get name error") t.Fatal("get name error")
} }
if jsonconf.String("database::host") != "host" {
t.Fatal("get database::host error")
}
if jsonconf.String("database::conns::connectioninfo") != "info" {
t.Fatal("get database::conns::connectioninfo error")
}
if maxconnection, err := jsonconf.Int("database::conns::maxconnection"); err != nil || maxconnection != 12 {
t.Fatal("get database::conns::maxconnection error")
}
if db, err := jsonconf.DIY("database"); err != nil { if db, err := jsonconf.DIY("database"); err != nil {
t.Fatal(err) t.Fatal(err)
} else if m, ok := db.(map[string]interface{}); !ok { } else if m, ok := db.(map[string]interface{}); !ok {
@ -179,4 +209,8 @@ func TestJson(t *testing.T) {
if _, err := jsonconf.Bool("unknown"); err == nil { if _, err := jsonconf.Bool("unknown"); err == nil {
t.Error("unknown keys should return an error when expecting a Bool") t.Error("unknown keys should return an error when expecting a Bool")
} }
if !jsonconf.DefaultBool("unknow", true) {
t.Error("unknown keys with default value wrong")
}
} }

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// package xml for config provider // Package xml for config provider
// //
// depend on github.com/beego/x2j // depend on github.com/beego/x2j
// //
@ -45,20 +45,20 @@ import (
"github.com/beego/x2j" "github.com/beego/x2j"
) )
// XmlConfig is a xml config parser and implements Config interface. // Config is a xml config parser and implements Config interface.
// xml configurations should be included in <config></config> tag. // xml configurations should be included in <config></config> tag.
// only support key/value pair as <key>value</key> as each item. // only support key/value pair as <key>value</key> as each item.
type XMLConfig struct{} type Config struct{}
// Parse returns a ConfigContainer with parsed xml config map. // Parse returns a ConfigContainer with parsed xml config map.
func (xc *XMLConfig) Parse(filename string) (config.ConfigContainer, error) { func (xc *Config) Parse(filename string) (config.Configer, error) {
file, err := os.Open(filename) file, err := os.Open(filename)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer file.Close() defer file.Close()
x := &XMLConfigContainer{data: make(map[string]interface{})} x := &ConfigContainer{data: make(map[string]interface{})}
content, err := ioutil.ReadAll(file) content, err := ioutil.ReadAll(file)
if err != nil { if err != nil {
return nil, err return nil, err
@ -73,84 +73,89 @@ func (xc *XMLConfig) Parse(filename string) (config.ConfigContainer, error) {
return x, nil return x, nil
} }
func (x *XMLConfig) ParseData(data []byte) (config.ConfigContainer, error) { // ParseData xml data
func (xc *Config) ParseData(data []byte) (config.Configer, error) {
// Save memory data to temporary file // Save memory data to temporary file
tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond())) tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond()))
os.MkdirAll(path.Dir(tmpName), os.ModePerm) os.MkdirAll(path.Dir(tmpName), os.ModePerm)
if err := ioutil.WriteFile(tmpName, data, 0655); err != nil { if err := ioutil.WriteFile(tmpName, data, 0655); err != nil {
return nil, err return nil, err
} }
return x.Parse(tmpName) return xc.Parse(tmpName)
} }
// A Config represents the xml configuration. // ConfigContainer A Config represents the xml configuration.
type XMLConfigContainer struct { type ConfigContainer struct {
data map[string]interface{} data map[string]interface{}
sync.Mutex sync.Mutex
} }
// Bool returns the boolean value for a given key. // Bool returns the boolean value for a given key.
func (c *XMLConfigContainer) Bool(key string) (bool, error) { func (c *ConfigContainer) Bool(key string) (bool, error) {
return strconv.ParseBool(c.data[key].(string)) if v, ok := c.data[key]; ok {
return config.ParseBool(v)
}
return false, fmt.Errorf("not exist key: %q", key)
} }
// DefaultBool return the bool value if has no error // DefaultBool return the bool value if has no error
// otherwise return the defaultval // otherwise return the defaultval
func (c *XMLConfigContainer) DefaultBool(key string, defaultval bool) bool { func (c *ConfigContainer) DefaultBool(key string, defaultval bool) bool {
if v, err := c.Bool(key); err != nil { v, err := c.Bool(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Int returns the integer value for a given key. // Int returns the integer value for a given key.
func (c *XMLConfigContainer) Int(key string) (int, error) { func (c *ConfigContainer) Int(key string) (int, error) {
return strconv.Atoi(c.data[key].(string)) return strconv.Atoi(c.data[key].(string))
} }
// DefaultInt returns the integer value for a given key. // DefaultInt returns the integer value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *XMLConfigContainer) DefaultInt(key string, defaultval int) int { func (c *ConfigContainer) DefaultInt(key string, defaultval int) int {
if v, err := c.Int(key); err != nil { v, err := c.Int(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Int64 returns the int64 value for a given key. // Int64 returns the int64 value for a given key.
func (c *XMLConfigContainer) Int64(key string) (int64, error) { func (c *ConfigContainer) Int64(key string) (int64, error) {
return strconv.ParseInt(c.data[key].(string), 10, 64) return strconv.ParseInt(c.data[key].(string), 10, 64)
} }
// DefaultInt64 returns the int64 value for a given key. // DefaultInt64 returns the int64 value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *XMLConfigContainer) DefaultInt64(key string, defaultval int64) int64 { func (c *ConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
if v, err := c.Int64(key); err != nil { v, err := c.Int64(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Float returns the float value for a given key. // Float returns the float value for a given key.
func (c *XMLConfigContainer) Float(key string) (float64, error) { func (c *ConfigContainer) Float(key string) (float64, error) {
return strconv.ParseFloat(c.data[key].(string), 64) return strconv.ParseFloat(c.data[key].(string), 64)
} }
// DefaultFloat returns the float64 value for a given key. // DefaultFloat returns the float64 value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *XMLConfigContainer) DefaultFloat(key string, defaultval float64) float64 { func (c *ConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
if v, err := c.Float(key); err != nil { v, err := c.Float(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// String returns the string value for a given key. // String returns the string value for a given key.
func (c *XMLConfigContainer) String(key string) string { func (c *ConfigContainer) String(key string) string {
if v, ok := c.data[key].(string); ok { if v, ok := c.data[key].(string); ok {
return v return v
} }
@ -159,40 +164,43 @@ func (c *XMLConfigContainer) String(key string) string {
// DefaultString returns the string value for a given key. // DefaultString returns the string value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *XMLConfigContainer) DefaultString(key string, defaultval string) string { func (c *ConfigContainer) DefaultString(key string, defaultval string) string {
if v := c.String(key); v == "" { v := c.String(key)
if v == "" {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Strings returns the []string value for a given key. // Strings returns the []string value for a given key.
func (c *XMLConfigContainer) Strings(key string) []string { func (c *ConfigContainer) Strings(key string) []string {
return strings.Split(c.String(key), ";") v := c.String(key)
if v == "" {
return nil
}
return strings.Split(v, ";")
} }
// DefaultStrings returns the []string value for a given key. // DefaultStrings returns the []string value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *XMLConfigContainer) DefaultStrings(key string, defaultval []string) []string { func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []string {
if v := c.Strings(key); len(v) == 0 { v := c.Strings(key)
if v == nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// GetSection returns map for the given section // GetSection returns map for the given section
func (c *XMLConfigContainer) GetSection(section string) (map[string]string, error) { func (c *ConfigContainer) GetSection(section string) (map[string]string, error) {
if v, ok := c.data[section]; ok { if v, ok := c.data[section]; ok {
return v.(map[string]string), nil return v.(map[string]string), nil
} else {
return nil, errors.New("not exist setction")
} }
return nil, errors.New("not exist setction")
} }
// SaveConfigFile save the config into file // SaveConfigFile save the config into file
func (c *XMLConfigContainer) SaveConfigFile(filename string) (err error) { func (c *ConfigContainer) SaveConfigFile(filename string) (err error) {
// Write configuration file by filename. // Write configuration file by filename.
f, err := os.Create(filename) f, err := os.Create(filename)
if err != nil { if err != nil {
@ -207,8 +215,8 @@ func (c *XMLConfigContainer) SaveConfigFile(filename string) (err error) {
return err return err
} }
// WriteValue writes a new value for key. // Set writes a new value for key.
func (c *XMLConfigContainer) Set(key, val string) error { func (c *ConfigContainer) Set(key, val string) error {
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
c.data[key] = val c.data[key] = val
@ -216,7 +224,7 @@ func (c *XMLConfigContainer) Set(key, val string) error {
} }
// DIY returns the raw value by a given key. // DIY returns the raw value by a given key.
func (c *XMLConfigContainer) DIY(key string) (v interface{}, err error) { func (c *ConfigContainer) DIY(key string) (v interface{}, err error) {
if v, ok := c.data[key]; ok { if v, ok := c.data[key]; ok {
return v, nil return v, nil
} }
@ -224,5 +232,5 @@ func (c *XMLConfigContainer) DIY(key string) (v interface{}, err error) {
} }
func init() { func init() {
config.Register("xml", &XMLConfig{}) config.Register("xml", &Config{})
} }

View File

@ -82,4 +82,7 @@ func TestXML(t *testing.T) {
if xmlconf.String("name") != "astaxie" { if xmlconf.String("name") != "astaxie" {
t.Fatal("get name error") t.Fatal("get name error")
} }
if xmlconf.Strings("emptystrings") != nil {
t.Fatal("get emtpy strings error")
}
} }

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// package yaml for config provider // Package yaml for config provider
// //
// depend on github.com/beego/goyaml2 // depend on github.com/beego/goyaml2
// //
@ -46,22 +46,23 @@ import (
"github.com/beego/goyaml2" "github.com/beego/goyaml2"
) )
// YAMLConfig is a yaml config parser and implements Config interface. // Config is a yaml config parser and implements Config interface.
type YAMLConfig struct{} type Config struct{}
// Parse returns a ConfigContainer with parsed yaml config map. // Parse returns a ConfigContainer with parsed yaml config map.
func (yaml *YAMLConfig) Parse(filename string) (y config.ConfigContainer, err error) { func (yaml *Config) Parse(filename string) (y config.Configer, err error) {
cnf, err := ReadYmlReader(filename) cnf, err := ReadYmlReader(filename)
if err != nil { if err != nil {
return return
} }
y = &YAMLConfigContainer{ y = &ConfigContainer{
data: cnf, data: cnf,
} }
return return
} }
func (yaml *YAMLConfig) ParseData(data []byte) (config.ConfigContainer, error) { // ParseData parse yaml data
func (yaml *Config) ParseData(data []byte) (config.Configer, error) {
// Save memory data to temporary file // Save memory data to temporary file
tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond())) tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond()))
os.MkdirAll(path.Dir(tmpName), os.ModePerm) os.MkdirAll(path.Dir(tmpName), os.ModePerm)
@ -71,7 +72,7 @@ func (yaml *YAMLConfig) ParseData(data []byte) (config.ConfigContainer, error) {
return yaml.Parse(tmpName) return yaml.Parse(tmpName)
} }
// Read yaml file to map. // ReadYmlReader Read yaml file to map.
// if json like, use json package, unless goyaml2 package. // if json like, use json package, unless goyaml2 package.
func ReadYmlReader(path string) (cnf map[string]interface{}, err error) { func ReadYmlReader(path string) (cnf map[string]interface{}, err error) {
f, err := os.Open(path) f, err := os.Open(path)
@ -112,32 +113,32 @@ func ReadYmlReader(path string) (cnf map[string]interface{}, err error) {
return return
} }
// A Config represents the yaml configuration. // ConfigContainer A Config represents the yaml configuration.
type YAMLConfigContainer struct { type ConfigContainer struct {
data map[string]interface{} data map[string]interface{}
sync.Mutex sync.Mutex
} }
// Bool returns the boolean value for a given key. // Bool returns the boolean value for a given key.
func (c *YAMLConfigContainer) Bool(key string) (bool, error) { func (c *ConfigContainer) Bool(key string) (bool, error) {
if v, ok := c.data[key].(bool); ok { if v, ok := c.data[key]; ok {
return v, nil return config.ParseBool(v)
} }
return false, errors.New("not bool value") return false, fmt.Errorf("not exist key: %q", key)
} }
// DefaultBool return the bool value if has no error // DefaultBool return the bool value if has no error
// otherwise return the defaultval // otherwise return the defaultval
func (c *YAMLConfigContainer) DefaultBool(key string, defaultval bool) bool { func (c *ConfigContainer) DefaultBool(key string, defaultval bool) bool {
if v, err := c.Bool(key); err != nil { v, err := c.Bool(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Int returns the integer value for a given key. // Int returns the integer value for a given key.
func (c *YAMLConfigContainer) Int(key string) (int, error) { func (c *ConfigContainer) Int(key string) (int, error) {
if v, ok := c.data[key].(int64); ok { if v, ok := c.data[key].(int64); ok {
return int(v), nil return int(v), nil
} }
@ -146,16 +147,16 @@ func (c *YAMLConfigContainer) Int(key string) (int, error) {
// DefaultInt returns the integer value for a given key. // DefaultInt returns the integer value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *YAMLConfigContainer) DefaultInt(key string, defaultval int) int { func (c *ConfigContainer) DefaultInt(key string, defaultval int) int {
if v, err := c.Int(key); err != nil { v, err := c.Int(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Int64 returns the int64 value for a given key. // Int64 returns the int64 value for a given key.
func (c *YAMLConfigContainer) Int64(key string) (int64, error) { func (c *ConfigContainer) Int64(key string) (int64, error) {
if v, ok := c.data[key].(int64); ok { if v, ok := c.data[key].(int64); ok {
return v, nil return v, nil
} }
@ -164,16 +165,16 @@ func (c *YAMLConfigContainer) Int64(key string) (int64, error) {
// DefaultInt64 returns the int64 value for a given key. // DefaultInt64 returns the int64 value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *YAMLConfigContainer) DefaultInt64(key string, defaultval int64) int64 { func (c *ConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
if v, err := c.Int64(key); err != nil { v, err := c.Int64(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Float returns the float value for a given key. // Float returns the float value for a given key.
func (c *YAMLConfigContainer) Float(key string) (float64, error) { func (c *ConfigContainer) Float(key string) (float64, error) {
if v, ok := c.data[key].(float64); ok { if v, ok := c.data[key].(float64); ok {
return v, nil return v, nil
} }
@ -182,16 +183,16 @@ func (c *YAMLConfigContainer) Float(key string) (float64, error) {
// DefaultFloat returns the float64 value for a given key. // DefaultFloat returns the float64 value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *YAMLConfigContainer) DefaultFloat(key string, defaultval float64) float64 { func (c *ConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
if v, err := c.Float(key); err != nil { v, err := c.Float(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// String returns the string value for a given key. // String returns the string value for a given key.
func (c *YAMLConfigContainer) String(key string) string { func (c *ConfigContainer) String(key string) string {
if v, ok := c.data[key].(string); ok { if v, ok := c.data[key].(string); ok {
return v return v
} }
@ -200,40 +201,44 @@ func (c *YAMLConfigContainer) String(key string) string {
// DefaultString returns the string value for a given key. // DefaultString returns the string value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *YAMLConfigContainer) DefaultString(key string, defaultval string) string { func (c *ConfigContainer) DefaultString(key string, defaultval string) string {
if v := c.String(key); v == "" { v := c.String(key)
if v == "" {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Strings returns the []string value for a given key. // Strings returns the []string value for a given key.
func (c *YAMLConfigContainer) Strings(key string) []string { func (c *ConfigContainer) Strings(key string) []string {
return strings.Split(c.String(key), ";") v := c.String(key)
if v == "" {
return nil
}
return strings.Split(v, ";")
} }
// DefaultStrings returns the []string value for a given key. // DefaultStrings returns the []string value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *YAMLConfigContainer) DefaultStrings(key string, defaultval []string) []string { func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []string {
if v := c.Strings(key); len(v) == 0 { v := c.Strings(key)
if v == nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// GetSection returns map for the given section // GetSection returns map for the given section
func (c *YAMLConfigContainer) GetSection(section string) (map[string]string, error) { func (c *ConfigContainer) GetSection(section string) (map[string]string, error) {
if v, ok := c.data[section]; ok { v, ok := c.data[section]
if ok {
return v.(map[string]string), nil return v.(map[string]string), nil
} else {
return nil, errors.New("not exist setction")
} }
return nil, errors.New("not exist setction")
} }
// SaveConfigFile save the config into file // SaveConfigFile save the config into file
func (c *YAMLConfigContainer) SaveConfigFile(filename string) (err error) { func (c *ConfigContainer) SaveConfigFile(filename string) (err error) {
// Write configuration file by filename. // Write configuration file by filename.
f, err := os.Create(filename) f, err := os.Create(filename)
if err != nil { if err != nil {
@ -244,8 +249,8 @@ func (c *YAMLConfigContainer) SaveConfigFile(filename string) (err error) {
return err return err
} }
// WriteValue writes a new value for key. // Set writes a new value for key.
func (c *YAMLConfigContainer) Set(key, val string) error { func (c *ConfigContainer) Set(key, val string) error {
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
c.data[key] = val c.data[key] = val
@ -253,7 +258,7 @@ func (c *YAMLConfigContainer) Set(key, val string) error {
} }
// DIY returns the raw value by a given key. // DIY returns the raw value by a given key.
func (c *YAMLConfigContainer) DIY(key string) (v interface{}, err error) { func (c *ConfigContainer) DIY(key string) (v interface{}, err error) {
if v, ok := c.data[key]; ok { if v, ok := c.data[key]; ok {
return v, nil return v, nil
} }
@ -261,5 +266,5 @@ func (c *YAMLConfigContainer) DIY(key string) (v interface{}, err error) {
} }
func init() { func init() {
config.Register("yaml", &YAMLConfig{}) config.Register("yaml", &Config{})
} }

View File

@ -79,4 +79,8 @@ func TestYaml(t *testing.T) {
if yamlconf.String("name") != "astaxie" { if yamlconf.String("name") != "astaxie" {
t.Fatal("get name error") t.Fatal("get name error")
} }
if yamlconf.Strings("emptystrings") != nil {
t.Fatal("get emtpy strings error")
}
} }

View File

@ -19,11 +19,11 @@ import (
) )
func TestDefaults(t *testing.T) { func TestDefaults(t *testing.T) {
if FlashName != "BEEGO_FLASH" { if BConfig.WebConfig.FlashName != "BEEGO_FLASH" {
t.Errorf("FlashName was not set to default.") t.Errorf("FlashName was not set to default.")
} }
if FlashSeperator != "BEEGOFLASH" { if BConfig.WebConfig.FlashSeparator != "BEEGOFLASH" {
t.Errorf("FlashName was not set to default.") t.Errorf("FlashName was not set to default.")
} }
} }

197
context/acceptencoder.go Normal file
View File

@ -0,0 +1,197 @@
// Copyright 2015 beego Author. All Rights Reserved.
//
// 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.
package context
import (
"bytes"
"compress/flate"
"compress/gzip"
"compress/zlib"
"io"
"net/http"
"os"
"strconv"
"strings"
"sync"
)
type resetWriter interface {
io.Writer
Reset(w io.Writer)
}
type nopResetWriter struct {
io.Writer
}
func (n nopResetWriter) Reset(w io.Writer) {
//do nothing
}
type acceptEncoder struct {
name string
levelEncode func(int) resetWriter
bestSpeedPool *sync.Pool
bestCompressionPool *sync.Pool
}
func (ac acceptEncoder) encode(wr io.Writer, level int) resetWriter {
if ac.bestSpeedPool == nil || ac.bestCompressionPool == nil {
return nopResetWriter{wr}
}
var rwr resetWriter
switch level {
case flate.BestSpeed:
rwr = ac.bestSpeedPool.Get().(resetWriter)
case flate.BestCompression:
rwr = ac.bestCompressionPool.Get().(resetWriter)
default:
rwr = ac.levelEncode(level)
}
rwr.Reset(wr)
return rwr
}
func (ac acceptEncoder) put(wr resetWriter, level int) {
if ac.bestSpeedPool == nil || ac.bestCompressionPool == nil {
return
}
wr.Reset(nil)
switch level {
case flate.BestSpeed:
ac.bestSpeedPool.Put(wr)
case flate.BestCompression:
ac.bestCompressionPool.Put(wr)
}
}
var (
noneCompressEncoder = acceptEncoder{"", nil, nil, nil}
gzipCompressEncoder = acceptEncoder{"gzip",
func(level int) resetWriter { wr, _ := gzip.NewWriterLevel(nil, level); return wr },
&sync.Pool{
New: func() interface{} { wr, _ := gzip.NewWriterLevel(nil, flate.BestSpeed); return wr },
},
&sync.Pool{
New: func() interface{} { wr, _ := gzip.NewWriterLevel(nil, flate.BestCompression); return wr },
},
}
//according to the sec :http://tools.ietf.org/html/rfc2616#section-3.5 ,the deflate compress in http is zlib indeed
//deflate
//The "zlib" format defined in RFC 1950 [31] in combination with
//the "deflate" compression mechanism described in RFC 1951 [29].
deflateCompressEncoder = acceptEncoder{"deflate",
func(level int) resetWriter { wr, _ := zlib.NewWriterLevel(nil, level); return wr },
&sync.Pool{
New: func() interface{} { wr, _ := zlib.NewWriterLevel(nil, flate.BestSpeed); return wr },
},
&sync.Pool{
New: func() interface{} { wr, _ := zlib.NewWriterLevel(nil, flate.BestCompression); return wr },
},
}
)
var (
encoderMap = map[string]acceptEncoder{ // all the other compress methods will ignore
"gzip": gzipCompressEncoder,
"deflate": deflateCompressEncoder,
"*": gzipCompressEncoder, // * means any compress will accept,we prefer gzip
"identity": noneCompressEncoder, // identity means none-compress
}
)
// WriteFile reads from file and writes to writer by the specific encoding(gzip/deflate)
func WriteFile(encoding string, writer io.Writer, file *os.File) (bool, string, error) {
return writeLevel(encoding, writer, file, flate.BestCompression)
}
// WriteBody reads writes content to writer by the specific encoding(gzip/deflate)
func WriteBody(encoding string, writer io.Writer, content []byte) (bool, string, error) {
return writeLevel(encoding, writer, bytes.NewReader(content), flate.BestSpeed)
}
// writeLevel reads from reader,writes to writer by specific encoding and compress level
// the compress level is defined by deflate package
func writeLevel(encoding string, writer io.Writer, reader io.Reader, level int) (bool, string, error) {
var outputWriter resetWriter
var err error
var ce = noneCompressEncoder
if cf, ok := encoderMap[encoding]; ok {
ce = cf
}
encoding = ce.name
outputWriter = ce.encode(writer, level)
defer ce.put(outputWriter, level)
_, err = io.Copy(outputWriter, reader)
if err != nil {
return false, "", err
}
switch outputWriter.(type) {
case io.WriteCloser:
outputWriter.(io.WriteCloser).Close()
}
return encoding != "", encoding, nil
}
// ParseEncoding will extract the right encoding for response
// the Accept-Encoding's sec is here:
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
func ParseEncoding(r *http.Request) string {
if r == nil {
return ""
}
return parseEncoding(r)
}
type q struct {
name string
value float64
}
func parseEncoding(r *http.Request) string {
acceptEncoding := r.Header.Get("Accept-Encoding")
if acceptEncoding == "" {
return ""
}
var lastQ q
for _, v := range strings.Split(acceptEncoding, ",") {
v = strings.TrimSpace(v)
if v == "" {
continue
}
vs := strings.Split(v, ";")
if len(vs) == 1 {
lastQ = q{vs[0], 1}
break
}
if len(vs) == 2 {
f, _ := strconv.ParseFloat(strings.Replace(vs[1], "q=", "", -1), 64)
if f == 0 {
continue
}
if f > lastQ.value {
lastQ = q{vs[0], f}
}
}
}
if cf, ok := encoderMap[lastQ.name]; ok {
return cf.name
}
return ""
}

View File

@ -0,0 +1,44 @@
// Copyright 2015 beego Author. All Rights Reserved.
//
// 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.
package context
import (
"net/http"
"testing"
)
func Test_ExtractEncoding(t *testing.T) {
if parseEncoding(&http.Request{Header: map[string][]string{"Accept-Encoding": {"gzip,deflate"}}}) != "gzip" {
t.Fail()
}
if parseEncoding(&http.Request{Header: map[string][]string{"Accept-Encoding": {"deflate,gzip"}}}) != "deflate" {
t.Fail()
}
if parseEncoding(&http.Request{Header: map[string][]string{"Accept-Encoding": {"gzip;q=.5,deflate"}}}) != "deflate" {
t.Fail()
}
if parseEncoding(&http.Request{Header: map[string][]string{"Accept-Encoding": {"gzip;q=.5,deflate;q=0.3"}}}) != "gzip" {
t.Fail()
}
if parseEncoding(&http.Request{Header: map[string][]string{"Accept-Encoding": {"gzip;q=0,deflate"}}}) != "deflate" {
t.Fail()
}
if parseEncoding(&http.Request{Header: map[string][]string{"Accept-Encoding": {"deflate;q=0.5,gzip;q=0.5,identity"}}}) != "" {
t.Fail()
}
if parseEncoding(&http.Request{Header: map[string][]string{"Accept-Encoding": {"*"}}}) != "gzip" {
t.Fail()
}
}

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// Package context provide the context utils
// Usage: // Usage:
// //
// import "github.com/astaxie/beego/context" // import "github.com/astaxie/beego/context"
@ -22,27 +23,50 @@
package context package context
import ( import (
"bufio"
"bytes"
"crypto/hmac" "crypto/hmac"
"crypto/sha1" "crypto/sha1"
"encoding/base64" "encoding/base64"
"errors"
"fmt" "fmt"
"io"
"net"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"github.com/astaxie/beego/middleware"
"github.com/astaxie/beego/utils" "github.com/astaxie/beego/utils"
) )
// Http request context struct including BeegoInput, BeegoOutput, http.Request and http.ResponseWriter. // NewContext return the Context with Input and Output
func NewContext() *Context {
return &Context{
Input: NewInput(),
Output: NewOutput(),
}
}
// Context Http request context struct including BeegoInput, BeegoOutput, http.Request and http.ResponseWriter.
// BeegoInput and BeegoOutput provides some api to operate request and response more easily. // BeegoInput and BeegoOutput provides some api to operate request and response more easily.
type Context struct { type Context struct {
Input *BeegoInput Input *BeegoInput
Output *BeegoOutput Output *BeegoOutput
Request *http.Request Request *http.Request
ResponseWriter http.ResponseWriter ResponseWriter *Response
_xsrf_token string _xsrfToken string
}
// Reset init Context, BeegoInput and BeegoOutput
func (ctx *Context) Reset(rw http.ResponseWriter, r *http.Request) {
ctx.Request = r
if ctx.ResponseWriter == nil {
ctx.ResponseWriter = &Response{}
}
ctx.ResponseWriter.reset(rw)
ctx.Input.Reset(ctx)
ctx.Output.Reset(ctx)
} }
// Redirect does redirection to localurl with http header status code. // Redirect does redirection to localurl with http header status code.
@ -53,45 +77,30 @@ func (ctx *Context) Redirect(status int, localurl string) {
} }
// Abort stops this request. // Abort stops this request.
// if middleware.ErrorMaps exists, panic body. // if beego.ErrorMaps exists, panic body.
// if middleware.HTTPExceptionMaps exists, panic HTTPException struct with status and body string.
func (ctx *Context) Abort(status int, body string) { func (ctx *Context) Abort(status int, body string) {
ctx.ResponseWriter.WriteHeader(status) panic(body)
// first panic from ErrorMaps, is is user defined error functions.
if _, ok := middleware.ErrorMaps[body]; ok {
panic(body)
}
// second panic from HTTPExceptionMaps, it is system defined functions.
if e, ok := middleware.HTTPExceptionMaps[status]; ok {
if len(body) >= 1 {
e.Description = body
}
panic(e)
}
// last panic user string
ctx.ResponseWriter.Write([]byte(body))
panic("User stop run")
} }
// Write string to response body. // WriteString Write string to response body.
// it sends response body. // it sends response body.
func (ctx *Context) WriteString(content string) { func (ctx *Context) WriteString(content string) {
ctx.ResponseWriter.Write([]byte(content)) ctx.ResponseWriter.Write([]byte(content))
} }
// Get cookie from request by a given key. // GetCookie Get cookie from request by a given key.
// It's alias of BeegoInput.Cookie. // It's alias of BeegoInput.Cookie.
func (ctx *Context) GetCookie(key string) string { func (ctx *Context) GetCookie(key string) string {
return ctx.Input.Cookie(key) return ctx.Input.Cookie(key)
} }
// Set cookie for response. // SetCookie Set cookie for response.
// It's alias of BeegoOutput.Cookie. // It's alias of BeegoOutput.Cookie.
func (ctx *Context) SetCookie(name string, value string, others ...interface{}) { func (ctx *Context) SetCookie(name string, value string, others ...interface{}) {
ctx.Output.Cookie(name, value, others...) ctx.Output.Cookie(name, value, others...)
} }
// Get secure cookie from request by a given key. // GetSecureCookie Get secure cookie from request by a given key.
func (ctx *Context) GetSecureCookie(Secret, key string) (string, bool) { func (ctx *Context) GetSecureCookie(Secret, key string) (string, bool) {
val := ctx.Input.Cookie(key) val := ctx.Input.Cookie(key)
if val == "" { if val == "" {
@ -118,7 +127,7 @@ func (ctx *Context) GetSecureCookie(Secret, key string) (string, bool) {
return string(res), true return string(res), true
} }
// Set Secure cookie for response. // SetSecureCookie Set Secure cookie for response.
func (ctx *Context) SetSecureCookie(Secret, name, value string, others ...interface{}) { func (ctx *Context) SetSecureCookie(Secret, name, value string, others ...interface{}) {
vs := base64.URLEncoding.EncodeToString([]byte(value)) vs := base64.URLEncoding.EncodeToString([]byte(value))
timestamp := strconv.FormatInt(time.Now().UnixNano(), 10) timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
@ -129,23 +138,23 @@ func (ctx *Context) SetSecureCookie(Secret, name, value string, others ...interf
ctx.Output.Cookie(name, cookie, others...) ctx.Output.Cookie(name, cookie, others...)
} }
// XsrfToken creates a xsrf token string and returns. // XSRFToken creates a xsrf token string and returns.
func (ctx *Context) XsrfToken(key string, expire int64) string { func (ctx *Context) XSRFToken(key string, expire int64) string {
if ctx._xsrf_token == "" { if ctx._xsrfToken == "" {
token, ok := ctx.GetSecureCookie(key, "_xsrf") token, ok := ctx.GetSecureCookie(key, "_xsrf")
if !ok { if !ok {
token = string(utils.RandomCreateBytes(32)) token = string(utils.RandomCreateBytes(32))
ctx.SetSecureCookie(key, "_xsrf", token, expire) ctx.SetSecureCookie(key, "_xsrf", token, expire)
} }
ctx._xsrf_token = token ctx._xsrfToken = token
} }
return ctx._xsrf_token return ctx._xsrfToken
} }
// CheckXsrfCookie checks xsrf token in this request is valid or not. // CheckXSRFCookie checks xsrf token in this request is valid or not.
// the token can provided in request header "X-Xsrftoken" and "X-CsrfToken" // the token can provided in request header "X-Xsrftoken" and "X-CsrfToken"
// or in form field value named as "_xsrf". // or in form field value named as "_xsrf".
func (ctx *Context) CheckXsrfCookie() bool { func (ctx *Context) CheckXSRFCookie() bool {
token := ctx.Input.Query("_xsrf") token := ctx.Input.Query("_xsrf")
if token == "" { if token == "" {
token = ctx.Request.Header.Get("X-Xsrftoken") token = ctx.Request.Header.Get("X-Xsrftoken")
@ -155,8 +164,77 @@ func (ctx *Context) CheckXsrfCookie() bool {
} }
if token == "" { if token == "" {
ctx.Abort(403, "'_xsrf' argument missing from POST") ctx.Abort(403, "'_xsrf' argument missing from POST")
} else if ctx._xsrf_token != token { return false
}
if ctx._xsrfToken != token {
ctx.Abort(403, "XSRF cookie does not match POST argument") ctx.Abort(403, "XSRF cookie does not match POST argument")
return false
} }
return true return true
} }
//Response is a wrapper for the http.ResponseWriter
//started set to true if response was written to then don't execute other handler
type Response struct {
http.ResponseWriter
Started bool
Status int
}
func (r *Response) reset(rw http.ResponseWriter) {
r.ResponseWriter = rw
r.Status = 0
r.Started = false
}
// Write writes the data to the connection as part of an HTTP reply,
// and sets `started` to true.
// started means the response has sent out.
func (r *Response) Write(p []byte) (int, error) {
r.Started = true
return r.ResponseWriter.Write(p)
}
// Copy writes the data to the connection as part of an HTTP reply,
// and sets `started` to true.
// started means the response has sent out.
func (r *Response) Copy(buf *bytes.Buffer) (int64, error) {
r.Started = true
return io.Copy(r.ResponseWriter, buf)
}
// WriteHeader sends an HTTP response header with status code,
// and sets `started` to true.
func (r *Response) WriteHeader(code int) {
if r.Status > 0 {
//prevent multiple response.WriteHeader calls
return
}
r.Status = code
r.Started = true
r.ResponseWriter.WriteHeader(code)
}
// Hijack hijacker for http
func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hj, ok := r.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, errors.New("webserver doesn't support hijacking")
}
return hj.Hijack()
}
// Flush http.Flusher
func (r *Response) Flush() {
if f, ok := r.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}
// CloseNotify http.CloseNotifier
func (r *Response) CloseNotify() <-chan bool {
if cn, ok := r.ResponseWriter.(http.CloseNotifier); ok {
return cn.CloseNotify()
}
return nil
}

View File

@ -17,50 +17,69 @@ package context
import ( import (
"bytes" "bytes"
"errors" "errors"
"io"
"io/ioutil" "io/ioutil"
"net/http"
"net/url" "net/url"
"reflect" "reflect"
"regexp"
"strconv" "strconv"
"strings" "strings"
"github.com/astaxie/beego/session" "github.com/astaxie/beego/session"
) )
// BeegoInput operates the http request header ,data ,cookie and body. // Regexes for checking the accept headers
// TODO make sure these are correct
var (
acceptsHTMLRegex = regexp.MustCompile(`(text/html|application/xhtml\+xml)(?:,|$)`)
acceptsXMLRegex = regexp.MustCompile(`(application/xml|text/xml)(?:,|$)`)
acceptsJSONRegex = regexp.MustCompile(`(application/json)(?:,|$)`)
maxParam = 50
)
// BeegoInput operates the http request header, data, cookie and body.
// it also contains router params and current session. // it also contains router params and current session.
type BeegoInput struct { type BeegoInput struct {
CruSession session.SessionStore Context *Context
Params map[string]string CruSession session.Store
Data map[interface{}]interface{} // store some values in this context when calling context in filter or controller. pnames []string
Request *http.Request pvalues []string
RequestBody []byte data map[interface{}]interface{} // store some values in this context when calling context in filter or controller.
RunController reflect.Type RequestBody []byte
RunMethod string
} }
// NewInput return BeegoInput generated by http.Request. // NewInput return BeegoInput generated by Context.
func NewInput(req *http.Request) *BeegoInput { func NewInput() *BeegoInput {
return &BeegoInput{ return &BeegoInput{
Params: make(map[string]string), pnames: make([]string, 0, maxParam),
Data: make(map[interface{}]interface{}), pvalues: make([]string, 0, maxParam),
Request: req, data: make(map[interface{}]interface{}),
} }
} }
// Reset init the BeegoInput
func (input *BeegoInput) Reset(ctx *Context) {
input.Context = ctx
input.CruSession = nil
input.pnames = input.pnames[:0]
input.pvalues = input.pvalues[:0]
input.data = nil
input.RequestBody = []byte{}
}
// Protocol returns request protocol name, such as HTTP/1.1 . // Protocol returns request protocol name, such as HTTP/1.1 .
func (input *BeegoInput) Protocol() string { func (input *BeegoInput) Protocol() string {
return input.Request.Proto return input.Context.Request.Proto
} }
// Uri returns full request url with query string, fragment. // URI returns full request url with query string, fragment.
func (input *BeegoInput) Uri() string { func (input *BeegoInput) URI() string {
return input.Request.RequestURI return input.Context.Request.RequestURI
} }
// Url returns request url path (without query string, fragment). // URL returns request url path (without query string, fragment).
func (input *BeegoInput) Url() string { func (input *BeegoInput) URL() string {
return input.Request.URL.Path return input.Context.Request.URL.Path
} }
// Site returns base site url as scheme://domain type. // Site returns base site url as scheme://domain type.
@ -70,13 +89,13 @@ func (input *BeegoInput) Site() string {
// Scheme returns request scheme as "http" or "https". // Scheme returns request scheme as "http" or "https".
func (input *BeegoInput) Scheme() string { func (input *BeegoInput) Scheme() string {
if input.Request.URL.Scheme != "" { if input.Context.Request.URL.Scheme != "" {
return input.Request.URL.Scheme return input.Context.Request.URL.Scheme
} else if input.Request.TLS == nil {
return "http"
} else {
return "https"
} }
if input.Context.Request.TLS == nil {
return "http"
}
return "https"
} }
// Domain returns host name. // Domain returns host name.
@ -88,19 +107,19 @@ func (input *BeegoInput) Domain() string {
// Host returns host name. // Host returns host name.
// if no host info in request, return localhost. // if no host info in request, return localhost.
func (input *BeegoInput) Host() string { func (input *BeegoInput) Host() string {
if input.Request.Host != "" { if input.Context.Request.Host != "" {
hostParts := strings.Split(input.Request.Host, ":") hostParts := strings.Split(input.Context.Request.Host, ":")
if len(hostParts) > 0 { if len(hostParts) > 0 {
return hostParts[0] return hostParts[0]
} }
return input.Request.Host return input.Context.Request.Host
} }
return "localhost" return "localhost"
} }
// Method returns http request method. // Method returns http request method.
func (input *BeegoInput) Method() string { func (input *BeegoInput) Method() string {
return input.Request.Method return input.Context.Request.Method
} }
// Is returns boolean of this request is on given method, such as Is("POST"). // Is returns boolean of this request is on given method, such as Is("POST").
@ -108,37 +127,37 @@ func (input *BeegoInput) Is(method string) bool {
return input.Method() == method return input.Method() == method
} }
// Is this a GET method request? // IsGet Is this a GET method request?
func (input *BeegoInput) IsGet() bool { func (input *BeegoInput) IsGet() bool {
return input.Is("GET") return input.Is("GET")
} }
// Is this a POST method request? // IsPost Is this a POST method request?
func (input *BeegoInput) IsPost() bool { func (input *BeegoInput) IsPost() bool {
return input.Is("POST") return input.Is("POST")
} }
// Is this a Head method request? // IsHead Is this a Head method request?
func (input *BeegoInput) IsHead() bool { func (input *BeegoInput) IsHead() bool {
return input.Is("HEAD") return input.Is("HEAD")
} }
// Is this a OPTIONS method request? // IsOptions Is this a OPTIONS method request?
func (input *BeegoInput) IsOptions() bool { func (input *BeegoInput) IsOptions() bool {
return input.Is("OPTIONS") return input.Is("OPTIONS")
} }
// Is this a PUT method request? // IsPut Is this a PUT method request?
func (input *BeegoInput) IsPut() bool { func (input *BeegoInput) IsPut() bool {
return input.Is("PUT") return input.Is("PUT")
} }
// Is this a DELETE method request? // IsDelete Is this a DELETE method request?
func (input *BeegoInput) IsDelete() bool { func (input *BeegoInput) IsDelete() bool {
return input.Is("DELETE") return input.Is("DELETE")
} }
// Is this a PATCH method request? // IsPatch Is this a PATCH method request?
func (input *BeegoInput) IsPatch() bool { func (input *BeegoInput) IsPatch() bool {
return input.Is("PATCH") return input.Is("PATCH")
} }
@ -153,16 +172,31 @@ func (input *BeegoInput) IsSecure() bool {
return input.Scheme() == "https" return input.Scheme() == "https"
} }
// IsSecure returns boolean of this request is in webSocket. // IsWebsocket returns boolean of this request is in webSocket.
func (input *BeegoInput) IsWebsocket() bool { func (input *BeegoInput) IsWebsocket() bool {
return input.Header("Upgrade") == "websocket" return input.Header("Upgrade") == "websocket"
} }
// IsSecure returns boolean of whether file uploads in this request or not.. // IsUpload returns boolean of whether file uploads in this request or not..
func (input *BeegoInput) IsUpload() bool { func (input *BeegoInput) IsUpload() bool {
return strings.Contains(input.Header("Content-Type"), "multipart/form-data") return strings.Contains(input.Header("Content-Type"), "multipart/form-data")
} }
// AcceptsHTML Checks if request accepts html response
func (input *BeegoInput) AcceptsHTML() bool {
return acceptsHTMLRegex.MatchString(input.Header("Accept"))
}
// AcceptsXML Checks if request accepts xml response
func (input *BeegoInput) AcceptsXML() bool {
return acceptsXMLRegex.MatchString(input.Header("Accept"))
}
// AcceptsJSON Checks if request accepts json response
func (input *BeegoInput) AcceptsJSON() bool {
return acceptsJSONRegex.MatchString(input.Header("Accept"))
}
// IP returns request client ip. // IP returns request client ip.
// if in proxy, return first proxy id. // if in proxy, return first proxy id.
// if error, return 127.0.0.1. // if error, return 127.0.0.1.
@ -172,7 +206,7 @@ func (input *BeegoInput) IP() string {
rip := strings.Split(ips[0], ":") rip := strings.Split(ips[0], ":")
return rip[0] return rip[0]
} }
ip := strings.Split(input.Request.RemoteAddr, ":") ip := strings.Split(input.Context.Request.RemoteAddr, ":")
if len(ip) > 0 { if len(ip) > 0 {
if ip[0] != "[" { if ip[0] != "[" {
return ip[0] return ip[0]
@ -189,22 +223,30 @@ func (input *BeegoInput) Proxy() []string {
return []string{} return []string{}
} }
// Referer returns http referer header.
func (input *BeegoInput) Referer() string {
return input.Header("Referer")
}
// Refer returns http referer header. // Refer returns http referer header.
func (input *BeegoInput) Refer() string { func (input *BeegoInput) Refer() string {
return input.Header("Referer") return input.Referer()
} }
// SubDomains returns sub domain string. // SubDomains returns sub domain string.
// if aa.bb.domain.com, returns aa.bb . // if aa.bb.domain.com, returns aa.bb .
func (input *BeegoInput) SubDomains() string { func (input *BeegoInput) SubDomains() string {
parts := strings.Split(input.Host(), ".") parts := strings.Split(input.Host(), ".")
return strings.Join(parts[len(parts)-2:], ".") if len(parts) >= 3 {
return strings.Join(parts[:len(parts)-2], ".")
}
return ""
} }
// Port returns request client port. // Port returns request client port.
// when error or empty, return 80. // when error or empty, return 80.
func (input *BeegoInput) Port() int { func (input *BeegoInput) Port() int {
parts := strings.Split(input.Request.Host, ":") parts := strings.Split(input.Context.Request.Host, ":")
if len(parts) == 2 { if len(parts) == 2 {
port, _ := strconv.Atoi(parts[1]) port, _ := strconv.Atoi(parts[1])
return port return port
@ -217,34 +259,66 @@ func (input *BeegoInput) UserAgent() string {
return input.Header("User-Agent") return input.Header("User-Agent")
} }
// ParamsLen return the length of the params
func (input *BeegoInput) ParamsLen() int {
return len(input.pnames)
}
// Param returns router param by a given key. // Param returns router param by a given key.
func (input *BeegoInput) Param(key string) string { func (input *BeegoInput) Param(key string) string {
if v, ok := input.Params[key]; ok { for i, v := range input.pnames {
return v if v == key && i <= len(input.pvalues) {
return input.pvalues[i]
}
} }
return "" return ""
} }
// Params returns the map[key]value.
func (input *BeegoInput) Params() map[string]string {
m := make(map[string]string)
for i, v := range input.pnames {
if i <= len(input.pvalues) {
m[v] = input.pvalues[i]
}
}
return m
}
// SetParam will set the param with key and value
func (input *BeegoInput) SetParam(key, val string) {
// check if already exists
for i, v := range input.pnames {
if v == key && i <= len(input.pvalues) {
input.pvalues[i] = val
return
}
}
input.pvalues = append(input.pvalues, val)
input.pnames = append(input.pnames, key)
}
// Query returns input data item string by a given string. // Query returns input data item string by a given string.
func (input *BeegoInput) Query(key string) string { func (input *BeegoInput) Query(key string) string {
if val := input.Param(key); val != "" { if val := input.Param(key); val != "" {
return val return val
} }
if input.Request.Form == nil { if input.Context.Request.Form == nil {
input.Request.ParseForm() input.Context.Request.ParseForm()
} }
return input.Request.Form.Get(key) return input.Context.Request.Form.Get(key)
} }
// Header returns request header item string by a given string. // Header returns request header item string by a given string.
// if non-existed, return empty string.
func (input *BeegoInput) Header(key string) string { func (input *BeegoInput) Header(key string) string {
return input.Request.Header.Get(key) return input.Context.Request.Header.Get(key)
} }
// Cookie returns request cookie item string by a given key. // Cookie returns request cookie item string by a given key.
// if non-existed, return empty string. // if non-existed, return empty string.
func (input *BeegoInput) Cookie(key string) string { func (input *BeegoInput) Cookie(key string) string {
ck, err := input.Request.Cookie(key) ck, err := input.Context.Request.Cookie(key)
if err != nil { if err != nil {
return "" return ""
} }
@ -252,23 +326,33 @@ func (input *BeegoInput) Cookie(key string) string {
} }
// Session returns current session item value by a given key. // Session returns current session item value by a given key.
// if non-existed, return empty string.
func (input *BeegoInput) Session(key interface{}) interface{} { func (input *BeegoInput) Session(key interface{}) interface{} {
return input.CruSession.Get(key) return input.CruSession.Get(key)
} }
// Body returns the raw request body data as bytes. // CopyBody returns the raw request body data as bytes.
func (input *BeegoInput) CopyBody() []byte { func (input *BeegoInput) CopyBody(MaxMemory int64) []byte {
requestbody, _ := ioutil.ReadAll(input.Request.Body) safe := &io.LimitedReader{R: input.Context.Request.Body, N: MaxMemory}
input.Request.Body.Close() requestbody, _ := ioutil.ReadAll(safe)
input.Context.Request.Body.Close()
bf := bytes.NewBuffer(requestbody) bf := bytes.NewBuffer(requestbody)
input.Request.Body = ioutil.NopCloser(bf) input.Context.Request.Body = ioutil.NopCloser(bf)
input.RequestBody = requestbody input.RequestBody = requestbody
return requestbody return requestbody
} }
// Data return the implicit data in the input
func (input *BeegoInput) Data() map[interface{}]interface{} {
if input.data == nil {
input.data = make(map[interface{}]interface{})
}
return input.data
}
// GetData returns the stored data in this context. // GetData returns the stored data in this context.
func (input *BeegoInput) GetData(key interface{}) interface{} { func (input *BeegoInput) GetData(key interface{}) interface{} {
if v, ok := input.Data[key]; ok { if v, ok := input.data[key]; ok {
return v return v
} }
return nil return nil
@ -277,17 +361,20 @@ func (input *BeegoInput) GetData(key interface{}) interface{} {
// SetData stores data with given key in this context. // SetData stores data with given key in this context.
// This data are only available in this context. // This data are only available in this context.
func (input *BeegoInput) SetData(key, val interface{}) { func (input *BeegoInput) SetData(key, val interface{}) {
input.Data[key] = val if input.data == nil {
input.data = make(map[interface{}]interface{})
}
input.data[key] = val
} }
// parseForm or parseMultiForm based on Content-type // ParseFormOrMulitForm parseForm or parseMultiForm based on Content-type
func (input *BeegoInput) ParseFormOrMulitForm(maxMemory int64) error { func (input *BeegoInput) ParseFormOrMulitForm(maxMemory int64) error {
// Parse the body depending on the content type. // Parse the body depending on the content type.
if strings.Contains(input.Header("Content-Type"), "multipart/form-data") { if strings.Contains(input.Header("Content-Type"), "multipart/form-data") {
if err := input.Request.ParseMultipartForm(maxMemory); err != nil { if err := input.Context.Request.ParseMultipartForm(maxMemory); err != nil {
return errors.New("Error parsing request body:" + err.Error()) return errors.New("Error parsing request body:" + err.Error())
} }
} else if err := input.Request.ParseForm(); err != nil { } else if err := input.Context.Request.ParseForm(); err != nil {
return errors.New("Error parsing request body:" + err.Error()) return errors.New("Error parsing request body:" + err.Error())
} }
return nil return nil
@ -296,7 +383,7 @@ func (input *BeegoInput) ParseFormOrMulitForm(maxMemory int64) error {
// Bind data from request.Form[key] to dest // Bind data from request.Form[key] to dest
// like /?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=astaxie // like /?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=astaxie
// var id int beegoInput.Bind(&id, "id") id ==123 // var id int beegoInput.Bind(&id, "id") id ==123
// var isok bool beegoInput.Bind(&isok, "isok") id ==true // var isok bool beegoInput.Bind(&isok, "isok") isok ==true
// var ft float64 beegoInput.Bind(&ft, "ft") ft ==1.2 // var ft float64 beegoInput.Bind(&ft, "ft") ft ==1.2
// ol := make([]int, 0, 2) beegoInput.Bind(&ol, "ol") ol ==[1 2] // ol := make([]int, 0, 2) beegoInput.Bind(&ol, "ol") ol ==[1 2]
// ul := make([]string, 0, 2) beegoInput.Bind(&ul, "ul") ul ==[str array] // ul := make([]string, 0, 2) beegoInput.Bind(&ul, "ul") ul ==[str array]
@ -319,7 +406,7 @@ func (input *BeegoInput) Bind(dest interface{}, key string) error {
} }
func (input *BeegoInput) bind(key string, typ reflect.Type) reflect.Value { func (input *BeegoInput) bind(key string, typ reflect.Type) reflect.Value {
rv := reflect.Zero(reflect.TypeOf(0)) rv := reflect.Zero(typ)
switch typ.Kind() { switch typ.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
val := input.Query(key) val := input.Query(key)
@ -352,19 +439,19 @@ func (input *BeegoInput) bind(key string, typ reflect.Type) reflect.Value {
} }
rv = input.bindBool(val, typ) rv = input.bindBool(val, typ)
case reflect.Slice: case reflect.Slice:
rv = input.bindSlice(&input.Request.Form, key, typ) rv = input.bindSlice(&input.Context.Request.Form, key, typ)
case reflect.Struct: case reflect.Struct:
rv = input.bindStruct(&input.Request.Form, key, typ) rv = input.bindStruct(&input.Context.Request.Form, key, typ)
case reflect.Ptr: case reflect.Ptr:
rv = input.bindPoint(key, typ) rv = input.bindPoint(key, typ)
case reflect.Map: case reflect.Map:
rv = input.bindMap(&input.Request.Form, key, typ) rv = input.bindMap(&input.Context.Request.Form, key, typ)
} }
return rv return rv
} }
func (input *BeegoInput) bindValue(val string, typ reflect.Type) reflect.Value { func (input *BeegoInput) bindValue(val string, typ reflect.Type) reflect.Value {
rv := reflect.Zero(reflect.TypeOf(0)) rv := reflect.Zero(typ)
switch typ.Kind() { switch typ.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
rv = input.bindInt(val, typ) rv = input.bindInt(val, typ)

View File

@ -17,12 +17,16 @@ package context
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"net/http/httptest"
"reflect"
"testing" "testing"
) )
func TestParse(t *testing.T) { func TestParse(t *testing.T) {
r, _ := http.NewRequest("GET", "/?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=astaxie", nil) r, _ := http.NewRequest("GET", "/?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=astaxie", nil)
beegoInput := NewInput(r) beegoInput := NewInput()
beegoInput.Context = NewContext()
beegoInput.Context.Reset(httptest.NewRecorder(), r)
beegoInput.ParseFormOrMulitForm(1 << 20) beegoInput.ParseFormOrMulitForm(1 << 20)
var id int var id int
@ -70,3 +74,100 @@ func TestParse(t *testing.T) {
} }
fmt.Println(user) fmt.Println(user)
} }
func TestSubDomain(t *testing.T) {
r, _ := http.NewRequest("GET", "http://www.example.com/?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=astaxie", nil)
beegoInput := NewInput()
beegoInput.Context = NewContext()
beegoInput.Context.Reset(httptest.NewRecorder(), r)
subdomain := beegoInput.SubDomains()
if subdomain != "www" {
t.Fatal("Subdomain parse error, got" + subdomain)
}
r, _ = http.NewRequest("GET", "http://localhost/", nil)
beegoInput.Context.Request = r
if beegoInput.SubDomains() != "" {
t.Fatal("Subdomain parse error, should be empty, got " + beegoInput.SubDomains())
}
r, _ = http.NewRequest("GET", "http://aa.bb.example.com/", nil)
beegoInput.Context.Request = r
if beegoInput.SubDomains() != "aa.bb" {
t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains())
}
/* TODO Fix this
r, _ = http.NewRequest("GET", "http://127.0.0.1/", nil)
beegoInput.Request = r
if beegoInput.SubDomains() != "" {
t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains())
}
*/
r, _ = http.NewRequest("GET", "http://example.com/", nil)
beegoInput.Context.Request = r
if beegoInput.SubDomains() != "" {
t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains())
}
r, _ = http.NewRequest("GET", "http://aa.bb.cc.dd.example.com/", nil)
beegoInput.Context.Request = r
if beegoInput.SubDomains() != "aa.bb.cc.dd" {
t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains())
}
}
func TestParams(t *testing.T) {
inp := NewInput()
inp.SetParam("p1", "val1_ver1")
inp.SetParam("p2", "val2_ver1")
inp.SetParam("p3", "val3_ver1")
if l := inp.ParamsLen(); l != 3 {
t.Fatalf("Input.ParamsLen wrong value: %d, expected %d", l, 3)
}
if val := inp.Param("p1"); val != "val1_ver1" {
t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val1_ver1")
}
if val := inp.Param("p3"); val != "val3_ver1" {
t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val3_ver1")
}
vals := inp.Params()
expected := map[string]string{
"p1": "val1_ver1",
"p2": "val2_ver1",
"p3": "val3_ver1",
}
if !reflect.DeepEqual(vals, expected) {
t.Fatalf("Input.Params wrong value: %s, expected %s", vals, expected)
}
// overwriting existing params
inp.SetParam("p1", "val1_ver2")
inp.SetParam("p2", "val2_ver2")
expected = map[string]string{
"p1": "val1_ver2",
"p2": "val2_ver2",
"p3": "val3_ver1",
}
vals = inp.Params()
if !reflect.DeepEqual(vals, expected) {
t.Fatalf("Input.Params wrong value: %s, expected %s", vals, expected)
}
if l := inp.ParamsLen(); l != 3 {
t.Fatalf("Input.ParamsLen wrong value: %d, expected %d", l, 3)
}
if val := inp.Param("p1"); val != "val1_ver2" {
t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val1_ver2")
}
if val := inp.Param("p2"); val != "val2_ver2" {
t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val1_ver2")
}
}

View File

@ -16,19 +16,17 @@ package context
import ( import (
"bytes" "bytes"
"compress/flate"
"compress/gzip"
"encoding/json" "encoding/json"
"encoding/xml" "encoding/xml"
"errors" "errors"
"fmt" "fmt"
"html/template" "html/template"
"io"
"mime" "mime"
"net/http" "net/http"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"time"
) )
// BeegoOutput does work for sending response header. // BeegoOutput does work for sending response header.
@ -44,6 +42,12 @@ func NewOutput() *BeegoOutput {
return &BeegoOutput{} return &BeegoOutput{}
} }
// Reset init BeegoOutput
func (output *BeegoOutput) Reset(ctx *Context) {
output.Context = ctx
output.Status = 0
}
// Header sets response header item string via given key. // Header sets response header item string via given key.
func (output *BeegoOutput) Header(key, val string) { func (output *BeegoOutput) Header(key, val string) {
output.Context.ResponseWriter.Header().Set(key, val) output.Context.ResponseWriter.Header().Set(key, val)
@ -52,31 +56,17 @@ func (output *BeegoOutput) Header(key, val string) {
// Body sets response body content. // Body sets response body content.
// if EnableGzip, compress content string. // if EnableGzip, compress content string.
// it sends out response body directly. // it sends out response body directly.
func (output *BeegoOutput) Body(content []byte) { func (output *BeegoOutput) Body(content []byte) error {
output_writer := output.Context.ResponseWriter.(io.Writer) var encoding string
if output.EnableGzip == true && output.Context.Input.Header("Accept-Encoding") != "" { var buf = &bytes.Buffer{}
splitted := strings.SplitN(output.Context.Input.Header("Accept-Encoding"), ",", -1) if output.EnableGzip {
encodings := make([]string, len(splitted)) encoding = ParseEncoding(output.Context.Request)
}
for i, val := range splitted { if b, n, _ := WriteBody(encoding, buf, content); b {
encodings[i] = strings.TrimSpace(val) output.Header("Content-Encoding", n)
}
for _, val := range encodings {
if val == "gzip" {
output.Header("Content-Encoding", "gzip")
output_writer, _ = gzip.NewWriterLevel(output.Context.ResponseWriter, gzip.BestSpeed)
break
} else if val == "deflate" {
output.Header("Content-Encoding", "deflate")
output_writer, _ = flate.NewWriter(output.Context.ResponseWriter, flate.BestSpeed)
break
}
}
} else { } else {
output.Header("Content-Length", strconv.Itoa(len(content))) output.Header("Content-Length", strconv.Itoa(len(content)))
} }
// Write status code if it has been set manually // Write status code if it has been set manually
// Set it to 0 afterwards to prevent "multiple response.WriteHeader calls" // Set it to 0 afterwards to prevent "multiple response.WriteHeader calls"
if output.Status != 0 { if output.Status != 0 {
@ -84,13 +74,8 @@ func (output *BeegoOutput) Body(content []byte) {
output.Status = 0 output.Status = 0
} }
output_writer.Write(content) _, err := output.Context.ResponseWriter.Copy(buf)
switch output_writer.(type) { return err
case *gzip.Writer:
output_writer.(*gzip.Writer).Close()
case *flate.Writer:
output_writer.(*flate.Writer).Close()
}
} }
// Cookie sets cookie value via given key. // Cookie sets cookie value via given key.
@ -98,26 +83,25 @@ func (output *BeegoOutput) Body(content []byte) {
func (output *BeegoOutput) Cookie(name string, value string, others ...interface{}) { func (output *BeegoOutput) Cookie(name string, value string, others ...interface{}) {
var b bytes.Buffer var b bytes.Buffer
fmt.Fprintf(&b, "%s=%s", sanitizeName(name), sanitizeValue(value)) fmt.Fprintf(&b, "%s=%s", sanitizeName(name), sanitizeValue(value))
//fix cookie not work in IE
if len(others) > 0 { if len(others) > 0 {
var maxAge int64
switch v := others[0].(type) { switch v := others[0].(type) {
case int: case int:
if v > 0 { maxAge = int64(v)
fmt.Fprintf(&b, "; Max-Age=%d", v)
} else if v < 0 {
fmt.Fprintf(&b, "; Max-Age=0")
}
case int64:
if v > 0 {
fmt.Fprintf(&b, "; Max-Age=%d", v)
} else if v < 0 {
fmt.Fprintf(&b, "; Max-Age=0")
}
case int32: case int32:
if v > 0 { maxAge = int64(v)
fmt.Fprintf(&b, "; Max-Age=%d", v) case int64:
} else if v < 0 { maxAge = v
fmt.Fprintf(&b, "; Max-Age=0") }
}
switch {
case maxAge > 0:
fmt.Fprintf(&b, "; Expires=%s; Max-Age=%d", time.Now().Add(time.Duration(maxAge)*time.Second).UTC().Format(time.RFC1123), maxAge)
case maxAge < 0:
fmt.Fprintf(&b, "; Max-Age=0")
} }
} }
@ -185,10 +169,10 @@ func sanitizeValue(v string) string {
return cookieValueSanitizer.Replace(v) return cookieValueSanitizer.Replace(v)
} }
// Json writes json to response body. // JSON writes json to response body.
// if coding is true, it converts utf-8 to \u0000 type. // if coding is true, it converts utf-8 to \u0000 type.
func (output *BeegoOutput) Json(data interface{}, hasIndent bool, coding bool) error { func (output *BeegoOutput) JSON(data interface{}, hasIndent bool, coding bool) error {
output.Header("Content-Type", "application/json;charset=UTF-8") output.Header("Content-Type", "application/json; charset=utf-8")
var content []byte var content []byte
var err error var err error
if hasIndent { if hasIndent {
@ -201,15 +185,14 @@ func (output *BeegoOutput) Json(data interface{}, hasIndent bool, coding bool) e
return err return err
} }
if coding { if coding {
content = []byte(stringsToJson(string(content))) content = []byte(stringsToJSON(string(content)))
} }
output.Body(content) return output.Body(content)
return nil
} }
// Jsonp writes jsonp to response body. // JSONP writes jsonp to response body.
func (output *BeegoOutput) Jsonp(data interface{}, hasIndent bool) error { func (output *BeegoOutput) JSONP(data interface{}, hasIndent bool) error {
output.Header("Content-Type", "application/javascript;charset=UTF-8") output.Header("Content-Type", "application/javascript; charset=utf-8")
var content []byte var content []byte
var err error var err error
if hasIndent { if hasIndent {
@ -225,17 +208,16 @@ func (output *BeegoOutput) Jsonp(data interface{}, hasIndent bool) error {
if callback == "" { if callback == "" {
return errors.New(`"callback" parameter required`) return errors.New(`"callback" parameter required`)
} }
callback_content := bytes.NewBufferString(" " + template.JSEscapeString(callback)) callbackContent := bytes.NewBufferString(" " + template.JSEscapeString(callback))
callback_content.WriteString("(") callbackContent.WriteString("(")
callback_content.Write(content) callbackContent.Write(content)
callback_content.WriteString(");\r\n") callbackContent.WriteString(");\r\n")
output.Body(callback_content.Bytes()) return output.Body(callbackContent.Bytes())
return nil
} }
// Xml writes xml string to response body. // XML writes xml string to response body.
func (output *BeegoOutput) Xml(data interface{}, hasIndent bool) error { func (output *BeegoOutput) XML(data interface{}, hasIndent bool) error {
output.Header("Content-Type", "application/xml;charset=UTF-8") output.Header("Content-Type", "application/xml; charset=utf-8")
var content []byte var content []byte
var err error var err error
if hasIndent { if hasIndent {
@ -247,8 +229,7 @@ func (output *BeegoOutput) Xml(data interface{}, hasIndent bool) error {
http.Error(output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError) http.Error(output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
return err return err
} }
output.Body(content) return output.Body(content)
return nil
} }
// Download forces response for download file. // Download forces response for download file.
@ -328,7 +309,7 @@ func (output *BeegoOutput) IsNotFound(status int) bool {
return output.Status == 404 return output.Status == 404
} }
// IsClient returns boolean of this request client sends error data. // IsClientError returns boolean of this request client sends error data.
// HTTP 4xx means forbidden. // HTTP 4xx means forbidden.
func (output *BeegoOutput) IsClientError(status int) bool { func (output *BeegoOutput) IsClientError(status int) bool {
return output.Status >= 400 && output.Status < 500 return output.Status >= 400 && output.Status < 500
@ -340,7 +321,7 @@ func (output *BeegoOutput) IsServerError(status int) bool {
return output.Status >= 500 && output.Status < 600 return output.Status >= 500 && output.Status < 600
} }
func stringsToJson(str string) string { func stringsToJSON(str string) string {
rs := []rune(str) rs := []rune(str)
jsons := "" jsons := ""
for _, r := range rs { for _, r := range rs {
@ -354,7 +335,7 @@ func stringsToJson(str string) string {
return jsons return jsons
} }
// Sessions sets session item value with given key. // Session sets session item value with given key.
func (output *BeegoOutput) Session(name interface{}, value interface{}) { func (output *BeegoOutput) Session(name interface{}, value interface{}) {
output.Context.Input.CruSession.Set(name, value) output.Context.Input.CruSession.Set(name, value)
} }

View File

@ -19,7 +19,6 @@ import (
"errors" "errors"
"html/template" "html/template"
"io" "io"
"io/ioutil"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"net/url" "net/url"
@ -34,18 +33,19 @@ import (
//commonly used mime-types //commonly used mime-types
const ( const (
applicationJson = "application/json" applicationJSON = "application/json"
applicationXml = "application/xml" applicationXML = "application/xml"
textXml = "text/xml" textXML = "text/xml"
) )
var ( var (
// custom error when user stop request handler manually. // ErrAbort custom error when user stop request handler manually.
USERSTOPRUN = errors.New("User stop run") ErrAbort = errors.New("User stop run")
GlobalControllerRouter map[string][]ControllerComments = make(map[string][]ControllerComments) //pkgpath+controller:comments // GlobalControllerRouter store comments with controller. pkgpath+controller:comments
GlobalControllerRouter = make(map[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
@ -56,22 +56,31 @@ type ControllerComments struct {
// Controller defines some basic http request handler operations, such as // Controller defines some basic http request handler operations, such as
// http context, template and view, session and xsrf. // http context, template and view, session and xsrf.
type Controller struct { type Controller struct {
Ctx *context.Context // context data
Data map[interface{}]interface{} Ctx *context.Context
Data map[interface{}]interface{}
// route controller info
controllerName string controllerName string
actionName string actionName string
TplNames string methodMapping map[string]func() //method:routertree
gotofunc string
AppController interface{}
// template data
TplName string
Layout string Layout string
LayoutSections map[string]string // the key is the section name and the value is the template name LayoutSections map[string]string // the key is the section name and the value is the template name
TplExt string TplExt string
_xsrf_token string
gotofunc string
CruSession session.SessionStore
XSRFExpire int
AppController interface{}
EnableRender bool EnableRender bool
EnableXSRF bool
methodMapping map[string]func() //method:routertree // xsrf data
_xsrfToken string
XSRFExpire int
EnableXSRF bool
// session
CruSession session.Store
} }
// ControllerInterface is an interface to uniform all controller handler. // ControllerInterface is an interface to uniform all controller handler.
@ -87,8 +96,8 @@ type ControllerInterface interface {
Options() Options()
Finish() Finish()
Render() error Render() error
XsrfToken() string XSRFToken() string
CheckXsrfCookie() bool CheckXSRFCookie() bool
HandlerFunc(fn string) bool HandlerFunc(fn string) bool
URLMapping() URLMapping()
} }
@ -96,7 +105,7 @@ type ControllerInterface interface {
// Init generates default values of controller operations. // Init generates default values of controller operations.
func (c *Controller) Init(ctx *context.Context, controllerName, actionName string, app interface{}) { func (c *Controller) Init(ctx *context.Context, controllerName, actionName string, app interface{}) {
c.Layout = "" c.Layout = ""
c.TplNames = "" c.TplName = ""
c.controllerName = controllerName c.controllerName = controllerName
c.actionName = actionName c.actionName = actionName
c.Ctx = ctx c.Ctx = ctx
@ -104,19 +113,15 @@ func (c *Controller) Init(ctx *context.Context, controllerName, actionName strin
c.AppController = app c.AppController = app
c.EnableRender = true c.EnableRender = true
c.EnableXSRF = true c.EnableXSRF = true
c.Data = ctx.Input.Data c.Data = ctx.Input.Data()
c.methodMapping = make(map[string]func()) c.methodMapping = make(map[string]func())
} }
// Prepare runs after Init before request function execution. // Prepare runs after Init before request function execution.
func (c *Controller) Prepare() { func (c *Controller) Prepare() {}
}
// Finish runs after request function execution. // Finish runs after request function execution.
func (c *Controller) Finish() { func (c *Controller) Finish() {}
}
// Get adds a request function to handle GET request. // Get adds a request function to handle GET request.
func (c *Controller) Get() { func (c *Controller) Get() {
@ -153,20 +158,19 @@ func (c *Controller) Options() {
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405) http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
} }
// call function fn // HandlerFunc call function with the name
func (c *Controller) HandlerFunc(fnname string) bool { func (c *Controller) HandlerFunc(fnname string) bool {
if v, ok := c.methodMapping[fnname]; ok { if v, ok := c.methodMapping[fnname]; ok {
v() v()
return true return true
} else {
return false
} }
return false
} }
// URLMapping register the internal Controller router. // URLMapping register the internal Controller router.
func (c *Controller) URLMapping() { func (c *Controller) URLMapping() {}
}
// Mapping the method to function
func (c *Controller) Mapping(method string, fn func()) { func (c *Controller) Mapping(method string, fn func()) {
c.methodMapping[method] = fn c.methodMapping[method] = fn
} }
@ -177,14 +181,11 @@ func (c *Controller) Render() error {
return nil return nil
} }
rb, err := c.RenderBytes() rb, err := c.RenderBytes()
if err != nil { if err != nil {
return err return err
} else {
c.Ctx.Output.Header("Content-Type", "text/html; charset=utf-8")
c.Ctx.Output.Body(rb)
} }
return nil c.Ctx.Output.Header("Content-Type", "text/html; charset=utf-8")
return c.Ctx.Output.Body(rb)
} }
// RenderString returns the rendered template string. Do not send out response. // RenderString returns the rendered template string. Do not send out response.
@ -195,25 +196,10 @@ func (c *Controller) RenderString() (string, error) {
// RenderBytes returns the bytes of rendered template string. Do not send out response. // RenderBytes returns the bytes of rendered template string. Do not send out response.
func (c *Controller) RenderBytes() ([]byte, error) { func (c *Controller) RenderBytes() ([]byte, error) {
//if the controller has set layout, then first get the tplname's content set the content to the layout buf, err := c.renderTemplate()
if c.Layout != "" { //if the controller has set layout, then first get the tplName's content set the content to the layout
if c.TplNames == "" { if err == nil && c.Layout != "" {
c.TplNames = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt c.Data["LayoutContent"] = template.HTML(buf.String())
}
if RunMode == "dev" {
BuildTemplate(ViewsPath)
}
newbytes := bytes.NewBufferString("")
if _, ok := BeeTemplates[c.TplNames]; !ok {
panic("can't find templatefile in the path:" + c.TplNames)
}
err := BeeTemplates[c.TplNames].ExecuteTemplate(newbytes, c.TplNames, c.Data)
if err != nil {
Trace("template Execute err:", err)
return nil, err
}
tplcontent, _ := ioutil.ReadAll(newbytes)
c.Data["LayoutContent"] = template.HTML(string(tplcontent))
if c.LayoutSections != nil { if c.LayoutSections != nil {
for sectionName, sectionTpl := range c.LayoutSections { for sectionName, sectionTpl := range c.LayoutSections {
@ -221,45 +207,42 @@ func (c *Controller) RenderBytes() ([]byte, error) {
c.Data[sectionName] = "" c.Data[sectionName] = ""
continue continue
} }
buf.Reset()
sectionBytes := bytes.NewBufferString("") err = executeTemplate(&buf, sectionTpl, c.Data)
err = BeeTemplates[sectionTpl].ExecuteTemplate(sectionBytes, sectionTpl, c.Data)
if err != nil { if err != nil {
Trace("template Execute err:", err)
return nil, err return nil, err
} }
sectionContent, _ := ioutil.ReadAll(sectionBytes) c.Data[sectionName] = template.HTML(buf.String())
c.Data[sectionName] = template.HTML(string(sectionContent))
} }
} }
ibytes := bytes.NewBufferString("") buf.Reset()
err = BeeTemplates[c.Layout].ExecuteTemplate(ibytes, c.Layout, c.Data) executeTemplate(&buf, c.Layout, c.Data)
if err != nil {
Trace("template Execute err:", err)
return nil, err
}
icontent, _ := ioutil.ReadAll(ibytes)
return icontent, nil
} else {
if c.TplNames == "" {
c.TplNames = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt
}
if RunMode == "dev" {
BuildTemplate(ViewsPath)
}
ibytes := bytes.NewBufferString("")
if _, ok := BeeTemplates[c.TplNames]; !ok {
panic("can't find templatefile in the path:" + c.TplNames)
}
err := BeeTemplates[c.TplNames].ExecuteTemplate(ibytes, c.TplNames, c.Data)
if err != nil {
Trace("template Execute err:", err)
return nil, err
}
icontent, _ := ioutil.ReadAll(ibytes)
return icontent, nil
} }
return buf.Bytes(), err
}
func (c *Controller) renderTemplate() (bytes.Buffer, error) {
var buf bytes.Buffer
if c.TplName == "" {
c.TplName = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt
}
if BConfig.RunMode == DEV {
buildFiles := []string{c.TplName}
if c.Layout != "" {
buildFiles = append(buildFiles, c.Layout)
if c.LayoutSections != nil {
for _, sectionTpl := range c.LayoutSections {
if sectionTpl == "" {
continue
}
buildFiles = append(buildFiles, sectionTpl)
}
}
}
BuildTemplate(BConfig.WebConfig.ViewsPath, buildFiles...)
}
return buf, executeTemplate(&buf, c.TplName, c.Data)
} }
// Redirect sends the redirection response to url with status code. // Redirect sends the redirection response to url with status code.
@ -267,86 +250,87 @@ func (c *Controller) Redirect(url string, code int) {
c.Ctx.Redirect(code, url) c.Ctx.Redirect(code, url)
} }
// Aborts stops controller handler and show the error data if code is defined in ErrorMap or code string. // Abort stops controller handler and show the error data if code is defined in ErrorMap or code string.
func (c *Controller) Abort(code string) { func (c *Controller) Abort(code string) {
status, err := strconv.Atoi(code) status, err := strconv.Atoi(code)
if err == nil { if err != nil {
c.Ctx.Abort(status, code) status = 200
} else {
c.Ctx.Abort(200, code)
} }
c.CustomAbort(status, code)
} }
// CustomAbort stops controller handler and show the error data, it's similar Aborts, but support status code and body. // CustomAbort stops controller handler and show the error data, it's similar Aborts, but support status code and body.
func (c *Controller) CustomAbort(status int, body string) { func (c *Controller) CustomAbort(status int, body string) {
c.Ctx.Abort(status, body) c.Ctx.Output.Status = status
// first panic from ErrorMaps, is is user defined error functions.
if _, ok := ErrorMaps[body]; ok {
panic(body)
}
// last panic user string
c.Ctx.ResponseWriter.Write([]byte(body))
panic(ErrAbort)
} }
// StopRun makes panic of USERSTOPRUN error and go to recover function if defined. // StopRun makes panic of USERSTOPRUN error and go to recover function if defined.
func (c *Controller) StopRun() { func (c *Controller) StopRun() {
panic(USERSTOPRUN) panic(ErrAbort)
} }
// UrlFor does another controller handler in this request function. // URLFor does another controller handler in this request function.
// it goes to this controller method if endpoint is not clear. // it goes to this controller method if endpoint is not clear.
func (c *Controller) UrlFor(endpoint string, values ...string) string { func (c *Controller) URLFor(endpoint string, values ...interface{}) string {
if len(endpoint) <= 0 { if len(endpoint) == 0 {
return "" return ""
} }
if endpoint[0] == '.' { if endpoint[0] == '.' {
return UrlFor(reflect.Indirect(reflect.ValueOf(c.AppController)).Type().Name()+endpoint, values...) return URLFor(reflect.Indirect(reflect.ValueOf(c.AppController)).Type().Name()+endpoint, values...)
} else {
return UrlFor(endpoint, values...)
} }
return URLFor(endpoint, values...)
} }
// 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 hasIndent bool var (
var hasencoding bool hasIndent = true
if RunMode == "prod" { hasEncoding = false
)
if BConfig.RunMode == PROD {
hasIndent = false hasIndent = false
} else {
hasIndent = true
} }
if len(encoding) > 0 && encoding[0] == true { if len(encoding) > 0 && encoding[0] == true {
hasencoding = true 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() {
var hasIndent bool hasIndent := true
if RunMode == "prod" { if BConfig.RunMode == PROD {
hasIndent = false hasIndent = false
} else {
hasIndent = true
} }
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() {
var hasIndent bool hasIndent := true
if RunMode == "prod" { if BConfig.RunMode == PROD {
hasIndent = false hasIndent = false
} else {
hasIndent = true
} }
c.Ctx.Output.Xml(c.Data["xml"], hasIndent) c.Ctx.Output.XML(c.Data["xml"], hasIndent)
} }
// ServeFormatted serve Xml OR Json, depending on the value of the Accept header // ServeFormatted serve Xml OR Json, depending on the value of the Accept header
func (c *Controller) ServeFormatted() { func (c *Controller) ServeFormatted() {
accept := c.Ctx.Input.Header("Accept") accept := c.Ctx.Input.Header("Accept")
switch accept { switch accept {
case applicationJson: case applicationJSON:
c.ServeJson() c.ServeJSON()
case applicationXml, textXml: case applicationXML, textXML:
c.ServeXml() c.ServeXML()
default: default:
c.ServeJson() c.ServeJSON()
} }
} }
@ -363,67 +347,98 @@ func (c *Controller) ParseForm(obj interface{}) error {
return ParseForm(c.Input(), obj) return ParseForm(c.Input(), obj)
} }
// GetString returns the input value by key string. // GetString returns the input value by key string or the default value while it's present and input is blank
func (c *Controller) GetString(key string) string { func (c *Controller) GetString(key string, def ...string) string {
return c.Ctx.Input.Query(key) if v := c.Ctx.Input.Query(key); v != "" {
return v
}
if len(def) > 0 {
return def[0]
}
return ""
} }
// GetStrings returns the input string slice by key string. // GetStrings returns the input string slice by key string or the default value while it's present and input is blank
// it's designed for multi-value input field such as checkbox(input[type=checkbox]), multi-selection. // it's designed for multi-value input field such as checkbox(input[type=checkbox]), multi-selection.
func (c *Controller) GetStrings(key string) []string { func (c *Controller) GetStrings(key string, def ...[]string) []string {
f := c.Input() var defv []string
if f == nil { if len(def) > 0 {
return []string{} defv = def[0]
} }
vs := f[key]
if len(vs) > 0 { if f := c.Input(); f == nil {
return defv
} else if vs := f[key]; len(vs) > 0 {
return vs return vs
} }
return []string{}
return defv
} }
// GetInt returns input as an int // GetInt returns input as an int or the default value while it's present and input is blank
func (c *Controller) GetInt(key string) (int, error) { func (c *Controller) GetInt(key string, def ...int) (int, error) {
return strconv.Atoi(c.Ctx.Input.Query(key)) strv := c.Ctx.Input.Query(key)
if len(strv) == 0 && len(def) > 0 {
return def[0], nil
}
return strconv.Atoi(strv)
} }
// GetInt8 return input as an int8 // GetInt8 return input as an int8 or the default value while it's present and input is blank
func (c *Controller) GetInt8(key string) (int8, error) { func (c *Controller) GetInt8(key string, def ...int8) (int8, error) {
i64, err := strconv.ParseInt(c.Ctx.Input.Query(key), 10, 8) strv := c.Ctx.Input.Query(key)
i8 := int8(i64) if len(strv) == 0 && len(def) > 0 {
return def[0], nil
return i8, err }
i64, err := strconv.ParseInt(strv, 10, 8)
return int8(i64), err
} }
// GetInt16 returns input as an int16 // GetInt16 returns input as an int16 or the default value while it's present and input is blank
func (c *Controller) GetInt16(key string) (int16, error) { func (c *Controller) GetInt16(key string, def ...int16) (int16, error) {
i64, err := strconv.ParseInt(c.Ctx.Input.Query(key), 10, 16) strv := c.Ctx.Input.Query(key)
i16 := int16(i64) if len(strv) == 0 && len(def) > 0 {
return def[0], nil
return i16, err }
i64, err := strconv.ParseInt(strv, 10, 16)
return int16(i64), err
} }
// GetInt32 returns input as an int32 // GetInt32 returns input as an int32 or the default value while it's present and input is blank
func (c *Controller) GetInt32(key string) (int32, error) { func (c *Controller) GetInt32(key string, def ...int32) (int32, error) {
i64, err := strconv.ParseInt(c.Ctx.Input.Query(key), 10, 32) strv := c.Ctx.Input.Query(key)
i32 := int32(i64) if len(strv) == 0 && len(def) > 0 {
return def[0], nil
return i32, err }
i64, err := strconv.ParseInt(strv, 10, 32)
return int32(i64), err
} }
// GetInt64 returns input value as int64. // GetInt64 returns input value as int64 or the default value while it's present and input is blank.
func (c *Controller) GetInt64(key string) (int64, error) { func (c *Controller) GetInt64(key string, def ...int64) (int64, error) {
return strconv.ParseInt(c.Ctx.Input.Query(key), 10, 64) strv := c.Ctx.Input.Query(key)
if len(strv) == 0 && len(def) > 0 {
return def[0], nil
}
return strconv.ParseInt(strv, 10, 64)
} }
// GetBool returns input value as bool. // GetBool returns input value as bool or the default value while it's present and input is blank.
func (c *Controller) GetBool(key string) (bool, error) { func (c *Controller) GetBool(key string, def ...bool) (bool, error) {
return strconv.ParseBool(c.Ctx.Input.Query(key)) strv := c.Ctx.Input.Query(key)
if len(strv) == 0 && len(def) > 0 {
return def[0], nil
}
return strconv.ParseBool(strv)
} }
// GetFloat returns input value as float64. // GetFloat returns input value as float64 or the default value while it's present and input is blank.
func (c *Controller) GetFloat(key string) (float64, error) { func (c *Controller) GetFloat(key string, def ...float64) (float64, error) {
return strconv.ParseFloat(c.Ctx.Input.Query(key), 64) strv := c.Ctx.Input.Query(key)
if len(strv) == 0 && len(def) > 0 {
return def[0], nil
}
return strconv.ParseFloat(strv, 64)
} }
// GetFile returns the file data in file upload field named as key. // GetFile returns the file data in file upload field named as key.
@ -432,6 +447,40 @@ func (c *Controller) GetFile(key string) (multipart.File, *multipart.FileHeader,
return c.Ctx.Request.FormFile(key) return c.Ctx.Request.FormFile(key)
} }
// GetFiles return multi-upload files
// files, err:=c.Getfiles("myfiles")
// if err != nil {
// http.Error(w, err.Error(), http.StatusNoContent)
// return
// }
// for i, _ := range files {
// //for each fileheader, get a handle to the actual file
// file, err := files[i].Open()
// defer file.Close()
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// //create destination file making sure the path is writeable.
// dst, err := os.Create("upload/" + files[i].Filename)
// defer dst.Close()
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// //copy the uploaded file to the destination file
// if _, err := io.Copy(dst, file); err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// }
func (c *Controller) GetFiles(key string) ([]*multipart.FileHeader, error) {
if files, ok := c.Ctx.Request.MultipartForm.File[key]; ok {
return files, nil
}
return nil, http.ErrMissingFile
}
// SaveToFile saves uploaded file to new path. // SaveToFile saves uploaded file to new path.
// it only operates the first one of mutil-upload form file field. // it only operates the first one of mutil-upload form file field.
func (c *Controller) SaveToFile(fromfile, tofile string) error { func (c *Controller) SaveToFile(fromfile, tofile string) error {
@ -450,7 +499,7 @@ func (c *Controller) SaveToFile(fromfile, tofile string) error {
} }
// StartSession starts session and load old session data info this controller. // StartSession starts session and load old session data info this controller.
func (c *Controller) StartSession() session.SessionStore { func (c *Controller) StartSession() session.Store {
if c.CruSession == nil { if c.CruSession == nil {
c.CruSession = c.Ctx.Input.CruSession c.CruSession = c.Ctx.Input.CruSession
} }
@ -473,7 +522,7 @@ func (c *Controller) GetSession(name interface{}) interface{} {
return c.CruSession.Get(name) return c.CruSession.Get(name)
} }
// SetSession removes value from session. // DelSession removes value from session.
func (c *Controller) DelSession(name interface{}) { func (c *Controller) DelSession(name interface{}) {
if c.CruSession == nil { if c.CruSession == nil {
c.StartSession() c.StartSession()
@ -487,13 +536,14 @@ func (c *Controller) SessionRegenerateID() {
if c.CruSession != nil { if c.CruSession != nil {
c.CruSession.SessionRelease(c.Ctx.ResponseWriter) c.CruSession.SessionRelease(c.Ctx.ResponseWriter)
} }
c.CruSession = GlobalSessions.SessionRegenerateId(c.Ctx.ResponseWriter, c.Ctx.Request) c.CruSession = GlobalSessions.SessionRegenerateID(c.Ctx.ResponseWriter, c.Ctx.Request)
c.Ctx.Input.CruSession = c.CruSession c.Ctx.Input.CruSession = c.CruSession
} }
// DestroySession cleans session data and session cookie. // DestroySession cleans session data and session cookie.
func (c *Controller) DestroySession() { func (c *Controller) DestroySession() {
c.Ctx.Input.CruSession.Flush() c.Ctx.Input.CruSession.Flush()
c.Ctx.Input.CruSession = nil
GlobalSessions.SessionDestroy(c.Ctx.ResponseWriter, c.Ctx.Request) GlobalSessions.SessionDestroy(c.Ctx.ResponseWriter, c.Ctx.Request)
} }
@ -512,37 +562,35 @@ func (c *Controller) SetSecureCookie(Secret, name, value string, others ...inter
c.Ctx.SetSecureCookie(Secret, name, value, others...) c.Ctx.SetSecureCookie(Secret, name, value, others...)
} }
// XsrfToken creates a xsrf token string and returns. // XSRFToken creates a CSRF token string and returns.
func (c *Controller) XsrfToken() string { func (c *Controller) XSRFToken() string {
if c._xsrf_token == "" { if c._xsrfToken == "" {
var expire int64 expire := int64(BConfig.WebConfig.XSRFExpire)
if c.XSRFExpire > 0 { if c.XSRFExpire > 0 {
expire = int64(c.XSRFExpire) expire = int64(c.XSRFExpire)
} else {
expire = int64(XSRFExpire)
} }
c._xsrf_token = c.Ctx.XsrfToken(XSRFKEY, expire) c._xsrfToken = c.Ctx.XSRFToken(BConfig.WebConfig.XSRFKey, expire)
} }
return c._xsrf_token return c._xsrfToken
} }
// CheckXsrfCookie checks xsrf token in this request is valid or not. // CheckXSRFCookie checks xsrf token in this request is valid or not.
// the token can provided in request header "X-Xsrftoken" and "X-CsrfToken" // the token can provided in request header "X-Xsrftoken" and "X-CsrfToken"
// or in form field value named as "_xsrf". // or in form field value named as "_xsrf".
func (c *Controller) CheckXsrfCookie() bool { func (c *Controller) CheckXSRFCookie() bool {
if !c.EnableXSRF { if !c.EnableXSRF {
return true return true
} }
return c.Ctx.CheckXsrfCookie() return c.Ctx.CheckXSRFCookie()
} }
// XsrfFormHtml writes an input field contains xsrf token value. // XSRFFormHTML writes an input field contains xsrf token value.
func (c *Controller) XsrfFormHtml() string { func (c *Controller) XSRFFormHTML() string {
return "<input type=\"hidden\" name=\"_xsrf\" value=\"" + return `<input type="hidden" name="_xsrf" value="` +
c._xsrf_token + "\"/>" c.XSRFToken() + `" />`
} }
// GetControllerAndAction gets the executing controller name and action name. // GetControllerAndAction gets the executing controller name and action name.
func (c *Controller) GetControllerAndAction() (controllerName, actionName string) { func (c *Controller) GetControllerAndAction() (string, string) {
return c.controllerName, c.actionName return c.controllerName, c.actionName
} }

View File

@ -15,61 +15,63 @@
package beego package beego
import ( import (
"fmt" "testing"
"github.com/astaxie/beego/context" "github.com/astaxie/beego/context"
) )
func ExampleGetInt() { func TestGetInt(t *testing.T) {
i := context.NewInput()
i := &context.BeegoInput{Params: map[string]string{"age": "40"}} i.SetParam("age", "40")
ctx := &context.Context{Input: i} ctx := &context.Context{Input: i}
ctrlr := Controller{Ctx: ctx} ctrlr := Controller{Ctx: ctx}
val, _ := ctrlr.GetInt("age") val, _ := ctrlr.GetInt("age")
fmt.Printf("%T", val) if val != 40 {
//Output: int t.Errorf("TestGetInt expect 40,get %T,%v", val, val)
}
} }
func ExampleGetInt8() { func TestGetInt8(t *testing.T) {
i := context.NewInput()
i := &context.BeegoInput{Params: map[string]string{"age": "40"}} i.SetParam("age", "40")
ctx := &context.Context{Input: i} ctx := &context.Context{Input: i}
ctrlr := Controller{Ctx: ctx} ctrlr := Controller{Ctx: ctx}
val, _ := ctrlr.GetInt8("age") val, _ := ctrlr.GetInt8("age")
fmt.Printf("%T", val) if val != 40 {
t.Errorf("TestGetInt8 expect 40,get %T,%v", val, val)
}
//Output: int8 //Output: int8
} }
func ExampleGetInt16() { func TestGetInt16(t *testing.T) {
i := context.NewInput()
i := &context.BeegoInput{Params: map[string]string{"age": "40"}} i.SetParam("age", "40")
ctx := &context.Context{Input: i} ctx := &context.Context{Input: i}
ctrlr := Controller{Ctx: ctx} ctrlr := Controller{Ctx: ctx}
val, _ := ctrlr.GetInt16("age") val, _ := ctrlr.GetInt16("age")
fmt.Printf("%T", val) if val != 40 {
//Output: int16 t.Errorf("TestGetInt16 expect 40,get %T,%v", val, val)
}
} }
func ExampleGetInt32() { func TestGetInt32(t *testing.T) {
i := context.NewInput()
i := &context.BeegoInput{Params: map[string]string{"age": "40"}} i.SetParam("age", "40")
ctx := &context.Context{Input: i} ctx := &context.Context{Input: i}
ctrlr := Controller{Ctx: ctx} ctrlr := Controller{Ctx: ctx}
val, _ := ctrlr.GetInt32("age") val, _ := ctrlr.GetInt32("age")
fmt.Printf("%T", val) if val != 40 {
//Output: int32 t.Errorf("TestGetInt32 expect 40,get %T,%v", val, val)
}
} }
func ExampleGetInt64() { func TestGetInt64(t *testing.T) {
i := context.NewInput()
i := &context.BeegoInput{Params: map[string]string{"age": "40"}} i.SetParam("age", "40")
ctx := &context.Context{Input: i} ctx := &context.Context{Input: i}
ctrlr := Controller{Ctx: ctx} ctrlr := Controller{Ctx: ctx}
val, _ := ctrlr.GetInt64("age") val, _ := ctrlr.GetInt64("age")
fmt.Printf("%T", val) if val != 40 {
//Output: int64 t.Errorf("TestGeetInt64 expect 40,get %T,%v", val, val)
}
} }

17
doc.go Normal file
View File

@ -0,0 +1,17 @@
/*
Package beego provide a MVC framework
beego: an open-source, high-performance, modular, full-stack web framework
It is used for rapid development of RESTful APIs, web apps and backend services in Go.
beego is inspired by Tornado, Sinatra and Flask with the added benefit of some Go-specific features such as interfaces and struct embedding.
package main
import "github.com/astaxie/beego"
func main() {
beego.Run()
}
more information: http://beego.me
*/
package beego

23
docs.go
View File

@ -15,37 +15,24 @@
package beego package beego
import ( import (
"encoding/json"
"github.com/astaxie/beego/context" "github.com/astaxie/beego/context"
) )
var GlobalDocApi map[string]interface{} // GlobalDocAPI store the swagger api documents
var GlobalDocAPI = make(map[string]interface{})
func init() {
if EnableDocs {
GlobalDocApi = make(map[string]interface{})
}
}
func serverDocs(ctx *context.Context) { func serverDocs(ctx *context.Context) {
var obj interface{} var obj interface{}
if splat := ctx.Input.Param(":splat"); splat == "" { if splat := ctx.Input.Param(":splat"); splat == "" {
obj = GlobalDocApi["Root"] obj = GlobalDocAPI["Root"]
} else { } else {
if v, ok := GlobalDocApi[splat]; ok { if v, ok := GlobalDocAPI[splat]; ok {
obj = v obj = v
} }
} }
if obj != nil { if obj != nil {
bt, err := json.Marshal(obj)
if err != nil {
ctx.Output.SetStatus(504)
return
}
ctx.Output.Header("Content-Type", "application/json;charset=UTF-8")
ctx.Output.Header("Access-Control-Allow-Origin", "*") ctx.Output.Header("Access-Control-Allow-Origin", "*")
ctx.Output.Body(bt) ctx.Output.JSON(obj, false, false)
return return
} }
ctx.Output.SetStatus(404) ctx.Output.SetStatus(404)

460
error.go Normal file
View File

@ -0,0 +1,460 @@
// Copyright 2014 beego Author. All Rights Reserved.
//
// 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.
package beego
import (
"fmt"
"html/template"
"net/http"
"reflect"
"runtime"
"strconv"
"strings"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/utils"
)
const (
errorTypeHandler = iota
errorTypeController
)
var tpl = `
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>beego application error</title>
<style>
html, body, body * {padding: 0; margin: 0;}
#header {background:#ffd; border-bottom:solid 2px #A31515; padding: 20px 10px;}
#header h2{ }
#footer {border-top:solid 1px #aaa; padding: 5px 10px; font-size: 12px; color:green;}
#content {padding: 5px;}
#content .stack b{ font-size: 13px; color: red;}
#content .stack pre{padding-left: 10px;}
table {}
td.t {text-align: right; padding-right: 5px; color: #888;}
</style>
<script type="text/javascript">
</script>
</head>
<body>
<div id="header">
<h2>{{.AppError}}</h2>
</div>
<div id="content">
<table>
<tr>
<td class="t">Request Method: </td><td>{{.RequestMethod}}</td>
</tr>
<tr>
<td class="t">Request URL: </td><td>{{.RequestURL}}</td>
</tr>
<tr>
<td class="t">RemoteAddr: </td><td>{{.RemoteAddr }}</td>
</tr>
</table>
<div class="stack">
<b>Stack</b>
<pre>{{.Stack}}</pre>
</div>
</div>
<div id="footer">
<p>beego {{ .BeegoVersion }} (beego framework)</p>
<p>golang version: {{.GoVersion}}</p>
</div>
</body>
</html>
`
// render default application error page with error and stack string.
func showErr(err interface{}, ctx *context.Context, stack string) {
t, _ := template.New("beegoerrortemp").Parse(tpl)
data := map[string]string{
"AppError": fmt.Sprintf("%s:%v", BConfig.AppName, err),
"RequestMethod": ctx.Input.Method(),
"RequestURL": ctx.Input.URI(),
"RemoteAddr": ctx.Input.IP(),
"Stack": stack,
"BeegoVersion": VERSION,
"GoVersion": runtime.Version(),
}
ctx.ResponseWriter.WriteHeader(500)
t.Execute(ctx.ResponseWriter, data)
}
var errtpl = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{{.Title}}</title>
<style type="text/css">
* {
margin:0;
padding:0;
}
body {
background-color:#EFEFEF;
font: .9em "Lucida Sans Unicode", "Lucida Grande", sans-serif;
}
#wrapper{
width:600px;
margin:40px auto 0;
text-align:center;
-moz-box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
-webkit-box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
}
#wrapper h1{
color:#FFF;
text-align:center;
margin-bottom:20px;
}
#wrapper a{
display:block;
font-size:.9em;
padding-top:20px;
color:#FFF;
text-decoration:none;
text-align:center;
}
#container {
width:600px;
padding-bottom:15px;
background-color:#FFFFFF;
}
.navtop{
height:40px;
background-color:#24B2EB;
padding:13px;
}
.content {
padding:10px 10px 25px;
background: #FFFFFF;
margin:;
color:#333;
}
a.button{
color:white;
padding:15px 20px;
text-shadow:1px 1px 0 #00A5FF;
font-weight:bold;
text-align:center;
border:1px solid #24B2EB;
margin:0px 200px;
clear:both;
background-color: #24B2EB;
border-radius:100px;
-moz-border-radius:100px;
-webkit-border-radius:100px;
}
a.button:hover{
text-decoration:none;
background-color: #24B2EB;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="container">
<div class="navtop">
<h1>{{.Title}}</h1>
</div>
<div id="content">
{{.Content}}
<a href="/" title="Home" class="button">Go Home</a><br />
<br>Powered by beego {{.BeegoVersion}}
</div>
</div>
</div>
</body>
</html>
`
type errorInfo struct {
controllerType reflect.Type
handler http.HandlerFunc
method string
errorType int
}
// ErrorMaps holds map of http handlers for each error string.
// there is 10 kinds default error(40x and 50x)
var ErrorMaps = make(map[string]*errorInfo, 10)
// show 401 unauthorized error.
func unauthorized(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
data := map[string]interface{}{
"Title": http.StatusText(401),
"BeegoVersion": VERSION,
}
data["Content"] = template.HTML("<br>The page you have requested can't be authorized." +
"<br>Perhaps you are here because:" +
"<br><br><ul>" +
"<br>The credentials you supplied are incorrect" +
"<br>There are errors in the website address" +
"</ul>")
t.Execute(rw, data)
}
// show 402 Payment Required
func paymentRequired(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
data := map[string]interface{}{
"Title": http.StatusText(402),
"BeegoVersion": VERSION,
}
data["Content"] = template.HTML("<br>The page you have requested Payment Required." +
"<br>Perhaps you are here because:" +
"<br><br><ul>" +
"<br>The credentials you supplied are incorrect" +
"<br>There are errors in the website address" +
"</ul>")
t.Execute(rw, data)
}
// show 403 forbidden error.
func forbidden(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
data := map[string]interface{}{
"Title": http.StatusText(403),
"BeegoVersion": VERSION,
}
data["Content"] = template.HTML("<br>The page you have requested is forbidden." +
"<br>Perhaps you are here because:" +
"<br><br><ul>" +
"<br>Your address may be blocked" +
"<br>The site may be disabled" +
"<br>You need to log in" +
"</ul>")
t.Execute(rw, data)
}
// show 404 notfound error.
func notFound(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
data := map[string]interface{}{
"Title": http.StatusText(404),
"BeegoVersion": VERSION,
}
data["Content"] = template.HTML("<br>The page you have requested has flown the coop." +
"<br>Perhaps you are here because:" +
"<br><br><ul>" +
"<br>The page has moved" +
"<br>The page no longer exists" +
"<br>You were looking for your puppy and got lost" +
"<br>You like 404 pages" +
"</ul>")
t.Execute(rw, data)
}
// show 405 Method Not Allowed
func methodNotAllowed(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
data := map[string]interface{}{
"Title": http.StatusText(405),
"BeegoVersion": VERSION,
}
data["Content"] = template.HTML("<br>The method you have requested Not Allowed." +
"<br>Perhaps you are here because:" +
"<br><br><ul>" +
"<br>The method specified in the Request-Line is not allowed for the resource identified by the Request-URI" +
"<br>The response MUST include an Allow header containing a list of valid methods for the requested resource." +
"</ul>")
t.Execute(rw, data)
}
// show 500 internal server error.
func internalServerError(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
data := map[string]interface{}{
"Title": http.StatusText(500),
"BeegoVersion": VERSION,
}
data["Content"] = template.HTML("<br>The page you have requested is down right now." +
"<br><br><ul>" +
"<br>Please try again later and report the error to the website administrator" +
"<br></ul>")
t.Execute(rw, data)
}
// show 501 Not Implemented.
func notImplemented(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
data := map[string]interface{}{
"Title": http.StatusText(504),
"BeegoVersion": VERSION,
}
data["Content"] = template.HTML("<br>The page you have requested is Not Implemented." +
"<br><br><ul>" +
"<br>Please try again later and report the error to the website administrator" +
"<br></ul>")
t.Execute(rw, data)
}
// show 502 Bad Gateway.
func badGateway(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
data := map[string]interface{}{
"Title": http.StatusText(502),
"BeegoVersion": VERSION,
}
data["Content"] = template.HTML("<br>The page you have requested is down right now." +
"<br><br><ul>" +
"<br>The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request." +
"<br>Please try again later and report the error to the website administrator" +
"<br></ul>")
t.Execute(rw, data)
}
// show 503 service unavailable error.
func serviceUnavailable(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
data := map[string]interface{}{
"Title": http.StatusText(503),
"BeegoVersion": VERSION,
}
data["Content"] = template.HTML("<br>The page you have requested is unavailable." +
"<br>Perhaps you are here because:" +
"<br><br><ul>" +
"<br><br>The page is overloaded" +
"<br>Please try again later." +
"</ul>")
t.Execute(rw, data)
}
// show 504 Gateway Timeout.
func gatewayTimeout(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
data := map[string]interface{}{
"Title": http.StatusText(504),
"BeegoVersion": VERSION,
}
data["Content"] = template.HTML("<br>The page you have requested is unavailable." +
"<br>Perhaps you are here because:" +
"<br><br><ul>" +
"<br><br>The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server specified by the URI." +
"<br>Please try again later." +
"</ul>")
t.Execute(rw, data)
}
// ErrorHandler registers http.HandlerFunc to each http err code string.
// usage:
// beego.ErrorHandler("404",NotFound)
// beego.ErrorHandler("500",InternalServerError)
func ErrorHandler(code string, h http.HandlerFunc) *App {
ErrorMaps[code] = &errorInfo{
errorType: errorTypeHandler,
handler: h,
method: code,
}
return BeeApp
}
// ErrorController registers ControllerInterface to each http err code string.
// usage:
// beego.ErrorController(&controllers.ErrorController{})
func ErrorController(c ControllerInterface) *App {
reflectVal := reflect.ValueOf(c)
rt := reflectVal.Type()
ct := reflect.Indirect(reflectVal).Type()
for i := 0; i < rt.NumMethod(); i++ {
methodName := rt.Method(i).Name
if !utils.InSlice(methodName, exceptMethod) && strings.HasPrefix(methodName, "Error") {
errName := strings.TrimPrefix(methodName, "Error")
ErrorMaps[errName] = &errorInfo{
errorType: errorTypeController,
controllerType: ct,
method: methodName,
}
}
}
return BeeApp
}
// show error string as simple text message.
// if error string is empty, show 503 or 500 error as default.
func exception(errCode string, ctx *context.Context) {
atoi := func(code string) int {
v, err := strconv.Atoi(code)
if err == nil {
return v
}
return 503
}
for _, ec := range []string{errCode, "503", "500"} {
if h, ok := ErrorMaps[ec]; ok {
executeError(h, ctx, atoi(ec))
return
}
}
//if 50x error has been removed from errorMap
ctx.ResponseWriter.WriteHeader(atoi(errCode))
ctx.WriteString(errCode)
}
func executeError(err *errorInfo, ctx *context.Context, code int) {
if err.errorType == errorTypeHandler {
ctx.ResponseWriter.WriteHeader(code)
err.handler(ctx.ResponseWriter, ctx.Request)
return
}
if err.errorType == errorTypeController {
ctx.Output.SetStatus(code)
//Invoke the request handler
vc := reflect.New(err.controllerType)
execController, ok := vc.Interface().(ControllerInterface)
if !ok {
panic("controller is not ControllerInterface")
}
//call the controller init function
execController.Init(ctx, err.controllerType.Name(), err.method, vc.Interface())
//call prepare function
execController.Prepare()
execController.URLMapping()
method := vc.MethodByName(err.method)
method.Call([]reflect.Value{})
//render template
if BConfig.WebConfig.AutoRender {
if err := execController.Render(); err != nil {
panic(err)
}
}
// finish all runrouter. release resource
execController.Finish()
}
}

View File

@ -1,5 +0,0 @@
appname = beeapi
httpport = 8080
runmode = dev
autorender = false
copyrequestbody = true

View File

@ -1,63 +0,0 @@
// Beego (http://beego.me/)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @authors astaxie
package controllers
import (
"encoding/json"
"github.com/astaxie/beego"
"github.com/astaxie/beego/example/beeapi/models"
)
type ObjectController struct {
beego.Controller
}
func (o *ObjectController) Post() {
var ob models.Object
json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
objectid := models.AddOne(ob)
o.Data["json"] = map[string]string{"ObjectId": objectid}
o.ServeJson()
}
func (o *ObjectController) Get() {
objectId := o.Ctx.Input.Params[":objectId"]
if objectId != "" {
ob, err := models.GetOne(objectId)
if err != nil {
o.Data["json"] = err
} else {
o.Data["json"] = ob
}
} else {
obs := models.GetAll()
o.Data["json"] = obs
}
o.ServeJson()
}
func (o *ObjectController) Put() {
objectId := o.Ctx.Input.Params[":objectId"]
var ob models.Object
json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
err := models.Update(objectId, ob.Score)
if err != nil {
o.Data["json"] = err
} else {
o.Data["json"] = "update success!"
}
o.ServeJson()
}
func (o *ObjectController) Delete() {
objectId := o.Ctx.Input.Params[":objectId"]
models.Delete(objectId)
o.Data["json"] = "delete success!"
o.ServeJson()
}

View File

@ -1,30 +0,0 @@
// Beego (http://beego.me/)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @authors astaxie
package main
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/example/beeapi/controllers"
)
// Objects
// URL HTTP Verb Functionality
// /object POST Creating Objects
// /object/<objectId> GET Retrieving Objects
// /object/<objectId> PUT Updating Objects
// /object GET Queries
// /object/<objectId> DELETE Deleting Objects
func main() {
beego.RESTRouter("/object", &controllers.ObjectController{})
beego.Run()
}

View File

@ -1,58 +0,0 @@
// Beego (http://beego.me/)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @authors astaxie
package models
import (
"errors"
"strconv"
"time"
)
var (
Objects map[string]*Object
)
type Object struct {
ObjectId string
Score int64
PlayerName string
}
func init() {
Objects = make(map[string]*Object)
Objects["hjkhsbnmn123"] = &Object{"hjkhsbnmn123", 100, "astaxie"}
Objects["mjjkxsxsaa23"] = &Object{"mjjkxsxsaa23", 101, "someone"}
}
func AddOne(object Object) (ObjectId string) {
object.ObjectId = "astaxie" + strconv.FormatInt(time.Now().UnixNano(), 10)
Objects[object.ObjectId] = &object
return object.ObjectId
}
func GetOne(ObjectId string) (object *Object, err error) {
if v, ok := Objects[ObjectId]; ok {
return v, nil
}
return nil, errors.New("ObjectId Not Exist")
}
func GetAll() map[string]*Object {
return Objects
}
func Update(ObjectId string, Score int64) (err error) {
if v, ok := Objects[ObjectId]; ok {
v.Score = Score
return nil
}
return errors.New("ObjectId Not Exist")
}
func Delete(ObjectId string) {
delete(Objects, ObjectId)
}

View File

@ -1,3 +0,0 @@
appname = chat
httpport = 8080
runmode = dev

View File

@ -1,20 +0,0 @@
// Beego (http://beego.me/)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @authors Unknwon
package controllers
import (
"github.com/astaxie/beego"
)
type MainController struct {
beego.Controller
}
func (m *MainController) Get() {
m.Data["host"] = m.Ctx.Request.Host
m.TplNames = "index.tpl"
}

View File

@ -1,181 +0,0 @@
// Beego (http://beego.me/)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @authors Unknwon
package controllers
import (
"io/ioutil"
"math/rand"
"net/http"
"time"
"github.com/astaxie/beego"
"github.com/gorilla/websocket"
)
const (
// Time allowed to write a message to the client.
writeWait = 10 * time.Second
// Time allowed to read the next message from the client.
readWait = 60 * time.Second
// Send pings to client with this period. Must be less than readWait.
pingPeriod = (readWait * 9) / 10
// Maximum message size allowed from client.
maxMessageSize = 512
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
go h.run()
}
// connection is an middleman between the websocket connection and the hub.
type connection struct {
username string
// The websocket connection.
ws *websocket.Conn
// Buffered channel of outbound messages.
send chan []byte
}
// readPump pumps messages from the websocket connection to the hub.
func (c *connection) readPump() {
defer func() {
h.unregister <- c
c.ws.Close()
}()
c.ws.SetReadLimit(maxMessageSize)
c.ws.SetReadDeadline(time.Now().Add(readWait))
for {
op, r, err := c.ws.NextReader()
if err != nil {
break
}
switch op {
case websocket.PongMessage:
c.ws.SetReadDeadline(time.Now().Add(readWait))
case websocket.TextMessage:
message, err := ioutil.ReadAll(r)
if err != nil {
break
}
h.broadcast <- []byte(c.username + "_" + time.Now().Format("15:04:05") + ":" + string(message))
}
}
}
// write writes a message with the given opCode and payload.
func (c *connection) write(opCode int, payload []byte) error {
c.ws.SetWriteDeadline(time.Now().Add(writeWait))
return c.ws.WriteMessage(opCode, payload)
}
// writePump pumps messages from the hub to the websocket connection.
func (c *connection) writePump() {
ticker := time.NewTicker(pingPeriod)
defer func() {
ticker.Stop()
c.ws.Close()
}()
for {
select {
case message, ok := <-c.send:
if !ok {
c.write(websocket.CloseMessage, []byte{})
return
}
if err := c.write(websocket.TextMessage, message); err != nil {
return
}
case <-ticker.C:
if err := c.write(websocket.PingMessage, []byte{}); err != nil {
return
}
}
}
}
type hub struct {
// Registered connections.
connections map[*connection]bool
// Inbound messages from the connections.
broadcast chan []byte
// Register requests from the connections.
register chan *connection
// Unregister requests from connections.
unregister chan *connection
}
var h = &hub{
broadcast: make(chan []byte, maxMessageSize),
register: make(chan *connection, 1),
unregister: make(chan *connection, 1),
connections: make(map[*connection]bool),
}
func (h *hub) run() {
for {
select {
case c := <-h.register:
h.connections[c] = true
case c := <-h.unregister:
delete(h.connections, c)
close(c.send)
case m := <-h.broadcast:
for c := range h.connections {
select {
case c.send <- m:
default:
close(c.send)
delete(h.connections, c)
}
}
}
}
}
type WSController struct {
beego.Controller
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func (w *WSController) Get() {
ws, err := upgrader.Upgrade(w.Ctx.ResponseWriter, w.Ctx.Request, nil)
if _, ok := err.(websocket.HandshakeError); ok {
http.Error(w.Ctx.ResponseWriter, "Not a websocket handshake", 400)
return
} else if err != nil {
return
}
c := &connection{send: make(chan []byte, 256), ws: ws, username: randomString(10)}
h.register <- c
go c.writePump()
c.readPump()
}
func randomString(l int) string {
bytes := make([]byte, l)
for i := 0; i < l; i++ {
bytes[i] = byte(randInt(65, 90))
}
return string(bytes)
}
func randInt(min int, max int) int {
return min + rand.Intn(max-min)
}

View File

@ -1,17 +0,0 @@
// Beego (http://beego.me/)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @authors Unknwon
package main
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/example/chat/controllers"
)
func main() {
beego.Router("/", &controllers.MainController{})
beego.Router("/ws", &controllers.WSController{})
beego.Run()
}

View File

@ -1,92 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chat Example</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
var conn;
var msg = $("#msg");
var log = $("#log");
function appendLog(msg) {
var d = log[0]
var doScroll = d.scrollTop == d.scrollHeight - d.clientHeight;
msg.appendTo(log)
if (doScroll) {
d.scrollTop = d.scrollHeight - d.clientHeight;
}
}
$("#form").submit(function() {
if (!conn) {
return false;
}
if (!msg.val()) {
return false;
}
conn.send(msg.val());
msg.val("");
return false
});
if (window["WebSocket"]) {
conn = new WebSocket("ws://{{.host}}/ws");
conn.onclose = function(evt) {
appendLog($("<div><b>Connection closed.</b></div>"))
}
conn.onmessage = function(evt) {
appendLog($("<div/>").text(evt.data))
}
} else {
appendLog($("<div><b>Your browser does not support WebSockets.</b></div>"))
}
});
</script>
<style type="text/css">
html {
overflow: hidden;
}
body {
overflow: hidden;
padding: 0;
margin: 0;
width: 100%;
height: 100%;
background: gray;
}
#log {
background: white;
margin: 0;
padding: 0.5em 0.5em 0.5em 0.5em;
position: absolute;
top: 0.5em;
left: 0.5em;
right: 0.5em;
bottom: 3em;
overflow: auto;
}
#form {
padding: 0 0.5em 0 0.5em;
margin: 0;
position: absolute;
bottom: 1em;
left: 0px;
width: 100%;
overflow: hidden;
}
</style>
</head>
<body>
<div id="log"></div>
<form id="form">
<input type="submit" value="Send" />
<input type="text" id="msg" size="64"/>
</form>
</body>
</html>

View File

@ -16,11 +16,12 @@ package beego
import "github.com/astaxie/beego/context" import "github.com/astaxie/beego/context"
// FilterFunc defines filter function type. // FilterFunc defines a filter function which is invoked before the controller handler is executed.
type FilterFunc func(*context.Context) type FilterFunc func(*context.Context)
// FilterRouter defines filter operation before controller handler execution. // FilterRouter defines a filter operation which is invoked before the controller handler is executed.
// it can match patterned url and do filter function when action arrives. // It can match the URL against a pattern, and execute a filter function
// when a request with a matching URL arrives.
type FilterRouter struct { type FilterRouter struct {
filterFunc FilterFunc filterFunc FilterFunc
tree *Tree tree *Tree
@ -28,16 +29,15 @@ type FilterRouter struct {
returnOnOutput bool returnOnOutput bool
} }
// ValidRouter check current request is valid for this filter. // ValidRouter checks if the current request is matched by this filter.
// if matched, returns parsed params in this request by defined filter router pattern. // If the request is matched, the values of the URL parameters defined
func (f *FilterRouter) ValidRouter(router string) (bool, map[string]string) { // by the filter pattern are also returned.
isok, params := f.tree.Match(router) func (f *FilterRouter) ValidRouter(url string, ctx *context.Context) bool {
if isok == nil { isOk := f.tree.Match(url, ctx)
return false, nil if isOk != nil {
} if b, ok := isOk.(bool); ok {
if isok, ok := isok.(bool); ok { return b
return isok, params }
} else {
return false, nil
} }
return false
} }

View File

@ -20,10 +20,16 @@ import (
"testing" "testing"
"github.com/astaxie/beego/context" "github.com/astaxie/beego/context"
"github.com/astaxie/beego/logs"
) )
func init() {
BeeLogger = logs.NewLogger(10000)
BeeLogger.SetLogger("console", "")
}
var FilterUser = func(ctx *context.Context) { var FilterUser = func(ctx *context.Context) {
ctx.Output.Body([]byte("i am " + ctx.Input.Params[":last"] + ctx.Input.Params[":first"])) ctx.Output.Body([]byte("i am " + ctx.Input.Param(":last") + ctx.Input.Param(":first")))
} }
func TestFilter(t *testing.T) { func TestFilter(t *testing.T) {

View File

@ -83,27 +83,27 @@ func (fd *FlashData) Store(c *Controller) {
c.Data["flash"] = fd.Data c.Data["flash"] = fd.Data
var flashValue string var flashValue string
for key, value := range fd.Data { for key, value := range fd.Data {
flashValue += "\x00" + key + "\x23" + FlashSeperator + "\x23" + value + "\x00" flashValue += "\x00" + key + "\x23" + BConfig.WebConfig.FlashSeparator + "\x23" + value + "\x00"
} }
c.Ctx.SetCookie(FlashName, url.QueryEscape(flashValue), 0, "/") c.Ctx.SetCookie(BConfig.WebConfig.FlashName, url.QueryEscape(flashValue), 0, "/")
} }
// ReadFromRequest parsed flash data from encoded values in cookie. // ReadFromRequest parsed flash data from encoded values in cookie.
func ReadFromRequest(c *Controller) *FlashData { func ReadFromRequest(c *Controller) *FlashData {
flash := NewFlash() flash := NewFlash()
if cookie, err := c.Ctx.Request.Cookie(FlashName); err == nil { if cookie, err := c.Ctx.Request.Cookie(BConfig.WebConfig.FlashName); err == nil {
v, _ := url.QueryUnescape(cookie.Value) v, _ := url.QueryUnescape(cookie.Value)
vals := strings.Split(v, "\x00") vals := strings.Split(v, "\x00")
for _, v := range vals { for _, v := range vals {
if len(v) > 0 { if len(v) > 0 {
kv := strings.Split(v, "\x23"+FlashSeperator+"\x23") kv := strings.Split(v, "\x23"+BConfig.WebConfig.FlashSeparator+"\x23")
if len(kv) == 2 { if len(kv) == 2 {
flash.Data[kv[0]] = kv[1] flash.Data[kv[0]] = kv[1]
} }
} }
} }
//read one time then delete it //read one time then delete it
c.Ctx.SetCookie(FlashName, "", -1, "/") c.Ctx.SetCookie(BConfig.WebConfig.FlashName, "", -1, "/")
} }
c.Data["flash"] = flash.Data c.Data["flash"] = flash.Data
return flash return flash

View File

@ -30,7 +30,7 @@ func (t *TestFlashController) TestWriteFlash() {
flash.Notice("TestFlashString") flash.Notice("TestFlashString")
flash.Store(&t.Controller) flash.Store(&t.Controller)
// we choose to serve json because we don't want to load a template html file // we choose to serve json because we don't want to load a template html file
t.ServeJson(true) t.ServeJSON(true)
} }
func TestFlashHeader(t *testing.T) { func TestFlashHeader(t *testing.T) {

28
grace/conn.go Normal file
View File

@ -0,0 +1,28 @@
package grace
import (
"errors"
"net"
)
type graceConn struct {
net.Conn
server *Server
}
func (c graceConn) Close() (err error) {
defer func() {
if r := recover(); r != nil {
switch x := r.(type) {
case string:
err = errors.New(x)
case error:
err = x
default:
err = errors.New("Unknown panic")
}
}
}()
c.server.wg.Done()
return c.Conn.Close()
}

158
grace/grace.go Normal file
View File

@ -0,0 +1,158 @@
// Copyright 2014 beego Author. All Rights Reserved.
//
// 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.
// Package grace use to hot reload
// Description: http://grisha.org/blog/2014/06/03/graceful-restart-in-golang/
//
// Usage:
//
// import(
// "log"
// "net/http"
// "os"
//
// "github.com/astaxie/beego/grace"
// )
//
// func handler(w http.ResponseWriter, r *http.Request) {
// w.Write([]byte("WORLD!"))
// }
//
// func main() {
// mux := http.NewServeMux()
// mux.HandleFunc("/hello", handler)
//
// err := grace.ListenAndServe("localhost:8080", mux)
// if err != nil {
// log.Println(err)
// }
// log.Println("Server on 8080 stopped")
// os.Exit(0)
// }
package grace
import (
"flag"
"net/http"
"os"
"strings"
"sync"
"syscall"
"time"
)
const (
// PreSignal is the position to add filter before signal
PreSignal = iota
// PostSignal is the position to add filter after signal
PostSignal
// StateInit represent the application inited
StateInit
// StateRunning represent the application is running
StateRunning
// StateShuttingDown represent the application is shutting down
StateShuttingDown
// StateTerminate represent the application is killed
StateTerminate
)
var (
regLock *sync.Mutex
runningServers map[string]*Server
runningServersOrder []string
socketPtrOffsetMap map[string]uint
runningServersForked bool
// DefaultReadTimeOut is the HTTP read timeout
DefaultReadTimeOut time.Duration
// DefaultWriteTimeOut is the HTTP Write timeout
DefaultWriteTimeOut time.Duration
// DefaultMaxHeaderBytes is the Max HTTP Herder size, default is 0, no limit
DefaultMaxHeaderBytes int
// DefaultTimeout is the shutdown server's timeout. default is 60s
DefaultTimeout = 60 * time.Second
isChild bool
socketOrder string
once sync.Once
)
func onceInit() {
regLock = &sync.Mutex{}
flag.BoolVar(&isChild, "graceful", false, "listen on open fd (after forking)")
flag.StringVar(&socketOrder, "socketorder", "", "previous initialization order - used when more than one listener was started")
runningServers = make(map[string]*Server)
runningServersOrder = []string{}
socketPtrOffsetMap = make(map[string]uint)
}
// NewServer returns a new graceServer.
func NewServer(addr string, handler http.Handler) (srv *Server) {
once.Do(onceInit)
regLock.Lock()
defer regLock.Unlock()
if !flag.Parsed() {
flag.Parse()
}
if len(socketOrder) > 0 {
for i, addr := range strings.Split(socketOrder, ",") {
socketPtrOffsetMap[addr] = uint(i)
}
} else {
socketPtrOffsetMap[addr] = uint(len(runningServersOrder))
}
srv = &Server{
wg: sync.WaitGroup{},
sigChan: make(chan os.Signal),
isChild: isChild,
SignalHooks: map[int]map[os.Signal][]func(){
PreSignal: {
syscall.SIGHUP: {},
syscall.SIGINT: {},
syscall.SIGTERM: {},
},
PostSignal: {
syscall.SIGHUP: {},
syscall.SIGINT: {},
syscall.SIGTERM: {},
},
},
state: StateInit,
Network: "tcp",
}
srv.Server = &http.Server{}
srv.Server.Addr = addr
srv.Server.ReadTimeout = DefaultReadTimeOut
srv.Server.WriteTimeout = DefaultWriteTimeOut
srv.Server.MaxHeaderBytes = DefaultMaxHeaderBytes
srv.Server.Handler = handler
runningServersOrder = append(runningServersOrder, addr)
runningServers[addr] = srv
return
}
// ListenAndServe refer http.ListenAndServe
func ListenAndServe(addr string, handler http.Handler) error {
server := NewServer(addr, handler)
return server.ListenAndServe()
}
// ListenAndServeTLS refer http.ListenAndServeTLS
func ListenAndServeTLS(addr string, certFile string, keyFile string, handler http.Handler) error {
server := NewServer(addr, handler)
return server.ListenAndServeTLS(certFile, keyFile)
}

62
grace/listener.go Normal file
View File

@ -0,0 +1,62 @@
package grace
import (
"net"
"os"
"syscall"
"time"
)
type graceListener struct {
net.Listener
stop chan error
stopped bool
server *Server
}
func newGraceListener(l net.Listener, srv *Server) (el *graceListener) {
el = &graceListener{
Listener: l,
stop: make(chan error),
server: srv,
}
go func() {
_ = <-el.stop
el.stopped = true
el.stop <- el.Listener.Close()
}()
return
}
func (gl *graceListener) Accept() (c net.Conn, err error) {
tc, err := gl.Listener.(*net.TCPListener).AcceptTCP()
if err != nil {
return
}
tc.SetKeepAlive(true)
tc.SetKeepAlivePeriod(3 * time.Minute)
c = graceConn{
Conn: tc,
server: gl.server,
}
gl.server.wg.Add(1)
return
}
func (gl *graceListener) Close() error {
if gl.stopped {
return syscall.EINVAL
}
gl.stop <- nil
return <-gl.stop
}
func (gl *graceListener) File() *os.File {
// returns a dup(2) - FD_CLOEXEC flag *not* set
tl := gl.Listener.(*net.TCPListener)
fl, _ := tl.File()
return fl
}

292
grace/server.go Normal file
View File

@ -0,0 +1,292 @@
package grace
import (
"crypto/tls"
"fmt"
"log"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"strings"
"sync"
"syscall"
"time"
)
// Server embedded http.Server
type Server struct {
*http.Server
GraceListener net.Listener
SignalHooks map[int]map[os.Signal][]func()
tlsInnerListener *graceListener
wg sync.WaitGroup
sigChan chan os.Signal
isChild bool
state uint8
Network string
}
// Serve accepts incoming connections on the Listener l,
// creating a new service goroutine for each.
// The service goroutines read requests and then call srv.Handler to reply to them.
func (srv *Server) Serve() (err error) {
srv.state = StateRunning
err = srv.Server.Serve(srv.GraceListener)
log.Println(syscall.Getpid(), "Waiting for connections to finish...")
srv.wg.Wait()
srv.state = StateTerminate
return
}
// ListenAndServe listens on the TCP network address srv.Addr and then calls Serve
// to handle requests on incoming connections. If srv.Addr is blank, ":http" is
// used.
func (srv *Server) ListenAndServe() (err error) {
addr := srv.Addr
if addr == "" {
addr = ":http"
}
go srv.handleSignals()
l, err := srv.getListener(addr)
if err != nil {
log.Println(err)
return err
}
srv.GraceListener = newGraceListener(l, srv)
if srv.isChild {
process, err := os.FindProcess(os.Getppid())
if err != nil {
log.Println(err)
return err
}
err = process.Kill()
if err != nil {
return err
}
}
log.Println(os.Getpid(), srv.Addr)
return srv.Serve()
}
// ListenAndServeTLS listens on the TCP network address srv.Addr and then calls
// Serve to handle requests on incoming TLS connections.
//
// Filenames containing a certificate and matching private key for the server must
// be provided. If the certificate is signed by a certificate authority, the
// certFile should be the concatenation of the server's certificate followed by the
// CA's certificate.
//
// If srv.Addr is blank, ":https" is used.
func (srv *Server) ListenAndServeTLS(certFile, keyFile string) (err error) {
addr := srv.Addr
if addr == "" {
addr = ":https"
}
if srv.TLSConfig == nil {
srv.TLSConfig = &tls.Config{}
}
if srv.TLSConfig.NextProtos == nil {
srv.TLSConfig.NextProtos = []string{"http/1.1"}
}
srv.TLSConfig.Certificates = make([]tls.Certificate, 1)
srv.TLSConfig.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return
}
go srv.handleSignals()
l, err := srv.getListener(addr)
if err != nil {
log.Println(err)
return err
}
srv.tlsInnerListener = newGraceListener(l, srv)
srv.GraceListener = tls.NewListener(srv.tlsInnerListener, srv.TLSConfig)
if srv.isChild {
process, err := os.FindProcess(os.Getppid())
if err != nil {
log.Println(err)
return err
}
err = process.Kill()
if err != nil {
return err
}
}
log.Println(os.Getpid(), srv.Addr)
return srv.Serve()
}
// getListener either opens a new socket to listen on, or takes the acceptor socket
// it got passed when restarted.
func (srv *Server) getListener(laddr string) (l net.Listener, err error) {
if srv.isChild {
var ptrOffset uint
if len(socketPtrOffsetMap) > 0 {
ptrOffset = socketPtrOffsetMap[laddr]
log.Println("laddr", laddr, "ptr offset", socketPtrOffsetMap[laddr])
}
f := os.NewFile(uintptr(3+ptrOffset), "")
l, err = net.FileListener(f)
if err != nil {
err = fmt.Errorf("net.FileListener error: %v", err)
return
}
} else {
l, err = net.Listen(srv.Network, laddr)
if err != nil {
err = fmt.Errorf("net.Listen error: %v", err)
return
}
}
return
}
// handleSignals listens for os Signals and calls any hooked in function that the
// user had registered with the signal.
func (srv *Server) handleSignals() {
var sig os.Signal
signal.Notify(
srv.sigChan,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
)
pid := syscall.Getpid()
for {
sig = <-srv.sigChan
srv.signalHooks(PreSignal, sig)
switch sig {
case syscall.SIGHUP:
log.Println(pid, "Received SIGHUP. forking.")
err := srv.fork()
if err != nil {
log.Println("Fork err:", err)
}
case syscall.SIGINT:
log.Println(pid, "Received SIGINT.")
srv.shutdown()
case syscall.SIGTERM:
log.Println(pid, "Received SIGTERM.")
srv.shutdown()
default:
log.Printf("Received %v: nothing i care about...\n", sig)
}
srv.signalHooks(PostSignal, sig)
}
}
func (srv *Server) signalHooks(ppFlag int, sig os.Signal) {
if _, notSet := srv.SignalHooks[ppFlag][sig]; !notSet {
return
}
for _, f := range srv.SignalHooks[ppFlag][sig] {
f()
}
return
}
// shutdown closes the listener so that no new connections are accepted. it also
// starts a goroutine that will serverTimeout (stop all running requests) the server
// after DefaultTimeout.
func (srv *Server) shutdown() {
if srv.state != StateRunning {
return
}
srv.state = StateShuttingDown
if DefaultTimeout >= 0 {
go srv.serverTimeout(DefaultTimeout)
}
err := srv.GraceListener.Close()
if err != nil {
log.Println(syscall.Getpid(), "Listener.Close() error:", err)
} else {
log.Println(syscall.Getpid(), srv.GraceListener.Addr(), "Listener closed.")
}
}
// serverTimeout forces the server to shutdown in a given timeout - whether it
// finished outstanding requests or not. if Read/WriteTimeout are not set or the
// max header size is very big a connection could hang
func (srv *Server) serverTimeout(d time.Duration) {
defer func() {
if r := recover(); r != nil {
log.Println("WaitGroup at 0", r)
}
}()
if srv.state != StateShuttingDown {
return
}
time.Sleep(d)
log.Println("[STOP - Hammer Time] Forcefully shutting down parent")
for {
if srv.state == StateTerminate {
break
}
srv.wg.Done()
}
}
func (srv *Server) fork() (err error) {
regLock.Lock()
defer regLock.Unlock()
if runningServersForked {
return
}
runningServersForked = true
var files = make([]*os.File, len(runningServers))
var orderArgs = make([]string, len(runningServers))
for _, srvPtr := range runningServers {
switch srvPtr.GraceListener.(type) {
case *graceListener:
files[socketPtrOffsetMap[srvPtr.Server.Addr]] = srvPtr.GraceListener.(*graceListener).File()
default:
files[socketPtrOffsetMap[srvPtr.Server.Addr]] = srvPtr.tlsInnerListener.File()
}
orderArgs[socketPtrOffsetMap[srvPtr.Server.Addr]] = srvPtr.Server.Addr
}
log.Println(files)
path := os.Args[0]
var args []string
if len(os.Args) > 1 {
for _, arg := range os.Args[1:] {
if arg == "-graceful" {
break
}
args = append(args, arg)
}
}
args = append(args, "-graceful")
if len(runningServers) > 1 {
args = append(args, fmt.Sprintf(`-socketorder=%s`, strings.Join(orderArgs, ",")))
log.Println(args)
}
cmd := exec.Command(path, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.ExtraFiles = files
err = cmd.Start()
if err != nil {
log.Fatalf("Restart: Failed to launch, error: %v", err)
}
return
}

93
hooks.go Normal file
View File

@ -0,0 +1,93 @@
package beego
import (
"encoding/json"
"mime"
"net/http"
"path/filepath"
"github.com/astaxie/beego/session"
)
//
func registerMime() error {
for k, v := range mimemaps {
mime.AddExtensionType(k, v)
}
return nil
}
// register default error http handlers, 404,401,403,500 and 503.
func registerDefaultErrorHandler() error {
m := map[string]func(http.ResponseWriter, *http.Request){
"401": unauthorized,
"402": paymentRequired,
"403": forbidden,
"404": notFound,
"405": methodNotAllowed,
"500": internalServerError,
"501": notImplemented,
"502": badGateway,
"503": serviceUnavailable,
"504": gatewayTimeout,
}
for e, h := range m {
if _, ok := ErrorMaps[e]; !ok {
ErrorHandler(e, h)
}
}
return nil
}
func registerSession() error {
if BConfig.WebConfig.Session.SessionOn {
var err error
sessionConfig := AppConfig.String("sessionConfig")
if sessionConfig == "" {
conf := map[string]interface{}{
"cookieName": BConfig.WebConfig.Session.SessionName,
"gclifetime": BConfig.WebConfig.Session.SessionGCMaxLifetime,
"providerConfig": filepath.ToSlash(BConfig.WebConfig.Session.SessionProviderConfig),
"secure": BConfig.Listen.EnableHTTPS,
"enableSetCookie": BConfig.WebConfig.Session.SessionAutoSetCookie,
"domain": BConfig.WebConfig.Session.SessionDomain,
"cookieLifeTime": BConfig.WebConfig.Session.SessionCookieLifeTime,
}
confBytes, err := json.Marshal(conf)
if err != nil {
return err
}
sessionConfig = string(confBytes)
}
if GlobalSessions, err = session.NewManager(BConfig.WebConfig.Session.SessionProvider, sessionConfig); err != nil {
return err
}
go GlobalSessions.GC()
}
return nil
}
func registerTemplate() error {
if err := BuildTemplate(BConfig.WebConfig.ViewsPath); err != nil {
if BConfig.RunMode == DEV {
Warn(err)
}
return err
}
return nil
}
func registerDocs() error {
if BConfig.WebConfig.EnableDocs {
Get("/docs", serverDocs)
Get("/docs/*", serverDocs)
}
return nil
}
func registerAdmin() error {
if BConfig.Listen.EnableAdmin {
go beeAdminApp.Run()
}
return nil
}

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// Package httplib is used as http.Client
// Usage: // Usage:
// //
// import "github.com/astaxie/beego/httplib" // import "github.com/astaxie/beego/httplib"
@ -32,6 +33,7 @@ package httplib
import ( import (
"bytes" "bytes"
"compress/gzip"
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"encoding/xml" "encoding/xml"
@ -50,7 +52,14 @@ import (
"time" "time"
) )
var defaultSetting = BeegoHttpSettings{false, "beegoServer", 60 * time.Second, 60 * time.Second, nil, nil, nil, false} var defaultSetting = BeegoHTTPSettings{
UserAgent: "beegoServer",
ConnectTimeout: 60 * time.Second,
ReadWriteTimeout: 60 * time.Second,
Gzip: true,
DumpBody: true,
}
var defaultCookieJar http.CookieJar var defaultCookieJar http.CookieJar
var settingMutex sync.Mutex var settingMutex sync.Mutex
@ -61,132 +70,163 @@ func createDefaultCookie() {
defaultCookieJar, _ = cookiejar.New(nil) defaultCookieJar, _ = cookiejar.New(nil)
} }
// Overwrite default settings // SetDefaultSetting Overwrite default settings
func SetDefaultSetting(setting BeegoHttpSettings) { func SetDefaultSetting(setting BeegoHTTPSettings) {
settingMutex.Lock() settingMutex.Lock()
defer settingMutex.Unlock() defer settingMutex.Unlock()
defaultSetting = setting defaultSetting = setting
if defaultSetting.ConnectTimeout == 0 {
defaultSetting.ConnectTimeout = 60 * time.Second
}
if defaultSetting.ReadWriteTimeout == 0 {
defaultSetting.ReadWriteTimeout = 60 * time.Second
}
} }
// return *BeegoHttpRequest with specific method // NewBeegoRequest return *BeegoHttpRequest with specific method
func newBeegoRequest(url, method string) *BeegoHttpRequest { func NewBeegoRequest(rawurl, method string) *BeegoHTTPRequest {
var resp http.Response var resp http.Response
u, err := url.Parse(rawurl)
if err != nil {
log.Println("Httplib:", err)
}
req := http.Request{ req := http.Request{
URL: u,
Method: method, Method: method,
Header: make(http.Header), Header: make(http.Header),
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
ProtoMajor: 1, ProtoMajor: 1,
ProtoMinor: 1, ProtoMinor: 1,
} }
return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil} return &BeegoHTTPRequest{
url: rawurl,
req: &req,
params: map[string][]string{},
files: map[string]string{},
setting: defaultSetting,
resp: &resp,
}
} }
// Get returns *BeegoHttpRequest with GET method. // Get returns *BeegoHttpRequest with GET method.
func Get(url string) *BeegoHttpRequest { func Get(url string) *BeegoHTTPRequest {
return newBeegoRequest(url, "GET") return NewBeegoRequest(url, "GET")
} }
// Post returns *BeegoHttpRequest with POST method. // Post returns *BeegoHttpRequest with POST method.
func Post(url string) *BeegoHttpRequest { func Post(url string) *BeegoHTTPRequest {
return newBeegoRequest(url, "POST") return NewBeegoRequest(url, "POST")
} }
// Put returns *BeegoHttpRequest with PUT method. // Put returns *BeegoHttpRequest with PUT method.
func Put(url string) *BeegoHttpRequest { func Put(url string) *BeegoHTTPRequest {
return newBeegoRequest(url, "PUT") return NewBeegoRequest(url, "PUT")
} }
// Delete returns *BeegoHttpRequest DELETE method. // Delete returns *BeegoHttpRequest DELETE method.
func Delete(url string) *BeegoHttpRequest { func Delete(url string) *BeegoHTTPRequest {
return newBeegoRequest(url, "DELETE") return NewBeegoRequest(url, "DELETE")
} }
// Head returns *BeegoHttpRequest with HEAD method. // Head returns *BeegoHttpRequest with HEAD method.
func Head(url string) *BeegoHttpRequest { func Head(url string) *BeegoHTTPRequest {
return newBeegoRequest(url, "HEAD") return NewBeegoRequest(url, "HEAD")
} }
// BeegoHttpSettings // BeegoHTTPSettings is the http.Client setting
type BeegoHttpSettings struct { type BeegoHTTPSettings struct {
ShowDebug bool ShowDebug bool
UserAgent string UserAgent string
ConnectTimeout time.Duration ConnectTimeout time.Duration
ReadWriteTimeout time.Duration ReadWriteTimeout time.Duration
TlsClientConfig *tls.Config TLSClientConfig *tls.Config
Proxy func(*http.Request) (*url.URL, error) Proxy func(*http.Request) (*url.URL, error)
Transport http.RoundTripper Transport http.RoundTripper
EnableCookie bool EnableCookie bool
Gzip bool
DumpBody bool
} }
// BeegoHttpRequest provides more useful methods for requesting one url than http.Request. // BeegoHTTPRequest provides more useful methods for requesting one url than http.Request.
type BeegoHttpRequest struct { type BeegoHTTPRequest struct {
url string url string
req *http.Request req *http.Request
params map[string]string params map[string][]string
files map[string]string files map[string]string
setting BeegoHttpSettings setting BeegoHTTPSettings
resp *http.Response resp *http.Response
body []byte body []byte
dump []byte
} }
// Change request settings // GetRequest return the request object
func (b *BeegoHttpRequest) Setting(setting BeegoHttpSettings) *BeegoHttpRequest { func (b *BeegoHTTPRequest) GetRequest() *http.Request {
return b.req
}
// Setting Change request settings
func (b *BeegoHTTPRequest) Setting(setting BeegoHTTPSettings) *BeegoHTTPRequest {
b.setting = setting b.setting = setting
return b return b
} }
// SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password. // SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.
func (b *BeegoHttpRequest) SetBasicAuth(username, password string) *BeegoHttpRequest { func (b *BeegoHTTPRequest) SetBasicAuth(username, password string) *BeegoHTTPRequest {
b.req.SetBasicAuth(username, password) b.req.SetBasicAuth(username, password)
return b return b
} }
// SetEnableCookie sets enable/disable cookiejar // SetEnableCookie sets enable/disable cookiejar
func (b *BeegoHttpRequest) SetEnableCookie(enable bool) *BeegoHttpRequest { func (b *BeegoHTTPRequest) SetEnableCookie(enable bool) *BeegoHTTPRequest {
b.setting.EnableCookie = enable b.setting.EnableCookie = enable
return b return b
} }
// SetUserAgent sets User-Agent header field // SetUserAgent sets User-Agent header field
func (b *BeegoHttpRequest) SetUserAgent(useragent string) *BeegoHttpRequest { func (b *BeegoHTTPRequest) SetUserAgent(useragent string) *BeegoHTTPRequest {
b.setting.UserAgent = useragent b.setting.UserAgent = useragent
return b return b
} }
// Debug sets show debug or not when executing request. // Debug sets show debug or not when executing request.
func (b *BeegoHttpRequest) Debug(isdebug bool) *BeegoHttpRequest { func (b *BeegoHTTPRequest) Debug(isdebug bool) *BeegoHTTPRequest {
b.setting.ShowDebug = isdebug b.setting.ShowDebug = isdebug
return b return b
} }
// DumpBody setting whether need to Dump the Body.
func (b *BeegoHTTPRequest) DumpBody(isdump bool) *BeegoHTTPRequest {
b.setting.DumpBody = isdump
return b
}
// DumpRequest return the DumpRequest
func (b *BeegoHTTPRequest) DumpRequest() []byte {
return b.dump
}
// SetTimeout sets connect time out and read-write time out for BeegoRequest. // SetTimeout sets connect time out and read-write time out for BeegoRequest.
func (b *BeegoHttpRequest) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *BeegoHttpRequest { func (b *BeegoHTTPRequest) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *BeegoHTTPRequest {
b.setting.ConnectTimeout = connectTimeout b.setting.ConnectTimeout = connectTimeout
b.setting.ReadWriteTimeout = readWriteTimeout b.setting.ReadWriteTimeout = readWriteTimeout
return b return b
} }
// SetTLSClientConfig sets tls connection configurations if visiting https url. // SetTLSClientConfig sets tls connection configurations if visiting https url.
func (b *BeegoHttpRequest) SetTLSClientConfig(config *tls.Config) *BeegoHttpRequest { func (b *BeegoHTTPRequest) SetTLSClientConfig(config *tls.Config) *BeegoHTTPRequest {
b.setting.TlsClientConfig = config b.setting.TLSClientConfig = config
return b return b
} }
// Header add header item string in request. // Header add header item string in request.
func (b *BeegoHttpRequest) Header(key, value string) *BeegoHttpRequest { func (b *BeegoHTTPRequest) Header(key, value string) *BeegoHTTPRequest {
b.req.Header.Set(key, value) b.req.Header.Set(key, value)
return b return b
} }
// Set the protocol version for incoming requests. // SetHost set the request host
func (b *BeegoHTTPRequest) SetHost(host string) *BeegoHTTPRequest {
b.req.Host = host
return b
}
// SetProtocolVersion Set the protocol version for incoming requests.
// Client requests always use HTTP/1.1. // Client requests always use HTTP/1.1.
func (b *BeegoHttpRequest) SetProtocolVersion(vers string) *BeegoHttpRequest { func (b *BeegoHTTPRequest) SetProtocolVersion(vers string) *BeegoHTTPRequest {
if len(vers) == 0 { if len(vers) == 0 {
vers = "HTTP/1.1" vers = "HTTP/1.1"
} }
@ -202,44 +242,49 @@ func (b *BeegoHttpRequest) SetProtocolVersion(vers string) *BeegoHttpRequest {
} }
// SetCookie add cookie into request. // SetCookie add cookie into request.
func (b *BeegoHttpRequest) SetCookie(cookie *http.Cookie) *BeegoHttpRequest { func (b *BeegoHTTPRequest) SetCookie(cookie *http.Cookie) *BeegoHTTPRequest {
b.req.Header.Add("Cookie", cookie.String()) b.req.Header.Add("Cookie", cookie.String())
return b return b
} }
// Set transport to // SetTransport set the setting transport
func (b *BeegoHttpRequest) SetTransport(transport http.RoundTripper) *BeegoHttpRequest { func (b *BeegoHTTPRequest) SetTransport(transport http.RoundTripper) *BeegoHTTPRequest {
b.setting.Transport = transport b.setting.Transport = transport
return b return b
} }
// Set http proxy // SetProxy set the http proxy
// example: // example:
// //
// func(req *http.Request) (*url.URL, error) { // func(req *http.Request) (*url.URL, error) {
// u, _ := url.ParseRequestURI("http://127.0.0.1:8118") // u, _ := url.ParseRequestURI("http://127.0.0.1:8118")
// return u, nil // return u, nil
// } // }
func (b *BeegoHttpRequest) SetProxy(proxy func(*http.Request) (*url.URL, error)) *BeegoHttpRequest { func (b *BeegoHTTPRequest) SetProxy(proxy func(*http.Request) (*url.URL, error)) *BeegoHTTPRequest {
b.setting.Proxy = proxy b.setting.Proxy = proxy
return b return b
} }
// Param adds query param in to request. // Param adds query param in to request.
// params build query string as ?key1=value1&key2=value2... // params build query string as ?key1=value1&key2=value2...
func (b *BeegoHttpRequest) Param(key, value string) *BeegoHttpRequest { func (b *BeegoHTTPRequest) Param(key, value string) *BeegoHTTPRequest {
b.params[key] = value if param, ok := b.params[key]; ok {
b.params[key] = append(param, value)
} else {
b.params[key] = []string{value}
}
return b return b
} }
func (b *BeegoHttpRequest) PostFile(formname, filename string) *BeegoHttpRequest { // PostFile add a post file to the request
func (b *BeegoHTTPRequest) PostFile(formname, filename string) *BeegoHTTPRequest {
b.files[formname] = filename b.files[formname] = filename
return b return b
} }
// Body adds request raw body. // Body adds request raw body.
// it supports string and []byte. // it supports string and []byte.
func (b *BeegoHttpRequest) Body(data interface{}) *BeegoHttpRequest { func (b *BeegoHTTPRequest) Body(data interface{}) *BeegoHTTPRequest {
switch t := data.(type) { switch t := data.(type) {
case string: case string:
bf := bytes.NewBufferString(t) bf := bytes.NewBufferString(t)
@ -253,30 +298,34 @@ func (b *BeegoHttpRequest) Body(data interface{}) *BeegoHttpRequest {
return b return b
} }
func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { // JSONBody adds request raw body encoding by JSON.
if b.resp.StatusCode != 0 { func (b *BeegoHTTPRequest) JSONBody(obj interface{}) (*BeegoHTTPRequest, error) {
return b.resp, nil if b.req.Body == nil && obj != nil {
} byts, err := json.Marshal(obj)
var paramBody string if err != nil {
if len(b.params) > 0 { return b, err
var buf bytes.Buffer
for k, v := range b.params {
buf.WriteString(url.QueryEscape(k))
buf.WriteByte('=')
buf.WriteString(url.QueryEscape(v))
buf.WriteByte('&')
} }
paramBody = buf.String() b.req.Body = ioutil.NopCloser(bytes.NewReader(byts))
paramBody = paramBody[0 : len(paramBody)-1] b.req.ContentLength = int64(len(byts))
b.req.Header.Set("Content-Type", "application/json")
} }
return b, nil
}
func (b *BeegoHTTPRequest) buildURL(paramBody string) {
// build GET url with query string
if b.req.Method == "GET" && len(paramBody) > 0 { if b.req.Method == "GET" && len(paramBody) > 0 {
if strings.Index(b.url, "?") != -1 { if strings.Index(b.url, "?") != -1 {
b.url += "&" + paramBody b.url += "&" + paramBody
} else { } else {
b.url = b.url + "?" + paramBody b.url = b.url + "?" + paramBody
} }
} else if b.req.Method == "POST" && b.req.Body == nil { return
}
// build POST/PUT/PATCH url and body
if (b.req.Method == "POST" || b.req.Method == "PUT" || b.req.Method == "PATCH") && b.req.Body == nil {
// with files
if len(b.files) > 0 { if len(b.files) > 0 {
pr, pw := io.Pipe() pr, pw := io.Pipe()
bodyWriter := multipart.NewWriter(pw) bodyWriter := multipart.NewWriter(pw)
@ -284,33 +333,70 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
for formname, filename := range b.files { for formname, filename := range b.files {
fileWriter, err := bodyWriter.CreateFormFile(formname, filename) fileWriter, err := bodyWriter.CreateFormFile(formname, filename)
if err != nil { if err != nil {
log.Fatal(err) log.Println("Httplib:", err)
} }
fh, err := os.Open(filename) fh, err := os.Open(filename)
if err != nil { if err != nil {
log.Fatal(err) log.Println("Httplib:", err)
} }
//iocopy //iocopy
_, err = io.Copy(fileWriter, fh) _, err = io.Copy(fileWriter, fh)
fh.Close() fh.Close()
if err != nil { if err != nil {
log.Fatal(err) log.Println("Httplib:", err)
} }
} }
for k, v := range b.params { for k, v := range b.params {
bodyWriter.WriteField(k, v) for _, vv := range v {
bodyWriter.WriteField(k, vv)
}
} }
bodyWriter.Close() bodyWriter.Close()
pw.Close() pw.Close()
}() }()
b.Header("Content-Type", bodyWriter.FormDataContentType()) b.Header("Content-Type", bodyWriter.FormDataContentType())
b.req.Body = ioutil.NopCloser(pr) b.req.Body = ioutil.NopCloser(pr)
} else if len(paramBody) > 0 { return
}
// with params
if len(paramBody) > 0 {
b.Header("Content-Type", "application/x-www-form-urlencoded") b.Header("Content-Type", "application/x-www-form-urlencoded")
b.Body(paramBody) b.Body(paramBody)
} }
} }
}
func (b *BeegoHTTPRequest) getResponse() (*http.Response, error) {
if b.resp.StatusCode != 0 {
return b.resp, nil
}
resp, err := b.DoRequest()
if err != nil {
return nil, err
}
b.resp = resp
return resp, nil
}
// DoRequest will do the client.Do
func (b *BeegoHTTPRequest) DoRequest() (*http.Response, error) {
var paramBody string
if len(b.params) > 0 {
var buf bytes.Buffer
for k, v := range b.params {
for _, vv := range v {
buf.WriteString(url.QueryEscape(k))
buf.WriteByte('=')
buf.WriteString(url.QueryEscape(vv))
buf.WriteByte('&')
}
}
paramBody = buf.String()
paramBody = paramBody[0 : len(paramBody)-1]
}
b.buildURL(paramBody)
url, err := url.Parse(b.url) url, err := url.Parse(b.url)
if err != nil { if err != nil {
return nil, err return nil, err
@ -323,7 +409,7 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
if trans == nil { if trans == nil {
// create default transport // create default transport
trans = &http.Transport{ trans = &http.Transport{
TLSClientConfig: b.setting.TlsClientConfig, TLSClientConfig: b.setting.TLSClientConfig,
Proxy: b.setting.Proxy, Proxy: b.setting.Proxy,
Dial: TimeoutDialer(b.setting.ConnectTimeout, b.setting.ReadWriteTimeout), Dial: TimeoutDialer(b.setting.ConnectTimeout, b.setting.ReadWriteTimeout),
} }
@ -331,7 +417,7 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
// if b.transport is *http.Transport then set the settings. // if b.transport is *http.Transport then set the settings.
if t, ok := trans.(*http.Transport); ok { if t, ok := trans.(*http.Transport); ok {
if t.TLSClientConfig == nil { if t.TLSClientConfig == nil {
t.TLSClientConfig = b.setting.TlsClientConfig t.TLSClientConfig = b.setting.TLSClientConfig
} }
if t.Proxy == nil { if t.Proxy == nil {
t.Proxy = b.setting.Proxy t.Proxy = b.setting.Proxy
@ -348,8 +434,6 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
createDefaultCookie() createDefaultCookie()
} }
jar = defaultCookieJar jar = defaultCookieJar
} else {
jar = nil
} }
client := &http.Client{ client := &http.Client{
@ -362,24 +446,18 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
} }
if b.setting.ShowDebug { if b.setting.ShowDebug {
dump, err := httputil.DumpRequest(b.req, true) dump, err := httputil.DumpRequest(b.req, b.setting.DumpBody)
if err != nil { if err != nil {
println(err.Error()) log.Println(err.Error())
} }
println(string(dump)) b.dump = dump
} }
return client.Do(b.req)
resp, err := client.Do(b.req)
if err != nil {
return nil, err
}
b.resp = resp
return resp, nil
} }
// String returns the body string in response. // String returns the body string in response.
// it calls Response inner. // it calls Response inner.
func (b *BeegoHttpRequest) String() (string, error) { func (b *BeegoHTTPRequest) String() (string, error) {
data, err := b.Bytes() data, err := b.Bytes()
if err != nil { if err != nil {
return "", err return "", err
@ -390,7 +468,7 @@ func (b *BeegoHttpRequest) String() (string, error) {
// Bytes returns the body []byte in response. // Bytes returns the body []byte in response.
// it calls Response inner. // it calls Response inner.
func (b *BeegoHttpRequest) Bytes() ([]byte, error) { func (b *BeegoHTTPRequest) Bytes() ([]byte, error) {
if b.body != nil { if b.body != nil {
return b.body, nil return b.body, nil
} }
@ -402,17 +480,21 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) {
return nil, nil return nil, nil
} }
defer resp.Body.Close() defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body) if b.setting.Gzip && resp.Header.Get("Content-Encoding") == "gzip" {
if err != nil { reader, err := gzip.NewReader(resp.Body)
return nil, err if err != nil {
return nil, err
}
b.body, err = ioutil.ReadAll(reader)
} else {
b.body, err = ioutil.ReadAll(resp.Body)
} }
b.body = data return b.body, err
return data, nil
} }
// ToFile saves the body data in response to one file. // ToFile saves the body data in response to one file.
// it calls Response inner. // it calls Response inner.
func (b *BeegoHttpRequest) ToFile(filename string) error { func (b *BeegoHTTPRequest) ToFile(filename string) error {
f, err := os.Create(filename) f, err := os.Create(filename)
if err != nil { if err != nil {
return err return err
@ -431,30 +513,28 @@ func (b *BeegoHttpRequest) ToFile(filename string) error {
return err return err
} }
// ToJson returns the map that marshals from the body bytes as json in response . // ToJSON returns the map that marshals from the body bytes as json in response .
// it calls Response inner. // it calls Response inner.
func (b *BeegoHttpRequest) ToJson(v interface{}) error { func (b *BeegoHTTPRequest) ToJSON(v interface{}) error {
data, err := b.Bytes() data, err := b.Bytes()
if err != nil { if err != nil {
return err return err
} }
err = json.Unmarshal(data, v) return json.Unmarshal(data, v)
return err
} }
// ToXml returns the map that marshals from the body bytes as xml in response . // ToXML returns the map that marshals from the body bytes as xml in response .
// it calls Response inner. // it calls Response inner.
func (b *BeegoHttpRequest) ToXml(v interface{}) error { func (b *BeegoHTTPRequest) ToXML(v interface{}) error {
data, err := b.Bytes() data, err := b.Bytes()
if err != nil { if err != nil {
return err return err
} }
err = xml.Unmarshal(data, v) return xml.Unmarshal(data, v)
return err
} }
// Response executes request client gets response mannually. // Response executes request client gets response mannually.
func (b *BeegoHttpRequest) Response() (*http.Response, error) { func (b *BeegoHTTPRequest) Response() (*http.Response, error) {
return b.getResponse() return b.getResponse()
} }
@ -465,7 +545,7 @@ func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, ad
if err != nil { if err != nil {
return nil, err return nil, err
} }
conn.SetDeadline(time.Now().Add(rwTimeout)) err = conn.SetDeadline(time.Now().Add(rwTimeout))
return conn, nil return conn, err
} }
} }

View File

@ -19,6 +19,7 @@ import (
"os" "os"
"strings" "strings"
"testing" "testing"
"time"
) )
func TestResponse(t *testing.T) { func TestResponse(t *testing.T) {
@ -149,10 +150,11 @@ func TestWithUserAgent(t *testing.T) {
func TestWithSetting(t *testing.T) { func TestWithSetting(t *testing.T) {
v := "beego" v := "beego"
var setting BeegoHttpSettings var setting BeegoHTTPSettings
setting.EnableCookie = true setting.EnableCookie = true
setting.UserAgent = v setting.UserAgent = v
setting.Transport = nil setting.Transport = nil
setting.ReadWriteTimeout = 5 * time.Second
SetDefaultSetting(setting) SetDefaultSetting(setting)
str, err := Get("http://httpbin.org/get").String() str, err := Get("http://httpbin.org/get").String()
@ -176,11 +178,11 @@ func TestToJson(t *testing.T) {
t.Log(resp) t.Log(resp)
// httpbin will return http remote addr // httpbin will return http remote addr
type Ip struct { type IP struct {
Origin string `json:"origin"` Origin string `json:"origin"`
} }
var ip Ip var ip IP
err = req.ToJson(&ip) err = req.ToJSON(&ip)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

25
log.go
View File

@ -32,20 +32,20 @@ const (
LevelDebug LevelDebug
) )
// SetLogLevel sets the global log level used by the simple // BeeLogger references the used application logger.
// logger. var BeeLogger = logs.NewLogger(100)
// SetLevel sets the global log level used by the simple logger.
func SetLevel(l int) { func SetLevel(l int) {
BeeLogger.SetLevel(l) BeeLogger.SetLevel(l)
} }
// SetLogFuncCall set the CallDepth, default is 3
func SetLogFuncCall(b bool) { func SetLogFuncCall(b bool) {
BeeLogger.EnableFuncCallDepth(b) BeeLogger.EnableFuncCallDepth(b)
BeeLogger.SetLogFuncCallDepth(3) BeeLogger.SetLogFuncCallDepth(3)
} }
// logger references the used application logger.
var BeeLogger *logs.BeeLogger
// SetLogger sets a new logger. // SetLogger sets a new logger.
func SetLogger(adaptername string, config string) error { func SetLogger(adaptername string, config string) error {
err := BeeLogger.SetLogger(adaptername, config) err := BeeLogger.SetLogger(adaptername, config)
@ -55,10 +55,12 @@ func SetLogger(adaptername string, config string) error {
return nil return nil
} }
// Emergency logs a message at emergency level.
func Emergency(v ...interface{}) { func Emergency(v ...interface{}) {
BeeLogger.Emergency(generateFmtStr(len(v)), v...) BeeLogger.Emergency(generateFmtStr(len(v)), v...)
} }
// Alert logs a message at alert level.
func Alert(v ...interface{}) { func Alert(v ...interface{}) {
BeeLogger.Alert(generateFmtStr(len(v)), v...) BeeLogger.Alert(generateFmtStr(len(v)), v...)
} }
@ -78,23 +80,24 @@ func Warning(v ...interface{}) {
BeeLogger.Warning(generateFmtStr(len(v)), v...) BeeLogger.Warning(generateFmtStr(len(v)), v...)
} }
// Deprecated: compatibility alias for Warning(), Will be removed in 1.5.0. // Warn compatibility alias for Warning()
func Warn(v ...interface{}) { func Warn(v ...interface{}) {
Warning(v...) BeeLogger.Warn(generateFmtStr(len(v)), v...)
} }
// Notice logs a message at notice level.
func Notice(v ...interface{}) { func Notice(v ...interface{}) {
BeeLogger.Notice(generateFmtStr(len(v)), v...) BeeLogger.Notice(generateFmtStr(len(v)), v...)
} }
// Info logs a message at info level. // Informational logs a message at info level.
func Informational(v ...interface{}) { func Informational(v ...interface{}) {
BeeLogger.Informational(generateFmtStr(len(v)), v...) BeeLogger.Informational(generateFmtStr(len(v)), v...)
} }
// Deprecated: compatibility alias for Warning(), Will be removed in 1.5.0. // Info compatibility alias for Warning()
func Info(v ...interface{}) { func Info(v ...interface{}) {
Informational(v...) BeeLogger.Info(generateFmtStr(len(v)), v...)
} }
// Debug logs a message at debug level. // Debug logs a message at debug level.
@ -103,7 +106,7 @@ func Debug(v ...interface{}) {
} }
// Trace logs a message at trace level. // Trace logs a message at trace level.
// Deprecated: compatibility alias for Warning(), Will be removed in 1.5.0. // compatibility alias for Warning()
func Trace(v ...interface{}) { func Trace(v ...interface{}) {
BeeLogger.Trace(generateFmtStr(len(v)), v...) BeeLogger.Trace(generateFmtStr(len(v)), v...)
} }

View File

@ -17,14 +17,14 @@ package logs
import ( import (
"encoding/json" "encoding/json"
"io" "io"
"log"
"net" "net"
"time"
) )
// ConnWriter implements LoggerInterface. // connWriter implements LoggerInterface.
// it writes messages in keep-live tcp connection. // it writes messages in keep-live tcp connection.
type ConnWriter struct { type connWriter struct {
lg *log.Logger lg *logWriter
innerWriter io.WriteCloser innerWriter io.WriteCloser
ReconnectOnMsg bool `json:"reconnectOnMsg"` ReconnectOnMsg bool `json:"reconnectOnMsg"`
Reconnect bool `json:"reconnect"` Reconnect bool `json:"reconnect"`
@ -33,30 +33,26 @@ type ConnWriter struct {
Level int `json:"level"` Level int `json:"level"`
} }
// create new ConnWrite returning as LoggerInterface. // NewConn create new ConnWrite returning as LoggerInterface.
func NewConn() LoggerInterface { func NewConn() Logger {
conn := new(ConnWriter) conn := new(connWriter)
conn.Level = LevelTrace conn.Level = LevelTrace
return conn return conn
} }
// init connection writer with json config. // Init init connection writer with json config.
// json config only need key "level". // json config only need key "level".
func (c *ConnWriter) Init(jsonconfig string) error { func (c *connWriter) Init(jsonConfig string) error {
err := json.Unmarshal([]byte(jsonconfig), c) return json.Unmarshal([]byte(jsonConfig), c)
if err != nil {
return err
}
return nil
} }
// write message in connection. // WriteMsg write message in connection.
// if connection is down, try to re-connect. // if connection is down, try to re-connect.
func (c *ConnWriter) WriteMsg(msg string, level int) error { func (c *connWriter) WriteMsg(when time.Time, msg string, level int) error {
if level > c.Level { if level > c.Level {
return nil return nil
} }
if c.neddedConnectOnMsg() { if c.needToConnectOnMsg() {
err := c.connect() err := c.connect()
if err != nil { if err != nil {
return err return err
@ -66,24 +62,24 @@ func (c *ConnWriter) WriteMsg(msg string, level int) error {
if c.ReconnectOnMsg { if c.ReconnectOnMsg {
defer c.innerWriter.Close() defer c.innerWriter.Close()
} }
c.lg.Println(msg)
c.lg.println(when, msg)
return nil return nil
} }
// implementing method. empty. // Flush implementing method. empty.
func (c *ConnWriter) Flush() { func (c *connWriter) Flush() {
} }
// destroy connection writer and close tcp listener. // Destroy destroy connection writer and close tcp listener.
func (c *ConnWriter) Destroy() { func (c *connWriter) Destroy() {
if c.innerWriter == nil { if c.innerWriter != nil {
return c.innerWriter.Close()
} }
c.innerWriter.Close()
} }
func (c *ConnWriter) connect() error { func (c *connWriter) connect() error {
if c.innerWriter != nil { if c.innerWriter != nil {
c.innerWriter.Close() c.innerWriter.Close()
c.innerWriter = nil c.innerWriter = nil
@ -99,11 +95,11 @@ func (c *ConnWriter) connect() error {
} }
c.innerWriter = conn c.innerWriter = conn
c.lg = log.New(conn, "", log.Ldate|log.Ltime) c.lg = newLogWriter(conn)
return nil return nil
} }
func (c *ConnWriter) neddedConnectOnMsg() bool { func (c *connWriter) needToConnectOnMsg() bool {
if c.Reconnect { if c.Reconnect {
c.Reconnect = false c.Reconnect = false
return true return true

View File

@ -16,14 +16,16 @@ package logs
import ( import (
"encoding/json" "encoding/json"
"log"
"os" "os"
"runtime" "runtime"
"time"
) )
type Brush func(string) string // brush is a color join function
type brush func(string) string
func NewBrush(color string) Brush { // newBrush return a fix color Brush
func newBrush(color string) brush {
pre := "\033[" pre := "\033["
reset := "\033[0m" reset := "\033[0m"
return func(text string) string { return func(text string) string {
@ -31,64 +33,66 @@ func NewBrush(color string) Brush {
} }
} }
var colors = []Brush{ var colors = []brush{
NewBrush("1;37"), // Emergency white newBrush("1;37"), // Emergency white
NewBrush("1;36"), // Alert cyan newBrush("1;36"), // Alert cyan
NewBrush("1;35"), // Critical magenta newBrush("1;35"), // Critical magenta
NewBrush("1;31"), // Error red newBrush("1;31"), // Error red
NewBrush("1;33"), // Warning yellow newBrush("1;33"), // Warning yellow
NewBrush("1;32"), // Notice green newBrush("1;32"), // Notice green
NewBrush("1;34"), // Informational blue newBrush("1;34"), // Informational blue
NewBrush("1;34"), // Debug blue newBrush("1;34"), // Debug blue
} }
// ConsoleWriter implements LoggerInterface and writes messages to terminal. // consoleWriter implements LoggerInterface and writes messages to terminal.
type ConsoleWriter struct { type consoleWriter struct {
lg *log.Logger lg *logWriter
Level int `json:"level"` Level int `json:"level"`
Colorful bool `json:"color"` //this filed is useful only when system's terminal supports color
} }
// create ConsoleWriter returning as LoggerInterface. // NewConsole create ConsoleWriter returning as LoggerInterface.
func NewConsole() LoggerInterface { func NewConsole() Logger {
cw := new(ConsoleWriter) cw := &consoleWriter{
cw.lg = log.New(os.Stdout, "", log.Ldate|log.Ltime) lg: newLogWriter(os.Stdout),
cw.Level = LevelDebug Level: LevelDebug,
Colorful: true,
}
return cw return cw
} }
// init console logger. // Init init console logger.
// jsonconfig like '{"level":LevelTrace}'. // jsonConfig like '{"level":LevelTrace}'.
func (c *ConsoleWriter) Init(jsonconfig string) error { func (c *consoleWriter) Init(jsonConfig string) error {
if len(jsonconfig) == 0 { if len(jsonConfig) == 0 {
return nil return nil
} }
err := json.Unmarshal([]byte(jsonconfig), c) err := json.Unmarshal([]byte(jsonConfig), c)
if err != nil { if runtime.GOOS == "windows" {
return err c.Colorful = false
} }
return nil return err
} }
// write message in console. // WriteMsg write message in console.
func (c *ConsoleWriter) WriteMsg(msg string, level int) error { func (c *consoleWriter) WriteMsg(when time.Time, msg string, level int) error {
if level > c.Level { if level > c.Level {
return nil return nil
} }
if goos := runtime.GOOS; goos == "windows" { if c.Colorful {
c.lg.Println(msg) msg = colors[level](msg)
} else {
c.lg.Println(colors[level](msg))
} }
c.lg.println(when, msg)
return nil return nil
} }
// implementing method. empty. // Destroy implementing method. empty.
func (c *ConsoleWriter) Destroy() { func (c *consoleWriter) Destroy() {
} }
// implementing method. empty. // Flush implementing method. empty.
func (c *ConsoleWriter) Flush() { func (c *consoleWriter) Flush() {
} }

View File

@ -43,11 +43,9 @@ func TestConsole(t *testing.T) {
testConsoleCalls(log2) testConsoleCalls(log2)
} }
func BenchmarkConsole(b *testing.B) { // Test console without color
log := NewLogger(10000) func TestConsoleNoColor(t *testing.T) {
log.EnableFuncCallDepth(true) log := NewLogger(100)
log.SetLogger("console", "") log.SetLogger("console", `{"color":false}`)
for i := 0; i < b.N; i++ { testConsoleCalls(log)
log.Debug("debug")
}
} }

80
logs/es/es.go Normal file
View File

@ -0,0 +1,80 @@
package es
import (
"encoding/json"
"errors"
"fmt"
"net"
"net/url"
"time"
"github.com/astaxie/beego/logs"
"github.com/belogik/goes"
)
// NewES return a LoggerInterface
func NewES() logs.Logger {
cw := &esLogger{
Level: logs.LevelDebug,
}
return cw
}
type esLogger struct {
*goes.Connection
DSN string `json:"dsn"`
Level int `json:"level"`
}
// {"dsn":"http://localhost:9200/","level":1}
func (el *esLogger) Init(jsonconfig string) error {
err := json.Unmarshal([]byte(jsonconfig), el)
if err != nil {
return err
}
if el.DSN == "" {
return errors.New("empty dsn")
} else if u, err := url.Parse(el.DSN); err != nil {
return err
} else if u.Path == "" {
return errors.New("missing prefix")
} else if host, port, err := net.SplitHostPort(u.Host); err != nil {
return err
} else {
conn := goes.NewConnection(host, port)
el.Connection = conn
}
return nil
}
// WriteMsg will write the msg and level into es
func (el *esLogger) WriteMsg(when time.Time, msg string, level int) error {
if level > el.Level {
return nil
}
vals := make(map[string]interface{})
vals["@timestamp"] = when.Format(time.RFC3339)
vals["@msg"] = msg
d := goes.Document{
Index: fmt.Sprintf("%04d.%02d.%02d", when.Year(), when.Month(), when.Day()),
Type: "logs",
Fields: vals,
}
_, err := el.Index(d, nil)
return err
}
// Destroy is a empty method
func (el *esLogger) Destroy() {
}
// Flush is a empty method
func (el *esLogger) Flush() {
}
func init() {
logs.Register("es", NewES)
}

View File

@ -15,11 +15,11 @@
package logs package logs
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io"
"log"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -27,215 +27,237 @@ import (
"time" "time"
) )
// FileLogWriter implements LoggerInterface. // fileLogWriter implements LoggerInterface.
// It writes messages by lines limit, file size limit, or time frequency. // It writes messages by lines limit, file size limit, or time frequency.
type FileLogWriter struct { type fileLogWriter struct {
*log.Logger sync.Mutex // write log order by order and atomic incr maxLinesCurLines and maxSizeCurSize
mw *MuxWriter
// The opened file // The opened file
Filename string `json:"filename"` Filename string `json:"filename"`
fileWriter *os.File
Maxlines int `json:"maxlines"` // Rotate at line
maxlines_curlines int MaxLines int `json:"maxlines"`
maxLinesCurLines int
// Rotate at size // Rotate at size
Maxsize int `json:"maxsize"` MaxSize int `json:"maxsize"`
maxsize_cursize int maxSizeCurSize int
// Rotate daily // Rotate daily
Daily bool `json:"daily"` Daily bool `json:"daily"`
Maxdays int64 `json:"maxdays"` MaxDays int64 `json:"maxdays"`
daily_opendate int dailyOpenDate int
Rotate bool `json:"rotate"` Rotate bool `json:"rotate"`
startLock sync.Mutex // Only one log can write to the file
Level int `json:"level"` Level int `json:"level"`
Perm os.FileMode `json:"perm"`
fileNameOnly, suffix string // like "project.log", project is fileNameOnly and .log is suffix
} }
// an *os.File writer with locker. // newFileWriter create a FileLogWriter returning as LoggerInterface.
type MuxWriter struct { func newFileWriter() Logger {
sync.Mutex w := &fileLogWriter{
fd *os.File
}
// write to os.File.
func (l *MuxWriter) Write(b []byte) (int, error) {
l.Lock()
defer l.Unlock()
return l.fd.Write(b)
}
// set os.File in writer.
func (l *MuxWriter) SetFd(fd *os.File) {
if l.fd != nil {
l.fd.Close()
}
l.fd = fd
}
// create a FileLogWriter returning as LoggerInterface.
func NewFileWriter() LoggerInterface {
w := &FileLogWriter{
Filename: "", Filename: "",
Maxlines: 1000000, MaxLines: 1000000,
Maxsize: 1 << 28, //256 MB MaxSize: 1 << 28, //256 MB
Daily: true, Daily: true,
Maxdays: 7, MaxDays: 7,
Rotate: true, Rotate: true,
Level: LevelTrace, Level: LevelTrace,
Perm: 0660,
} }
// use MuxWriter instead direct use os.File for lock write when rotate
w.mw = new(MuxWriter)
// set MuxWriter as Logger's io.Writer
w.Logger = log.New(w.mw, "", log.Ldate|log.Ltime)
return w return w
} }
// Init file logger with json config. // Init file logger with json config.
// jsonconfig like: // jsonConfig like:
// { // {
// "filename":"logs/beego.log", // "filename":"logs/beego.log",
// "maxlines":10000, // "maxLines":10000,
// "maxsize":1<<30, // "maxsize":1<<30,
// "daily":true, // "daily":true,
// "maxdays":15, // "maxDays":15,
// "rotate":true // "rotate":true,
// "perm":0600
// } // }
func (w *FileLogWriter) Init(jsonconfig string) error { func (w *fileLogWriter) Init(jsonConfig string) error {
err := json.Unmarshal([]byte(jsonconfig), w) err := json.Unmarshal([]byte(jsonConfig), w)
if err != nil { if err != nil {
return err return err
} }
if len(w.Filename) == 0 { if len(w.Filename) == 0 {
return errors.New("jsonconfig must have filename") return errors.New("jsonconfig must have filename")
} }
w.suffix = filepath.Ext(w.Filename)
w.fileNameOnly = strings.TrimSuffix(w.Filename, w.suffix)
if w.suffix == "" {
w.suffix = ".log"
}
err = w.startLogger() err = w.startLogger()
return err return err
} }
// start file logger. create log file and set to locker-inside file writer. // start file logger. create log file and set to locker-inside file writer.
func (w *FileLogWriter) startLogger() error { func (w *fileLogWriter) startLogger() error {
fd, err := w.createLogFile() file, err := w.createLogFile()
if err != nil { if err != nil {
return err return err
} }
w.mw.SetFd(fd) if w.fileWriter != nil {
err = w.initFd() w.fileWriter.Close()
if err != nil {
return err
} }
return nil w.fileWriter = file
return w.initFd()
} }
func (w *FileLogWriter) docheck(size int) { func (w *fileLogWriter) needRotate(size int, day int) bool {
w.startLock.Lock() return (w.MaxLines > 0 && w.maxLinesCurLines >= w.MaxLines) ||
defer w.startLock.Unlock() (w.MaxSize > 0 && w.maxSizeCurSize >= w.MaxSize) ||
if w.Rotate && ((w.Maxlines > 0 && w.maxlines_curlines >= w.Maxlines) || (w.Daily && day != w.dailyOpenDate)
(w.Maxsize > 0 && w.maxsize_cursize >= w.Maxsize) ||
(w.Daily && time.Now().Day() != w.daily_opendate)) {
if err := w.DoRotate(); err != nil {
fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err)
return
}
}
w.maxlines_curlines++
w.maxsize_cursize += size
} }
// write logger message into file. // WriteMsg write logger message into file.
func (w *FileLogWriter) WriteMsg(msg string, level int) error { func (w *fileLogWriter) WriteMsg(when time.Time, msg string, level int) error {
if level > w.Level { if level > w.Level {
return nil return nil
} }
n := 24 + len(msg) // 24 stand for the length "2013/06/23 21:00:22 [T] " h, d := formatTimeHeader(when)
w.docheck(n) msg = string(h) + msg + "\n"
w.Logger.Println(msg) if w.Rotate {
return nil if w.needRotate(len(msg), d) {
w.Lock()
if w.needRotate(len(msg), d) {
if err := w.doRotate(when); err != nil {
fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err)
}
}
w.Unlock()
}
}
w.Lock()
_, err := w.fileWriter.Write([]byte(msg))
if err == nil {
w.maxLinesCurLines++
w.maxSizeCurSize += len(msg)
}
w.Unlock()
return err
} }
func (w *FileLogWriter) createLogFile() (*os.File, error) { func (w *fileLogWriter) createLogFile() (*os.File, error) {
// Open the log file // Open the log file
fd, err := os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660) fd, err := os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, w.Perm)
return fd, err return fd, err
} }
func (w *FileLogWriter) initFd() error { func (w *fileLogWriter) initFd() error {
fd := w.mw.fd fd := w.fileWriter
finfo, err := fd.Stat() fInfo, err := fd.Stat()
if err != nil { if err != nil {
return fmt.Errorf("get stat err: %s\n", err) return fmt.Errorf("get stat err: %s\n", err)
} }
w.maxsize_cursize = int(finfo.Size()) w.maxSizeCurSize = int(fInfo.Size())
w.daily_opendate = time.Now().Day() w.dailyOpenDate = time.Now().Day()
if finfo.Size() > 0 { w.maxLinesCurLines = 0
content, err := ioutil.ReadFile(w.Filename) if fInfo.Size() > 0 {
count, err := w.lines()
if err != nil { if err != nil {
return err return err
} }
w.maxlines_curlines = len(strings.Split(string(content), "\n")) w.maxLinesCurLines = count
} else {
w.maxlines_curlines = 0
} }
return nil return nil
} }
func (w *fileLogWriter) lines() (int, error) {
fd, err := os.Open(w.Filename)
if err != nil {
return 0, err
}
defer fd.Close()
buf := make([]byte, 32768) // 32k
count := 0
lineSep := []byte{'\n'}
for {
c, err := fd.Read(buf)
if err != nil && err != io.EOF {
return count, err
}
count += bytes.Count(buf[:c], lineSep)
if err == io.EOF {
break
}
}
return count, nil
}
// DoRotate means it need to write file in new file. // DoRotate means it need to write file in new file.
// new file name like xx.log.2013-01-01.2 // new file name like xx.2013-01-01.log (daily) or xx.001.log (by line or size)
func (w *FileLogWriter) DoRotate() error { func (w *fileLogWriter) doRotate(logTime time.Time) error {
_, err := os.Lstat(w.Filename) _, err := os.Lstat(w.Filename)
if err == nil { // file exists if err != nil {
// Find the next available number return err
num := 1 }
fname := "" // file exists
// Find the next available number
num := 1
fName := ""
if w.MaxLines > 0 || w.MaxSize > 0 {
for ; err == nil && num <= 999; num++ { for ; err == nil && num <= 999; num++ {
fname = w.Filename + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), num) fName = w.fileNameOnly + fmt.Sprintf(".%s.%03d%s", logTime.Format("2006-01-02"), num, w.suffix)
_, err = os.Lstat(fname) _, err = os.Lstat(fName)
} }
// return error if the last file checked still existed } else {
if err == nil { fName = fmt.Sprintf("%s.%s%s", w.fileNameOnly, logTime.Format("2006-01-02"), w.suffix)
return fmt.Errorf("Rotate: Cannot find free log number to rename %s\n", w.Filename) _, err = os.Lstat(fName)
} }
// return error if the last file checked still existed
// block Logger's io.Writer if err == nil {
w.mw.Lock() return fmt.Errorf("Rotate: Cannot find free log number to rename %s\n", w.Filename)
defer w.mw.Unlock()
fd := w.mw.fd
fd.Close()
// close fd before rename
// Rename the file to its newfound home
err = os.Rename(w.Filename, fname)
if err != nil {
return fmt.Errorf("Rotate: %s\n", err)
}
// re-start logger
err = w.startLogger()
if err != nil {
return fmt.Errorf("Rotate StartLogger: %s\n", err)
}
go w.deleteOldLog()
} }
// close fileWriter before rename
w.fileWriter.Close()
// Rename the file to its new found name
// even if occurs error,we MUST guarantee to restart new logger
renameErr := os.Rename(w.Filename, fName)
// re-start logger
startLoggerErr := w.startLogger()
go w.deleteOldLog()
if startLoggerErr != nil {
return fmt.Errorf("Rotate StartLogger: %s\n", startLoggerErr)
}
if renameErr != nil {
return fmt.Errorf("Rotate: %s\n", renameErr)
}
return nil return nil
} }
func (w *FileLogWriter) deleteOldLog() { func (w *fileLogWriter) deleteOldLog() {
dir := filepath.Dir(w.Filename) dir := filepath.Dir(w.Filename)
filepath.Walk(dir, func(path string, info os.FileInfo, err error) (returnErr error) { filepath.Walk(dir, func(path string, info os.FileInfo, err error) (returnErr error) {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
returnErr = fmt.Errorf("Unable to delete old log '%s', error: %+v", path, r) fmt.Fprintf(os.Stderr, "Unable to delete old log '%s', error: %v\n", path, r)
fmt.Println(returnErr)
} }
}() }()
if !info.IsDir() && info.ModTime().Unix() < (time.Now().Unix()-60*60*24*w.Maxdays) { if !info.IsDir() && info.ModTime().Unix() < (time.Now().Unix()-60*60*24*w.MaxDays) {
if strings.HasPrefix(filepath.Base(path), filepath.Base(w.Filename)) { if strings.HasPrefix(filepath.Base(path), w.fileNameOnly) &&
strings.HasSuffix(filepath.Base(path), w.suffix) {
os.Remove(path) os.Remove(path)
} }
} }
@ -243,18 +265,18 @@ func (w *FileLogWriter) deleteOldLog() {
}) })
} }
// destroy file logger, close file writer. // Destroy close the file description, close file writer.
func (w *FileLogWriter) Destroy() { func (w *fileLogWriter) Destroy() {
w.mw.fd.Close() w.fileWriter.Close()
} }
// flush file logger. // Flush flush file logger.
// there are no buffering messages in file logger in memory. // there are no buffering messages in file logger in memory.
// flush file means sync file from disk. // flush file means sync file from disk.
func (w *FileLogWriter) Flush() { func (w *fileLogWriter) Flush() {
w.mw.fd.Sync() w.fileWriter.Sync()
} }
func init() { func init() {
Register("file", NewFileWriter) Register("file", newFileWriter)
} }

View File

@ -23,7 +23,7 @@ import (
"time" "time"
) )
func TestFile(t *testing.T) { func TestFile1(t *testing.T) {
log := NewLogger(10000) log := NewLogger(10000)
log.SetLogger("file", `{"filename":"test.log"}`) log.SetLogger("file", `{"filename":"test.log"}`)
log.Debug("debug") log.Debug("debug")
@ -34,25 +34,24 @@ func TestFile(t *testing.T) {
log.Alert("alert") log.Alert("alert")
log.Critical("critical") log.Critical("critical")
log.Emergency("emergency") log.Emergency("emergency")
time.Sleep(time.Second * 4)
f, err := os.Open("test.log") f, err := os.Open("test.log")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
b := bufio.NewReader(f) b := bufio.NewReader(f)
linenum := 0 lineNum := 0
for { for {
line, _, err := b.ReadLine() line, _, err := b.ReadLine()
if err != nil { if err != nil {
break break
} }
if len(line) > 0 { if len(line) > 0 {
linenum++ lineNum++
} }
} }
var expected = LevelDebug + 1 var expected = LevelDebug + 1
if linenum != expected { if lineNum != expected {
t.Fatal(linenum, "not "+strconv.Itoa(expected)+" lines") t.Fatal(lineNum, "not "+strconv.Itoa(expected)+" lines")
} }
os.Remove("test.log") os.Remove("test.log")
} }
@ -68,25 +67,24 @@ func TestFile2(t *testing.T) {
log.Alert("alert") log.Alert("alert")
log.Critical("critical") log.Critical("critical")
log.Emergency("emergency") log.Emergency("emergency")
time.Sleep(time.Second * 4)
f, err := os.Open("test2.log") f, err := os.Open("test2.log")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
b := bufio.NewReader(f) b := bufio.NewReader(f)
linenum := 0 lineNum := 0
for { for {
line, _, err := b.ReadLine() line, _, err := b.ReadLine()
if err != nil { if err != nil {
break break
} }
if len(line) > 0 { if len(line) > 0 {
linenum++ lineNum++
} }
} }
var expected = LevelError + 1 var expected = LevelError + 1
if linenum != expected { if lineNum != expected {
t.Fatal(linenum, "not "+strconv.Itoa(expected)+" lines") t.Fatal(lineNum, "not "+strconv.Itoa(expected)+" lines")
} }
os.Remove("test2.log") os.Remove("test2.log")
} }
@ -102,13 +100,13 @@ func TestFileRotate(t *testing.T) {
log.Alert("alert") log.Alert("alert")
log.Critical("critical") log.Critical("critical")
log.Emergency("emergency") log.Emergency("emergency")
time.Sleep(time.Second * 4) rotateName := "test3" + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), 1) + ".log"
rotatename := "test3.log" + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), 1) b, err := exists(rotateName)
b, err := exists(rotatename)
if !b || err != nil { if !b || err != nil {
os.Remove("test3.log")
t.Fatal("rotate not generated") t.Fatal("rotate not generated")
} }
os.Remove(rotatename) os.Remove(rotateName)
os.Remove("test3.log") os.Remove("test3.log")
} }
@ -131,3 +129,45 @@ func BenchmarkFile(b *testing.B) {
} }
os.Remove("test4.log") os.Remove("test4.log")
} }
func BenchmarkFileAsynchronous(b *testing.B) {
log := NewLogger(100000)
log.SetLogger("file", `{"filename":"test4.log"}`)
log.Async()
for i := 0; i < b.N; i++ {
log.Debug("debug")
}
os.Remove("test4.log")
}
func BenchmarkFileCallDepth(b *testing.B) {
log := NewLogger(100000)
log.SetLogger("file", `{"filename":"test4.log"}`)
log.EnableFuncCallDepth(true)
log.SetLogFuncCallDepth(2)
for i := 0; i < b.N; i++ {
log.Debug("debug")
}
os.Remove("test4.log")
}
func BenchmarkFileAsynchronousCallDepth(b *testing.B) {
log := NewLogger(100000)
log.SetLogger("file", `{"filename":"test4.log"}`)
log.EnableFuncCallDepth(true)
log.SetLogFuncCallDepth(2)
log.Async()
for i := 0; i < b.N; i++ {
log.Debug("debug")
}
os.Remove("test4.log")
}
func BenchmarkFileOnGoroutine(b *testing.B) {
log := NewLogger(100000)
log.SetLogger("file", `{"filename":"test4.log"}`)
for i := 0; i < b.N; i++ {
go log.Debug("debug")
}
os.Remove("test4.log")
}

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// Package logs provide a general log interface
// Usage: // Usage:
// //
// import "github.com/astaxie/beego/logs" // import "github.com/astaxie/beego/logs"
@ -34,9 +35,12 @@ package logs
import ( import (
"fmt" "fmt"
"os"
"path" "path"
"runtime" "runtime"
"strconv"
"sync" "sync"
"time"
) )
// RFC5424 log message levels. // RFC5424 log message levels.
@ -60,12 +64,12 @@ const (
LevelWarn = LevelWarning LevelWarn = LevelWarning
) )
type loggerType func() LoggerInterface type loggerType func() Logger
// LoggerInterface defines the behavior of a log provider. // Logger defines the behavior of a log provider.
type LoggerInterface interface { type Logger interface {
Init(config string) error Init(config string) error
WriteMsg(msg string, level int) error WriteMsg(when time.Time, msg string, level int) error
Destroy() Destroy()
Flush() Flush()
} }
@ -92,99 +96,147 @@ type BeeLogger struct {
level int level int
enableFuncCallDepth bool enableFuncCallDepth bool
loggerFuncCallDepth int loggerFuncCallDepth int
msg chan *logMsg asynchronous bool
outputs map[string]LoggerInterface msgChan chan *logMsg
signalChan chan string
wg sync.WaitGroup
outputs []*nameLogger
}
type nameLogger struct {
Logger
name string
} }
type logMsg struct { type logMsg struct {
level int level int
msg string msg string
when time.Time
} }
var logMsgPool *sync.Pool
// NewLogger returns a new BeeLogger. // NewLogger returns a new BeeLogger.
// channellen means the number of messages in chan. // channelLen means the number of messages in chan(used where asynchronous is true).
// if the buffering chan is full, logger adapters write to file or other way. // if the buffering chan is full, logger adapters write to file or other way.
func NewLogger(channellen int64) *BeeLogger { func NewLogger(channelLen int64) *BeeLogger {
bl := new(BeeLogger) bl := new(BeeLogger)
bl.level = LevelDebug bl.level = LevelDebug
bl.loggerFuncCallDepth = 2 bl.loggerFuncCallDepth = 2
bl.msg = make(chan *logMsg, channellen) bl.msgChan = make(chan *logMsg, channelLen)
bl.outputs = make(map[string]LoggerInterface) bl.signalChan = make(chan string, 1)
//bl.SetLogger("console", "") // default output to console return bl
}
// Async set the log to asynchronous and start the goroutine
func (bl *BeeLogger) Async() *BeeLogger {
bl.asynchronous = true
logMsgPool = &sync.Pool{
New: func() interface{} {
return &logMsg{}
},
}
bl.wg.Add(1)
go bl.startLogger() go bl.startLogger()
return bl return bl
} }
// SetLogger provides a given logger adapter into BeeLogger with config string. // SetLogger provides a given logger adapter into BeeLogger with config string.
// config need to be correct JSON as string: {"interval":360}. // config need to be correct JSON as string: {"interval":360}.
func (bl *BeeLogger) SetLogger(adaptername string, config string) error { func (bl *BeeLogger) SetLogger(adapterName string, config string) error {
bl.lock.Lock() bl.lock.Lock()
defer bl.lock.Unlock() defer bl.lock.Unlock()
if log, ok := adapters[adaptername]; ok {
lg := log() for _, l := range bl.outputs {
err := lg.Init(config) if l.name == adapterName {
bl.outputs[adaptername] = lg return fmt.Errorf("logs: duplicate adaptername %q (you have set this logger before)", adapterName)
if err != nil {
fmt.Println("logs.BeeLogger.SetLogger: " + err.Error())
return err
} }
} else {
return fmt.Errorf("logs: unknown adaptername %q (forgotten Register?)", adaptername)
} }
log, ok := adapters[adapterName]
if !ok {
return fmt.Errorf("logs: unknown adaptername %q (forgotten Register?)", adapterName)
}
lg := log()
err := lg.Init(config)
if err != nil {
fmt.Fprintln(os.Stderr, "logs.BeeLogger.SetLogger: "+err.Error())
return err
}
bl.outputs = append(bl.outputs, &nameLogger{name: adapterName, Logger: lg})
return nil return nil
} }
// remove a logger adapter in BeeLogger. // DelLogger remove a logger adapter in BeeLogger.
func (bl *BeeLogger) DelLogger(adaptername string) error { func (bl *BeeLogger) DelLogger(adapterName string) error {
bl.lock.Lock() bl.lock.Lock()
defer bl.lock.Unlock() defer bl.lock.Unlock()
if lg, ok := bl.outputs[adaptername]; ok { outputs := []*nameLogger{}
lg.Destroy() for _, lg := range bl.outputs {
delete(bl.outputs, adaptername) if lg.name == adapterName {
return nil lg.Destroy()
} else { } else {
return fmt.Errorf("logs: unknown adaptername %q (forgotten Register?)", adaptername) outputs = append(outputs, lg)
}
}
if len(outputs) == len(bl.outputs) {
return fmt.Errorf("logs: unknown adaptername %q (forgotten Register?)", adapterName)
}
bl.outputs = outputs
return nil
}
func (bl *BeeLogger) writeToLoggers(when time.Time, msg string, level int) {
for _, l := range bl.outputs {
err := l.WriteMsg(when, msg, level)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to WriteMsg to adapter:%v,error:%v\n", l.name, err)
}
} }
} }
func (bl *BeeLogger) writerMsg(loglevel int, msg string) error { func (bl *BeeLogger) writeMsg(logLevel int, msg string) error {
if loglevel > bl.level { when := time.Now()
return nil
}
lm := new(logMsg)
lm.level = loglevel
if bl.enableFuncCallDepth { if bl.enableFuncCallDepth {
_, file, line, ok := runtime.Caller(bl.loggerFuncCallDepth) _, file, line, ok := runtime.Caller(bl.loggerFuncCallDepth)
if _, filename := path.Split(file); filename == "log.go" && (line == 97 || line == 83) { if !ok {
_, file, line, ok = runtime.Caller(bl.loggerFuncCallDepth + 1) file = "???"
line = 0
} }
if ok { _, filename := path.Split(file)
_, filename := path.Split(file) msg = "[" + filename + ":" + strconv.FormatInt(int64(line), 10) + "]" + msg
lm.msg = fmt.Sprintf("[%s:%d] %s", filename, line, msg) }
} else { if bl.asynchronous {
lm.msg = msg lm := logMsgPool.Get().(*logMsg)
} lm.level = logLevel
} else { lm.msg = msg
lm.msg = msg lm.when = when
bl.msgChan <- lm
} else {
bl.writeToLoggers(when, msg, logLevel)
} }
bl.msg <- lm
return nil return nil
} }
// Set log message level. // SetLevel Set log message level.
//
// If message level (such as LevelDebug) is higher than logger level (such as LevelWarning), // If message level (such as LevelDebug) is higher than logger level (such as LevelWarning),
// log providers will not even be sent the message. // log providers will not even be sent the message.
func (bl *BeeLogger) SetLevel(l int) { func (bl *BeeLogger) SetLevel(l int) {
bl.level = l bl.level = l
} }
// set log funcCallDepth // SetLogFuncCallDepth set log funcCallDepth
func (bl *BeeLogger) SetLogFuncCallDepth(d int) { func (bl *BeeLogger) SetLogFuncCallDepth(d int) {
bl.loggerFuncCallDepth = d bl.loggerFuncCallDepth = d
} }
// enable log funcCallDepth // GetLogFuncCallDepth return log funcCallDepth for wrapper
func (bl *BeeLogger) GetLogFuncCallDepth() int {
return bl.loggerFuncCallDepth
}
// EnableFuncCallDepth enable log funcCallDepth
func (bl *BeeLogger) EnableFuncCallDepth(b bool) { func (bl *BeeLogger) EnableFuncCallDepth(b bool) {
bl.enableFuncCallDepth = b bl.enableFuncCallDepth = b
} }
@ -192,112 +244,179 @@ func (bl *BeeLogger) EnableFuncCallDepth(b bool) {
// start logger chan reading. // start logger chan reading.
// when chan is not empty, write logs. // when chan is not empty, write logs.
func (bl *BeeLogger) startLogger() { func (bl *BeeLogger) startLogger() {
gameOver := false
for { for {
select { select {
case bm := <-bl.msg: case bm := <-bl.msgChan:
for _, l := range bl.outputs { bl.writeToLoggers(bm.when, bm.msg, bm.level)
err := l.WriteMsg(bm.msg, bm.level) logMsgPool.Put(bm)
if err != nil { case sg := <-bl.signalChan:
fmt.Println("ERROR, unable to WriteMsg:", err) // Now should only send "flush" or "close" to bl.signalChan
bl.flush()
if sg == "close" {
for _, l := range bl.outputs {
l.Destroy()
} }
bl.outputs = nil
gameOver = true
} }
bl.wg.Done()
} }
} if gameOver {
}
// Log EMERGENCY level message.
func (bl *BeeLogger) Emergency(format string, v ...interface{}) {
msg := fmt.Sprintf("[M] "+format, v...)
bl.writerMsg(LevelEmergency, msg)
}
// Log ALERT level message.
func (bl *BeeLogger) Alert(format string, v ...interface{}) {
msg := fmt.Sprintf("[A] "+format, v...)
bl.writerMsg(LevelAlert, msg)
}
// Log CRITICAL level message.
func (bl *BeeLogger) Critical(format string, v ...interface{}) {
msg := fmt.Sprintf("[C] "+format, v...)
bl.writerMsg(LevelCritical, msg)
}
// Log ERROR level message.
func (bl *BeeLogger) Error(format string, v ...interface{}) {
msg := fmt.Sprintf("[E] "+format, v...)
bl.writerMsg(LevelError, msg)
}
// Log WARNING level message.
func (bl *BeeLogger) Warning(format string, v ...interface{}) {
msg := fmt.Sprintf("[W] "+format, v...)
bl.writerMsg(LevelWarning, msg)
}
// Log NOTICE level message.
func (bl *BeeLogger) Notice(format string, v ...interface{}) {
msg := fmt.Sprintf("[N] "+format, v...)
bl.writerMsg(LevelNotice, msg)
}
// Log INFORMATIONAL level message.
func (bl *BeeLogger) Informational(format string, v ...interface{}) {
msg := fmt.Sprintf("[I] "+format, v...)
bl.writerMsg(LevelInformational, msg)
}
// Log DEBUG level message.
func (bl *BeeLogger) Debug(format string, v ...interface{}) {
msg := fmt.Sprintf("[D] "+format, v...)
bl.writerMsg(LevelDebug, msg)
}
// Log WARN level message.
//
// Deprecated: compatibility alias for Warning(), Will be removed in 1.5.0.
func (bl *BeeLogger) Warn(format string, v ...interface{}) {
bl.Warning(format, v...)
}
// Log INFO level message.
//
// Deprecated: compatibility alias for Informational(), Will be removed in 1.5.0.
func (bl *BeeLogger) Info(format string, v ...interface{}) {
bl.Informational(format, v...)
}
// Log TRACE level message.
//
// Deprecated: compatibility alias for Debug(), Will be removed in 1.5.0.
func (bl *BeeLogger) Trace(format string, v ...interface{}) {
bl.Debug(format, v...)
}
// flush all chan data.
func (bl *BeeLogger) Flush() {
for _, l := range bl.outputs {
l.Flush()
}
}
// close logger, flush all chan data and destroy all adapters in BeeLogger.
func (bl *BeeLogger) Close() {
for {
if len(bl.msg) > 0 {
bm := <-bl.msg
for _, l := range bl.outputs {
err := l.WriteMsg(bm.msg, bm.level)
if err != nil {
fmt.Println("ERROR, unable to WriteMsg (while closing logger):", err)
}
}
} else {
break break
} }
} }
}
// Emergency Log EMERGENCY level message.
func (bl *BeeLogger) Emergency(format string, v ...interface{}) {
if LevelEmergency > bl.level {
return
}
msg := fmt.Sprintf("[M] "+format, v...)
bl.writeMsg(LevelEmergency, msg)
}
// Alert Log ALERT level message.
func (bl *BeeLogger) Alert(format string, v ...interface{}) {
if LevelAlert > bl.level {
return
}
msg := fmt.Sprintf("[A] "+format, v...)
bl.writeMsg(LevelAlert, msg)
}
// Critical Log CRITICAL level message.
func (bl *BeeLogger) Critical(format string, v ...interface{}) {
if LevelCritical > bl.level {
return
}
msg := fmt.Sprintf("[C] "+format, v...)
bl.writeMsg(LevelCritical, msg)
}
// Error Log ERROR level message.
func (bl *BeeLogger) Error(format string, v ...interface{}) {
if LevelError > bl.level {
return
}
msg := fmt.Sprintf("[E] "+format, v...)
bl.writeMsg(LevelError, msg)
}
// Warning Log WARNING level message.
func (bl *BeeLogger) Warning(format string, v ...interface{}) {
if LevelWarning > bl.level {
return
}
msg := fmt.Sprintf("[W] "+format, v...)
bl.writeMsg(LevelWarning, msg)
}
// Notice Log NOTICE level message.
func (bl *BeeLogger) Notice(format string, v ...interface{}) {
if LevelNotice > bl.level {
return
}
msg := fmt.Sprintf("[N] "+format, v...)
bl.writeMsg(LevelNotice, msg)
}
// Informational Log INFORMATIONAL level message.
func (bl *BeeLogger) Informational(format string, v ...interface{}) {
if LevelInformational > bl.level {
return
}
msg := fmt.Sprintf("[I] "+format, v...)
bl.writeMsg(LevelInformational, msg)
}
// Debug Log DEBUG level message.
func (bl *BeeLogger) Debug(format string, v ...interface{}) {
if LevelDebug > bl.level {
return
}
msg := fmt.Sprintf("[D] "+format, v...)
bl.writeMsg(LevelDebug, msg)
}
// Warn Log WARN level message.
// compatibility alias for Warning()
func (bl *BeeLogger) Warn(format string, v ...interface{}) {
if LevelWarning > bl.level {
return
}
msg := fmt.Sprintf("[W] "+format, v...)
bl.writeMsg(LevelWarning, msg)
}
// Info Log INFO level message.
// compatibility alias for Informational()
func (bl *BeeLogger) Info(format string, v ...interface{}) {
if LevelInformational > bl.level {
return
}
msg := fmt.Sprintf("[I] "+format, v...)
bl.writeMsg(LevelInformational, msg)
}
// Trace Log TRACE level message.
// compatibility alias for Debug()
func (bl *BeeLogger) Trace(format string, v ...interface{}) {
if LevelDebug > bl.level {
return
}
msg := fmt.Sprintf("[D] "+format, v...)
bl.writeMsg(LevelDebug, msg)
}
// Flush flush all chan data.
func (bl *BeeLogger) Flush() {
if bl.asynchronous {
bl.signalChan <- "flush"
bl.wg.Wait()
bl.wg.Add(1)
return
}
bl.flush()
}
// Close close logger, flush all chan data and destroy all adapters in BeeLogger.
func (bl *BeeLogger) Close() {
if bl.asynchronous {
bl.signalChan <- "close"
bl.wg.Wait()
} else {
bl.flush()
for _, l := range bl.outputs {
l.Destroy()
}
bl.outputs = nil
}
close(bl.msgChan)
close(bl.signalChan)
}
// Reset close all outputs, and set bl.outputs to nil
func (bl *BeeLogger) Reset() {
bl.Flush()
for _, l := range bl.outputs { for _, l := range bl.outputs {
l.Flush()
l.Destroy() l.Destroy()
} }
bl.outputs = nil
}
func (bl *BeeLogger) flush() {
for {
if len(bl.msgChan) > 0 {
bm := <-bl.msgChan
bl.writeToLoggers(bm.when, bm.msg, bm.level)
logMsgPool.Put(bm)
continue
}
break
}
for _, l := range bl.outputs {
l.Flush()
}
} }

80
logs/logger.go Normal file
View File

@ -0,0 +1,80 @@
// Copyright 2014 beego Author. All Rights Reserved.
//
// 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.
package logs
import (
"io"
"sync"
"time"
)
type logWriter struct {
sync.Mutex
writer io.Writer
}
func newLogWriter(wr io.Writer) *logWriter {
return &logWriter{writer: wr}
}
func (lg *logWriter) println(when time.Time, msg string) {
lg.Lock()
h, _ := formatTimeHeader(when)
lg.writer.Write(append(append(h, msg...), '\n'))
lg.Unlock()
}
func formatTimeHeader(when time.Time) ([]byte, int) {
y, mo, d := when.Date()
h, mi, s := when.Clock()
//len(2006/01/02 15:03:04)==19
var buf [20]byte
t := 3
for y >= 10 {
p := y / 10
buf[t] = byte('0' + y - p*10)
y = p
t--
}
buf[0] = byte('0' + y)
buf[4] = '/'
if mo > 9 {
buf[5] = '1'
buf[6] = byte('0' + mo - 9)
} else {
buf[5] = '0'
buf[6] = byte('0' + mo)
}
buf[7] = '/'
t = d / 10
buf[8] = byte('0' + t)
buf[9] = byte('0' + d - t*10)
buf[10] = ' '
t = h / 10
buf[11] = byte('0' + t)
buf[12] = byte('0' + h - t*10)
buf[13] = ':'
t = mi / 10
buf[14] = byte('0' + t)
buf[15] = byte('0' + mi - t*10)
buf[16] = ':'
t = s / 10
buf[17] = byte('0' + t)
buf[18] = byte('0' + s - t*10)
buf[19] = ' '
return buf[0:], d
}

116
logs/multifile.go Normal file
View File

@ -0,0 +1,116 @@
// Copyright 2014 beego Author. All Rights Reserved.
//
// 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.
package logs
import (
"encoding/json"
"time"
)
// A filesLogWriter manages several fileLogWriter
// filesLogWriter will write logs to the file in json configuration and write the same level log to correspond file
// means if the file name in configuration is project.log filesLogWriter will create project.error.log/project.debug.log
// and write the error-level logs to project.error.log and write the debug-level logs to project.debug.log
// the rotate attribute also acts like fileLogWriter
type multiFileLogWriter struct {
writers [LevelDebug + 1 + 1]*fileLogWriter // the last one for fullLogWriter
fullLogWriter *fileLogWriter
Separate []string `json:"separate"`
}
var levelNames = [...]string{"emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"}
// Init file logger with json config.
// jsonConfig like:
// {
// "filename":"logs/beego.log",
// "maxLines":0,
// "maxsize":0,
// "daily":true,
// "maxDays":15,
// "rotate":true,
// "perm":0600,
// "separate":["emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"],
// }
func (f *multiFileLogWriter) Init(config string) error {
writer := newFileWriter().(*fileLogWriter)
err := writer.Init(config)
if err != nil {
return err
}
f.fullLogWriter = writer
f.writers[LevelDebug+1] = writer
//unmarshal "separate" field to f.Separate
json.Unmarshal([]byte(config), f)
jsonMap := map[string]interface{}{}
json.Unmarshal([]byte(config), &jsonMap)
for i := LevelEmergency; i < LevelDebug+1; i++ {
for _, v := range f.Separate {
if v == levelNames[i] {
jsonMap["filename"] = f.fullLogWriter.fileNameOnly + "." + levelNames[i] + f.fullLogWriter.suffix
jsonMap["level"] = i
bs, _ := json.Marshal(jsonMap)
writer = newFileWriter().(*fileLogWriter)
writer.Init(string(bs))
f.writers[i] = writer
}
}
}
return nil
}
func (f *multiFileLogWriter) Destroy() {
for i := 0; i < len(f.writers); i++ {
if f.writers[i] != nil {
f.writers[i].Destroy()
}
}
}
func (f *multiFileLogWriter) WriteMsg(when time.Time, msg string, level int) error {
if f.fullLogWriter != nil {
f.fullLogWriter.WriteMsg(when, msg, level)
}
for i := 0; i < len(f.writers)-1; i++ {
if f.writers[i] != nil {
if level == f.writers[i].Level {
f.writers[i].WriteMsg(when, msg, level)
}
}
}
return nil
}
func (f *multiFileLogWriter) Flush() {
for i := 0; i < len(f.writers); i++ {
if f.writers[i] != nil {
f.writers[i].Flush()
}
}
}
// newFilesWriter create a FileLogWriter returning as LoggerInterface.
func newFilesWriter() Logger {
return &multiFileLogWriter{}
}
func init() {
Register("multifile", newFilesWriter)
}

78
logs/multifile_test.go Normal file
View File

@ -0,0 +1,78 @@
// Copyright 2014 beego Author. All Rights Reserved.
//
// 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.
package logs
import (
"bufio"
"os"
"strconv"
"strings"
"testing"
)
func TestFiles_1(t *testing.T) {
log := NewLogger(10000)
log.SetLogger("multifile", `{"filename":"test.log","separate":["emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"]}`)
log.Debug("debug")
log.Informational("info")
log.Notice("notice")
log.Warning("warning")
log.Error("error")
log.Alert("alert")
log.Critical("critical")
log.Emergency("emergency")
fns := []string{""}
fns = append(fns, levelNames[0:]...)
name := "test"
suffix := ".log"
for _, fn := range fns {
file := name + suffix
if fn != "" {
file = name + "." + fn + suffix
}
f, err := os.Open(file)
if err != nil {
t.Fatal(err)
}
b := bufio.NewReader(f)
lineNum := 0
lastLine := ""
for {
line, _, err := b.ReadLine()
if err != nil {
break
}
if len(line) > 0 {
lastLine = string(line)
lineNum++
}
}
var expected = 1
if fn == "" {
expected = LevelDebug + 1
}
if lineNum != expected {
t.Fatal(file, "has", lineNum, "lines not "+strconv.Itoa(expected)+" lines")
}
if lineNum == 1 {
if !strings.Contains(lastLine, fn) {
t.Fatal(file + " " + lastLine + " not contains the log msg " + fn)
}
}
os.Remove(file)
}
}

View File

@ -24,30 +24,26 @@ import (
"time" "time"
) )
const ( // SMTPWriter implements LoggerInterface and is used to send emails via given SMTP-server.
subjectPhrase = "Diagnostic message from server" type SMTPWriter struct {
) Username string `json:"username"`
// smtpWriter implements LoggerInterface and is used to send emails via given SMTP-server.
type SmtpWriter struct {
Username string `json:"Username"`
Password string `json:"password"` Password string `json:"password"`
Host string `json:"Host"` Host string `json:"host"`
Subject string `json:"subject"` Subject string `json:"subject"`
FromAddress string `json:"fromAddress"` FromAddress string `json:"fromAddress"`
RecipientAddresses []string `json:"sendTos"` RecipientAddresses []string `json:"sendTos"`
Level int `json:"level"` Level int `json:"level"`
} }
// create smtp writer. // NewSMTPWriter create smtp writer.
func NewSmtpWriter() LoggerInterface { func newSMTPWriter() Logger {
return &SmtpWriter{Level: LevelTrace} return &SMTPWriter{Level: LevelTrace}
} }
// init smtp writer with json config. // Init smtp writer with json config.
// config like: // config like:
// { // {
// "Username":"example@gmail.com", // "username":"example@gmail.com",
// "password:"password", // "password:"password",
// "host":"smtp.gmail.com:465", // "host":"smtp.gmail.com:465",
// "subject":"email title", // "subject":"email title",
@ -55,7 +51,7 @@ func NewSmtpWriter() LoggerInterface {
// "sendTos":["email1","email2"], // "sendTos":["email1","email2"],
// "level":LevelError // "level":LevelError
// } // }
func (s *SmtpWriter) Init(jsonconfig string) error { func (s *SMTPWriter) Init(jsonconfig string) error {
err := json.Unmarshal([]byte(jsonconfig), s) err := json.Unmarshal([]byte(jsonconfig), s)
if err != nil { if err != nil {
return err return err
@ -63,7 +59,7 @@ func (s *SmtpWriter) Init(jsonconfig string) error {
return nil return nil
} }
func (s *SmtpWriter) GetSmtpAuth(host string) smtp.Auth { func (s *SMTPWriter) getSMTPAuth(host string) smtp.Auth {
if len(strings.Trim(s.Username, " ")) == 0 && len(strings.Trim(s.Password, " ")) == 0 { if len(strings.Trim(s.Username, " ")) == 0 && len(strings.Trim(s.Password, " ")) == 0 {
return nil return nil
} }
@ -75,7 +71,7 @@ func (s *SmtpWriter) GetSmtpAuth(host string) smtp.Auth {
) )
} }
func (s *SmtpWriter) sendMail(hostAddressWithPort string, auth smtp.Auth, fromAddress string, recipients []string, msgContent []byte) error { func (s *SMTPWriter) sendMail(hostAddressWithPort string, auth smtp.Auth, fromAddress string, recipients []string, msgContent []byte) error {
client, err := smtp.Dial(hostAddressWithPort) client, err := smtp.Dial(hostAddressWithPort)
if err != nil { if err != nil {
return err return err
@ -128,9 +124,9 @@ func (s *SmtpWriter) sendMail(hostAddressWithPort string, auth smtp.Auth, fromAd
return nil return nil
} }
// write message in smtp writer. // WriteMsg write message in smtp writer.
// it will send an email with subject and only this message. // it will send an email with subject and only this message.
func (s *SmtpWriter) WriteMsg(msg string, level int) error { func (s *SMTPWriter) WriteMsg(when time.Time, msg string, level int) error {
if level > s.Level { if level > s.Level {
return nil return nil
} }
@ -138,29 +134,27 @@ func (s *SmtpWriter) WriteMsg(msg string, level int) error {
hp := strings.Split(s.Host, ":") hp := strings.Split(s.Host, ":")
// Set up authentication information. // Set up authentication information.
auth := s.GetSmtpAuth(hp[0]) auth := s.getSMTPAuth(hp[0])
// Connect to the server, authenticate, set the sender and recipient, // Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step. // and send the email all in one step.
content_type := "Content-Type: text/plain" + "; charset=UTF-8" contentType := "Content-Type: text/plain" + "; charset=UTF-8"
mailmsg := []byte("To: " + strings.Join(s.RecipientAddresses, ";") + "\r\nFrom: " + s.FromAddress + "<" + s.FromAddress + mailmsg := []byte("To: " + strings.Join(s.RecipientAddresses, ";") + "\r\nFrom: " + s.FromAddress + "<" + s.FromAddress +
">\r\nSubject: " + s.Subject + "\r\n" + content_type + "\r\n\r\n" + fmt.Sprintf(".%s", time.Now().Format("2006-01-02 15:04:05")) + msg) ">\r\nSubject: " + s.Subject + "\r\n" + contentType + "\r\n\r\n" + fmt.Sprintf(".%s", when.Format("2006-01-02 15:04:05")) + msg)
err := s.sendMail(s.Host, auth, s.FromAddress, s.RecipientAddresses, mailmsg) return s.sendMail(s.Host, auth, s.FromAddress, s.RecipientAddresses, mailmsg)
return err
} }
// implementing method. empty. // Flush implementing method. empty.
func (s *SmtpWriter) Flush() { func (s *SMTPWriter) Flush() {
return return
} }
// implementing method. empty. // Destroy implementing method. empty.
func (s *SmtpWriter) Destroy() { func (s *SMTPWriter) Destroy() {
return return
} }
func init() { func init() {
Register("smtp", NewSmtpWriter) Register("smtp", newSMTPWriter)
} }

View File

@ -1,212 +0,0 @@
// Copyright 2014 beego Author. All Rights Reserved.
//
// 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.
package beego
import (
"bytes"
"compress/flate"
"compress/gzip"
"errors"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"sync"
"time"
)
var gmfim map[string]*memFileInfo = make(map[string]*memFileInfo)
var lock sync.RWMutex
// OpenMemZipFile returns MemFile object with a compressed static file.
// it's used for serve static file if gzip enable.
func openMemZipFile(path string, zip string) (*memFile, error) {
osfile, e := os.Open(path)
if e != nil {
return nil, e
}
defer osfile.Close()
osfileinfo, e := osfile.Stat()
if e != nil {
return nil, e
}
modtime := osfileinfo.ModTime()
fileSize := osfileinfo.Size()
lock.RLock()
cfi, ok := gmfim[zip+":"+path]
lock.RUnlock()
if !(ok && cfi.ModTime() == modtime && cfi.fileSize == fileSize) {
var content []byte
if zip == "gzip" {
var zipbuf bytes.Buffer
gzipwriter, e := gzip.NewWriterLevel(&zipbuf, gzip.BestCompression)
if e != nil {
return nil, e
}
_, e = io.Copy(gzipwriter, osfile)
gzipwriter.Close()
if e != nil {
return nil, e
}
content, e = ioutil.ReadAll(&zipbuf)
if e != nil {
return nil, e
}
} else if zip == "deflate" {
var zipbuf bytes.Buffer
deflatewriter, e := flate.NewWriter(&zipbuf, flate.BestCompression)
if e != nil {
return nil, e
}
_, e = io.Copy(deflatewriter, osfile)
deflatewriter.Close()
if e != nil {
return nil, e
}
content, e = ioutil.ReadAll(&zipbuf)
if e != nil {
return nil, e
}
} else {
content, e = ioutil.ReadAll(osfile)
if e != nil {
return nil, e
}
}
cfi = &memFileInfo{osfileinfo, modtime, content, int64(len(content)), fileSize}
lock.Lock()
defer lock.Unlock()
gmfim[zip+":"+path] = cfi
}
return &memFile{fi: cfi, offset: 0}, nil
}
// MemFileInfo contains a compressed file bytes and file information.
// it implements os.FileInfo interface.
type memFileInfo struct {
os.FileInfo
modTime time.Time
content []byte
contentSize int64
fileSize int64
}
// Name returns the compressed filename.
func (fi *memFileInfo) Name() string {
return fi.Name()
}
// Size returns the raw file content size, not compressed size.
func (fi *memFileInfo) Size() int64 {
return fi.contentSize
}
// Mode returns file mode.
func (fi *memFileInfo) Mode() os.FileMode {
return fi.Mode()
}
// ModTime returns the last modified time of raw file.
func (fi *memFileInfo) ModTime() time.Time {
return fi.modTime
}
// IsDir returns the compressing file is a directory or not.
func (fi *memFileInfo) IsDir() bool {
return fi.IsDir()
}
// return nil. implement the os.FileInfo interface method.
func (fi *memFileInfo) Sys() interface{} {
return nil
}
// MemFile contains MemFileInfo and bytes offset when reading.
// it implements io.Reader,io.ReadCloser and io.Seeker.
type memFile struct {
fi *memFileInfo
offset int64
}
// Close memfile.
func (f *memFile) Close() error {
return nil
}
// Get os.FileInfo of memfile.
func (f *memFile) Stat() (os.FileInfo, error) {
return f.fi, nil
}
// read os.FileInfo of files in directory of memfile.
// it returns empty slice.
func (f *memFile) Readdir(count int) ([]os.FileInfo, error) {
infos := []os.FileInfo{}
return infos, nil
}
// Read bytes from the compressed file bytes.
func (f *memFile) Read(p []byte) (n int, err error) {
if len(f.fi.content)-int(f.offset) >= len(p) {
n = len(p)
} else {
n = len(f.fi.content) - int(f.offset)
err = io.EOF
}
copy(p, f.fi.content[f.offset:f.offset+int64(n)])
f.offset += int64(n)
return
}
var errWhence = errors.New("Seek: invalid whence")
var errOffset = errors.New("Seek: invalid offset")
// Read bytes from the compressed file bytes by seeker.
func (f *memFile) Seek(offset int64, whence int) (ret int64, err error) {
switch whence {
default:
return 0, errWhence
case os.SEEK_SET:
case os.SEEK_CUR:
offset += f.offset
case os.SEEK_END:
offset += int64(len(f.fi.content))
}
if offset < 0 || int(offset) > len(f.fi.content) {
return 0, errOffset
}
f.offset = offset
return f.offset, nil
}
// GetAcceptEncodingZip returns accept encoding format in http header.
// zip is first, then deflate if both accepted.
// If no accepted, return empty string.
func getAcceptEncodingZip(r *http.Request) string {
ss := r.Header.Get("Accept-Encoding")
ss = strings.ToLower(ss)
if strings.Contains(ss, "gzip") {
return "gzip"
} else if strings.Contains(ss, "deflate") {
return "deflate"
} else {
return ""
}
}

View File

@ -1,339 +0,0 @@
// Copyright 2014 beego Author. All Rights Reserved.
//
// 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.
package middleware
import (
"fmt"
"html/template"
"net/http"
"runtime"
"strconv"
)
var (
AppName string
VERSION string
)
var tpl = `
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>beego application error</title>
<style>
html, body, body * {padding: 0; margin: 0;}
#header {background:#ffd; border-bottom:solid 2px #A31515; padding: 20px 10px;}
#header h2{ }
#footer {border-top:solid 1px #aaa; padding: 5px 10px; font-size: 12px; color:green;}
#content {padding: 5px;}
#content .stack b{ font-size: 13px; color: red;}
#content .stack pre{padding-left: 10px;}
table {}
td.t {text-align: right; padding-right: 5px; color: #888;}
</style>
<script type="text/javascript">
</script>
</head>
<body>
<div id="header">
<h2>{{.AppError}}</h2>
</div>
<div id="content">
<table>
<tr>
<td class="t">Request Method: </td><td>{{.RequestMethod}}</td>
</tr>
<tr>
<td class="t">Request URL: </td><td>{{.RequestURL}}</td>
</tr>
<tr>
<td class="t">RemoteAddr: </td><td>{{.RemoteAddr }}</td>
</tr>
</table>
<div class="stack">
<b>Stack</b>
<pre>{{.Stack}}</pre>
</div>
</div>
<div id="footer">
<p>beego {{ .BeegoVersion }} (beego framework)</p>
<p>golang version: {{.GoVersion}}</p>
</div>
</body>
</html>
`
// render default application error page with error and stack string.
func ShowErr(err interface{}, rw http.ResponseWriter, r *http.Request, Stack string) {
t, _ := template.New("beegoerrortemp").Parse(tpl)
data := make(map[string]string)
data["AppError"] = AppName + ":" + fmt.Sprint(err)
data["RequestMethod"] = r.Method
data["RequestURL"] = r.RequestURI
data["RemoteAddr"] = r.RemoteAddr
data["Stack"] = Stack
data["BeegoVersion"] = VERSION
data["GoVersion"] = runtime.Version()
rw.WriteHeader(500)
t.Execute(rw, data)
}
var errtpl = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{{.Title}}</title>
<style type="text/css">
* {
margin:0;
padding:0;
}
body {
background-color:#EFEFEF;
font: .9em "Lucida Sans Unicode", "Lucida Grande", sans-serif;
}
#wrapper{
width:600px;
margin:40px auto 0;
text-align:center;
-moz-box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
-webkit-box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
}
#wrapper h1{
color:#FFF;
text-align:center;
margin-bottom:20px;
}
#wrapper a{
display:block;
font-size:.9em;
padding-top:20px;
color:#FFF;
text-decoration:none;
text-align:center;
}
#container {
width:600px;
padding-bottom:15px;
background-color:#FFFFFF;
}
.navtop{
height:40px;
background-color:#24B2EB;
padding:13px;
}
.content {
padding:10px 10px 25px;
background: #FFFFFF;
margin:;
color:#333;
}
a.button{
color:white;
padding:15px 20px;
text-shadow:1px 1px 0 #00A5FF;
font-weight:bold;
text-align:center;
border:1px solid #24B2EB;
margin:0px 200px;
clear:both;
background-color: #24B2EB;
border-radius:100px;
-moz-border-radius:100px;
-webkit-border-radius:100px;
}
a.button:hover{
text-decoration:none;
background-color: #24B2EB;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="container">
<div class="navtop">
<h1>{{.Title}}</h1>
</div>
<div id="content">
{{.Content}}
<a href="/" title="Home" class="button">Go Home</a><br />
<br>Powered by beego {{.BeegoVersion}}
</div>
</div>
</div>
</body>
</html>
`
// map of http handlers for each error string.
var ErrorMaps map[string]http.HandlerFunc
func init() {
ErrorMaps = make(map[string]http.HandlerFunc)
}
// show 404 notfound error.
func NotFound(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
data := make(map[string]interface{})
data["Title"] = "Page Not Found"
data["Content"] = template.HTML("<br>The page you have requested has flown the coop." +
"<br>Perhaps you are here because:" +
"<br><br><ul>" +
"<br>The page has moved" +
"<br>The page no longer exists" +
"<br>You were looking for your puppy and got lost" +
"<br>You like 404 pages" +
"</ul>")
data["BeegoVersion"] = VERSION
//rw.WriteHeader(http.StatusNotFound)
t.Execute(rw, data)
}
// show 401 unauthorized error.
func Unauthorized(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
data := make(map[string]interface{})
data["Title"] = "Unauthorized"
data["Content"] = template.HTML("<br>The page you have requested can't be authorized." +
"<br>Perhaps you are here because:" +
"<br><br><ul>" +
"<br>The credentials you supplied are incorrect" +
"<br>There are errors in the website address" +
"</ul>")
data["BeegoVersion"] = VERSION
//rw.WriteHeader(http.StatusUnauthorized)
t.Execute(rw, data)
}
// show 403 forbidden error.
func Forbidden(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
data := make(map[string]interface{})
data["Title"] = "Forbidden"
data["Content"] = template.HTML("<br>The page you have requested is forbidden." +
"<br>Perhaps you are here because:" +
"<br><br><ul>" +
"<br>Your address may be blocked" +
"<br>The site may be disabled" +
"<br>You need to log in" +
"</ul>")
data["BeegoVersion"] = VERSION
//rw.WriteHeader(http.StatusForbidden)
t.Execute(rw, data)
}
// show 503 service unavailable error.
func ServiceUnavailable(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
data := make(map[string]interface{})
data["Title"] = "Service Unavailable"
data["Content"] = template.HTML("<br>The page you have requested is unavailable." +
"<br>Perhaps you are here because:" +
"<br><br><ul>" +
"<br><br>The page is overloaded" +
"<br>Please try again later." +
"</ul>")
data["BeegoVersion"] = VERSION
//rw.WriteHeader(http.StatusServiceUnavailable)
t.Execute(rw, data)
}
// show 500 internal server error.
func InternalServerError(rw http.ResponseWriter, r *http.Request) {
t, _ := template.New("beegoerrortemp").Parse(errtpl)
data := make(map[string]interface{})
data["Title"] = "Internal Server Error"
data["Content"] = template.HTML("<br>The page you have requested is down right now." +
"<br><br><ul>" +
"<br>Please try again later and report the error to the website administrator" +
"<br></ul>")
data["BeegoVersion"] = VERSION
//rw.WriteHeader(http.StatusInternalServerError)
t.Execute(rw, data)
}
// show 500 internal error with simple text string.
func SimpleServerError(rw http.ResponseWriter, r *http.Request) {
http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
// add http handler for given error string.
func Errorhandler(err string, h http.HandlerFunc) {
ErrorMaps[err] = h
}
// register default error http handlers, 404,401,403,500 and 503.
func RegisterErrorHandler() {
if _, ok := ErrorMaps["404"]; !ok {
ErrorMaps["404"] = NotFound
}
if _, ok := ErrorMaps["401"]; !ok {
ErrorMaps["401"] = Unauthorized
}
if _, ok := ErrorMaps["403"]; !ok {
ErrorMaps["403"] = Forbidden
}
if _, ok := ErrorMaps["503"]; !ok {
ErrorMaps["503"] = ServiceUnavailable
}
if _, ok := ErrorMaps["500"]; !ok {
ErrorMaps["500"] = InternalServerError
}
}
// show error string as simple text message.
// if error string is empty, show 500 error as default.
func Exception(errcode string, w http.ResponseWriter, r *http.Request, msg string) {
if h, ok := ErrorMaps[errcode]; ok {
isint, err := strconv.Atoi(errcode)
if err != nil {
isint = 500
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(isint)
h(w, r)
return
} else {
isint, err := strconv.Atoi(errcode)
if err != nil {
isint = 500
}
if isint == 400 {
msg = "404 page not found"
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(isint)
fmt.Fprintln(w, msg)
return
}
}

View File

@ -1,49 +0,0 @@
// Copyright 2014 beego Author. All Rights Reserved.
//
// 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.
package middleware
import "fmt"
// http exceptions
type HTTPException struct {
StatusCode int // http status code 4xx, 5xx
Description string
}
// return http exception error string, e.g. "400 Bad Request".
func (e *HTTPException) Error() string {
return fmt.Sprintf("%d %s", e.StatusCode, e.Description)
}
// map of http exceptions for each http status code int.
// defined 400,401,403,404,405,500,502,503 and 504 default.
var HTTPExceptionMaps map[int]HTTPException
func init() {
HTTPExceptionMaps = make(map[int]HTTPException)
// Normal 4XX HTTP Status
HTTPExceptionMaps[400] = HTTPException{400, "Bad Request"}
HTTPExceptionMaps[401] = HTTPException{401, "Unauthorized"}
HTTPExceptionMaps[403] = HTTPException{403, "Forbidden"}
HTTPExceptionMaps[404] = HTTPException{404, "Not Found"}
HTTPExceptionMaps[405] = HTTPException{405, "Method Not Allowed"}
// Normal 5XX HTTP Status
HTTPExceptionMaps[500] = HTTPException{500, "Internal Server Error"}
HTTPExceptionMaps[502] = HTTPException{502, "Bad Gateway"}
HTTPExceptionMaps[503] = HTTPException{503, "Service Unavailable"}
HTTPExceptionMaps[504] = HTTPException{504, "Gateway Timeout"}
}

View File

@ -1,71 +0,0 @@
// Copyright 2014 beego Author. All Rights Reserved.
//
// 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.
// Usage:
//
// import "github.com/astaxie/beego/middleware"
//
// I18N = middleware.NewLocale("conf/i18n.conf", beego.AppConfig.String("language"))
//
// more docs: http://beego.me/docs/module/i18n.md
package middleware
import (
"encoding/json"
"io/ioutil"
"os"
)
type Translation struct {
filepath string
CurrentLocal string
Locales map[string]map[string]string
}
func NewLocale(filepath string, defaultlocal string) *Translation {
i18n := make(map[string]map[string]string)
file, err := os.Open(filepath)
if err != nil {
panic("open " + filepath + " err :" + err.Error())
}
data, err := ioutil.ReadAll(file)
if err != nil {
panic("read " + filepath + " err :" + err.Error())
}
err = json.Unmarshal(data, &i18n)
if err != nil {
panic("json.Unmarshal " + filepath + " err :" + err.Error())
}
return &Translation{
filepath: filepath,
CurrentLocal: defaultlocal,
Locales: i18n,
}
}
func (t *Translation) SetLocale(local string) {
t.CurrentLocal = local
}
func (t *Translation) Translate(key string, local string) string {
if local == "" {
local = t.CurrentLocal
}
if ct, ok := t.Locales[key]; ok {
if v, o := ct[local]; o {
return v
}
}
return key
}

View File

@ -14,33 +14,40 @@
package migration package migration
// Table store the tablename and Column
type Table struct { type Table struct {
TableName string TableName string
Columns []*Column Columns []*Column
} }
// Create return the create sql
func (t *Table) Create() string { func (t *Table) Create() string {
return "" return ""
} }
// Drop return the drop sql
func (t *Table) Drop() string { func (t *Table) Drop() string {
return "" return ""
} }
// Column define the columns name type and Default
type Column struct { type Column struct {
Name string Name string
Type string Type string
Default interface{} Default interface{}
} }
// Create return create sql with the provided tbname and columns
func Create(tbname string, columns ...Column) string { func Create(tbname string, columns ...Column) string {
return "" return ""
} }
// Drop return the drop sql with the provided tbname and columns
func Drop(tbname string, columns ...Column) string { func Drop(tbname string, columns ...Column) string {
return "" return ""
} }
// TableDDL is still in think
func TableDDL(tbname string, columns ...Column) string { func TableDDL(tbname string, columns ...Column) string {
return "" return ""
} }

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// migration package for migration // Package migration is used for migration
// //
// The table structure is as follow: // The table structure is as follow:
// //
@ -39,8 +39,8 @@ import (
// const the data format for the bee generate migration datatype // const the data format for the bee generate migration datatype
const ( const (
M_DATE_FORMAT = "20060102_150405" DateFormat = "20060102_150405"
M_DB_DATE_FORMAT = "2006-01-02 15:04:05" DBDateFormat = "2006-01-02 15:04:05"
) )
// Migrationer is an interface for all Migration struct // Migrationer is an interface for all Migration struct
@ -60,24 +60,24 @@ func init() {
migrationMap = make(map[string]Migrationer) migrationMap = make(map[string]Migrationer)
} }
// the basic type which will implement the basic type // Migration the basic type which will implement the basic type
type Migration struct { type Migration struct {
sqls []string sqls []string
Created string Created string
} }
// implement in the Inheritance struct for upgrade // Up implement in the Inheritance struct for upgrade
func (m *Migration) Up() { func (m *Migration) Up() {
} }
// implement in the Inheritance struct for down // Down implement in the Inheritance struct for down
func (m *Migration) Down() { func (m *Migration) Down() {
} }
// add sql want to execute // SQL add sql want to execute
func (m *Migration) Sql(sql string) { func (m *Migration) SQL(sql string) {
m.sqls = append(m.sqls, sql) m.sqls = append(m.sqls, sql)
} }
@ -86,7 +86,7 @@ func (m *Migration) Reset() {
m.sqls = make([]string, 0) m.sqls = make([]string, 0)
} }
// execute the sql already add in the sql // Exec execute the sql already add in the sql
func (m *Migration) Exec(name, status string) error { func (m *Migration) Exec(name, status string) error {
o := orm.NewOrm() o := orm.NewOrm()
for _, s := range m.sqls { for _, s := range m.sqls {
@ -104,33 +104,32 @@ func (m *Migration) addOrUpdateRecord(name, status string) error {
o := orm.NewOrm() o := orm.NewOrm()
if status == "down" { if status == "down" {
status = "rollback" status = "rollback"
p, err := o.Raw("update migrations set `status` = ?, `rollback_statements` = ?, `created_at` = ? where name = ?").Prepare() p, err := o.Raw("update migrations set status = ?, rollback_statements = ?, created_at = ? where name = ?").Prepare()
if err != nil { if err != nil {
return nil return nil
} }
_, err = p.Exec(status, strings.Join(m.sqls, "; "), time.Now().Format(M_DB_DATE_FORMAT), name) _, err = p.Exec(status, strings.Join(m.sqls, "; "), time.Now().Format(DBDateFormat), name)
return err
} else {
status = "update"
p, err := o.Raw("insert into migrations(`name`, `created_at`, `statements`, `status`) values(?,?,?,?)").Prepare()
if err != nil {
return err
}
_, err = p.Exec(name, time.Now().Format(M_DB_DATE_FORMAT), strings.Join(m.sqls, "; "), status)
return err return err
} }
status = "update"
p, err := o.Raw("insert into migrations(name, created_at, statements, status) values(?,?,?,?)").Prepare()
if err != nil {
return err
}
_, err = p.Exec(name, time.Now().Format(DBDateFormat), strings.Join(m.sqls, "; "), status)
return err
} }
// get the unixtime from the Created // GetCreated get the unixtime from the Created
func (m *Migration) GetCreated() int64 { func (m *Migration) GetCreated() int64 {
t, err := time.Parse(M_DATE_FORMAT, m.Created) t, err := time.Parse(DateFormat, m.Created)
if err != nil { if err != nil {
return 0 return 0
} }
return t.Unix() return t.Unix()
} }
// register the Migration in the map // Register register the Migration in the map
func Register(name string, m Migrationer) error { func Register(name string, m Migrationer) error {
if _, ok := migrationMap[name]; ok { if _, ok := migrationMap[name]; ok {
return errors.New("already exist name:" + name) return errors.New("already exist name:" + name)
@ -139,7 +138,7 @@ func Register(name string, m Migrationer) error {
return nil return nil
} }
// upgrate the migration from lasttime // Upgrade upgrate the migration from lasttime
func Upgrade(lasttime int64) error { func Upgrade(lasttime int64) error {
sm := sortMap(migrationMap) sm := sortMap(migrationMap)
i := 0 i := 0
@ -163,7 +162,7 @@ func Upgrade(lasttime int64) error {
return nil return nil
} }
//rollback the migration by the name // Rollback rollback the migration by the name
func Rollback(name string) error { func Rollback(name string) error {
if v, ok := migrationMap[name]; ok { if v, ok := migrationMap[name]; ok {
beego.Info("start rollback") beego.Info("start rollback")
@ -178,14 +177,13 @@ func Rollback(name string) error {
beego.Info("end rollback") beego.Info("end rollback")
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
return nil return nil
} else {
beego.Error("not exist the migrationMap name:" + name)
time.Sleep(2 * time.Second)
return errors.New("not exist the migrationMap name:" + name)
} }
beego.Error("not exist the migrationMap name:" + name)
time.Sleep(2 * time.Second)
return errors.New("not exist the migrationMap name:" + name)
} }
// reset all migration // Reset reset all migration
// run all migration's down function // run all migration's down function
func Reset() error { func Reset() error {
sm := sortMap(migrationMap) sm := sortMap(migrationMap)
@ -214,7 +212,7 @@ func Reset() error {
return nil return nil
} }
// first Reset, then Upgrade // Refresh first Reset, then Upgrade
func Refresh() error { func Refresh() error {
err := Reset() err := Reset()
if err != nil { if err != nil {

14
mime.go
View File

@ -14,11 +14,7 @@
package beego package beego
import ( var mimemaps = map[string]string{
"mime"
)
var mimemaps map[string]string = map[string]string{
".3dm": "x-world/x-3dmf", ".3dm": "x-world/x-3dmf",
".3dmf": "x-world/x-3dmf", ".3dmf": "x-world/x-3dmf",
".7z": "application/x-7z-compressed", ".7z": "application/x-7z-compressed",
@ -40,6 +36,7 @@ var mimemaps map[string]string = map[string]string{
".ani": "application/x-navi-animation", ".ani": "application/x-navi-animation",
".aos": "application/x-nokia-9000-communicator-add-on-software", ".aos": "application/x-nokia-9000-communicator-add-on-software",
".aps": "application/mime", ".aps": "application/mime",
".apk": "application/vnd.android.package-archive",
".arc": "application/x-arc-compressed", ".arc": "application/x-arc-compressed",
".arj": "application/arj", ".arj": "application/arj",
".art": "image/x-jg", ".art": "image/x-jg",
@ -557,10 +554,3 @@ var mimemaps map[string]string = map[string]string{
".oex": "application/x-opera-extension", ".oex": "application/x-opera-extension",
".mustache": "text/html", ".mustache": "text/html",
} }
func initMime() error {
for k, v := range mimemaps {
mime.AddExtensionType(k, v)
}
return nil
}

View File

@ -19,21 +19,21 @@ import (
"strings" "strings"
beecontext "github.com/astaxie/beego/context" beecontext "github.com/astaxie/beego/context"
"github.com/astaxie/beego/middleware"
) )
type namespaceCond func(*beecontext.Context) bool type namespaceCond func(*beecontext.Context) bool
type innnerNamespace func(*Namespace) // LinkNamespace used as link action
type LinkNamespace func(*Namespace)
// Namespace is store all the info // Namespace is store all the info
type Namespace struct { type Namespace struct {
prefix string prefix string
handlers *ControllerRegistor handlers *ControllerRegister
} }
// get new Namespace // NewNamespace get new Namespace
func NewNamespace(prefix string, params ...innnerNamespace) *Namespace { func NewNamespace(prefix string, params ...LinkNamespace) *Namespace {
ns := &Namespace{ ns := &Namespace{
prefix: prefix, prefix: prefix,
handlers: NewControllerRegister(), handlers: NewControllerRegister(),
@ -44,7 +44,7 @@ func NewNamespace(prefix string, params ...innnerNamespace) *Namespace {
return ns return ns
} }
// set condtion function // Cond set condtion function
// if cond return true can run this namespace, else can't // if cond return true can run this namespace, else can't
// usage: // usage:
// ns.Cond(func (ctx *context.Context) bool{ // ns.Cond(func (ctx *context.Context) bool{
@ -57,7 +57,7 @@ func NewNamespace(prefix string, params ...innnerNamespace) *Namespace {
func (n *Namespace) Cond(cond namespaceCond) *Namespace { func (n *Namespace) Cond(cond namespaceCond) *Namespace {
fn := func(ctx *beecontext.Context) { fn := func(ctx *beecontext.Context) {
if !cond(ctx) { if !cond(ctx) {
middleware.Exception("405", ctx.ResponseWriter, ctx.Request, "Method not allowed") exception("405", ctx)
} }
} }
if v, ok := n.handlers.filters[BeforeRouter]; ok { if v, ok := n.handlers.filters[BeforeRouter]; ok {
@ -73,7 +73,7 @@ func (n *Namespace) Cond(cond namespaceCond) *Namespace {
return n return n
} }
// add filter in the Namespace // Filter add filter in the Namespace
// action has before & after // action has before & after
// FilterFunc // FilterFunc
// usage: // usage:
@ -96,98 +96,98 @@ func (n *Namespace) Filter(action string, filter ...FilterFunc) *Namespace {
return n return n
} }
// same as beego.Rourer // Router same as beego.Rourer
// refer: https://godoc.org/github.com/astaxie/beego#Router // refer: https://godoc.org/github.com/astaxie/beego#Router
func (n *Namespace) Router(rootpath string, c ControllerInterface, mappingMethods ...string) *Namespace { func (n *Namespace) Router(rootpath string, c ControllerInterface, mappingMethods ...string) *Namespace {
n.handlers.Add(rootpath, c, mappingMethods...) n.handlers.Add(rootpath, c, mappingMethods...)
return n return n
} }
// same as beego.AutoRouter // AutoRouter same as beego.AutoRouter
// refer: https://godoc.org/github.com/astaxie/beego#AutoRouter // refer: https://godoc.org/github.com/astaxie/beego#AutoRouter
func (n *Namespace) AutoRouter(c ControllerInterface) *Namespace { func (n *Namespace) AutoRouter(c ControllerInterface) *Namespace {
n.handlers.AddAuto(c) n.handlers.AddAuto(c)
return n return n
} }
// same as beego.AutoPrefix // AutoPrefix same as beego.AutoPrefix
// refer: https://godoc.org/github.com/astaxie/beego#AutoPrefix // refer: https://godoc.org/github.com/astaxie/beego#AutoPrefix
func (n *Namespace) AutoPrefix(prefix string, c ControllerInterface) *Namespace { func (n *Namespace) AutoPrefix(prefix string, c ControllerInterface) *Namespace {
n.handlers.AddAutoPrefix(prefix, c) n.handlers.AddAutoPrefix(prefix, c)
return n return n
} }
// same as beego.Get // Get same as beego.Get
// refer: https://godoc.org/github.com/astaxie/beego#Get // refer: https://godoc.org/github.com/astaxie/beego#Get
func (n *Namespace) Get(rootpath string, f FilterFunc) *Namespace { func (n *Namespace) Get(rootpath string, f FilterFunc) *Namespace {
n.handlers.Get(rootpath, f) n.handlers.Get(rootpath, f)
return n return n
} }
// same as beego.Post // Post same as beego.Post
// refer: https://godoc.org/github.com/astaxie/beego#Post // refer: https://godoc.org/github.com/astaxie/beego#Post
func (n *Namespace) Post(rootpath string, f FilterFunc) *Namespace { func (n *Namespace) Post(rootpath string, f FilterFunc) *Namespace {
n.handlers.Post(rootpath, f) n.handlers.Post(rootpath, f)
return n return n
} }
// same as beego.Delete // Delete same as beego.Delete
// refer: https://godoc.org/github.com/astaxie/beego#Delete // refer: https://godoc.org/github.com/astaxie/beego#Delete
func (n *Namespace) Delete(rootpath string, f FilterFunc) *Namespace { func (n *Namespace) Delete(rootpath string, f FilterFunc) *Namespace {
n.handlers.Delete(rootpath, f) n.handlers.Delete(rootpath, f)
return n return n
} }
// same as beego.Put // Put same as beego.Put
// refer: https://godoc.org/github.com/astaxie/beego#Put // refer: https://godoc.org/github.com/astaxie/beego#Put
func (n *Namespace) Put(rootpath string, f FilterFunc) *Namespace { func (n *Namespace) Put(rootpath string, f FilterFunc) *Namespace {
n.handlers.Put(rootpath, f) n.handlers.Put(rootpath, f)
return n return n
} }
// same as beego.Head // Head same as beego.Head
// refer: https://godoc.org/github.com/astaxie/beego#Head // refer: https://godoc.org/github.com/astaxie/beego#Head
func (n *Namespace) Head(rootpath string, f FilterFunc) *Namespace { func (n *Namespace) Head(rootpath string, f FilterFunc) *Namespace {
n.handlers.Head(rootpath, f) n.handlers.Head(rootpath, f)
return n return n
} }
// same as beego.Options // Options same as beego.Options
// refer: https://godoc.org/github.com/astaxie/beego#Options // refer: https://godoc.org/github.com/astaxie/beego#Options
func (n *Namespace) Options(rootpath string, f FilterFunc) *Namespace { func (n *Namespace) Options(rootpath string, f FilterFunc) *Namespace {
n.handlers.Options(rootpath, f) n.handlers.Options(rootpath, f)
return n return n
} }
// same as beego.Patch // Patch same as beego.Patch
// refer: https://godoc.org/github.com/astaxie/beego#Patch // refer: https://godoc.org/github.com/astaxie/beego#Patch
func (n *Namespace) Patch(rootpath string, f FilterFunc) *Namespace { func (n *Namespace) Patch(rootpath string, f FilterFunc) *Namespace {
n.handlers.Patch(rootpath, f) n.handlers.Patch(rootpath, f)
return n return n
} }
// same as beego.Any // Any same as beego.Any
// refer: https://godoc.org/github.com/astaxie/beego#Any // refer: https://godoc.org/github.com/astaxie/beego#Any
func (n *Namespace) Any(rootpath string, f FilterFunc) *Namespace { func (n *Namespace) Any(rootpath string, f FilterFunc) *Namespace {
n.handlers.Any(rootpath, f) n.handlers.Any(rootpath, f)
return n return n
} }
// same as beego.Handler // Handler same as beego.Handler
// refer: https://godoc.org/github.com/astaxie/beego#Handler // refer: https://godoc.org/github.com/astaxie/beego#Handler
func (n *Namespace) Handler(rootpath string, h http.Handler) *Namespace { func (n *Namespace) Handler(rootpath string, h http.Handler) *Namespace {
n.handlers.Handler(rootpath, h) n.handlers.Handler(rootpath, h)
return n return n
} }
// add include class // Include add include class
// refer: https://godoc.org/github.com/astaxie/beego#Include // refer: https://godoc.org/github.com/astaxie/beego#Include
func (n *Namespace) Include(cList ...ControllerInterface) *Namespace { func (n *Namespace) Include(cList ...ControllerInterface) *Namespace {
n.handlers.Include(cList...) n.handlers.Include(cList...)
return n return n
} }
// nest Namespace // Namespace add nest Namespace
// usage: // usage:
//ns := beego.NewNamespace(“/v1”). //ns := beego.NewNamespace(“/v1”).
//Namespace( //Namespace(
@ -231,7 +231,7 @@ func (n *Namespace) Namespace(ns ...*Namespace) *Namespace {
return n return n
} }
// register Namespace into beego.Handler // AddNamespace register Namespace into beego.Handler
// support multi Namespace // support multi Namespace
func AddNamespace(nl ...*Namespace) { func AddNamespace(nl ...*Namespace) {
for _, n := range nl { for _, n := range nl {
@ -276,115 +276,122 @@ func addPrefix(t *Tree, prefix string) {
} }
// Namespace Condition // NSCond is Namespace Condition
func NSCond(cond namespaceCond) innnerNamespace { func NSCond(cond namespaceCond) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Cond(cond) ns.Cond(cond)
} }
} }
// Namespace BeforeRouter filter // NSBefore Namespace BeforeRouter filter
func NSBefore(filiterList ...FilterFunc) innnerNamespace { func NSBefore(filiterList ...FilterFunc) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Filter("before", filiterList...) ns.Filter("before", filiterList...)
} }
} }
// Namespace FinishRouter filter // NSAfter add Namespace FinishRouter filter
func NSAfter(filiterList ...FilterFunc) innnerNamespace { func NSAfter(filiterList ...FilterFunc) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Filter("after", filiterList...) ns.Filter("after", filiterList...)
} }
} }
// Namespace Include ControllerInterface // NSInclude Namespace Include ControllerInterface
func NSInclude(cList ...ControllerInterface) innnerNamespace { func NSInclude(cList ...ControllerInterface) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Include(cList...) ns.Include(cList...)
} }
} }
// Namespace Router // NSRouter call Namespace Router
func NSRouter(rootpath string, c ControllerInterface, mappingMethods ...string) innnerNamespace { func NSRouter(rootpath string, c ControllerInterface, mappingMethods ...string) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Router(rootpath, c, mappingMethods...) ns.Router(rootpath, c, mappingMethods...)
} }
} }
// Namespace Get // NSGet call Namespace Get
func NSGet(rootpath string, f FilterFunc) innnerNamespace { func NSGet(rootpath string, f FilterFunc) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Get(rootpath, f) ns.Get(rootpath, f)
} }
} }
// Namespace Post // NSPost call Namespace Post
func NSPost(rootpath string, f FilterFunc) innnerNamespace { func NSPost(rootpath string, f FilterFunc) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Post(rootpath, f) ns.Post(rootpath, f)
} }
} }
// Namespace Head // NSHead call Namespace Head
func NSHead(rootpath string, f FilterFunc) innnerNamespace { func NSHead(rootpath string, f FilterFunc) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Head(rootpath, f) ns.Head(rootpath, f)
} }
} }
// Namespace Put // NSPut call Namespace Put
func NSPut(rootpath string, f FilterFunc) innnerNamespace { func NSPut(rootpath string, f FilterFunc) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Put(rootpath, f) ns.Put(rootpath, f)
} }
} }
// Namespace Delete // NSDelete call Namespace Delete
func NSDelete(rootpath string, f FilterFunc) innnerNamespace { func NSDelete(rootpath string, f FilterFunc) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Delete(rootpath, f) ns.Delete(rootpath, f)
} }
} }
// Namespace Any // NSAny call Namespace Any
func NSAny(rootpath string, f FilterFunc) innnerNamespace { func NSAny(rootpath string, f FilterFunc) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Any(rootpath, f) ns.Any(rootpath, f)
} }
} }
// Namespace Options // NSOptions call Namespace Options
func NSOptions(rootpath string, f FilterFunc) innnerNamespace { func NSOptions(rootpath string, f FilterFunc) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Options(rootpath, f) ns.Options(rootpath, f)
} }
} }
// Namespace Patch // NSPatch call Namespace Patch
func NSPatch(rootpath string, f FilterFunc) innnerNamespace { func NSPatch(rootpath string, f FilterFunc) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.Patch(rootpath, f) ns.Patch(rootpath, f)
} }
} }
//Namespace AutoRouter // NSAutoRouter call Namespace AutoRouter
func NSAutoRouter(c ControllerInterface) innnerNamespace { func NSAutoRouter(c ControllerInterface) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.AutoRouter(c) ns.AutoRouter(c)
} }
} }
// Namespace AutoPrefix // NSAutoPrefix call Namespace AutoPrefix
func NSAutoPrefix(prefix string, c ControllerInterface) innnerNamespace { func NSAutoPrefix(prefix string, c ControllerInterface) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
ns.AutoPrefix(prefix, c) ns.AutoPrefix(prefix, c)
} }
} }
// Namespace add sub Namespace // NSNamespace add sub Namespace
func NSNamespace(prefix string, params ...innnerNamespace) innnerNamespace { func NSNamespace(prefix string, params ...LinkNamespace) LinkNamespace {
return func(ns *Namespace) { return func(ns *Namespace) {
n := NewNamespace(prefix, params...) n := NewNamespace(prefix, params...)
ns.Namespace(n) ns.Namespace(n)
} }
} }
// NSHandler add handler
func NSHandler(rootpath string, h http.Handler) LinkNamespace {
return func(ns *Namespace) {
ns.Handler(rootpath, h)
}
}

View File

@ -117,7 +117,7 @@ o.Begin()
... ...
user := User{Name: "slene"} user := User{Name: "slene"}
id, err := o.Insert(&user) id, err := o.Insert(&user)
if err != nil { if err == nil {
o.Commit() o.Commit()
} else { } else {
o.Rollback() o.Rollback()

View File

@ -46,7 +46,7 @@ func printHelp(errs ...string) {
os.Exit(2) os.Exit(2)
} }
// listen for orm command and then run it if command arguments passed. // RunCommand listen for orm command and then run it if command arguments passed.
func RunCommand() { func RunCommand() {
if len(os.Args) < 2 || os.Args[1] != "orm" { if len(os.Args) < 2 || os.Args[1] != "orm" {
return return
@ -100,7 +100,7 @@ func (d *commandSyncDb) Parse(args []string) {
func (d *commandSyncDb) Run() error { func (d *commandSyncDb) Run() error {
var drops []string var drops []string
if d.force { if d.force {
drops = getDbDropSql(d.al) drops = getDbDropSQL(d.al)
} }
db := d.al.DB db := d.al.DB
@ -124,7 +124,7 @@ func (d *commandSyncDb) Run() error {
} }
} }
sqls, indexes := getDbCreateSql(d.al) sqls, indexes := getDbCreateSQL(d.al)
tables, err := d.al.DbBaser.GetTables(db) tables, err := d.al.DbBaser.GetTables(db)
if err != nil { if err != nil {
@ -180,7 +180,7 @@ func (d *commandSyncDb) Run() error {
fmt.Printf("create index `%s` for table `%s`\n", idx.Name, idx.Table) fmt.Printf("create index `%s` for table `%s`\n", idx.Name, idx.Table)
} }
query := idx.Sql query := idx.SQL
_, err := db.Exec(query) _, err := db.Exec(query)
if d.verbose { if d.verbose {
fmt.Printf(" %s\n", query) fmt.Printf(" %s\n", query)
@ -203,7 +203,7 @@ func (d *commandSyncDb) Run() error {
queries := []string{sqls[i]} queries := []string{sqls[i]}
for _, idx := range indexes[mi.table] { for _, idx := range indexes[mi.table] {
queries = append(queries, idx.Sql) queries = append(queries, idx.SQL)
} }
for _, query := range queries { for _, query := range queries {
@ -228,12 +228,12 @@ func (d *commandSyncDb) Run() error {
} }
// database creation commander interface implement. // database creation commander interface implement.
type commandSqlAll struct { type commandSQLAll struct {
al *alias al *alias
} }
// parse orm command line arguments. // parse orm command line arguments.
func (d *commandSqlAll) Parse(args []string) { func (d *commandSQLAll) Parse(args []string) {
var name string var name string
flagSet := flag.NewFlagSet("orm command: sqlall", flag.ExitOnError) flagSet := flag.NewFlagSet("orm command: sqlall", flag.ExitOnError)
@ -244,13 +244,13 @@ func (d *commandSqlAll) Parse(args []string) {
} }
// run orm line command. // run orm line command.
func (d *commandSqlAll) Run() error { func (d *commandSQLAll) Run() error {
sqls, indexes := getDbCreateSql(d.al) sqls, indexes := getDbCreateSQL(d.al)
var all []string var all []string
for i, mi := range modelCache.allOrdered() { for i, mi := range modelCache.allOrdered() {
queries := []string{sqls[i]} queries := []string{sqls[i]}
for _, idx := range indexes[mi.table] { for _, idx := range indexes[mi.table] {
queries = append(queries, idx.Sql) queries = append(queries, idx.SQL)
} }
sql := strings.Join(queries, "\n") sql := strings.Join(queries, "\n")
all = append(all, sql) all = append(all, sql)
@ -262,10 +262,10 @@ func (d *commandSqlAll) Run() error {
func init() { func init() {
commands["syncdb"] = new(commandSyncDb) commands["syncdb"] = new(commandSyncDb)
commands["sqlall"] = new(commandSqlAll) commands["sqlall"] = new(commandSQLAll)
} }
// run syncdb command line. // RunSyncdb run syncdb command line.
// name means table's alias name. default is "default". // name means table's alias name. default is "default".
// force means run next sql if the current is error. // force means run next sql if the current is error.
// verbose means show all info when running command or not. // verbose means show all info when running command or not.

View File

@ -23,11 +23,11 @@ import (
type dbIndex struct { type dbIndex struct {
Table string Table string
Name string Name string
Sql string SQL string
} }
// create database drop sql. // create database drop sql.
func getDbDropSql(al *alias) (sqls []string) { func getDbDropSQL(al *alias) (sqls []string) {
if len(modelCache.cache) == 0 { if len(modelCache.cache) == 0 {
fmt.Println("no Model found, need register your model") fmt.Println("no Model found, need register your model")
os.Exit(2) os.Exit(2)
@ -45,13 +45,14 @@ func getDbDropSql(al *alias) (sqls []string) {
func getColumnTyp(al *alias, fi *fieldInfo) (col string) { func getColumnTyp(al *alias, fi *fieldInfo) (col string) {
T := al.DbBaser.DbTypes() T := al.DbBaser.DbTypes()
fieldType := fi.fieldType fieldType := fi.fieldType
fieldSize := fi.size
checkColumn: checkColumn:
switch fieldType { switch fieldType {
case TypeBooleanField: case TypeBooleanField:
col = T["bool"] col = T["bool"]
case TypeCharField: case TypeCharField:
col = fmt.Sprintf(T["string"], fi.size) col = fmt.Sprintf(T["string"], fieldSize)
case TypeTextField: case TypeTextField:
col = T["string-text"] col = T["string-text"]
case TypeDateField: case TypeDateField:
@ -65,7 +66,7 @@ checkColumn:
case TypeIntegerField: case TypeIntegerField:
col = T["int32"] col = T["int32"]
case TypeBigIntegerField: case TypeBigIntegerField:
if al.Driver == DR_Sqlite { if al.Driver == DRSqlite {
fieldType = TypeIntegerField fieldType = TypeIntegerField
goto checkColumn goto checkColumn
} }
@ -89,6 +90,7 @@ checkColumn:
} }
case RelForeignKey, RelOneToOne: case RelForeignKey, RelOneToOne:
fieldType = fi.relModelInfo.fields.pk.fieldType fieldType = fi.relModelInfo.fields.pk.fieldType
fieldSize = fi.relModelInfo.fields.pk.size
goto checkColumn goto checkColumn
} }
@ -104,11 +106,15 @@ func getColumnAddQuery(al *alias, fi *fieldInfo) string {
typ += " " + "NOT NULL" typ += " " + "NOT NULL"
} }
return fmt.Sprintf("ALTER TABLE %s%s%s ADD COLUMN %s%s%s %s", Q, fi.mi.table, Q, Q, fi.column, Q, typ) return fmt.Sprintf("ALTER TABLE %s%s%s ADD COLUMN %s%s%s %s %s",
Q, fi.mi.table, Q,
Q, fi.column, Q,
typ, getColumnDefault(fi),
)
} }
// create database creation string. // create database creation string.
func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex) { func getDbCreateSQL(al *alias) (sqls []string, tableIndexes map[string][]dbIndex) {
if len(modelCache.cache) == 0 { if len(modelCache.cache) == 0 {
fmt.Println("no Model found, need register your model") fmt.Println("no Model found, need register your model")
os.Exit(2) os.Exit(2)
@ -138,7 +144,7 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex
if fi.auto { if fi.auto {
switch al.Driver { switch al.Driver {
case DR_Sqlite, DR_Postgres: case DRSqlite, DRPostgres:
column += T["auto"] column += T["auto"]
default: default:
column += col + " " + T["auto"] column += col + " " + T["auto"]
@ -156,6 +162,9 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex
// column += " DEFAULT " + fi.initial.String() // column += " DEFAULT " + fi.initial.String()
//} //}
// Append attribute DEFAULT
column += getColumnDefault(fi)
if fi.unique { if fi.unique {
column += " " + "UNIQUE" column += " " + "UNIQUE"
} }
@ -194,7 +203,7 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex
sql += strings.Join(columns, ",\n") sql += strings.Join(columns, ",\n")
sql += "\n)" sql += "\n)"
if al.Driver == DR_MySQL { if al.Driver == DRMySQL {
var engine string var engine string
if mi.model != nil { if mi.model != nil {
engine = getTableEngine(mi.addrField) engine = getTableEngine(mi.addrField)
@ -230,7 +239,7 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex
index := dbIndex{} index := dbIndex{}
index.Table = mi.table index.Table = mi.table
index.Name = name index.Name = name
index.Sql = sql index.SQL = sql
tableIndexes[mi.table] = append(tableIndexes[mi.table], index) tableIndexes[mi.table] = append(tableIndexes[mi.table], index)
} }
@ -239,3 +248,47 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex
return return
} }
// Get string value for the attribute "DEFAULT" for the CREATE, ALTER commands
func getColumnDefault(fi *fieldInfo) string {
var (
v, t, d string
)
// Skip default attribute if field is in relations
if fi.rel || fi.reverse {
return v
}
t = " DEFAULT '%s' "
// These defaults will be useful if there no config value orm:"default" and NOT NULL is on
switch fi.fieldType {
case TypeDateField, TypeDateTimeField, TypeTextField:
return v
case TypeBitField, TypeSmallIntegerField, TypeIntegerField,
TypeBigIntegerField, TypePositiveBitField, TypePositiveSmallIntegerField,
TypePositiveIntegerField, TypePositiveBigIntegerField, TypeFloatField,
TypeDecimalField:
t = " DEFAULT %s "
d = "0"
case TypeBooleanField:
t = " DEFAULT %s "
d = "FALSE"
}
if fi.colDefault {
if !fi.initial.Exist() {
v = fmt.Sprintf(t, "")
} else {
v = fmt.Sprintf(t, fi.initial.String())
}
} else {
if !fi.null {
v = fmt.Sprintf(t, d)
}
}
return v
}

203
orm/db.go
View File

@ -24,12 +24,13 @@ import (
) )
const ( const (
format_Date = "2006-01-02" formatDate = "2006-01-02"
format_DateTime = "2006-01-02 15:04:05" formatDateTime = "2006-01-02 15:04:05"
) )
var ( var (
ErrMissPK = errors.New("missed pk value") // missing pk error // ErrMissPK missing pk error
ErrMissPK = errors.New("missed pk value")
) )
var ( var (
@ -44,6 +45,8 @@ var (
"gte": true, "gte": true,
"lt": true, "lt": true,
"lte": true, "lte": true,
"eq": true,
"nq": true,
"startswith": true, "startswith": true,
"endswith": true, "endswith": true,
"istartswith": true, "istartswith": true,
@ -110,7 +113,7 @@ func (d *dbBase) collectFieldValue(mi *modelInfo, fi *fieldInfo, ind reflect.Val
if fi.pk { if fi.pk {
_, value, _ = getExistPk(mi, ind) _, value, _ = getExistPk(mi, ind)
} else { } else {
field := ind.Field(fi.fieldIndex) field := ind.FieldByIndex(fi.fieldIndex)
if fi.isFielder { if fi.isFielder {
f := field.Addr().Interface().(Fielder) f := field.Addr().Interface().(Fielder)
value = f.RawValue() value = f.RawValue()
@ -214,14 +217,14 @@ func (d *dbBase) collectFieldValue(mi *modelInfo, fi *fieldInfo, ind reflect.Val
} }
} }
if fi.null == false && value == nil { if fi.null == false && value == nil {
return nil, errors.New(fmt.Sprintf("field `%s` cannot be NULL", fi.fullName)) return nil, fmt.Errorf("field `%s` cannot be NULL", fi.fullName)
} }
} }
} }
} }
switch fi.fieldType { switch fi.fieldType {
case TypeDateField, TypeDateTimeField: case TypeDateField, TypeDateTimeField:
if fi.auto_now || fi.auto_now_add && insert { if fi.autoNow || fi.autoNowAdd && insert {
if insert { if insert {
if t, ok := value.(time.Time); ok && !t.IsZero() { if t, ok := value.(time.Time); ok && !t.IsZero() {
break break
@ -280,13 +283,12 @@ func (d *dbBase) InsertStmt(stmt stmtQuerier, mi *modelInfo, ind reflect.Value,
var id int64 var id int64
err := row.Scan(&id) err := row.Scan(&id)
return id, err return id, err
} else {
if res, err := stmt.Exec(values...); err == nil {
return res.LastInsertId()
} else {
return 0, err
}
} }
res, err := stmt.Exec(values...)
if err == nil {
return res.LastInsertId()
}
return 0, err
} }
// query sql ,read records and persist in dbBaser. // query sql ,read records and persist in dbBaser.
@ -324,7 +326,7 @@ func (d *dbBase) Read(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.Lo
query := fmt.Sprintf("SELECT %s%s%s FROM %s%s%s WHERE %s%s%s = ?", Q, sels, Q, Q, mi.table, Q, Q, wheres, Q) query := fmt.Sprintf("SELECT %s%s%s FROM %s%s%s WHERE %s%s%s = ?", Q, sels, Q, Q, mi.table, Q, Q, wheres, Q)
refs := make([]interface{}, colsNum) refs := make([]interface{}, colsNum)
for i, _ := range refs { for i := range refs {
var ref interface{} var ref interface{}
refs[i] = &ref refs[i] = &ref
} }
@ -337,15 +339,11 @@ func (d *dbBase) Read(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.Lo
return ErrNoRows return ErrNoRows
} }
return err return err
} else {
elm := reflect.New(mi.addrField.Elem().Type())
mind := reflect.Indirect(elm)
d.setColsValues(mi, &mind, mi.fields.dbcols, refs, tz)
ind.Set(mind)
} }
elm := reflect.New(mi.addrField.Elem().Type())
mind := reflect.Indirect(elm)
d.setColsValues(mi, &mind, mi.fields.dbcols, refs, tz)
ind.Set(mind)
return nil return nil
} }
@ -423,7 +421,7 @@ func (d *dbBase) InsertValue(q dbQuerier, mi *modelInfo, isMulti bool, names []s
Q := d.ins.TableQuote() Q := d.ins.TableQuote()
marks := make([]string, len(names)) marks := make([]string, len(names))
for i, _ := range marks { for i := range marks {
marks[i] = "?" marks[i] = "?"
} }
@ -442,20 +440,19 @@ func (d *dbBase) InsertValue(q dbQuerier, mi *modelInfo, isMulti bool, names []s
d.ins.ReplaceMarks(&query) d.ins.ReplaceMarks(&query)
if isMulti || !d.ins.HasReturningID(mi, &query) { if isMulti || !d.ins.HasReturningID(mi, &query) {
if res, err := q.Exec(query, values...); err == nil { res, err := q.Exec(query, values...)
if err == nil {
if isMulti { if isMulti {
return res.RowsAffected() return res.RowsAffected()
} }
return res.LastInsertId() return res.LastInsertId()
} else {
return 0, err
} }
} else { return 0, err
row := q.QueryRow(query, values...)
var id int64
err := row.Scan(&id)
return id, err
} }
row := q.QueryRow(query, values...)
var id int64
err := row.Scan(&id)
return id, err
} }
// execute update sql dbQuerier with given struct reflect.Value. // execute update sql dbQuerier with given struct reflect.Value.
@ -491,11 +488,11 @@ func (d *dbBase) Update(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.
d.ins.ReplaceMarks(&query) d.ins.ReplaceMarks(&query)
if res, err := q.Exec(query, setValues...); err == nil { res, err := q.Exec(query, setValues...)
if err == nil {
return res.RowsAffected() return res.RowsAffected()
} else {
return 0, err
} }
return 0, err
} }
// execute delete sql dbQuerier with given struct reflect.Value. // execute delete sql dbQuerier with given struct reflect.Value.
@ -511,33 +508,28 @@ func (d *dbBase) Delete(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.
query := fmt.Sprintf("DELETE FROM %s%s%s WHERE %s%s%s = ?", Q, mi.table, Q, Q, pkName, Q) query := fmt.Sprintf("DELETE FROM %s%s%s WHERE %s%s%s = ?", Q, mi.table, Q, Q, pkName, Q)
d.ins.ReplaceMarks(&query) d.ins.ReplaceMarks(&query)
res, err := q.Exec(query, pkValue)
if res, err := q.Exec(query, pkValue); err == nil { if err == nil {
num, err := res.RowsAffected() num, err := res.RowsAffected()
if err != nil { if err != nil {
return 0, err return 0, err
} }
if num > 0 { if num > 0 {
if mi.fields.pk.auto { if mi.fields.pk.auto {
if mi.fields.pk.fieldType&IsPostiveIntegerField > 0 { if mi.fields.pk.fieldType&IsPostiveIntegerField > 0 {
ind.Field(mi.fields.pk.fieldIndex).SetUint(0) ind.FieldByIndex(mi.fields.pk.fieldIndex).SetUint(0)
} else { } else {
ind.Field(mi.fields.pk.fieldIndex).SetInt(0) ind.FieldByIndex(mi.fields.pk.fieldIndex).SetInt(0)
} }
} }
err := d.deleteRels(q, mi, []interface{}{pkValue}, tz) err := d.deleteRels(q, mi, []interface{}{pkValue}, tz)
if err != nil { if err != nil {
return num, err return num, err
} }
} }
return num, err return num, err
} else {
return 0, err
} }
return 0, err
} }
// update table-related record by querySet. // update table-related record by querySet.
@ -563,11 +555,11 @@ func (d *dbBase) UpdateBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con
tables.parseRelated(qs.related, qs.relDepth) tables.parseRelated(qs.related, qs.relDepth)
} }
where, args := tables.getCondSql(cond, false, tz) where, args := tables.getCondSQL(cond, false, tz)
values = append(values, args...) values = append(values, args...)
join := tables.getJoinSql() join := tables.getJoinSQL()
var query, T string var query, T string
@ -583,13 +575,13 @@ func (d *dbBase) UpdateBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con
col := fmt.Sprintf("%s%s%s%s", T, Q, v, Q) col := fmt.Sprintf("%s%s%s%s", T, Q, v, Q)
if c, ok := values[i].(colValue); ok { if c, ok := values[i].(colValue); ok {
switch c.opt { switch c.opt {
case Col_Add: case ColAdd:
cols = append(cols, col+" = "+col+" + ?") cols = append(cols, col+" = "+col+" + ?")
case Col_Minus: case ColMinus:
cols = append(cols, col+" = "+col+" - ?") cols = append(cols, col+" = "+col+" - ?")
case Col_Multiply: case ColMultiply:
cols = append(cols, col+" = "+col+" * ?") cols = append(cols, col+" = "+col+" * ?")
case Col_Except: case ColExcept:
cols = append(cols, col+" = "+col+" / ?") cols = append(cols, col+" = "+col+" / ?")
} }
values[i] = c.value values[i] = c.value
@ -608,12 +600,11 @@ 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...)
if res, err := q.Exec(query, values...); err == nil { if err == nil {
return res.RowsAffected() return res.RowsAffected()
} else {
return 0, err
} }
return 0, err
} }
// delete related records. // delete related records.
@ -622,23 +613,23 @@ func (d *dbBase) deleteRels(q dbQuerier, mi *modelInfo, args []interface{}, tz *
for _, fi := range mi.fields.fieldsReverse { for _, fi := range mi.fields.fieldsReverse {
fi = fi.reverseFieldInfo fi = fi.reverseFieldInfo
switch fi.onDelete { switch fi.onDelete {
case od_CASCADE: case odCascade:
cond := NewCondition().And(fmt.Sprintf("%s__in", fi.name), args...) cond := NewCondition().And(fmt.Sprintf("%s__in", fi.name), args...)
_, err := d.DeleteBatch(q, nil, fi.mi, cond, tz) _, err := d.DeleteBatch(q, nil, fi.mi, cond, tz)
if err != nil { if err != nil {
return err return err
} }
case od_SET_DEFAULT, od_SET_NULL: case odSetDefault, odSetNULL:
cond := NewCondition().And(fmt.Sprintf("%s__in", fi.name), args...) cond := NewCondition().And(fmt.Sprintf("%s__in", fi.name), args...)
params := Params{fi.column: nil} params := Params{fi.column: nil}
if fi.onDelete == od_SET_DEFAULT { if fi.onDelete == odSetDefault {
params[fi.column] = fi.initial.String() params[fi.column] = fi.initial.String()
} }
_, err := d.UpdateBatch(q, nil, fi.mi, cond, params, tz) _, err := d.UpdateBatch(q, nil, fi.mi, cond, params, tz)
if err != nil { if err != nil {
return err return err
} }
case od_DO_NOTHING: case odDoNothing:
} }
} }
return nil return nil
@ -659,8 +650,8 @@ func (d *dbBase) DeleteBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con
Q := d.ins.TableQuote() Q := d.ins.TableQuote()
where, args := tables.getCondSql(cond, false, tz) where, args := tables.getCondSQL(cond, false, tz)
join := tables.getJoinSql() join := tables.getJoinSQL()
cols := fmt.Sprintf("T0.%s%s%s", Q, mi.fields.pk.column, Q) cols := fmt.Sprintf("T0.%s%s%s", Q, mi.fields.pk.column, Q)
query := fmt.Sprintf("SELECT %s FROM %s%s%s T0 %s%s", cols, Q, mi.table, Q, join, where) query := fmt.Sprintf("SELECT %s FROM %s%s%s T0 %s%s", cols, Q, mi.table, Q, join, where)
@ -668,16 +659,14 @@ func (d *dbBase) DeleteBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con
d.ins.ReplaceMarks(&query) d.ins.ReplaceMarks(&query)
var rs *sql.Rows var rs *sql.Rows
if r, err := q.Query(query, args...); err != nil { r, err := q.Query(query, args...)
if err != nil {
return 0, err return 0, err
} else {
rs = r
} }
rs = r
defer rs.Close() defer rs.Close()
var ref interface{} var ref interface{}
args = make([]interface{}, 0) args = make([]interface{}, 0)
cnt := 0 cnt := 0
for rs.Next() { for rs.Next() {
@ -693,31 +682,28 @@ func (d *dbBase) DeleteBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con
} }
marks := make([]string, len(args)) marks := make([]string, len(args))
for i, _ := range marks { for i := range marks {
marks[i] = "?" marks[i] = "?"
} }
sql := fmt.Sprintf("IN (%s)", strings.Join(marks, ", ")) sql := 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, sql)
d.ins.ReplaceMarks(&query) d.ins.ReplaceMarks(&query)
res, err := q.Exec(query, args...)
if res, err := q.Exec(query, args...); err == nil { if err == nil {
num, err := res.RowsAffected() num, err := res.RowsAffected()
if err != nil { if err != nil {
return 0, err return 0, err
} }
if num > 0 { if num > 0 {
err := d.deleteRels(q, mi, args, tz) err := d.deleteRels(q, mi, args, tz)
if err != nil { if err != nil {
return num, err return num, err
} }
} }
return num, nil return num, nil
} else {
return 0, err
} }
return 0, err
} }
// read related records. // read related records.
@ -799,10 +785,11 @@ func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condi
tables := newDbTables(mi, d.ins) tables := newDbTables(mi, d.ins)
tables.parseRelated(qs.related, qs.relDepth) tables.parseRelated(qs.related, qs.relDepth)
where, args := tables.getCondSql(cond, false, tz) where, args := tables.getCondSQL(cond, false, tz)
orderBy := tables.getOrderSql(qs.orders) groupBy := tables.getGroupSQL(qs.groups)
limit := tables.getLimitSql(mi, offset, rlimit) orderBy := tables.getOrderSQL(qs.orders)
join := tables.getJoinSql() limit := tables.getLimitSQL(mi, offset, rlimit)
join := tables.getJoinSQL()
for _, tbl := range tables.tables { for _, tbl := range tables.tables {
if tbl.sel { if tbl.sel {
@ -812,19 +799,23 @@ func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condi
} }
} }
query := fmt.Sprintf("SELECT %s FROM %s%s%s T0 %s%s%s%s", sels, Q, mi.table, Q, join, where, orderBy, limit) sqlSelect := "SELECT"
if qs.distinct {
sqlSelect += " DISTINCT"
}
query := fmt.Sprintf("%s %s FROM %s%s%s T0 %s%s%s%s%s", sqlSelect, sels, Q, mi.table, Q, join, where, groupBy, orderBy, limit)
d.ins.ReplaceMarks(&query) d.ins.ReplaceMarks(&query)
var rs *sql.Rows var rs *sql.Rows
if r, err := q.Query(query, args...); err != nil { r, err := q.Query(query, args...)
if err != nil {
return 0, err return 0, err
} else {
rs = r
} }
rs = r
refs := make([]interface{}, colsNum) refs := make([]interface{}, colsNum)
for i, _ := range refs { for i := range refs {
var ref interface{} var ref interface{}
refs[i] = &ref refs[i] = &ref
} }
@ -868,13 +859,13 @@ func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condi
mmi = fi.relModelInfo mmi = fi.relModelInfo
field := last field := last
if last.Kind() != reflect.Invalid { if last.Kind() != reflect.Invalid {
field = reflect.Indirect(last.Field(fi.fieldIndex)) field = reflect.Indirect(last.FieldByIndex(fi.fieldIndex))
if field.IsValid() { if field.IsValid() {
d.setColsValues(mmi, &field, mmi.fields.dbcols, trefs[:len(mmi.fields.dbcols)], tz) d.setColsValues(mmi, &field, mmi.fields.dbcols, trefs[:len(mmi.fields.dbcols)], tz)
for _, fi := range mmi.fields.fieldsReverse { for _, fi := range mmi.fields.fieldsReverse {
if fi.inModel && fi.reverseFieldInfo.mi == lastm { if fi.inModel && fi.reverseFieldInfo.mi == lastm {
if fi.reverseFieldInfo != nil { if fi.reverseFieldInfo != nil {
f := field.Field(fi.fieldIndex) f := field.FieldByIndex(fi.fieldIndex)
if f.Kind() == reflect.Ptr { if f.Kind() == reflect.Ptr {
f.Set(last.Addr()) f.Set(last.Addr())
} }
@ -935,9 +926,9 @@ func (d *dbBase) Count(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition
tables := newDbTables(mi, d.ins) tables := newDbTables(mi, d.ins)
tables.parseRelated(qs.related, qs.relDepth) tables.parseRelated(qs.related, qs.relDepth)
where, args := tables.getCondSql(cond, false, tz) where, args := tables.getCondSQL(cond, false, tz)
tables.getOrderSql(qs.orders) tables.getOrderSQL(qs.orders)
join := tables.getJoinSql() join := tables.getJoinSQL()
Q := d.ins.TableQuote() Q := d.ins.TableQuote()
@ -952,7 +943,7 @@ func (d *dbBase) Count(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition
} }
// generate sql with replacing operator string placeholders and replaced values. // generate sql with replacing operator string placeholders and replaced values.
func (d *dbBase) GenerateOperatorSql(mi *modelInfo, fi *fieldInfo, operator string, args []interface{}, tz *time.Location) (string, []interface{}) { func (d *dbBase) GenerateOperatorSQL(mi *modelInfo, fi *fieldInfo, operator string, args []interface{}, tz *time.Location) (string, []interface{}) {
sql := "" sql := ""
params := getFlatParams(fi, args, tz) params := getFlatParams(fi, args, tz)
@ -964,7 +955,7 @@ func (d *dbBase) GenerateOperatorSql(mi *modelInfo, fi *fieldInfo, operator stri
switch operator { switch operator {
case "in": case "in":
marks := make([]string, len(params)) marks := make([]string, len(params))
for i, _ := range marks { for i := range marks {
marks[i] = "?" marks[i] = "?"
} }
sql = fmt.Sprintf("IN (%s)", strings.Join(marks, ", ")) sql = fmt.Sprintf("IN (%s)", strings.Join(marks, ", "))
@ -977,7 +968,7 @@ func (d *dbBase) GenerateOperatorSql(mi *modelInfo, fi *fieldInfo, operator stri
if len(params) > 1 { if len(params) > 1 {
panic(fmt.Errorf("operator `%s` need 1 args not %d", operator, len(params))) panic(fmt.Errorf("operator `%s` need 1 args not %d", operator, len(params)))
} }
sql = d.ins.OperatorSql(operator) sql = d.ins.OperatorSQL(operator)
switch operator { switch operator {
case "exact": case "exact":
if arg == nil { if arg == nil {
@ -1023,7 +1014,7 @@ func (d *dbBase) setColsValues(mi *modelInfo, ind *reflect.Value, cols []string,
fi := mi.fields.GetByColumn(column) fi := mi.fields.GetByColumn(column)
field := ind.Field(fi.fieldIndex) field := ind.FieldByIndex(fi.fieldIndex)
value, err := d.convertValueFromDB(fi, val, tz) value, err := d.convertValueFromDB(fi, val, tz)
if err != nil { if err != nil {
@ -1105,12 +1096,12 @@ setValue:
) )
if len(s) >= 19 { if len(s) >= 19 {
s = s[:19] s = s[:19]
t, err = time.ParseInLocation(format_DateTime, s, tz) t, err = time.ParseInLocation(formatDateTime, s, tz)
} else { } else {
if len(s) > 10 { if len(s) > 10 {
s = s[:10] s = s[:10]
} }
t, err = time.ParseInLocation(format_Date, s, tz) t, err = time.ParseInLocation(formatDate, s, tz)
} }
t = t.In(DefaultTimeLoc) t = t.In(DefaultTimeLoc)
@ -1359,7 +1350,7 @@ setValue:
fieldType = fi.relModelInfo.fields.pk.fieldType fieldType = fi.relModelInfo.fields.pk.fieldType
mf := reflect.New(fi.relModelInfo.addrField.Elem().Type()) mf := reflect.New(fi.relModelInfo.addrField.Elem().Type())
field.Set(mf) field.Set(mf)
f := mf.Elem().Field(fi.relModelInfo.fields.pk.fieldIndex) f := mf.Elem().FieldByIndex(fi.relModelInfo.fields.pk.fieldIndex)
field = f field = f
goto setValue goto setValue
} }
@ -1441,26 +1432,24 @@ func (d *dbBase) ReadValues(q dbQuerier, qs *querySet, mi *modelInfo, cond *Cond
} }
} }
where, args := tables.getCondSql(cond, false, tz) where, args := tables.getCondSQL(cond, false, tz)
orderBy := tables.getOrderSql(qs.orders) groupBy := tables.getGroupSQL(qs.groups)
limit := tables.getLimitSql(mi, qs.offset, qs.limit) orderBy := tables.getOrderSQL(qs.orders)
join := tables.getJoinSql() limit := tables.getLimitSQL(mi, qs.offset, qs.limit)
join := tables.getJoinSQL()
sels := strings.Join(cols, ", ") sels := strings.Join(cols, ", ")
query := fmt.Sprintf("SELECT %s FROM %s%s%s T0 %s%s%s%s", sels, Q, mi.table, Q, join, where, orderBy, limit) query := fmt.Sprintf("SELECT %s FROM %s%s%s T0 %s%s%s%s%s", sels, Q, mi.table, Q, join, where, groupBy, orderBy, limit)
d.ins.ReplaceMarks(&query) d.ins.ReplaceMarks(&query)
var rs *sql.Rows rs, err := q.Query(query, args...)
if r, err := q.Query(query, args...); err != nil { if err != nil {
return 0, err return 0, err
} else {
rs = r
} }
refs := make([]interface{}, len(cols)) refs := make([]interface{}, len(cols))
for i, _ := range refs { for i := range refs {
var ref interface{} var ref interface{}
refs[i] = &ref refs[i] = &ref
} }
@ -1473,11 +1462,11 @@ func (d *dbBase) ReadValues(q dbQuerier, qs *querySet, mi *modelInfo, cond *Cond
) )
for rs.Next() { for rs.Next() {
if cnt == 0 { if cnt == 0 {
if cols, err := rs.Columns(); err != nil { cols, err := rs.Columns()
if err != nil {
return 0, err return 0, err
} else {
columns = cols
} }
columns = cols
} }
if err := rs.Scan(refs...); err != nil { if err := rs.Scan(refs...); err != nil {
@ -1641,7 +1630,7 @@ func (d *dbBase) GetColumns(db dbQuerier, table string) (map[string][3]string, e
} }
// not implement. // not implement.
func (d *dbBase) OperatorSql(operator string) string { func (d *dbBase) OperatorSQL(operator string) string {
panic(ErrNotImplement) panic(ErrNotImplement)
} }

View File

@ -22,15 +22,17 @@ import (
"time" "time"
) )
// database driver constant int. // DriverType database driver constant int.
type DriverType int type DriverType int
// Enum the Database driver
const ( const (
_ DriverType = iota // int enum type _ DriverType = iota // int enum type
DR_MySQL // mysql DRMySQL // mysql
DR_Sqlite // sqlite DRSqlite // sqlite
DR_Oracle // oracle DROracle // oracle
DR_Postgres // pgsql DRPostgres // pgsql
DRTiDB // TiDB
) )
// database driver string. // database driver string.
@ -53,15 +55,18 @@ var _ Driver = new(driver)
var ( var (
dataBaseCache = &_dbCache{cache: make(map[string]*alias)} dataBaseCache = &_dbCache{cache: make(map[string]*alias)}
drivers = map[string]DriverType{ drivers = map[string]DriverType{
"mysql": DR_MySQL, "mysql": DRMySQL,
"postgres": DR_Postgres, "postgres": DRPostgres,
"sqlite3": DR_Sqlite, "sqlite3": DRSqlite,
"tidb": DRTiDB,
"oracle": DROracle,
} }
dbBasers = map[DriverType]dbBaser{ dbBasers = map[DriverType]dbBaser{
DR_MySQL: newdbBaseMysql(), DRMySQL: newdbBaseMysql(),
DR_Sqlite: newdbBaseSqlite(), DRSqlite: newdbBaseSqlite(),
DR_Oracle: newdbBaseMysql(), DROracle: newdbBaseOracle(),
DR_Postgres: newdbBasePostgres(), DRPostgres: newdbBasePostgres(),
DRTiDB: newdbBaseTidb(),
} }
) )
@ -119,7 +124,7 @@ func detectTZ(al *alias) {
} }
switch al.Driver { switch al.Driver {
case DR_MySQL: case DRMySQL:
row := al.DB.QueryRow("SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP)") row := al.DB.QueryRow("SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP)")
var tz string var tz string
row.Scan(&tz) row.Scan(&tz)
@ -147,10 +152,10 @@ func detectTZ(al *alias) {
al.Engine = "INNODB" al.Engine = "INNODB"
} }
case DR_Sqlite: case DRSqlite, DROracle:
al.TZ = time.UTC al.TZ = time.UTC
case DR_Postgres: case DRPostgres:
row := al.DB.QueryRow("SELECT current_setting('TIMEZONE')") row := al.DB.QueryRow("SELECT current_setting('TIMEZONE')")
var tz string var tz string
row.Scan(&tz) row.Scan(&tz)
@ -188,12 +193,13 @@ func addAliasWthDB(aliasName, driverName string, db *sql.DB) (*alias, error) {
return al, nil return al, nil
} }
// AddAliasWthDB add a aliasName for the drivename
func AddAliasWthDB(aliasName, driverName string, db *sql.DB) error { func AddAliasWthDB(aliasName, driverName string, db *sql.DB) error {
_, err := addAliasWthDB(aliasName, driverName, db) _, err := addAliasWthDB(aliasName, driverName, db)
return err return err
} }
// Setting the database connect params. Use the database driver self dataSource args. // RegisterDataBase Setting the database connect params. Use the database driver self dataSource args.
func RegisterDataBase(aliasName, driverName, dataSource string, params ...int) error { func RegisterDataBase(aliasName, driverName, dataSource string, params ...int) error {
var ( var (
err error err error
@ -236,7 +242,7 @@ end:
return err return err
} }
// Register a database driver use specify driver name, this can be definition the driver is which database type. // RegisterDriver Register a database driver use specify driver name, this can be definition the driver is which database type.
func RegisterDriver(driverName string, typ DriverType) error { func RegisterDriver(driverName string, typ DriverType) error {
if t, ok := drivers[driverName]; ok == false { if t, ok := drivers[driverName]; ok == false {
drivers[driverName] = typ drivers[driverName] = typ
@ -248,7 +254,7 @@ func RegisterDriver(driverName string, typ DriverType) error {
return nil return nil
} }
// Change the database default used timezone // SetDataBaseTZ Change the database default used timezone
func SetDataBaseTZ(aliasName string, tz *time.Location) error { func SetDataBaseTZ(aliasName string, tz *time.Location) error {
if al, ok := dataBaseCache.get(aliasName); ok { if al, ok := dataBaseCache.get(aliasName); ok {
al.TZ = tz al.TZ = tz
@ -258,14 +264,14 @@ func SetDataBaseTZ(aliasName string, tz *time.Location) error {
return nil return nil
} }
// Change the max idle conns for *sql.DB, use specify database alias name // SetMaxIdleConns Change the max idle conns for *sql.DB, use specify database alias name
func SetMaxIdleConns(aliasName string, maxIdleConns int) { func SetMaxIdleConns(aliasName string, maxIdleConns int) {
al := getDbAlias(aliasName) al := getDbAlias(aliasName)
al.MaxIdleConns = maxIdleConns al.MaxIdleConns = maxIdleConns
al.DB.SetMaxIdleConns(maxIdleConns) al.DB.SetMaxIdleConns(maxIdleConns)
} }
// Change the max open conns for *sql.DB, use specify database alias name // SetMaxOpenConns Change the max open conns for *sql.DB, use specify database alias name
func SetMaxOpenConns(aliasName string, maxOpenConns int) { func SetMaxOpenConns(aliasName string, maxOpenConns int) {
al := getDbAlias(aliasName) al := getDbAlias(aliasName)
al.MaxOpenConns = maxOpenConns al.MaxOpenConns = maxOpenConns
@ -275,7 +281,7 @@ func SetMaxOpenConns(aliasName string, maxOpenConns int) {
} }
} }
// Get *sql.DB from registered database by db alias name. // GetDB Get *sql.DB from registered database by db alias name.
// Use "default" as alias name if you not set. // Use "default" as alias name if you not set.
func GetDB(aliasNames ...string) (*sql.DB, error) { func GetDB(aliasNames ...string) (*sql.DB, error) {
var name string var name string
@ -284,9 +290,9 @@ func GetDB(aliasNames ...string) (*sql.DB, error) {
} else { } else {
name = "default" name = "default"
} }
if al, ok := dataBaseCache.get(name); ok { al, ok := dataBaseCache.get(name)
if ok {
return al.DB, nil return al.DB, nil
} else {
return nil, fmt.Errorf("DataBase of alias name `%s` not found\n", name)
} }
return nil, fmt.Errorf("DataBase of alias name `%s` not found\n", name)
} }

View File

@ -30,6 +30,8 @@ var mysqlOperators = map[string]string{
"gte": ">= ?", "gte": ">= ?",
"lt": "< ?", "lt": "< ?",
"lte": "<= ?", "lte": "<= ?",
"eq": "= ?",
"ne": "!= ?",
"startswith": "LIKE BINARY ?", "startswith": "LIKE BINARY ?",
"endswith": "LIKE BINARY ?", "endswith": "LIKE BINARY ?",
"istartswith": "LIKE ?", "istartswith": "LIKE ?",
@ -65,7 +67,7 @@ type dbBaseMysql struct {
var _ dbBaser = new(dbBaseMysql) var _ dbBaser = new(dbBaseMysql)
// get mysql operator. // get mysql operator.
func (d *dbBaseMysql) OperatorSql(operator string) string { func (d *dbBaseMysql) OperatorSQL(operator string) string {
return mysqlOperators[operator] return mysqlOperators[operator]
} }

View File

@ -14,6 +14,41 @@
package orm package orm
import (
"fmt"
"strings"
)
// oracle operators.
var oracleOperators = map[string]string{
"exact": "= ?",
"gt": "> ?",
"gte": ">= ?",
"lt": "< ?",
"lte": "<= ?",
"//iendswith": "LIKE ?",
}
// oracle column field types.
var oracleTypes = map[string]string{
"pk": "NOT NULL PRIMARY KEY",
"bool": "bool",
"string": "VARCHAR2(%d)",
"string-text": "VARCHAR2(%d)",
"time.Time-date": "DATE",
"time.Time": "TIMESTAMP",
"int8": "INTEGER",
"int16": "INTEGER",
"int32": "INTEGER",
"int64": "INTEGER",
"uint8": "INTEGER",
"uint16": "INTEGER",
"uint32": "INTEGER",
"uint64": "INTEGER",
"float64": "NUMBER",
"float64-decimal": "NUMBER(%d, %d)",
}
// oracle dbBaser // oracle dbBaser
type dbBaseOracle struct { type dbBaseOracle struct {
dbBase dbBase
@ -27,3 +62,35 @@ func newdbBaseOracle() dbBaser {
b.ins = b b.ins = b
return b return b
} }
// OperatorSQL get oracle operator.
func (d *dbBaseOracle) OperatorSQL(operator string) string {
return oracleOperators[operator]
}
// DbTypes get oracle table field types.
func (d *dbBaseOracle) DbTypes() map[string]string {
return oracleTypes
}
//ShowTablesQuery show all the tables in database
func (d *dbBaseOracle) ShowTablesQuery() string {
return "SELECT TABLE_NAME FROM USER_TABLES"
}
// Oracle
func (d *dbBaseOracle) ShowColumnsQuery(table string) string {
return fmt.Sprintf("SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS "+
"WHERE TABLE_NAME ='%s'", strings.ToUpper(table))
}
// check index is exist
func (d *dbBaseOracle) IndexExists(db dbQuerier, table string, name string) bool {
row := db.QueryRow("SELECT COUNT(*) FROM USER_IND_COLUMNS, USER_INDEXES "+
"WHERE USER_IND_COLUMNS.INDEX_NAME = USER_INDEXES.INDEX_NAME "+
"AND USER_IND_COLUMNS.TABLE_NAME = ? AND USER_IND_COLUMNS.INDEX_NAME = ?", strings.ToUpper(table), strings.ToUpper(name))
var cnt int
row.Scan(&cnt)
return cnt > 0
}

View File

@ -29,6 +29,8 @@ var postgresOperators = map[string]string{
"gte": ">= ?", "gte": ">= ?",
"lt": "< ?", "lt": "< ?",
"lte": "<= ?", "lte": "<= ?",
"eq": "= ?",
"ne": "!= ?",
"startswith": "LIKE ?", "startswith": "LIKE ?",
"endswith": "LIKE ?", "endswith": "LIKE ?",
"istartswith": "LIKE UPPER(?)", "istartswith": "LIKE UPPER(?)",
@ -64,7 +66,7 @@ type dbBasePostgres struct {
var _ dbBaser = new(dbBasePostgres) var _ dbBaser = new(dbBasePostgres)
// get postgresql operator. // get postgresql operator.
func (d *dbBasePostgres) OperatorSql(operator string) string { func (d *dbBasePostgres) OperatorSQL(operator string) string {
return postgresOperators[operator] return postgresOperators[operator]
} }
@ -99,7 +101,7 @@ func (d *dbBasePostgres) ReplaceMarks(query *string) {
num := 0 num := 0
for _, c := range q { for _, c := range q {
if c == '?' { if c == '?' {
num += 1 num++
} }
} }
if num == 0 { if num == 0 {
@ -112,7 +114,7 @@ func (d *dbBasePostgres) ReplaceMarks(query *string) {
if c == '?' { if c == '?' {
data = append(data, '$') data = append(data, '$')
data = append(data, []byte(strconv.Itoa(num))...) data = append(data, []byte(strconv.Itoa(num))...)
num += 1 num++
} else { } else {
data = append(data, c) data = append(data, c)
} }

View File

@ -29,6 +29,8 @@ var sqliteOperators = map[string]string{
"gte": ">= ?", "gte": ">= ?",
"lt": "< ?", "lt": "< ?",
"lte": "<= ?", "lte": "<= ?",
"eq": "= ?",
"ne": "!= ?",
"startswith": "LIKE ? ESCAPE '\\'", "startswith": "LIKE ? ESCAPE '\\'",
"endswith": "LIKE ? ESCAPE '\\'", "endswith": "LIKE ? ESCAPE '\\'",
"istartswith": "LIKE ? ESCAPE '\\'", "istartswith": "LIKE ? ESCAPE '\\'",
@ -64,7 +66,7 @@ type dbBaseSqlite struct {
var _ dbBaser = new(dbBaseSqlite) var _ dbBaser = new(dbBaseSqlite)
// get sqlite operator. // get sqlite operator.
func (d *dbBaseSqlite) OperatorSql(operator string) string { func (d *dbBaseSqlite) OperatorSQL(operator string) string {
return sqliteOperators[operator] return sqliteOperators[operator]
} }

View File

@ -164,7 +164,7 @@ func (t *dbTables) parseRelated(rels []string, depth int) {
} }
// generate join string. // generate join string.
func (t *dbTables) getJoinSql() (join string) { func (t *dbTables) getJoinSQL() (join string) {
Q := t.base.TableQuote() Q := t.base.TableQuote()
for _, jt := range t.tables { for _, jt := range t.tables {
@ -186,7 +186,7 @@ func (t *dbTables) getJoinSql() (join string) {
table = jt.mi.table table = jt.mi.table
switch { switch {
case jt.fi.fieldType == RelManyToMany || jt.fi.reverse && jt.fi.reverseFieldInfo.fieldType == RelManyToMany: case jt.fi.fieldType == RelManyToMany || jt.fi.fieldType == RelReverseMany || jt.fi.reverse && jt.fi.reverseFieldInfo.fieldType == RelManyToMany:
c1 = jt.fi.mi.fields.pk.column c1 = jt.fi.mi.fields.pk.column
for _, ffi := range jt.mi.fields.fieldsRel { for _, ffi := range jt.mi.fields.fieldsRel {
if jt.fi.mi == ffi.relModelInfo { if jt.fi.mi == ffi.relModelInfo {
@ -220,7 +220,7 @@ func (t *dbTables) parseExprs(mi *modelInfo, exprs []string) (index, name string
) )
num := len(exprs) - 1 num := len(exprs) - 1
names := make([]string, 0) var names []string
inner := true inner := true
@ -326,7 +326,7 @@ loopFor:
} }
// generate condition sql. // generate condition sql.
func (t *dbTables) getCondSql(cond *Condition, sub bool, tz *time.Location) (where string, params []interface{}) { func (t *dbTables) getCondSQL(cond *Condition, sub bool, tz *time.Location) (where string, params []interface{}) {
if cond == nil || cond.IsEmpty() { if cond == nil || cond.IsEmpty() {
return return
} }
@ -347,7 +347,7 @@ func (t *dbTables) getCondSql(cond *Condition, sub bool, tz *time.Location) (whe
where += "NOT " where += "NOT "
} }
if p.isCond { if p.isCond {
w, ps := t.getCondSql(p.cond, true, tz) w, ps := t.getCondSQL(p.cond, true, tz)
if w != "" { if w != "" {
w = fmt.Sprintf("( %s) ", w) w = fmt.Sprintf("( %s) ", w)
} }
@ -372,12 +372,12 @@ 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) 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)
where += fmt.Sprintf("%s %s ", leftCol, operSql) where += fmt.Sprintf("%s %s ", leftCol, operSQL)
params = append(params, args...) params = append(params, args...)
} }
@ -390,8 +390,32 @@ func (t *dbTables) getCondSql(cond *Condition, sub bool, tz *time.Location) (whe
return return
} }
// generate group sql.
func (t *dbTables) getGroupSQL(groups []string) (groupSQL string) {
if len(groups) == 0 {
return
}
Q := t.base.TableQuote()
groupSqls := make([]string, 0, len(groups))
for _, group := range groups {
exprs := strings.Split(group, ExprSep)
index, _, fi, suc := t.parseExprs(t.mi, exprs)
if suc == false {
panic(fmt.Errorf("unknown field/column name `%s`", strings.Join(exprs, ExprSep)))
}
groupSqls = append(groupSqls, fmt.Sprintf("%s.%s%s%s", index, Q, fi.column, Q))
}
groupSQL = fmt.Sprintf("GROUP BY %s ", strings.Join(groupSqls, ", "))
return
}
// generate order sql. // generate order sql.
func (t *dbTables) getOrderSql(orders []string) (orderSql string) { func (t *dbTables) getOrderSQL(orders []string) (orderSQL string) {
if len(orders) == 0 { if len(orders) == 0 {
return return
} }
@ -415,12 +439,12 @@ func (t *dbTables) getOrderSql(orders []string) (orderSql string) {
orderSqls = append(orderSqls, fmt.Sprintf("%s.%s%s%s %s", index, Q, fi.column, Q, asc)) orderSqls = append(orderSqls, fmt.Sprintf("%s.%s%s%s %s", index, Q, fi.column, Q, asc))
} }
orderSql = fmt.Sprintf("ORDER BY %s ", strings.Join(orderSqls, ", ")) orderSQL = fmt.Sprintf("ORDER BY %s ", strings.Join(orderSqls, ", "))
return return
} }
// generate limit sql. // generate limit sql.
func (t *dbTables) getLimitSql(mi *modelInfo, offset int64, limit int64) (limits string) { func (t *dbTables) getLimitSQL(mi *modelInfo, offset int64, limit int64) (limits string) {
if limit == 0 { if limit == 0 {
limit = int64(DefaultRowsLimit) limit = int64(DefaultRowsLimit)
} }

63
orm/db_tidb.go Normal file
View File

@ -0,0 +1,63 @@
// Copyright 2015 TiDB Author. All Rights Reserved.
//
// 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.
package orm
import (
"fmt"
)
// mysql dbBaser implementation.
type dbBaseTidb struct {
dbBase
}
var _ dbBaser = new(dbBaseTidb)
// get mysql operator.
func (d *dbBaseTidb) OperatorSQL(operator string) string {
return mysqlOperators[operator]
}
// get mysql table field types.
func (d *dbBaseTidb) DbTypes() map[string]string {
return mysqlTypes
}
// show table sql for mysql.
func (d *dbBaseTidb) ShowTablesQuery() string {
return "SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema = DATABASE()"
}
// show columns sql of table for mysql.
func (d *dbBaseTidb) ShowColumnsQuery(table string) string {
return fmt.Sprintf("SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE FROM information_schema.columns "+
"WHERE table_schema = DATABASE() AND table_name = '%s'", table)
}
// execute sql to check index exist.
func (d *dbBaseTidb) IndexExists(db dbQuerier, table string, name string) bool {
row := db.QueryRow("SELECT count(*) FROM information_schema.statistics "+
"WHERE table_schema = DATABASE() AND table_name = ? AND index_name = ?", table, name)
var cnt int
row.Scan(&cnt)
return cnt > 0
}
// create new mysql dbBaser.
func newdbBaseTidb() dbBaser {
b := new(dbBaseTidb)
b.ins = b
return b
}

View File

@ -24,16 +24,15 @@ import (
func getDbAlias(name string) *alias { func getDbAlias(name string) *alias {
if al, ok := dataBaseCache.get(name); ok { if al, ok := dataBaseCache.get(name); ok {
return al return al
} else {
panic(fmt.Errorf("unknown DataBase alias name %s", name))
} }
panic(fmt.Errorf("unknown DataBase alias name %s", name))
} }
// get pk column info. // get pk column info.
func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interface{}, exist bool) { func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interface{}, exist bool) {
fi := mi.fields.pk fi := mi.fields.pk
v := ind.Field(fi.fieldIndex) v := ind.FieldByIndex(fi.fieldIndex)
if fi.fieldType&IsPostiveIntegerField > 0 { if fi.fieldType&IsPostiveIntegerField > 0 {
vu := v.Uint() vu := v.Uint()
exist = vu > 0 exist = vu > 0
@ -80,19 +79,19 @@ outFor:
var err error var err error
if len(v) >= 19 { if len(v) >= 19 {
s := v[:19] s := v[:19]
t, err = time.ParseInLocation(format_DateTime, s, DefaultTimeLoc) t, err = time.ParseInLocation(formatDateTime, s, DefaultTimeLoc)
} else { } else {
s := v s := v
if len(v) > 10 { if len(v) > 10 {
s = v[:10] s = v[:10]
} }
t, err = time.ParseInLocation(format_Date, s, tz) t, err = time.ParseInLocation(formatDate, s, tz)
} }
if err == nil { if err == nil {
if fi.fieldType == TypeDateField { if fi.fieldType == TypeDateField {
v = t.In(tz).Format(format_Date) v = t.In(tz).Format(formatDate)
} else { } else {
v = t.In(tz).Format(format_DateTime) v = t.In(tz).Format(formatDateTime)
} }
} }
} }
@ -137,9 +136,9 @@ outFor:
case reflect.Struct: case reflect.Struct:
if v, ok := arg.(time.Time); ok { if v, ok := arg.(time.Time); ok {
if fi != nil && fi.fieldType == TypeDateField { if fi != nil && fi.fieldType == TypeDateField {
arg = v.In(tz).Format(format_Date) arg = v.In(tz).Format(formatDate)
} else { } else {
arg = v.In(tz).Format(format_DateTime) arg = v.In(tz).Format(formatDateTime)
} }
} else { } else {
typ := val.Type() typ := val.Type()

View File

@ -19,10 +19,10 @@ import (
) )
const ( const (
od_CASCADE = "cascade" odCascade = "cascade"
od_SET_NULL = "set_null" odSetNULL = "set_null"
od_SET_DEFAULT = "set_default" odSetDefault = "set_default"
od_DO_NOTHING = "do_nothing" odDoNothing = "do_nothing"
defaultStructTagName = "orm" defaultStructTagName = "orm"
defaultStructTagDelim = ";" defaultStructTagDelim = ";"
) )
@ -113,7 +113,7 @@ func (mc *_modelCache) clean() {
mc.done = false mc.done = false
} }
// Clean model cache. Then you can re-RegisterModel. // ResetModelCache Clean model cache. Then you can re-RegisterModel.
// Common use this api for test case. // Common use this api for test case.
func ResetModelCache() { func ResetModelCache() {
modelCache.clean() modelCache.clean()

View File

@ -51,19 +51,16 @@ func registerModel(prefix string, model interface{}) {
} }
info := newModelInfo(val) info := newModelInfo(val)
if info.fields.pk == nil { if info.fields.pk == nil {
outFor: outFor:
for _, fi := range info.fields.fieldsDB { for _, fi := range info.fields.fieldsDB {
if fi.name == "Id" { if strings.ToLower(fi.name) == "id" {
if fi.sf.Tag.Get(defaultStructTagName) == "" { switch fi.addrValue.Elem().Kind() {
switch fi.addrValue.Elem().Kind() { case reflect.Int, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint32, reflect.Uint64:
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint32, reflect.Uint64: fi.auto = true
fi.auto = true fi.pk = true
fi.pk = true info.fields.pk = fi
info.fields.pk = fi break outFor
break outFor
}
} }
} }
} }
@ -269,7 +266,10 @@ func bootStrap() {
if found == false { if found == false {
mForC: mForC:
for _, ffi := range fi.relModelInfo.fields.fieldsByType[RelManyToMany] { for _, ffi := range fi.relModelInfo.fields.fieldsByType[RelManyToMany] {
if ffi.relModelInfo == mi { conditions := fi.relThrough != "" && fi.relThrough == ffi.relThrough ||
fi.relTable != "" && fi.relTable == ffi.relTable ||
fi.relThrough == "" && fi.relTable == ""
if ffi.relModelInfo == mi && conditions {
found = true found = true
fi.reverseField = ffi.reverseFieldInfoTwo.name fi.reverseField = ffi.reverseFieldInfoTwo.name
@ -298,12 +298,12 @@ end:
} }
} }
// register models // RegisterModel register models
func RegisterModel(models ...interface{}) { func RegisterModel(models ...interface{}) {
RegisterModelWithPrefix("", models...) RegisterModelWithPrefix("", models...)
} }
// register models with a prefix // RegisterModelWithPrefix register models with a prefix
func RegisterModelWithPrefix(prefix string, models ...interface{}) { func RegisterModelWithPrefix(prefix string, models ...interface{}) {
if modelCache.done { if modelCache.done {
panic(fmt.Errorf("RegisterModel must be run before BootStrap")) panic(fmt.Errorf("RegisterModel must be run before BootStrap"))
@ -314,7 +314,7 @@ func RegisterModelWithPrefix(prefix string, models ...interface{}) {
} }
} }
// bootrap models. // BootStrap bootrap models.
// make all model parsed and can not add more models // make all model parsed and can not add more models
func BootStrap() { func BootStrap() {
if modelCache.done { if modelCache.done {

View File

@ -15,49 +15,28 @@
package orm package orm
import ( import (
"errors"
"fmt" "fmt"
"strconv" "strconv"
"time" "time"
) )
// Define the Type enum
const ( const (
// bool
TypeBooleanField = 1 << iota TypeBooleanField = 1 << iota
// string
TypeCharField TypeCharField
// string
TypeTextField TypeTextField
// time.Time
TypeDateField TypeDateField
// time.Time
TypeDateTimeField TypeDateTimeField
// int8
TypeBitField TypeBitField
// int16
TypeSmallIntegerField TypeSmallIntegerField
// int32
TypeIntegerField TypeIntegerField
// int64
TypeBigIntegerField TypeBigIntegerField
// uint8
TypePositiveBitField TypePositiveBitField
// uint16
TypePositiveSmallIntegerField TypePositiveSmallIntegerField
// uint32
TypePositiveIntegerField TypePositiveIntegerField
// uint64
TypePositiveBigIntegerField TypePositiveBigIntegerField
// float64
TypeFloatField TypeFloatField
// float64
TypeDecimalField TypeDecimalField
RelForeignKey RelForeignKey
RelOneToOne RelOneToOne
RelManyToMany RelManyToMany
@ -65,6 +44,7 @@ const (
RelReverseMany RelReverseMany
) )
// Define some logic enum
const ( const (
IsIntegerField = ^-TypePositiveBigIntegerField >> 4 << 5 IsIntegerField = ^-TypePositiveBigIntegerField >> 4 << 5
IsPostiveIntegerField = ^-TypePositiveBigIntegerField >> 8 << 9 IsPostiveIntegerField = ^-TypePositiveBigIntegerField >> 8 << 9
@ -72,25 +52,30 @@ const (
IsFieldType = ^-RelReverseMany<<1 + 1 IsFieldType = ^-RelReverseMany<<1 + 1
) )
// A true/false field. // BooleanField A true/false field.
type BooleanField bool type BooleanField bool
// Value return the BooleanField
func (e BooleanField) Value() bool { func (e BooleanField) Value() bool {
return bool(e) return bool(e)
} }
// Set will set the BooleanField
func (e *BooleanField) Set(d bool) { func (e *BooleanField) Set(d bool) {
*e = BooleanField(d) *e = BooleanField(d)
} }
// String format the Bool to string
func (e *BooleanField) String() string { func (e *BooleanField) String() string {
return strconv.FormatBool(e.Value()) return strconv.FormatBool(e.Value())
} }
// FieldType return BooleanField the type
func (e *BooleanField) FieldType() int { func (e *BooleanField) FieldType() int {
return TypeBooleanField return TypeBooleanField
} }
// SetRaw set the interface to bool
func (e *BooleanField) SetRaw(value interface{}) error { func (e *BooleanField) SetRaw(value interface{}) error {
switch d := value.(type) { switch d := value.(type) {
case bool: case bool:
@ -102,56 +87,65 @@ func (e *BooleanField) SetRaw(value interface{}) error {
} }
return err return err
default: default:
return errors.New(fmt.Sprintf("<BooleanField.SetRaw> unknown value `%s`", value)) return fmt.Errorf("<BooleanField.SetRaw> unknown value `%s`", value)
} }
return nil return nil
} }
// RawValue return the current value
func (e *BooleanField) RawValue() interface{} { func (e *BooleanField) RawValue() interface{} {
return e.Value() return e.Value()
} }
// verify the BooleanField implement the Fielder interface
var _ Fielder = new(BooleanField) var _ Fielder = new(BooleanField)
// A string field // CharField A string field
// required values tag: size // required values tag: size
// The size is enforced at the database level and in modelss validation. // The size is enforced at the database level and in modelss validation.
// eg: `orm:"size(120)"` // eg: `orm:"size(120)"`
type CharField string type CharField string
// Value return the CharField's Value
func (e CharField) Value() string { func (e CharField) Value() string {
return string(e) return string(e)
} }
// Set CharField value
func (e *CharField) Set(d string) { func (e *CharField) Set(d string) {
*e = CharField(d) *e = CharField(d)
} }
// String return the CharField
func (e *CharField) String() string { func (e *CharField) String() string {
return e.Value() return e.Value()
} }
// FieldType return the enum type
func (e *CharField) FieldType() int { func (e *CharField) FieldType() int {
return TypeCharField return TypeCharField
} }
// SetRaw set the interface to string
func (e *CharField) SetRaw(value interface{}) error { func (e *CharField) SetRaw(value interface{}) error {
switch d := value.(type) { switch d := value.(type) {
case string: case string:
e.Set(d) e.Set(d)
default: default:
return errors.New(fmt.Sprintf("<CharField.SetRaw> unknown value `%s`", value)) return fmt.Errorf("<CharField.SetRaw> unknown value `%s`", value)
} }
return nil return nil
} }
// RawValue return the CharField value
func (e *CharField) RawValue() interface{} { func (e *CharField) RawValue() interface{} {
return e.Value() return e.Value()
} }
// verify CharField implement Fielder
var _ Fielder = new(CharField) var _ Fielder = new(CharField)
// A date, represented in go by a time.Time instance. // DateField A date, represented in go by a time.Time instance.
// only date values like 2006-01-02 // only date values like 2006-01-02
// Has a few extra, optional attr tag: // Has a few extra, optional attr tag:
// //
@ -166,106 +160,125 @@ var _ Fielder = new(CharField)
// eg: `orm:"auto_now"` or `orm:"auto_now_add"` // eg: `orm:"auto_now"` or `orm:"auto_now_add"`
type DateField time.Time type DateField time.Time
// Value return the time.Time
func (e DateField) Value() time.Time { func (e DateField) Value() time.Time {
return time.Time(e) return time.Time(e)
} }
// Set set the DateField's value
func (e *DateField) Set(d time.Time) { func (e *DateField) Set(d time.Time) {
*e = DateField(d) *e = DateField(d)
} }
// String convert datatime to string
func (e *DateField) String() string { func (e *DateField) String() string {
return e.Value().String() return e.Value().String()
} }
// FieldType return enum type Date
func (e *DateField) FieldType() int { func (e *DateField) FieldType() int {
return TypeDateField return TypeDateField
} }
// SetRaw convert the interface to time.Time. Allow string and time.Time
func (e *DateField) SetRaw(value interface{}) error { func (e *DateField) SetRaw(value interface{}) error {
switch d := value.(type) { switch d := value.(type) {
case time.Time: case time.Time:
e.Set(d) e.Set(d)
case string: case string:
v, err := timeParse(d, format_Date) v, err := timeParse(d, formatDate)
if err != nil { if err != nil {
e.Set(v) e.Set(v)
} }
return err return err
default: default:
return errors.New(fmt.Sprintf("<DateField.SetRaw> unknown value `%s`", value)) return fmt.Errorf("<DateField.SetRaw> unknown value `%s`", value)
} }
return nil return nil
} }
// RawValue return Date value
func (e *DateField) RawValue() interface{} { func (e *DateField) RawValue() interface{} {
return e.Value() return e.Value()
} }
// verify DateField implement fielder interface
var _ Fielder = new(DateField) var _ Fielder = new(DateField)
// A date, represented in go by a time.Time instance. // DateTimeField A date, represented in go by a time.Time instance.
// datetime values like 2006-01-02 15:04:05 // datetime values like 2006-01-02 15:04:05
// Takes the same extra arguments as DateField. // Takes the same extra arguments as DateField.
type DateTimeField time.Time type DateTimeField time.Time
// Value return the datatime value
func (e DateTimeField) Value() time.Time { func (e DateTimeField) Value() time.Time {
return time.Time(e) return time.Time(e)
} }
// Set set the time.Time to datatime
func (e *DateTimeField) Set(d time.Time) { func (e *DateTimeField) Set(d time.Time) {
*e = DateTimeField(d) *e = DateTimeField(d)
} }
// String return the time's String
func (e *DateTimeField) String() string { func (e *DateTimeField) String() string {
return e.Value().String() return e.Value().String()
} }
// FieldType return the enum TypeDateTimeField
func (e *DateTimeField) FieldType() int { func (e *DateTimeField) FieldType() int {
return TypeDateTimeField return TypeDateTimeField
} }
// SetRaw convert the string or time.Time to DateTimeField
func (e *DateTimeField) SetRaw(value interface{}) error { func (e *DateTimeField) SetRaw(value interface{}) error {
switch d := value.(type) { switch d := value.(type) {
case time.Time: case time.Time:
e.Set(d) e.Set(d)
case string: case string:
v, err := timeParse(d, format_DateTime) v, err := timeParse(d, formatDateTime)
if err != nil { if err != nil {
e.Set(v) e.Set(v)
} }
return err return err
default: default:
return errors.New(fmt.Sprintf("<DateTimeField.SetRaw> unknown value `%s`", value)) return fmt.Errorf("<DateTimeField.SetRaw> unknown value `%s`", value)
} }
return nil return nil
} }
// RawValue return the datatime value
func (e *DateTimeField) RawValue() interface{} { func (e *DateTimeField) RawValue() interface{} {
return e.Value() return e.Value()
} }
// verify datatime implement fielder
var _ Fielder = new(DateTimeField) var _ Fielder = new(DateTimeField)
// A floating-point number represented in go by a float32 value. // FloatField A floating-point number represented in go by a float32 value.
type FloatField float64 type FloatField float64
// Value return the FloatField value
func (e FloatField) Value() float64 { func (e FloatField) Value() float64 {
return float64(e) return float64(e)
} }
// Set the Float64
func (e *FloatField) Set(d float64) { func (e *FloatField) Set(d float64) {
*e = FloatField(d) *e = FloatField(d)
} }
// String return the string
func (e *FloatField) String() string { func (e *FloatField) String() string {
return ToStr(e.Value(), -1, 32) return ToStr(e.Value(), -1, 32)
} }
// FieldType return the enum type
func (e *FloatField) FieldType() int { func (e *FloatField) FieldType() int {
return TypeFloatField return TypeFloatField
} }
// SetRaw converter interface Float64 float32 or string to FloatField
func (e *FloatField) SetRaw(value interface{}) error { func (e *FloatField) SetRaw(value interface{}) error {
switch d := value.(type) { switch d := value.(type) {
case float32: case float32:
@ -278,36 +291,43 @@ func (e *FloatField) SetRaw(value interface{}) error {
e.Set(v) e.Set(v)
} }
default: default:
return errors.New(fmt.Sprintf("<FloatField.SetRaw> unknown value `%s`", value)) return fmt.Errorf("<FloatField.SetRaw> unknown value `%s`", value)
} }
return nil return nil
} }
// RawValue return the FloatField value
func (e *FloatField) RawValue() interface{} { func (e *FloatField) RawValue() interface{} {
return e.Value() return e.Value()
} }
// verify FloatField implement Fielder
var _ Fielder = new(FloatField) var _ Fielder = new(FloatField)
// -32768 to 32767 // SmallIntegerField -32768 to 32767
type SmallIntegerField int16 type SmallIntegerField int16
// Value return int16 value
func (e SmallIntegerField) Value() int16 { func (e SmallIntegerField) Value() int16 {
return int16(e) return int16(e)
} }
// Set the SmallIntegerField value
func (e *SmallIntegerField) Set(d int16) { func (e *SmallIntegerField) Set(d int16) {
*e = SmallIntegerField(d) *e = SmallIntegerField(d)
} }
// String convert smallint to string
func (e *SmallIntegerField) String() string { func (e *SmallIntegerField) String() string {
return ToStr(e.Value()) return ToStr(e.Value())
} }
// FieldType return enum type SmallIntegerField
func (e *SmallIntegerField) FieldType() int { func (e *SmallIntegerField) FieldType() int {
return TypeSmallIntegerField return TypeSmallIntegerField
} }
// SetRaw convert interface int16/string to int16
func (e *SmallIntegerField) SetRaw(value interface{}) error { func (e *SmallIntegerField) SetRaw(value interface{}) error {
switch d := value.(type) { switch d := value.(type) {
case int16: case int16:
@ -318,36 +338,43 @@ func (e *SmallIntegerField) SetRaw(value interface{}) error {
e.Set(v) e.Set(v)
} }
default: default:
return errors.New(fmt.Sprintf("<SmallIntegerField.SetRaw> unknown value `%s`", value)) return fmt.Errorf("<SmallIntegerField.SetRaw> unknown value `%s`", value)
} }
return nil return nil
} }
// RawValue return smallint value
func (e *SmallIntegerField) RawValue() interface{} { func (e *SmallIntegerField) RawValue() interface{} {
return e.Value() return e.Value()
} }
// verify SmallIntegerField implement Fielder
var _ Fielder = new(SmallIntegerField) var _ Fielder = new(SmallIntegerField)
// -2147483648 to 2147483647 // IntegerField -2147483648 to 2147483647
type IntegerField int32 type IntegerField int32
// Value return the int32
func (e IntegerField) Value() int32 { func (e IntegerField) Value() int32 {
return int32(e) return int32(e)
} }
// Set IntegerField value
func (e *IntegerField) Set(d int32) { func (e *IntegerField) Set(d int32) {
*e = IntegerField(d) *e = IntegerField(d)
} }
// String convert Int32 to string
func (e *IntegerField) String() string { func (e *IntegerField) String() string {
return ToStr(e.Value()) return ToStr(e.Value())
} }
// FieldType return the enum type
func (e *IntegerField) FieldType() int { func (e *IntegerField) FieldType() int {
return TypeIntegerField return TypeIntegerField
} }
// SetRaw convert interface int32/string to int32
func (e *IntegerField) SetRaw(value interface{}) error { func (e *IntegerField) SetRaw(value interface{}) error {
switch d := value.(type) { switch d := value.(type) {
case int32: case int32:
@ -358,36 +385,43 @@ func (e *IntegerField) SetRaw(value interface{}) error {
e.Set(v) e.Set(v)
} }
default: default:
return errors.New(fmt.Sprintf("<IntegerField.SetRaw> unknown value `%s`", value)) return fmt.Errorf("<IntegerField.SetRaw> unknown value `%s`", value)
} }
return nil return nil
} }
// RawValue return IntegerField value
func (e *IntegerField) RawValue() interface{} { func (e *IntegerField) RawValue() interface{} {
return e.Value() return e.Value()
} }
// verify IntegerField implement Fielder
var _ Fielder = new(IntegerField) var _ Fielder = new(IntegerField)
// -9223372036854775808 to 9223372036854775807. // BigIntegerField -9223372036854775808 to 9223372036854775807.
type BigIntegerField int64 type BigIntegerField int64
// Value return int64
func (e BigIntegerField) Value() int64 { func (e BigIntegerField) Value() int64 {
return int64(e) return int64(e)
} }
// Set the BigIntegerField value
func (e *BigIntegerField) Set(d int64) { func (e *BigIntegerField) Set(d int64) {
*e = BigIntegerField(d) *e = BigIntegerField(d)
} }
// String convert BigIntegerField to string
func (e *BigIntegerField) String() string { func (e *BigIntegerField) String() string {
return ToStr(e.Value()) return ToStr(e.Value())
} }
// FieldType return enum type
func (e *BigIntegerField) FieldType() int { func (e *BigIntegerField) FieldType() int {
return TypeBigIntegerField return TypeBigIntegerField
} }
// SetRaw convert interface int64/string to int64
func (e *BigIntegerField) SetRaw(value interface{}) error { func (e *BigIntegerField) SetRaw(value interface{}) error {
switch d := value.(type) { switch d := value.(type) {
case int64: case int64:
@ -398,36 +432,43 @@ func (e *BigIntegerField) SetRaw(value interface{}) error {
e.Set(v) e.Set(v)
} }
default: default:
return errors.New(fmt.Sprintf("<BigIntegerField.SetRaw> unknown value `%s`", value)) return fmt.Errorf("<BigIntegerField.SetRaw> unknown value `%s`", value)
} }
return nil return nil
} }
// RawValue return BigIntegerField value
func (e *BigIntegerField) RawValue() interface{} { func (e *BigIntegerField) RawValue() interface{} {
return e.Value() return e.Value()
} }
// verify BigIntegerField implement Fielder
var _ Fielder = new(BigIntegerField) var _ Fielder = new(BigIntegerField)
// 0 to 65535 // PositiveSmallIntegerField 0 to 65535
type PositiveSmallIntegerField uint16 type PositiveSmallIntegerField uint16
// Value return uint16
func (e PositiveSmallIntegerField) Value() uint16 { func (e PositiveSmallIntegerField) Value() uint16 {
return uint16(e) return uint16(e)
} }
// Set PositiveSmallIntegerField value
func (e *PositiveSmallIntegerField) Set(d uint16) { func (e *PositiveSmallIntegerField) Set(d uint16) {
*e = PositiveSmallIntegerField(d) *e = PositiveSmallIntegerField(d)
} }
// String convert uint16 to string
func (e *PositiveSmallIntegerField) String() string { func (e *PositiveSmallIntegerField) String() string {
return ToStr(e.Value()) return ToStr(e.Value())
} }
// FieldType return enum type
func (e *PositiveSmallIntegerField) FieldType() int { func (e *PositiveSmallIntegerField) FieldType() int {
return TypePositiveSmallIntegerField return TypePositiveSmallIntegerField
} }
// SetRaw convert Interface uint16/string to uint16
func (e *PositiveSmallIntegerField) SetRaw(value interface{}) error { func (e *PositiveSmallIntegerField) SetRaw(value interface{}) error {
switch d := value.(type) { switch d := value.(type) {
case uint16: case uint16:
@ -438,36 +479,43 @@ func (e *PositiveSmallIntegerField) SetRaw(value interface{}) error {
e.Set(v) e.Set(v)
} }
default: default:
return errors.New(fmt.Sprintf("<PositiveSmallIntegerField.SetRaw> unknown value `%s`", value)) return fmt.Errorf("<PositiveSmallIntegerField.SetRaw> unknown value `%s`", value)
} }
return nil return nil
} }
// RawValue returns PositiveSmallIntegerField value
func (e *PositiveSmallIntegerField) RawValue() interface{} { func (e *PositiveSmallIntegerField) RawValue() interface{} {
return e.Value() return e.Value()
} }
// verify PositiveSmallIntegerField implement Fielder
var _ Fielder = new(PositiveSmallIntegerField) var _ Fielder = new(PositiveSmallIntegerField)
// 0 to 4294967295 // PositiveIntegerField 0 to 4294967295
type PositiveIntegerField uint32 type PositiveIntegerField uint32
// Value return PositiveIntegerField value. Uint32
func (e PositiveIntegerField) Value() uint32 { func (e PositiveIntegerField) Value() uint32 {
return uint32(e) return uint32(e)
} }
// Set the PositiveIntegerField value
func (e *PositiveIntegerField) Set(d uint32) { func (e *PositiveIntegerField) Set(d uint32) {
*e = PositiveIntegerField(d) *e = PositiveIntegerField(d)
} }
// String convert PositiveIntegerField to string
func (e *PositiveIntegerField) String() string { func (e *PositiveIntegerField) String() string {
return ToStr(e.Value()) return ToStr(e.Value())
} }
// FieldType return enum type
func (e *PositiveIntegerField) FieldType() int { func (e *PositiveIntegerField) FieldType() int {
return TypePositiveIntegerField return TypePositiveIntegerField
} }
// SetRaw convert interface uint32/string to Uint32
func (e *PositiveIntegerField) SetRaw(value interface{}) error { func (e *PositiveIntegerField) SetRaw(value interface{}) error {
switch d := value.(type) { switch d := value.(type) {
case uint32: case uint32:
@ -478,36 +526,43 @@ func (e *PositiveIntegerField) SetRaw(value interface{}) error {
e.Set(v) e.Set(v)
} }
default: default:
return errors.New(fmt.Sprintf("<PositiveIntegerField.SetRaw> unknown value `%s`", value)) return fmt.Errorf("<PositiveIntegerField.SetRaw> unknown value `%s`", value)
} }
return nil return nil
} }
// RawValue return the PositiveIntegerField Value
func (e *PositiveIntegerField) RawValue() interface{} { func (e *PositiveIntegerField) RawValue() interface{} {
return e.Value() return e.Value()
} }
// verify PositiveIntegerField implement Fielder
var _ Fielder = new(PositiveIntegerField) var _ Fielder = new(PositiveIntegerField)
// 0 to 18446744073709551615 // PositiveBigIntegerField 0 to 18446744073709551615
type PositiveBigIntegerField uint64 type PositiveBigIntegerField uint64
// Value return uint64
func (e PositiveBigIntegerField) Value() uint64 { func (e PositiveBigIntegerField) Value() uint64 {
return uint64(e) return uint64(e)
} }
// Set PositiveBigIntegerField value
func (e *PositiveBigIntegerField) Set(d uint64) { func (e *PositiveBigIntegerField) Set(d uint64) {
*e = PositiveBigIntegerField(d) *e = PositiveBigIntegerField(d)
} }
// String convert PositiveBigIntegerField to string
func (e *PositiveBigIntegerField) String() string { func (e *PositiveBigIntegerField) String() string {
return ToStr(e.Value()) return ToStr(e.Value())
} }
// FieldType return enum type
func (e *PositiveBigIntegerField) FieldType() int { func (e *PositiveBigIntegerField) FieldType() int {
return TypePositiveIntegerField return TypePositiveIntegerField
} }
// SetRaw convert interface uint64/string to Uint64
func (e *PositiveBigIntegerField) SetRaw(value interface{}) error { func (e *PositiveBigIntegerField) SetRaw(value interface{}) error {
switch d := value.(type) { switch d := value.(type) {
case uint64: case uint64:
@ -518,48 +573,57 @@ func (e *PositiveBigIntegerField) SetRaw(value interface{}) error {
e.Set(v) e.Set(v)
} }
default: default:
return errors.New(fmt.Sprintf("<PositiveBigIntegerField.SetRaw> unknown value `%s`", value)) return fmt.Errorf("<PositiveBigIntegerField.SetRaw> unknown value `%s`", value)
} }
return nil return nil
} }
// RawValue return PositiveBigIntegerField value
func (e *PositiveBigIntegerField) RawValue() interface{} { func (e *PositiveBigIntegerField) RawValue() interface{} {
return e.Value() return e.Value()
} }
// verify PositiveBigIntegerField implement Fielder
var _ Fielder = new(PositiveBigIntegerField) var _ Fielder = new(PositiveBigIntegerField)
// A large text field. // TextField A large text field.
type TextField string type TextField string
// Value return TextField value
func (e TextField) Value() string { func (e TextField) Value() string {
return string(e) return string(e)
} }
// Set the TextField value
func (e *TextField) Set(d string) { func (e *TextField) Set(d string) {
*e = TextField(d) *e = TextField(d)
} }
// String convert TextField to string
func (e *TextField) String() string { func (e *TextField) String() string {
return e.Value() return e.Value()
} }
// FieldType return enum type
func (e *TextField) FieldType() int { func (e *TextField) FieldType() int {
return TypeTextField return TypeTextField
} }
// SetRaw convert interface string to string
func (e *TextField) SetRaw(value interface{}) error { func (e *TextField) SetRaw(value interface{}) error {
switch d := value.(type) { switch d := value.(type) {
case string: case string:
e.Set(d) e.Set(d)
default: default:
return errors.New(fmt.Sprintf("<TextField.SetRaw> unknown value `%s`", value)) return fmt.Errorf("<TextField.SetRaw> unknown value `%s`", value)
} }
return nil return nil
} }
// RawValue return TextField value
func (e *TextField) RawValue() interface{} { func (e *TextField) RawValue() interface{} {
return e.Value() return e.Value()
} }
// verify TextField implement Fielder
var _ Fielder = new(TextField) var _ Fielder = new(TextField)

View File

@ -102,7 +102,7 @@ func newFields() *fields {
// single field info // single field info
type fieldInfo struct { type fieldInfo struct {
mi *modelInfo mi *modelInfo
fieldIndex int fieldIndex []int
fieldType int fieldType int
dbcol bool dbcol bool
inModel bool inModel bool
@ -116,10 +116,11 @@ type fieldInfo struct {
null bool null bool
index bool index bool
unique bool unique bool
colDefault bool
initial StrTo initial StrTo
size int size int
auto_now bool autoNow bool
auto_now_add bool autoNowAdd bool
rel bool rel bool
reverse bool reverse bool
reverseField string reverseField string
@ -137,7 +138,7 @@ type fieldInfo struct {
} }
// new field info // new field info
func newFieldInfo(mi *modelInfo, field reflect.Value, sf reflect.StructField) (fi *fieldInfo, err error) { func newFieldInfo(mi *modelInfo, field reflect.Value, sf reflect.StructField, mName string) (fi *fieldInfo, err error) {
var ( var (
tag string tag string
tagValue string tagValue string
@ -222,6 +223,11 @@ checkType:
break checkType break checkType
case "many": case "many":
fieldType = RelReverseMany fieldType = RelReverseMany
if tv := tags["rel_table"]; tv != "" {
fi.relTable = tv
} else if tv := tags["rel_through"]; tv != "" {
fi.relThrough = tv
}
break checkType break checkType
default: default:
err = fmt.Errorf("error") err = fmt.Errorf("error")
@ -272,7 +278,7 @@ checkType:
fi.column = getColumnName(fieldType, addrField, sf, tags["column"]) fi.column = getColumnName(fieldType, addrField, sf, tags["column"])
fi.addrValue = addrField fi.addrValue = addrField
fi.sf = sf fi.sf = sf
fi.fullName = mi.fullName + "." + sf.Name fi.fullName = mi.fullName + mName + "." + sf.Name
fi.null = attrs["null"] fi.null = attrs["null"]
fi.index = attrs["index"] fi.index = attrs["index"]
@ -280,6 +286,11 @@ checkType:
fi.pk = attrs["pk"] fi.pk = attrs["pk"]
fi.unique = attrs["unique"] fi.unique = attrs["unique"]
// Mark object property if there is attribute "default" in the orm configuration
if _, ok := tags["default"]; ok {
fi.colDefault = true
}
switch fieldType { switch fieldType {
case RelManyToMany, RelReverseMany, RelReverseOne: case RelManyToMany, RelReverseMany, RelReverseOne:
fi.null = false fi.null = false
@ -303,20 +314,20 @@ checkType:
if fi.rel && fi.dbcol { if fi.rel && fi.dbcol {
switch onDelete { switch onDelete {
case od_CASCADE, od_DO_NOTHING: case odCascade, odDoNothing:
case od_SET_DEFAULT: case odSetDefault:
if initial.Exist() == false { if initial.Exist() == false {
err = errors.New("on_delete: set_default need set field a default value") err = errors.New("on_delete: set_default need set field a default value")
goto end goto end
} }
case od_SET_NULL: case odSetNULL:
if fi.null == false { if fi.null == false {
err = errors.New("on_delete: set_null need set field null") err = errors.New("on_delete: set_null need set field null")
goto end goto end
} }
default: default:
if onDelete == "" { if onDelete == "" {
onDelete = od_CASCADE onDelete = odCascade
} else { } else {
err = fmt.Errorf("on_delete value expected choice in `cascade,set_null,set_default,do_nothing`, unknown `%s`", onDelete) err = fmt.Errorf("on_delete value expected choice in `cascade,set_null,set_default,do_nothing`, unknown `%s`", onDelete)
goto end goto end
@ -344,9 +355,9 @@ checkType:
fi.unique = false fi.unique = false
case TypeDateField, TypeDateTimeField: case TypeDateField, TypeDateTimeField:
if attrs["auto_now"] { if attrs["auto_now"] {
fi.auto_now = true fi.autoNow = true
} else if attrs["auto_now_add"] { } else if attrs["auto_now_add"] {
fi.auto_now_add = true fi.autoNowAdd = true
} }
case TypeFloatField: case TypeFloatField:
case TypeDecimalField: case TypeDecimalField:

Some files were not shown because too many files have changed in this diff Show More