From 9e5005a516e840692a534dd599e1859e6bd0aaee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20P=C3=A9rez?= Date: Thu, 14 Nov 2024 11:13:26 +0100 Subject: [PATCH] serves static files --- .gitignore | 1 + example/main.go | 4 ++- example/static/img/dummy.png | Bin 0 -> 1785 bytes .../{page => }/component.list.gohtml | 0 .../templates/{page => }/page.index.gohtml | 2 ++ ron.go | 26 ++++++++++++++++++ 6 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 example/static/img/dummy.png rename example/templates/{page => }/component.list.gohtml (100%) rename example/templates/{page => }/page.index.gohtml (86%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5292519 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +logs/ \ No newline at end of file diff --git a/example/main.go b/example/main.go index 56c01bc..d9a188a 100644 --- a/example/main.go +++ b/example/main.go @@ -14,7 +14,9 @@ func main() { htmlRender := ron.NewHTMLRender() r.Renderer = htmlRender - r.GET("/", helloWorld) + r.Static("static", "static") + + //r.GET("/", helloWorld) r.GET("/json", helloWorldJSON) r.POST("/another", anotherHelloWorld) r.GET("/html", helloWorldHTML) diff --git a/example/static/img/dummy.png b/example/static/img/dummy.png new file mode 100644 index 0000000000000000000000000000000000000000..885878ca0b06af9f79305c4ba33729187a048467 GIT binary patch literal 1785 zcmb_ci&qi`7iabPc?{E)pPCw4`#zcvtj$8Fi(2L*hp*5Ov~s- zcL4x^-9cyk5CFgq6aer^{=aq_JwH3_WEqV^cyOdYgTdI|-exkH0)arGP-rxo#l^** zo}T*p`i#T*UPhSNr88(E0ATU_qkcknvv2?a{+$`*cPjFyl6R^;;$Xl&@#UCXgAKr- z*ErjW@n7s7!(O;ID%u7!9lvyM3_LG07w=Qzx8}ZWXORmW?P#alB{xrIf4W_d*;r)C zlfi4(jamLNS?Fw8+nigjUOT%iUGl2dRSCipOWoTksnc(g>f(;il0D#(h)R%QyfCGP zD(rn7ix__93o}nudxfnN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JXLugd=;Z;*eeU#;(Vcm59({$IHO literal 0 HcmV?d00001 diff --git a/example/templates/page/component.list.gohtml b/example/templates/component.list.gohtml similarity index 100% rename from example/templates/page/component.list.gohtml rename to example/templates/component.list.gohtml diff --git a/example/templates/page/page.index.gohtml b/example/templates/page.index.gohtml similarity index 86% rename from example/templates/page/page.index.gohtml rename to example/templates/page.index.gohtml index b66954b..8d43466 100644 --- a/example/templates/page/page.index.gohtml +++ b/example/templates/page.index.gohtml @@ -8,6 +8,8 @@ + + {{ .Data.message }} diff --git a/ron.go b/ron.go index e9e5b9b..a7c2fa8 100644 --- a/ron.go +++ b/ron.go @@ -8,6 +8,7 @@ import ( "log/slog" "net/http" "os" + "strings" "time" ) @@ -85,6 +86,31 @@ func (e *Engine) POST(path string, handler func(*Context)) { }) } +// Static serves static files from a specified directory, accessible through a defined URL path. +// +// The `path` parameter represents the URL prefix to access the static files. +// The `dir` parameter represents the actual filesystem path where the static files are located. +// +// Example: +// Calling r.Static("assets", "./folder") will make the contents of the "./folder" directory +// accessible in the browser at "/assets". For instance, a file located at "./folder/image.png" +// would be available at "/assets/image.png" in HTML templates. +func (e *Engine) Static(path, dir string) { + if !strings.HasPrefix(path, "/") { + path = "/" + path + } + if !strings.HasSuffix(path, "/") { + path = path + "/" + } + if !strings.HasPrefix(dir, "./") { + dir = "./" + dir + } + + fs := http.FileServer(http.Dir(dir)) + e.mux.Handle(path, http.StripPrefix(path, fs)) + slog.Info("Static files served", "path", path, "dir", dir) +} + func (c *Context) JSON(code int, data any) { c.W.WriteHeader(code) c.W.Header().Set("Content-Type", "application/json")