From 269cbdcd3a8e7bcb045c7731f51c80f524a9c6b0 Mon Sep 17 00:00:00 2001 From: Benaiah Mischenko Date: Mon, 11 Feb 2019 10:49:20 -0800 Subject: [PATCH] Initial OpenResty+Fennel setup --- .gitignore | 3 +++ .gitmodules | 3 +++ conf/nginx.conf | 15 +++++++++++++++ logs/.gitignore | 2 ++ makefile | 15 +++++++++++++++ modules/fennel | 1 + server/html.fnl | 33 +++++++++++++++++++++++++++++++++ server/server.fnl | 8 ++++++++ 8 files changed, 80 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 conf/nginx.conf create mode 100644 logs/.gitignore create mode 100644 makefile create mode 160000 modules/fennel create mode 100644 server/html.fnl create mode 100644 server/server.fnl diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0c3cd77 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# openresty nginx temporary directories +*_temp +server/*.lua \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..891763c --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "fennel"] + path = modules/fennel + url = git@github.com:bakpakin/fennel.git diff --git a/conf/nginx.conf b/conf/nginx.conf new file mode 100644 index 0000000..bce01ca --- /dev/null +++ b/conf/nginx.conf @@ -0,0 +1,15 @@ +worker_processes 1; +error_log logs/error.log; +events { + worker_connections 1024; +} +http { + server { + listen 8090; + location / { + default_type text/html; + content_by_lua_file server/server.lua; + lua_code_cache off; + } + } +} \ No newline at end of file diff --git a/logs/.gitignore b/logs/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/logs/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000..6cbff38 --- /dev/null +++ b/makefile @@ -0,0 +1,15 @@ +.PHONY: all +all: server + +.PHONY: clean +clean: clean-server + +.PHONY: server +server: server/server.lua server/html.lua + +.PHONY: clean-server +clean-server: + rm server/*.lua + +server/server.lua: server/server.fnl ; modules/fennel/fennel --compile $^ > $@ +server/html.lua: server/html.fnl ; modules/fennel/fennel --compile $^ > $@ diff --git a/modules/fennel b/modules/fennel new file mode 160000 index 0000000..472deb1 --- /dev/null +++ b/modules/fennel @@ -0,0 +1 @@ +Subproject commit 472deb1fc18f1e1f9021b2254e3060dcd82ac968 diff --git a/server/html.fnl b/server/html.fnl new file mode 100644 index 0000000..9a75c5e --- /dev/null +++ b/server/html.fnl @@ -0,0 +1,33 @@ +;; borrowed from https://github.com/technomancy/fennel-lang.org/blob/3c5d70e2edf9434dc7b7582015b0457da448c87b/html.fnl + +;; A *very* basic HTML generation library. +;; No escaping features; never use this on user input! + +(local map (fn [f tbl] + (let [out {}] + (each [i v (ipairs tbl)] + (tset out i (f v))) + out))) + +(local map-kv (fn [f tbl] + (let [out {}] + (each [k v (pairs tbl)] + (table.insert out (f k v))) + out))) + +(local to-attr (fn [k v] + (if (= v true) k + (.. k "=\"" v"\"")))) + +(local tag (fn [tag-name attrs] + (assert (= (type attrs) "table") "Missing attrs table") + (let [attr-str (table.concat (map-kv to-attr attrs) " ")] + (.. "<" tag-name " " attr-str">")))) + +(fn html [doc] + (if (= (type doc) "string") + doc + (let [[tag-name attrs & body] doc] + (.. (tag tag-name attrs) + (table.concat (map html body) " ") +"")))) diff --git a/server/server.fnl b/server/server.fnl new file mode 100644 index 0000000..6b98d34 --- /dev/null +++ b/server/server.fnl @@ -0,0 +1,8 @@ +(global ngx ngx) +(local html (require :html)) + +(local homepage + [:div {} + [:h1 {} "Hello, world!"]]) + +(ngx.say (html homepage)) -- 2.45.2