summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md11
-rw-r--r--example/main.lua5
-rw-r--r--main.go30
3 files changed, 32 insertions, 14 deletions
diff --git a/README.md b/README.md
index 5a16c26..58ed14c 100644
--- a/README.md
+++ b/README.md
@@ -67,7 +67,7 @@ as `luna.db["exec-tx"]`, `luna.http.encodeURL` as `luna.http["encode-url"]`
and so on.
Every argument in every API function is required: luna does not support optional
-arguments yet. Pass an empty value for corresponding type instead if you do not
+arguments yet. Pass an empty value of corresponding type instead if you do not
want to specify an argument: {} for tables, "" for strings, 0 for numbers.
@@ -84,11 +84,12 @@ end)
```
-__luna.router.static(pattern, directory)__:
-registers a new route for Go to serve static files.
+__luna.router.static(pattern, directory, cacheControl)__:
+registers a new route for Go to serve static files. If `cacheControl` is
+specified include Cache-Control header with specified value in a response.
```lua
-luna.router.static("GET /static/", "./whatever/path/static")
+luna.router.static("GET /static/", "./some/path/", "public, immutable, max-age=86400")
```
@@ -268,7 +269,7 @@ I will not accept patches that are
* relying on too many Go (or external) dependencies.
* adding too much complexity into the existing codebase.
* adding a new major functionality that should be implemented outside
- (for example service management, use systemd, shepherd or whatever).
+ (for example service management: use systemd, shepherd or whatever).
## Useful links
diff --git a/example/main.lua b/example/main.lua
index 335eaad..8cca405 100644
--- a/example/main.lua
+++ b/example/main.lua
@@ -90,8 +90,9 @@ luna.router.route("GET /request", function ()
return 200, {}, "Your IP is: " .. response.body
end)
--- serve static files from static/ directory
-luna.router.static("GET /static/", "static/")
+-- serve static files from static/ directory. also cache them on the client
+-- side for 1 hour.
+luna.router.static("GET /static/", "static/", "public, immutable, max-age=3600")
-- register a custom eval handler that will evaluate whatever is passed into
-- luna stdio when it's run with -D flag. in this case we print the result of
diff --git a/main.go b/main.go
index a290ce7..33d1cee 100644
--- a/main.go
+++ b/main.go
@@ -114,11 +114,21 @@ func main() {
return luaOk(l, nil)
}
routeModule["static"] = func (l *Lua) int {
- var route, dir string
- err := l.Scan(&route, &dir)
+ var route, dir, cacheControl string
+ err := l.Scan(&route, &dir, &cacheControl)
if err != nil {
return luaErr(l, err)
}
+ if route == "" {
+ return luaErr(l, errors.New("route must not be empty"))
+ }
+ if dir == "" {
+ return luaErr(l, errors.New("dir must not be empty"))
+ }
+ if !strings.HasSuffix(route, "/") {
+ route = route + "/"
+ }
+ dir = strings.TrimSuffix(dir, "/")
// check if route is already registered. otherwise
// mux.HandleFunc will panic
_, ok := routes[route]
@@ -127,12 +137,18 @@ func main() {
}
routes[route] = true
mu.Lock()
- mux.Handle(
+ mux.HandleFunc(
route,
- http.StripPrefix(
- strings.TrimPrefix(route, "GET "),
- http.FileServer(http.Dir(dir)),
- ),
+ func (w http.ResponseWriter, r *http.Request) {
+ path := strings.Trim(r.URL.Path, "/")
+ route := strings.TrimPrefix(route, "GET ")
+ route = strings.Trim(route, "/")
+ filepath := dir + strings.TrimPrefix(path, route)
+ if cacheControl != "" {
+ w.Header().Set("Cache-Control", cacheControl)
+ }
+ http.ServeFile(w, r, filepath)
+ },
)
mu.Unlock()
return luaOk(l, nil)