Skip to content

Commit

Permalink
mostly-working poc
Browse files Browse the repository at this point in the history
add luarocks packages, use dnf, set WORKDIR in Dockerfile for easier 'lapis migrate' command'

model, view updates

add ldoc comments

/subreddits works again

subreddits listing and subs work
  • Loading branch information
mburns committed Jun 26, 2023
1 parent e0adba6 commit 833da32
Show file tree
Hide file tree
Showing 73 changed files with 3,209 additions and 35 deletions.
49 changes: 21 additions & 28 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,46 +1,39 @@
FROM openresty/openresty:centos
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"
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"]
11 changes: 5 additions & 6 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 Down
50 changes: 50 additions & 0 deletions app/app.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--- 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 console = require("lapis.console")

local app = lapis.Application()

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, "User hit unknown path " .. self.req.parsed_url.path)

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

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

app:enable("etlua")

app.layout = require "views.layout"

app:match("homepage", "/", r2(require "actions.index"))
app:match("subreddits", "/subreddits(/:type)", r2(require "actions.subreddits"))
app:match("subreddit", "/r/:subreddit[%w](/:sort)", r2(require "actions.subreddit"))
app:match("post", "/r/:subreddit/comments/:post_id(/:title_stub)", r2(require "actions.post"))
app:match("comment", "/r/:subreddit/comments/:post_id/:title_stub/:comment_id(/:q)", r2(require "actions.comment"))

app:match("/console", console.make()) -- only available in Development builds

require("src.admin")(app) -- Admin endpoints
require("src.api")(app) -- API endpoints
require("src.auth")(app) -- User-authenticated endpoints
require("src.urls")(app) -- additional URLs

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"
}
})
Empty file added app/libs/utilities.lua
Empty file.
91 changes: 91 additions & 0 deletions app/migrations.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
--- Migrations
-- @script migrations

local db = require "lapis.db"
-- local schema = require("lapis.db.schema")
-- local types = schema.types
local json = require("cjson")
-- local misc = require("utils.misc")

local Users = require("src.models.users")
local Pagesix = require("src.models.pagesix")
-- local Posts = require("src.models.posts")
local io = require("io")

-- local Subreddit = require("src.models.subreddit")
local Subreddits = require("src.models.subreddits")

-- add each incremental migration whose key is the unix timestamp
return {
-- create initial tables: Users, Subreddits
[1] = function()
Pagesix:bootstrap()
end,

-- create first User
[2] = function()
Users:create({
user_name = "anonymous_coward",
user_email = "anonymous@localhost",
user_pass = "hunter42!"
})
end,

-- create initial subreddits
[3] = function()
-- TODO figure out utils module
local data = {}
local path = "/var/data/initial_subs.json"
local file = io.open(path, "rb")

if file then
local content = file:read "*a" -- *a or *all reads the whole file
file:close()
data = json.decode(content)
-- require 'pl.pretty'.dump(data)
-- print("Read in " .. #data .. " subreddits from " .. path)
end

for _, sub in ipairs(data) do
-- print("About to create new sub: " .. sub.name .. ".")
local s, e = Subreddits:create({
name = sub.name,
description = sub.description or "",
creator_id = sub.creator_id or 1,
})
if not s then
print("error creating " .. s.name)
print(e)
end
Subreddits:create_db_tables(s.id)
end
end,

[4] = function()
-- loop through all subreddits and create 10 posts for each
local subs = Subreddits:select()
for _, sub in ipairs(subs) do
print("About to create 10 posts for " .. sub.name .. ".")
local table_name = sub.id .. "_posts"
for i = 1, 10 do
print(i)

local s, e = db.insert(table_name, {
title = "Post " .. i .. " for " .. sub.name,
permalink = "http://www.example.com/" .. i,
url = "http://www.example.com/" .. i,
user_id = 1,
})
if not s then
print("error creating " .. s.title)
print(e)
break
end
end
end

end,

-- classify text : https://github.com/leafo/lapis-bayes
[1439944992] = require("lapis.bayes.schema").run_migrations,
}
Loading

0 comments on commit 833da32

Please sign in to comment.