Skip to content

Commit

Permalink
static: support http filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jan 19, 2024
1 parent 0eb3814 commit 4f1fbc2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
10 changes: 10 additions & 0 deletions classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ func (p App) Delete(path string, handle func(ctx *Context)) {
p.Route(http.MethodDelete, path, handle)
}

// Static serves static files from a dir (default is "$YapFS/static").
func (p *App) Static__0(pattern string, dir ...fs.FS) {
p.Static(pattern, dir...)
}

// Static serves static files from a http file system.
func (p *App) Static__1(pattern string, fs http.FileSystem) {
p.StaticHttp(pattern, fs)
}

// Gopt_App_Main is required by Go+ compiler as the entry of a YAP project.
func Gopt_App_Main(app interface{ initApp() }) {
app.initApp()
Expand Down
4 changes: 2 additions & 2 deletions demo/classfile_static/gop_autogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ type staticfile struct {
//line demo/classfile_static/staticfile_yap.gox:1
func (this *staticfile) MainEntry() {
//line demo/classfile_static/staticfile_yap.gox:1:1
this.Static("/foo", this.FS("public"))
this.Static__0("/foo", this.FS("public"))
//line demo/classfile_static/staticfile_yap.gox:2:1
this.Static("/")
this.Static__0("/")
//line demo/classfile_static/staticfile_yap.gox:4:1
this.Run(":8888")
}
Expand Down
7 changes: 6 additions & 1 deletion yap.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,15 @@ func (p *Engine) Static(pattern string, dir ...fs.FS) {
} else {
fsys = p.FS("static")
}
p.StaticHttp(pattern, http.FS(fsys))
}

// StaticHttp serves static files from fsys (http.FileSystem).
func (p *Engine) StaticHttp(pattern string, fsys http.FileSystem) {
if !strings.HasSuffix(pattern, "/") {
pattern += "/"
}
p.Mux.Handle(pattern, http.StripPrefix(pattern, http.FileServer(http.FS(fsys))))
p.Mux.Handle(pattern, http.StripPrefix(pattern, http.FileServer(fsys)))
}

// Handle registers the handler function for the given pattern.
Expand Down

0 comments on commit 4f1fbc2

Please sign in to comment.