Skip to content
GitLab
菜单
为什么选择 GitLab
定价
联系销售
探索
为什么选择 GitLab
定价
联系销售
探索
登录
获取免费试用
主导航
搜索或转到…
项目
GitLab
管理
动态
成员
标记
计划
议题
议题看板
里程碑
迭代
需求
代码
合并请求
仓库
分支
提交
标签
仓库图
比较修订版本
代码片段
锁定的文件
构建
流水线
作业
流水线计划
测试用例
产物
部署
发布
Package registry
Container registry
模型注册表
运维
环境
Terraform 模块
监控
事件
服务台
分析
价值流分析
贡献者分析
CI/CD 分析
仓库分析
代码评审分析
议题分析
洞察
模型实验
效能分析
帮助
帮助
支持
GitLab 文档
比较 GitLab 各版本
社区论坛
为极狐GitLab 提交贡献
提交反馈
隐私声明
快捷键
?
新增功能
4
代码片段
群组
项目
显示更多面包屑
gitlab-cn
GitLab
提交
ac409d67
提交
ac409d67
编辑于
9 years ago
作者:
Kamil Trzciński
浏览文件
操作
下载
补丁
差异文件
Add caching and serving precompiled assets
上级
afcae9ed
No related branches found
分支 包含提交
No related tags found
标签 包含提交
无相关合并请求
变更
3
隐藏空白变更内容
行内
左右并排
显示
3 个更改的文件
main.go
+9
-5
9 个添加, 5 个删除
main.go
servefile.go
+34
-3
34 个添加, 3 个删除
servefile.go
servefile_test.go
+67
-5
67 个添加, 5 个删除
servefile_test.go
有
110 个添加
和
13 个删除
main.go
+
9
−
5
浏览文件 @
ac409d67
...
@@ -86,11 +86,15 @@ var httpRoutes = [...]httpRoute{
...
@@ -86,11 +86,15 @@ var httpRoutes = [...]httpRoute{
httpRoute
{
""
,
regexp
.
MustCompile
(
ciAPIPattern
),
proxyRequest
},
httpRoute
{
""
,
regexp
.
MustCompile
(
ciAPIPattern
),
proxyRequest
},
// Serve static files or forward the requests
// Serve static files or forward the requests
httpRoute
{
""
,
nil
,
handleServeFile
(
documentRoot
,
httpRoute
{
""
,
nil
,
handleDeployPage
(
documentRoot
,
handleServeFile
(
documentRoot
,
CacheDisabled
,
handleRailsError
(
documentRoot
,
handleDeployPage
(
documentRoot
,
proxyRequest
,
handleRailsError
(
documentRoot
,
)))},
proxyRequest
,
),
),
),
},
}
}
func
main
()
{
func
main
()
{
...
...
此差异已折叠。
点击以展开。
servefile.go
+
34
−
3
浏览文件 @
ac409d67
...
@@ -6,9 +6,17 @@ import (
...
@@ -6,9 +6,17 @@ import (
"os"
"os"
"path/filepath"
"path/filepath"
"strings"
"strings"
"time"
)
)
func
handleServeFile
(
documentRoot
*
string
,
notFoundHandler
serviceHandleFunc
)
serviceHandleFunc
{
type
CacheMode
int
const
(
CacheDisabled
CacheMode
=
iota
CacheExpireMax
)
func
handleServeFile
(
documentRoot
*
string
,
cache
CacheMode
,
notFoundHandler
serviceHandleFunc
)
serviceHandleFunc
{
return
func
(
w
http
.
ResponseWriter
,
r
*
gitRequest
)
{
return
func
(
w
http
.
ResponseWriter
,
r
*
gitRequest
)
{
file
:=
filepath
.
Join
(
*
documentRoot
,
r
.
relativeURIPath
)
file
:=
filepath
.
Join
(
*
documentRoot
,
r
.
relativeURIPath
)
...
@@ -22,7 +30,22 @@ func handleServeFile(documentRoot *string, notFoundHandler serviceHandleFunc) se
...
@@ -22,7 +30,22 @@ func handleServeFile(documentRoot *string, notFoundHandler serviceHandleFunc) se
return
return
}
}
content
,
fi
,
err
:=
openFile
(
file
)
var
content
*
os
.
File
var
fi
os
.
FileInfo
var
err
error
// Serve pre-gzipped assets
if
acceptEncoding
:=
r
.
Header
.
Get
(
"Accept-Encoding"
);
strings
.
Contains
(
acceptEncoding
,
"gzip"
)
{
content
,
fi
,
err
=
openFile
(
file
+
".gz"
)
if
err
==
nil
{
w
.
Header
()
.
Set
(
"Content-Encoding"
,
"gzip"
)
}
}
// If not found open the file
if
content
==
nil
||
err
!=
nil
{
content
,
fi
,
err
=
openFile
(
file
)
}
if
err
!=
nil
{
if
err
!=
nil
{
if
notFoundHandler
!=
nil
{
if
notFoundHandler
!=
nil
{
notFoundHandler
(
w
,
r
)
notFoundHandler
(
w
,
r
)
...
@@ -33,7 +56,15 @@ func handleServeFile(documentRoot *string, notFoundHandler serviceHandleFunc) se
...
@@ -33,7 +56,15 @@ func handleServeFile(documentRoot *string, notFoundHandler serviceHandleFunc) se
}
}
defer
content
.
Close
()
defer
content
.
Close
()
log
.
Printf
(
"StaticFile: serving %q"
,
file
)
switch
cache
{
case
CacheExpireMax
:
// Cache statically served files for 1 year
cacheUntil
:=
time
.
Now
()
.
AddDate
(
1
,
0
,
0
)
.
Format
(
http
.
TimeFormat
)
w
.
Header
()
.
Set
(
"Cache-Control"
,
"public"
)
w
.
Header
()
.
Set
(
"Expires"
,
cacheUntil
)
}
log
.
Printf
(
"StaticFile: serving %q %q"
,
file
,
w
.
Header
()
.
Get
(
"Content-Encoding"
))
http
.
ServeContent
(
w
,
r
.
Request
,
filepath
.
Base
(
file
),
fi
.
ModTime
(),
content
)
http
.
ServeContent
(
w
,
r
.
Request
,
filepath
.
Base
(
file
),
fi
.
ModTime
(),
content
)
}
}
}
}
此差异已折叠。
点击以展开。
servefile_test.go
+
67
−
5
浏览文件 @
ac409d67
package
main
package
main
import
(
import
(
"bytes"
"compress/gzip"
"io/ioutil"
"io/ioutil"
"net/http"
"net/http"
"net/http/httptest"
"net/http/httptest"
...
@@ -11,12 +13,14 @@ import (
...
@@ -11,12 +13,14 @@ import (
func
TestServingNonExistingFile
(
t
*
testing
.
T
)
{
func
TestServingNonExistingFile
(
t
*
testing
.
T
)
{
dir
:=
"/path/to/non/existing/directory"
dir
:=
"/path/to/non/existing/directory"
httpRequest
,
_
:=
http
.
NewRequest
(
"GET"
,
"/file"
,
nil
)
request
:=
&
gitRequest
{
request
:=
&
gitRequest
{
Request
:
httpRequest
,
relativeURIPath
:
"/static/file"
,
relativeURIPath
:
"/static/file"
,
}
}
w
:=
httptest
.
NewRecorder
()
w
:=
httptest
.
NewRecorder
()
handleServeFile
(
&
dir
,
nil
)(
w
,
request
)
handleServeFile
(
&
dir
,
CacheDisabled
,
nil
)(
w
,
request
)
assertResponseCode
(
t
,
w
,
404
)
assertResponseCode
(
t
,
w
,
404
)
}
}
...
@@ -27,34 +31,40 @@ func TestServingDirectory(t *testing.T) {
...
@@ -27,34 +31,40 @@ func TestServingDirectory(t *testing.T) {
}
}
defer
os
.
RemoveAll
(
dir
)
defer
os
.
RemoveAll
(
dir
)
httpRequest
,
_
:=
http
.
NewRequest
(
"GET"
,
"/file"
,
nil
)
request
:=
&
gitRequest
{
request
:=
&
gitRequest
{
Request
:
httpRequest
,
relativeURIPath
:
"/"
,
relativeURIPath
:
"/"
,
}
}
w
:=
httptest
.
NewRecorder
()
w
:=
httptest
.
NewRecorder
()
handleServeFile
(
&
dir
,
nil
)(
w
,
request
)
handleServeFile
(
&
dir
,
CacheDisabled
,
nil
)(
w
,
request
)
assertResponseCode
(
t
,
w
,
404
)
assertResponseCode
(
t
,
w
,
404
)
}
}
func
TestServingMalformedUri
(
t
*
testing
.
T
)
{
func
TestServingMalformedUri
(
t
*
testing
.
T
)
{
dir
:=
"/path/to/non/existing/directory"
dir
:=
"/path/to/non/existing/directory"
httpRequest
,
_
:=
http
.
NewRequest
(
"GET"
,
"/file"
,
nil
)
request
:=
&
gitRequest
{
request
:=
&
gitRequest
{
Request
:
httpRequest
,
relativeURIPath
:
"/../../../static/file"
,
relativeURIPath
:
"/../../../static/file"
,
}
}
w
:=
httptest
.
NewRecorder
()
w
:=
httptest
.
NewRecorder
()
handleServeFile
(
&
dir
,
nil
)(
w
,
request
)
handleServeFile
(
&
dir
,
CacheDisabled
,
nil
)(
w
,
request
)
assertResponseCode
(
t
,
w
,
500
)
assertResponseCode
(
t
,
w
,
500
)
}
}
func
TestExecutingHandlerWhenNoFileFound
(
t
*
testing
.
T
)
{
func
TestExecutingHandlerWhenNoFileFound
(
t
*
testing
.
T
)
{
dir
:=
"/path/to/non/existing/directory"
dir
:=
"/path/to/non/existing/directory"
httpRequest
,
_
:=
http
.
NewRequest
(
"GET"
,
"/file"
,
nil
)
request
:=
&
gitRequest
{
request
:=
&
gitRequest
{
Request
:
httpRequest
,
relativeURIPath
:
"/static/file"
,
relativeURIPath
:
"/static/file"
,
}
}
executed
:=
false
executed
:=
false
handleServeFile
(
&
dir
,
func
(
w
http
.
ResponseWriter
,
r
*
gitRequest
)
{
handleServeFile
(
&
dir
,
CacheDisabled
,
func
(
w
http
.
ResponseWriter
,
r
*
gitRequest
)
{
executed
=
(
r
==
request
)
executed
=
(
r
==
request
)
})(
nil
,
request
)
})(
nil
,
request
)
if
!
executed
{
if
!
executed
{
...
@@ -79,9 +89,61 @@ func TestServingTheActualFile(t *testing.T) {
...
@@ -79,9 +89,61 @@ func TestServingTheActualFile(t *testing.T) {
ioutil
.
WriteFile
(
filepath
.
Join
(
dir
,
"file"
),
[]
byte
(
fileContent
),
0600
)
ioutil
.
WriteFile
(
filepath
.
Join
(
dir
,
"file"
),
[]
byte
(
fileContent
),
0600
)
w
:=
httptest
.
NewRecorder
()
w
:=
httptest
.
NewRecorder
()
handleServeFile
(
&
dir
,
nil
)(
w
,
request
)
handleServeFile
(
&
dir
,
CacheDisabled
,
nil
)(
w
,
request
)
assertResponseCode
(
t
,
w
,
200
)
assertResponseCode
(
t
,
w
,
200
)
if
w
.
Body
.
String
()
!=
fileContent
{
if
w
.
Body
.
String
()
!=
fileContent
{
t
.
Error
(
"We should serve the file: "
,
w
.
Body
.
String
())
t
.
Error
(
"We should serve the file: "
,
w
.
Body
.
String
())
}
}
}
}
func
testServingThePregzippedFile
(
t
*
testing
.
T
,
enableGzip
bool
)
{
dir
,
err
:=
ioutil
.
TempDir
(
""
,
"deploy"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
os
.
RemoveAll
(
dir
)
httpRequest
,
_
:=
http
.
NewRequest
(
"GET"
,
"/file"
,
nil
)
request
:=
&
gitRequest
{
Request
:
httpRequest
,
relativeURIPath
:
"/file"
,
}
if
enableGzip
{
httpRequest
.
Header
.
Set
(
"Accept-Encoding"
,
"gzip, deflate"
)
}
fileContent
:=
"STATIC"
var
fileGzipContent
bytes
.
Buffer
fileGzip
:=
gzip
.
NewWriter
(
&
fileGzipContent
)
fileGzip
.
Write
([]
byte
(
fileContent
))
fileGzip
.
Close
()
ioutil
.
WriteFile
(
filepath
.
Join
(
dir
,
"file.gz"
),
fileGzipContent
.
Bytes
(),
0600
)
ioutil
.
WriteFile
(
filepath
.
Join
(
dir
,
"file"
),
[]
byte
(
fileContent
),
0600
)
w
:=
httptest
.
NewRecorder
()
handleServeFile
(
&
dir
,
CacheDisabled
,
nil
)(
w
,
request
)
assertResponseCode
(
t
,
w
,
200
)
if
enableGzip
{
assertResponseHeader
(
t
,
w
,
"Content-Encoding"
,
"gzip"
)
if
bytes
.
Compare
(
w
.
Body
.
Bytes
(),
fileGzipContent
.
Bytes
())
!=
0
{
t
.
Error
(
"We should serve the pregzipped file"
)
}
}
else
{
assertResponseCode
(
t
,
w
,
200
)
assertResponseHeader
(
t
,
w
,
"Content-Encoding"
,
""
)
if
w
.
Body
.
String
()
!=
fileContent
{
t
.
Error
(
"We should serve the file: "
,
w
.
Body
.
String
())
}
}
}
func
TestServingThePregzippedFile
(
t
*
testing
.
T
)
{
testServingThePregzippedFile
(
t
,
true
)
}
func
TestServingThePregzippedFileWithoutEncoding
(
t
*
testing
.
T
)
{
testServingThePregzippedFile
(
t
,
false
)
}
此差异已折叠。
点击以展开。
预览
0%
加载中
请重试
或
添加新附件
.
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
保存评论
取消
想要评论请
注册
或
登录