Skip to content

Commit

Permalink
Migration from lighttouch-base
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-zam committed Jan 29, 2019
1 parent c91b9e9 commit a593a4b
Show file tree
Hide file tree
Showing 157 changed files with 1,758 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.mp
/contentdb
/third-party
/log
65 changes: 65 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<p align="center"><img width="100" src="https://i.imgur.com/zwjfmrF.png" alt="lighttouch logo"><br>This Project is Currently in Stealth Mode.<br>please do not post a news story until v0.1 is released very shortly.<br>thank you.</p>

<p align="center">
<a href="https://github.com/foundpatterns/lighttouch/issues"><img src="https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=" alt="Contributions Welcome"></a>
<a href="https://discord.gg/b6MY7dG"><img src="https://img.shields.io/badge/chat-on%20discord-7289da.svg" alt="Chat"></a>
</p>

Lighttouch is a framework that makes complex application development simpler. It does this through broad use of [component-oriented design](https://en.wikipedia.org/wiki/Component-based_software_engineering) intended to offer programmers a well-researched alternative to starting with a blank canvas - "put your code anywhere".

[Code organized](https://en.wikipedia.org/wiki/Structured_programming) this way is:
- easier to program
- unlimited in its reusability of implementations
- easier to extend with other developers' plugins
- possible to socially cross-pollenate diverse and complex applications
- possible to use as a base for [visual programming environments](https://en.wikipedia.org/wiki/Visual_programming_language)

Without this, applications tend to:
- get walled into handling only a single use case
- grow, but at huge cost and pain while extending
- repetively [make messy mistakes](https://news.ycombinator.com/item?id=18443327), like [spaghetti code](http://wiki.c2.com/?SpaghettiCode) and [code duplication](http://wiki.c2.com/?DuplicatedCode)
- suffer too many layers of abstraction

Below you'll learn what is ECA-Rules, but to begin let's start with a simple diagram from the [Drupal Rules module](https://drupal.org/project/rules), a project which greatly influenced this one.

<img src="https://dev.acquia.com/sites/default/files/blog/rules_eca.png" alt="ECA-Rules diagram">

### Lighttouch Packages

Packages are Lighttouch's main unit of addon functionality. They leverage [event-driven](https://en.wikipedia.org/wiki/Event-driven_programming), [rule-based](https://en.wikipedia.org/wiki/Rule-based_system) programming. This basically means that packages have actions, events, and rules. Events are like [hooks](https://stackoverflow.com/questions/467557/what-is-meant-by-the-term-hook-in-programming) where additional logic can run. Rules check a conditional statement of any complexity and trigger events. When an event triggers, its actions run in weight order. Actions are the individual mechanisms of additional functionality that have a distilled purpose and can run on any associated event.

80% of this functionality builds on top of [Luvent: A Simple Event Library for Lua](https://github.com/ejmr/Luvent). You could probably get away with the summary below of Lighttouch's use of its API, but it's still worth checking out. It has a great Readme. The basic difference between what Luvent does and what Lighttouch does is that while writing a Lighttouch app, you put functionality into individual files, and Lighttouch sets everything up for you.

Events are very simple. They get loaded into a global list by reading each package's `events.txt` file. They can be disabled in a `disabled_events.txt` file. (aside: Potential improvements include a simpler interface and ordering execution by weight.)

Rules are basically an `IF` statement with some metadata. If the configured conditions, specified in the body of a rule as Lua code are `TRUE`, then the specified events will trigger and the objects specified in the input parameters will get passed to the attached actions.

Actions are individual, [lazy loaded functions](https://whatis.techtarget.com/definition/lazy-loading-dynamic-function-loading) that can run where they are needed. You simply code what you want to do, and leave the parts about when it will run and in what order to the other logic connected through the yaml header. Actions should not have conditions in them.

### Core Modules (including third-party)

Lighttouch also provides modules for content management, robust logging, syntax sugar, and more. These core modules + loaders mentioned for packages + and init script make up [Lighttouch Base](https://github.com/foundpatterns/lighttouch-base), which has:
- Packaged ECA-Rules Loaders: described above
- Content Module: file-based, document-oriented databases (see Grav) with data models in YAML, similar to JSON Schema (content types), and validation
- Logging: automatically setup out of the box (set log level in `config.toml`, default is `INFO`)
- ...

### Packages

- Lighttouch Web Logging: (for web automation) log every incoming request, outgoing response, outgoing request, and incoming response (server and client logging out of the box.. makes debugging web applications much easier) to log directory (log entries are content, btw)
- Lighttouch JSON Interface: REST API built on content module
- Lighttouch HTTP Interface: form-driven content entry mechanism, username:password protected (autogenerated at each initialization)
- Lighttouch Crypto Tools: setup a profile with cryptographic signing keys for yourself and friends to verify incoming responses and requests, and sign outgoing requests and responses + and more

### Installation

Lightouch has 3 dependencies:
* **[torchbear](https://github.com/foundpatterns/torchbear)** (an [application framework](https://stackoverflow.com/questions/4241919/what-is-meant-by-application-framework))
* **[git](https://git-scm.com)** (a [version control manager](https://en.wikipedia.org/wiki/Version_control))
* **[peru](https://github.com/buildinspace/peru)** (a [package manager](https://en.wikipedia.org/wiki/Package_manager))

Once these are installed, clone the repo with git, run `peru sync` to install its components, and run it with `torchbear`. To update, use `git pull` and `peru reup`.

### Developing

To modify any functionality in Lighttouch, create a git repo outside of it for the component your are modifying, and add an override in `peru.yaml` so that it will use that source directory. For example, to modify `lightouch-base`, clone it somewhere else, then run `peru override add lighttouch-base {{ path to lighttouch-base repo }}`. Then, after making each change to the source, run `peru sync` to update your Lighttouch.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title = "Fast"
model = "slide"
slideshow = "c800a360-d198-4a87-877e-b353f7dd0a9d"
image-address = "http://unsplash.com/photos/CpkOjOcXdUY/download?force=true"
weight = 1
...
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title = "Simple"
model = "slide"
slideshow = "c800a360-d198-4a87-877e-b353f7dd0a9d"
image-address = "http://unsplash.com/photos/x2Tmfd1-SgA/download?force=true"
weight = 2
...
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title = "Powerful"
model = "slide"
slideshow = "c800a360-d198-4a87-877e-b353f7dd0a9d"
image-address = "http://unsplash.com/photos/pqvp1P7re-w/download?force=true"
weight = 3
...
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title = "Found Patterns Introduction"
model = "slideshow"
...
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
model = "key"
kind = "sign_private"
profile = "d3562615-c6c9-4a41-8fb9-f39117ed55cd"
...
+qEY1pRSYy7gTfJ58GLrDQTuhgiTf49Cy9yEgvix3vHGkq2b5t55E36RPtVYgnTn+2SF0Of8nEeVOyTvcvlnnQ==
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
model = "key"
kind = "sign_public"
profile = "d3562615-c6c9-4a41-8fb9-f39117ed55cd"
...
xpKtm+beeRN+kT7VWIJ05/tkhdDn/JxHlTsk73L5Z50=
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
host = "http://foundpatterns.com:3000/"
model = "place"
profile = "d3562615-c6c9-4a41-8fb9-f39117ed55cd"
...
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
model = "profile"
name = "foundpatterns"
...
1 change: 1 addition & 0 deletions home-store.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0382e080-dd03-46e2-9610-115b72b3af27
63 changes: 49 additions & 14 deletions import.scl
Original file line number Diff line number Diff line change
@@ -1,30 +1,65 @@
imports = {
luvent = "third-party/",
valua = "third-party/",
torchbear-libs = "third-party/",
contentdb-lua = "contentdb",
torchbear-libs-dev = "third-party/",
lighttouch-logging = "packages/lighttouch-logging",
lighttouch-json-interface = "packages/lighttouch-json-interface",
lighttouch-html-interface = "packages/lighttouch-html-interface",
homepage-package = "packages/homepage-package",
base-theme = "themes/base-theme",
send-file-package = "packages/send-file-package",
lighttouch-change-management = "packages/lighttouch-change-management",
luvent = "third-party/",
valua = "third-party/",
torchbear-libs = "third-party/",
contentdb-lua = "contentdb",
torchbear-libs-dev = "third-party/",
}

torchbear-libs = {
url = "https://github.com/foundpatterns/torchbear-libs",
repo = true,
url = "https://github.com/foundpatterns/torchbear-libs",
repo = true,
}

torchbear-libs-dev = {
url = "https://github.com/foundpatterns/torchbear-libs-dev",
repo = true,
url = "https://github.com/foundpatterns/torchbear-libs-dev",
repo = true,
}

luvent = {
url = "https://github.com/ejmr/Luvent",
export = "src"
url = "https://github.com/ejmr/Luvent",
export = "src"
}
valua = {
url = "https://github.com/sailorproject/valua",
pluck = "valua.lua",
url = "https://github.com/sailorproject/valua",
pluck = "valua.lua",
}

contentdb-lua = {
url = "https://github.com/foundpatterns/contentdb.lua",
url = "https://github.com/foundpatterns/contentdb-lua",
}

lighttouch-logging = {
url = "https://github.com/foundpatterns/lighttouch-logging",
}

lighttouch-json-interface = {
url = "https://github.com/foundpatterns/lighttouch-json-interface",
}

lighttouch-html-interface = {
url = "https://github.com/foundpatterns/lighttouch-html-interface",
}

homepage-package = {
url = "https://github.com/foundpatterns/homepage-package",
}

base-theme = {
url = "https://github.com/foundpatterns/base-theme",
}

send-file-package = {
url = "https://github.com/foundpatterns/send-file-package",
}

lighttouch-change-management = {
url = "https://github.com/foundpatterns/lighttouch-change-management",
}
1 change: 1 addition & 0 deletions init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env torchbear
-- Lighttouch · Torchbear App

-- this config must be before requires
Expand Down
11 changes: 11 additions & 0 deletions loaders/actions/base.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
actions_loader = { }

require("loaders.actions.mod")

default_package_searchers2 = package.searchers[2]
package.searchers[2] = actions_loader.custom_require
-- actions requiring
-- runs the function against all packages in packages_path
each(fs.directory_list(_G.packages_path), actions_loader.create_actions)

return actions_loader
53 changes: 53 additions & 0 deletions loaders/classes.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
local function load_classes (themes)
for _, theme in pairs(themes) do
log.trace("Loading classes in theme " .. theme.name)

for class_filename, classes in pairs(theme.files) do
--local pattern = "^(.+/)_class/(.+%.%w+)%-(.+)%.txt$"
--local dir, basename, elem = class_filename:match(pattern)

local pattern = [[^((?:.+/)?)_class/(.+\.\w+)/(.+)\.txt$]]
local regex_object = regex.new(pattern)
local matches = regex_object:capture(class_filename)

-- If current file is a class file
if matches then
log.trace("Processing class file " .. class_filename)

local dir, basename, elem = matches:get(1), matches:get(2), matches:get(3)

local filename = basename
if dir ~= "" then
filename = dir .. basename
end

local content = theme.files[filename]

-- Execute the sustitution
if content then

-- Either an id or just a tag name
if elem:sub(1,1) == "#" then
local id = elem:sub(2, -1)
content = string.gsub(content,
"<([^>]*%sid=\"" .. id .. "\"[^>]*)>",
"<%1 class=\"\n" .. classes .. "\">"
)
else
-- This *maaay* break if a tag name contains another tag name at the beggining
content = string.gsub(content,
"<(" .. elem .. "[^>]*)>",
"<%1 class=\"\n" .. classes .. "\">"
)
end

theme.files[filename] = content
else
log.error("File " .. filename .. " not found")
end
end
end
end
end

themes_loader.add_preprocessor(load_classes)
19 changes: 19 additions & 0 deletions loaders/events/base.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
events_loader = { }

require("loaders.events.mod")

events_loader.create_base_events()

-- Loop over each package, parse its events, and create any not seen before

each(
fs.directory_list(_G.packages_path),
function (package_name)
local package_path = _G.packages_path .. "/" .. package_name .. "/"
local events_strings = events_loader.parse_events_list(package_name)
events_loader.create_events( #events_strings, events_strings )
events_loader.read_disabled_actions(package_path)
end
)

return events_loader
8 changes: 8 additions & 0 deletions loaders/themes/base.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
themes = {}
themes_loader = {
preprocessors = {}
}

require("loaders.themes.mod")

return themes_loader
13 changes: 13 additions & 0 deletions packages/homepage-package/actions/send_about.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
event = ["about_requested"]
priority = 1
input_parameters = ["request"]

local about = fs.read_file("about.md")

return {
status = 200,
headers = {
["content-type"] = "text/html",
},
body = markdown_to_html(about)
}
39 changes: 39 additions & 0 deletions packages/homepage-package/actions/send_homepage.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
event = ["homepage_requested"]
priority = 1
input_parameters = ["request"]

local slideshow_id = torchbear.settings.slideshow
local slideshow_fields = content.read_document(slideshow_id)

local slide_weights = {}

content.walk_documents(nil, function (slide_id, fields, body)
if fields.model == "slide" and fields.slideshow == slideshow_id then
table.insert(slide_weights, {
content = render(torchbear.settings.theme .. "/homepage-slide.html", {
img = fields["image-address"],
title = fields.title,
body = body,
}),
weight = fields.weight or math.maxinteger,
})
end
end)

table.sort(slide_weights, function (a, b) return a.weight < b.weight end)
local slides = map(slide_weights, function (it) return it.content end)

local homepage = render(torchbear.settings.theme .. "/index.html", {
SITE_URL = "/",
SITENAME = torchbear.settings.sitename,
articles = {},
slides = slides,
})

return {
status = 200,
headers = {
["content-type"] = "text/html",
},
body = homepage
}
3 changes: 3 additions & 0 deletions packages/homepage-package/events.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
homepage_requested
about_requested

9 changes: 9 additions & 0 deletions packages/homepage-package/rules/about_requested.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
priority = 1
input_parameter = "request"
events_table = ["about_requested"]

request.method == "GET"
and
#request.path_segments == 0
and
request.query.about
9 changes: 9 additions & 0 deletions packages/homepage-package/rules/homepage_requested.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
priority = 1
input_parameter = "request"
events_table = ["homepage_requested"]

request.method == "GET"
and
#request.path_segments == 0
and
count_pairs(request.query) == 0
6 changes: 6 additions & 0 deletions packages/lighttouch-change-management/models/project.scl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
description = "Project"
fields = {
name = {
type = "string"
}
}
Loading

0 comments on commit a593a4b

Please sign in to comment.