Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
凌波微步_大先生
dashboard
提交
0317071a
D
dashboard
项目概览
凌波微步_大先生
/
dashboard
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dashboard
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0317071a
编写于
1月 22, 2016
作者:
M
Marcin Maciaszczyk
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix logger issue
上级
81f479d4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
17 addition
and
58 deletion
+17
-58
src/app/backend/apihandler.go
src/app/backend/apihandler.go
+6
-18
src/test/backend/apihandler_test.go
src/test/backend/apihandler_test.go
+11
-40
未找到文件。
src/app/backend/apihandler.go
浏览文件 @
0317071a
...
...
@@ -19,7 +19,6 @@ import (
"log"
"net/http"
"strconv"
"strings"
restful
"github.com/emicklei/go-restful"
client
"k8s.io/kubernetes/pkg/client/unversioned"
...
...
@@ -27,44 +26,33 @@ import (
)
const
(
Colon
=
":"
RequestLogString
=
"Incoming %s %s %s request from %s"
ResponseLogString
=
"Outcoming response to %s with %d status code"
)
// Web-service filter function used for request and response logging.
func
wsLogger
(
req
*
restful
.
Request
,
resp
*
restful
.
Response
,
chain
*
restful
.
FilterChain
)
{
remoteAddr
:=
GetRemoteAddr
(
req
)
log
.
Printf
(
FormatRequestLog
(
req
,
remoteAddr
))
log
.
Printf
(
FormatRequestLog
(
req
))
chain
.
ProcessFilter
(
req
,
resp
)
log
.
Printf
(
FormatResponseLog
(
resp
,
remoteAddr
))
}
// Returns remote address of the request (without port number).
func
GetRemoteAddr
(
req
*
restful
.
Request
)
string
{
if
strings
.
Contains
(
req
.
Request
.
RemoteAddr
,
Colon
)
{
return
strings
.
Split
(
req
.
Request
.
RemoteAddr
,
Colon
)[
0
]
}
else
{
return
req
.
Request
.
RemoteAddr
}
log
.
Printf
(
FormatResponseLog
(
resp
,
req
))
}
// Formats request log string.
// TODO(maciaszczykm): Display request body.
func
FormatRequestLog
(
req
*
restful
.
Request
,
remoteAddr
string
)
string
{
func
FormatRequestLog
(
req
*
restful
.
Request
)
string
{
reqURI
:=
""
if
req
.
Request
.
URL
!=
nil
{
reqURI
=
req
.
Request
.
URL
.
RequestURI
()
}
return
fmt
.
Sprintf
(
RequestLogString
,
req
.
Request
.
Proto
,
req
.
Request
.
Method
,
reqURI
,
remoteAddr
)
reqURI
,
re
q
.
Request
.
Re
moteAddr
)
}
// Formats response log string.
// TODO(maciaszczykm): Display response content.
func
FormatResponseLog
(
resp
*
restful
.
Response
,
re
moteAddr
string
)
string
{
return
fmt
.
Sprintf
(
ResponseLogString
,
remoteAddr
,
resp
.
StatusCode
())
func
FormatResponseLog
(
resp
*
restful
.
Response
,
re
q
*
restful
.
Request
)
string
{
return
fmt
.
Sprintf
(
ResponseLogString
,
re
q
.
Request
.
Re
moteAddr
,
resp
.
StatusCode
())
}
// Creates a new HTTP handler that handles all requests to the API of the backend.
...
...
src/test/backend/apihandler_test.go
浏览文件 @
0317071a
...
...
@@ -23,40 +23,9 @@ import (
restful
"github.com/emicklei/go-restful"
)
func
TestGetRemoteAddr
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
request
*
restful
.
Request
expected
string
}{
{
&
restful
.
Request
{
Request
:
&
http
.
Request
{
RemoteAddr
:
"192.168.1.1:8080"
,
},
},
"192.168.1.1"
,
},
{
&
restful
.
Request
{
Request
:
&
http
.
Request
{
RemoteAddr
:
"192.168.1.2"
,
},
},
"192.168.1.2"
,
},
}
for
_
,
c
:=
range
cases
{
actual
:=
GetRemoteAddr
(
c
.
request
)
if
!
reflect
.
DeepEqual
(
actual
,
c
.
expected
)
{
t
.
Errorf
(
"GetRemoteAddr(%#v) == %#v, expected %#v"
,
c
.
request
,
actual
,
c
.
expected
)
}
}
}
func
TestFormatRequestLog
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
request
*
restful
.
Request
remoteAddr
string
expected
string
}{
{
...
...
@@ -67,15 +36,13 @@ func TestFormatRequestLog(t *testing.T) {
Method
:
"GET"
,
},
},
"192.168.1.1"
,
fmt
.
Sprintf
(
RequestLogString
,
"HTTP 1.1"
,
"GET"
,
""
,
"192.168.1.1"
),
fmt
.
Sprintf
(
RequestLogString
,
"HTTP 1.1"
,
"GET"
,
""
,
"192.168.1.1:8080"
),
},
}
for
_
,
c
:=
range
cases
{
actual
:=
FormatRequestLog
(
c
.
request
,
c
.
remoteAddr
)
actual
:=
FormatRequestLog
(
c
.
request
,)
if
!
reflect
.
DeepEqual
(
actual
,
c
.
expected
)
{
t
.
Errorf
(
"GetRemoteAddr(%#v, %#v) == %#v, expected %#v"
,
c
.
request
,
c
.
remoteAddr
,
actual
,
c
.
expected
)
t
.
Errorf
(
"FormatRequestLog(%#v) == %#v, expected %#v"
,
c
.
request
,
actual
,
c
.
expected
)
}
}
}
...
...
@@ -83,19 +50,23 @@ func TestFormatRequestLog(t *testing.T) {
func
TestFormatResponseLog
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
response
*
restful
.
Response
re
moteAddr
string
re
quest
*
restful
.
Request
expected
string
}{
{
&
restful
.
Response
{},
"192.168.1.1"
,
&
restful
.
Request
{
Request
:
&
http
.
Request
{
RemoteAddr
:
"192.168.1.1"
,
},
},
fmt
.
Sprintf
(
ResponseLogString
,
"192.168.1.1"
,
http
.
StatusOK
),
},
}
for
_
,
c
:=
range
cases
{
actual
:=
FormatResponseLog
(
c
.
response
,
c
.
re
moteAddr
)
actual
:=
FormatResponseLog
(
c
.
response
,
c
.
re
quest
)
if
!
reflect
.
DeepEqual
(
actual
,
c
.
expected
)
{
t
.
Errorf
(
"
GetRemoteAddr(%#v, %#v) == %#v, expected %#v"
,
c
.
response
,
c
.
remoteAddr
,
t
.
Errorf
(
"
FormatResponseLog(%#v, %#v) == %#v, expected %#v"
,
c
.
response
,
c
.
request
,
actual
,
c
.
expected
)
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录