Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

static: support http filesystem #26

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading