11 Star 11 Fork 0

Gitee 极速下载 / goa

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/goadesign/goa
克隆/下载
http_file_server.go 2.25 KB
一键复制 编辑 原始数据 按行查看 历史
ikawaha 提交于 2019-11-21 04:55 . v2: Files must appear in Service (#2384)
package dsl
import (
"strings"
"goa.design/goa/eval"
"goa.design/goa/expr"
)
// Files defines a endpoint that serves static assets via HTTP. The logic for
// what to do when the filename points to a file vs. a directory is the same as
// the standard http package ServeFile function. The path may end with a
// wildcard that matches the rest of the URL (e.g. {*filepath}). If it does the
// matching path is appended to filename to form the full file path, so:
//
// Files("/index.html", "/www/data/index.html")
//
// returns the content of the file "/www/data/index.html" when requests are sent
// to "/index.html" and:
//
// Files("/assets/{*filepath}", "/www/data/assets")
//
// returns the content of the file "/www/data/assets/x/y/z" when requests are
// sent to "/assets/x/y/z".
//
// Files must appear in Service.
//
// Files accepts 2 arguments and an optional DSL. The first argument is the
// request path which may use a wildcard starting with {* and ending with }.
// The second argument is the path on disk to the files being served. The
// file path may be absolute or relative to the current path of the process.
// The DSL allows specifying a description and documentation as well.
//
// Example:
//
// var _ = Service("bottle", func() {
// Files("/index.html", "/www/data/index.html", func() {
// Description("Serve home page.")
// Docs(func() {
// Description("Additional documentation")
// URL("https://goa.design")
// })
// })
// Files("/static/{*path}", "/www/data/static", func() {
// Description("Serve static content.")
// })
// })
//
func Files(path, filename string, fns ...func()) {
if len(fns) > 1 {
eval.ReportError("too many arguments given to Files")
return
}
// Make sure request path starts with a "/" so codegen can rely on it.
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
s, ok := eval.Current().(*expr.ServiceExpr)
if !ok {
eval.IncompatibleDSL()
return
}
r := expr.Root.API.HTTP.ServiceFor(s)
server := &expr.HTTPFileServerExpr{
Service: r,
RequestPaths: []string{path},
FilePath: filename,
}
if len(fns) > 0 {
eval.Execute(fns[0], server)
}
r.FileServers = append(r.FileServers, server)
}
1
https://gitee.com/mirrors/goa.git
git@gitee.com:mirrors/goa.git
mirrors
goa
goa
v2.2.5

搜索帮助