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

[wip] mostly-working proof-of-concept #1

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly

- package-ecosystem: docker
directory: /
schedule:
interval: weekly

3 changes: 3 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
std = "min"
ignore = {"212"}

4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- repo: https://github.com/JohnnyMorganz/StyLua
rev: v0.20.0
hooks:
- id: stylua # or stylua-system / stylua-github
46 changes: 15 additions & 31 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,46 +1,30 @@
FROM openresty/openresty:centos
# TODO move to leaner (alpine?) image
# https://github.com/leafo/lapis-archlinux-docker/blob/master/lapis-archlinux-itchio/Dockerfile
FROM ghcr.io/leafo/lapis-archlinux-itchio:latest

# Build Args
ARG OPENSSL_DIR="/usr/local/openresty/openssl"
# RUN pacman -Sy sqlite --noconfirm && \
# (yes | pacman -Scc || :)

# Environment
ENV LAPIS_ENV="production"
ENV LAPIS_ENV="development"

# Prepare volumes
VOLUME /var/data
VOLUME /var/www

# Install from Yum
RUN yum -y install \
epel-release \
gcc \
openresty-openssl-devel \
openssl-devel \
sqlite-devel \
; yum clean all

RUN yum config-manager --set-enabled powertools

# Install from LuaRocks
RUN luarocks install luasec
RUN luarocks install bcrypt
RUN luarocks install busted
RUN luarocks install i18n
RUN luarocks install lapis \
CRYPTO_DIR=${OPENSSL_DIR} \
CRYPTO_INCDIR=${OPENSSL_DIR}/include \
OPENSSL_DIR=${OPENSSL_DIR} \
OPENSSL_INCDIR=${OPENSSL_DIR}/include
RUN luarocks install lsqlite3
RUN luarocks install luacov
RUN luarocks install mailgun
RUN luarocks install markdown
RUN eval $(luarocks --lua-version=5.1 path)
RUN export LUA_PATH="$LUA_PATH;/usr/local/openresty/lualib/?.lua"

# install lua dependencies
COPY pagesix-dev-1.rockspec /
RUN luarocks --lua-version=5.1 build --tree "$HOME/.luarocks" --only-deps /pagesix-dev-1.rockspec

# Entrypoint
ADD docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

# Standard web port (use a reverse proxy for SSL)
EXPOSE 80

WORKDIR /var/www

ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
48 changes: 48 additions & 0 deletions Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# FROM archlinux:latest

# RUN pacman -Sy base-devel lua51 sqlite luarocks redis geoip libmaxminddb tup git openssl-1.1 --noconfirm && \
# (yes | pacman -Scc || :)

# # setup openresty
# ARG OPENRESTY_VERSION="1.21.4.2rc1"
# RUN curl -O https://openresty.org/download/openresty-${OPENRESTY_VERSION}.tar.gz && \
# tar xvfz openresty-${OPENRESTY_VERSION}.tar.gz && \
# (cd openresty-${OPENRESTY_VERSION} && ./configure --with-pcre-jit --with-cc-opt="-I/usr/include/openssl-1.1" --with-ld-opt="-L/usr/lib/openssl-1.1" && make && make install) && \
# rm -rf openresty-${OPENRESTY_VERSION} && rm openresty-${OPENRESTY_VERSION}.tar.gz

# # Build Args
# ARG OPENSSL_DIR="/usr/local/openresty/openssl"

# # Environment
# ENV LAPIS_ENV="production"

# # Prepare volumes
# VOLUME /var/data

# RUN eval $(luarocks --lua-version=5.1 path)
# RUN export LUA_PATH="$LUA_PATH;/usr/local/openresty/lualib/?.lua"

# # install lua dependencies
# COPY pagesix-dev-1.rockspec /
# RUN luarocks --lua-version=5.1 build --tree "$HOME/.luarocks" --only-deps /pagesix-dev-1.rockspec

# # Entrypoint
# COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
# RUN chmod +x /usr/local/bin/docker-entrypoint.sh

# # Standard web port (use a reverse proxy for SSL)
# EXPOSE 80

# WORKDIR /var/www

# COPY app/app.lua ./app.lua
# COPY app/config.lua ./config.lua

# COPY app/migrations.lua ./migrations.lua
# COPY app/mime.types ./mime.types
# COPY app/nginx.conf ./nginx.conf

# COPY app/src ./src
# COPY app/static ./static

# ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# Page Six

A better social link-sharing website.
A better social link-sharing site.

## TODO

- [ ] change the name
- [ ] model relationships defined:
* 1 User -> 1 Comment
* many comments -> 1 Post
* many posts -> 1 Subreddit
* many subreddits -> 1 Subreddits listing
* 1 User -> 1 Comment
* many comments -> 1 Post
* many posts -> 1 Subreddit
* many subreddits -> 1 Subreddits listing
- [ ] add [Constraints](https://leafo.net/lapis/reference/models.html#constraints) to models (?)
- [ ] add table indexes (hot-sorted subreddit posts, homepage, user accounts)
- [ ] user accounts w/[CSRF](https://leafo.net/lapis/reference/utilities.html#csrf-protection )
Expand All @@ -29,6 +28,14 @@ A better social link-sharing website.

From the root directory:

Build:

```
docker build . -t pagesix
```

Run:

```
docker run \
-dti \
Expand All @@ -38,10 +45,16 @@ docker run \
-p 8080:80 \
--name pagesix \
--platform=linux/amd64 \
karai17/lapis-centos:latest
-d pagesix
```

Run migrations to populate the DB

```
lapis migrate
```

Then, visit: http://localhost:8080/
(wait patiently) then, visit: http://localhost:8080/

# Notes

Expand Down
100 changes: 100 additions & 0 deletions app/app.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
--- Page Six - A Reddit Clone
-- @script pagesix
-- @author Michael Burns
-- @license Apache License v2.0

local lapis = require("lapis")
local r2 = require("lapis.application").respond_to
local after_dispatch = require("lapis.nginx.context").after_dispatch
-- local to_json = require("lapis.util").to_json
local cached = require("lapis.cache").cached
local console = require("lapis.console")

local app = lapis.Application()

-- print("HANDLING REQUEST") -- DEBUG

app:before_filter(function(self)
after_dispatch(function()
-- https://leafo.net/lapis/reference/configuration.html#performance-measurement
-- print(to_json(ngx.ctx.performance))
end)
end)

function app:default_route()
ngx.log(ngx.NOTICE, "Unknown path " .. self.req.parsed_url.path) -- luacheck: ignore

-- call the original implementaiton to preserve the functionality it provides
return lapis.Application.default_route(self)
end

function app:handle_404()
error("\n\nFailed to find route: " .. self.req.request_uri .. "\n")
return { status = 404, layout = true, "Not Found!" }
end

app:enable("etlua")

app.layout = require("views.layout")

app:match("homepage", "/", r2(require("actions.index"))) -- hot sort

-- best
-- hot
app:match("new", "/new", cached({ exptime = 1, r2(require("actions.index")) }))
app:match("rising", "/rising", cached({ exptime = 1, r2(require("actions.index")) }))
app:match("controversial", "/controversial", cached({ exptime = 1, r2(require("actions.index")) }))
app:match("top", "/top", cached({ exptime = 1, r2(require("actions.index")) }))


app:match("new_subreddit", "/r/:subreddit/new", cached({ exptime = 1, r2(require("actions.r_subreddit")) }))
app:match("rising_subreddit", "/r/:subreddit/rising", cached({ exptime = 1, r2(require("actions.index")) }))
app:match(
"controversial_subreddit",
"/r/:subreddit/controversial",
cached({ exptime = 1, r2(require("actions.index")) })
)
app:match("top_subreddit", "/r/:subreddit/top", cached({ exptime = 1, r2(require("actions.index")) }))

app:match("user_profile", "/user/:user_name(/:type)", r2(require("actions.user")))

app:match("comments", "/comments", cached({ exptime = 1, r2(require("actions.index")) }))
app:match("domains", "/domain/:domain", cached({ exptime = 1, r2(require("actions.domain")) }))
app:match("subreddits", "/subreddits(/:type)", cached({ exptime = 1, r2(require("actions.subreddits")) }))

-- app:match("subreddits", "/subreddits/search", r2(require "actions.subreddits"))
-- app:match("subscribed", "/subscribed", r2(require "actions.index")) -- only for logged in users

-- meta subreddits
app:match("popular", "/r/popular", cached({ exptime = 1, r2(require("actions.r_popular")) }))
app:match("all", "/r/all", cached({ exptime = 1, r2(require("actions.r_all")) }))
app:match("random", "/r/random", cached({ exptime = 1, r2(require("actions.r_random")) }))
app:match("subreddit", "/r/:subreddit", cached({ exptime = 1, r2(require("actions.r_subreddit")) }))

app:match(
"post",
"/r/:subreddit/comments/:post_id[%w](/:title_stub)",
cached({ exptime = 1, r2(require("actions.post")) })
)
app:match(
"comment",
"/r/:subreddit/comments/:post_id[%w]/:title_stub/:comment_id[%w](/:q)",
cached({ exptime = 1, r2(require("actions.comment")) })
)

app:match("prefs", "/prefs", cached({ exptime = 10, function(self) end })) -- stub

app:match("about", "/about", cached({ exptime = 10, function(self) end })) -- stub
app:match("contact", "/contact", cached({ exptime = 10, function(self) end })) -- stub
app:match("help", "/help", cached({ exptime = 10, function(self) end })) -- stub
app:match("submit", "/submit", cached({ exptime = 10, function(self) end })) -- stub

app:get("/admin", function(self)
return "Go away"
end)
app:match("/console", console.make()) -- only available in Development builds

require("src.api")(app) -- API endpoints
require("src.auth")(app) -- User-authenticated endpoints

return app
49 changes: 49 additions & 0 deletions app/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--- Pagesix config
-- @script pagesix.config

local config = require("lapis.config")

-- Maximum file size
local body_size = "1m"

-- Path to your local project files
local lua_path = "./src/?.lua;./src/?/init.lua;./libs/?.lua;./libs/?/init.lua"
local lua_cpath = ""

config("development", {
port = 80,
body_size = body_size,
lua_path = lua_path,
lua_cpath = lua_cpath,
server = "nginx",
code_cache = "off",
num_workers = "1",
name = "[DEVEL] Page Six",
session_name = "dev_app_session",
secret = "hunter42", -- TODO: manage Secrets
measure_performance = true,
sqlite = {
database = "/var/data/dev.sqlite",
},
})

config("production", {
port = 80,
body_size = body_size,
lua_path = lua_path,
lua_cpath = lua_cpath,
code_cache = "on",
server = "nginx",
num_workers = "3",
name = "Page Six",
session_name = "prod_app_session",
secret = os.getenv("LAPIS_SECRET"),
logging = {
requests = true,
queries = false,
server = true,
},
sqlite = {
database = "/var/data/production.sqlite",
},
})
14 changes: 14 additions & 0 deletions app/helpers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--- Helpers script
-- @script helpers

local rand_id = function(n)
local r = {}
for i = 1, n do
r[i] = string.char(math.random(65, 90))
end
return table.concat(r)
end

return {
rand_id = rand_id,
}
Empty file added app/libs/utilities.lua
Empty file.
Loading
Loading