Files defines an 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
(path, filename string, fns ...func())
| 48 | // }) |
| 49 | // }) |
| 50 | func Files(path, filename string, fns ...func()) { |
| 51 | if len(fns) > 1 { |
| 52 | eval.TooManyArgError() |
| 53 | return |
| 54 | } |
| 55 | // Make sure request path starts with a "/" so codegen can rely on it. |
| 56 | if !strings.HasPrefix(path, "/") { |
| 57 | path = "/" + path |
| 58 | } |
| 59 | s, ok := eval.Current().(*expr.ServiceExpr) |
| 60 | if !ok { |
| 61 | eval.IncompatibleDSL() |
| 62 | return |
| 63 | } |
| 64 | r := expr.Root.API.HTTP.ServiceFor(s, expr.Root.API.HTTP) |
| 65 | server := &expr.HTTPFileServerExpr{ |
| 66 | Service: r, |
| 67 | RequestPaths: []string{path}, |
| 68 | FilePath: filename, |
| 69 | } |
| 70 | if len(fns) > 0 { |
| 71 | eval.Execute(fns[0], server) |
| 72 | } |
| 73 | r.FileServers = append(r.FileServers, server) |
| 74 | } |