Merge branch 'replacejs/server' into beta
This commit is contained in:
@@ -37,11 +37,6 @@ http {
|
|||||||
alias /app/gen/;
|
alias /app/gen/;
|
||||||
}
|
}
|
||||||
|
|
||||||
location /api {
|
|
||||||
proxy_pass https://raymon.dev/api;
|
|
||||||
proxy_buffering on;
|
|
||||||
}
|
|
||||||
|
|
||||||
error_page 404 /404.html;
|
error_page 404 /404.html;
|
||||||
location = /404.html {
|
location = /404.html {
|
||||||
root /app/error;
|
root /app/error;
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
# Templates will be filled by posts
|
# Templates will be filled by posts
|
||||||
index_template = File.read!("./templates/index.html")
|
index_template = File.read!("./templates/post_index_page.html")
|
||||||
post_template = File.read!("./templates/post.html")
|
post_template = File.read!("./templates/post_single_page.html")
|
||||||
|
|
||||||
|
post_feed_item_template = File.read!("./templates/post_item.xml")
|
||||||
|
post_feed_template = File.read!("./templates/posts.xml")
|
||||||
|
|
||||||
post_contents = File.ls!("./posts")
|
post_contents = File.ls!("./posts")
|
||||||
|> Enum.reject(fn(x) -> String.starts_with?(x, ".") end)
|
|> Enum.reject(fn(x) -> String.starts_with?(x, ".") end)
|
||||||
@@ -47,3 +50,20 @@ post_contents
|
|||||||
end))
|
end))
|
||||||
|> File.close()
|
|> File.close()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
post_feed = post_contents
|
||||||
|
|> Enum.sort_by(fn m -> Map.get(m, :date) end)
|
||||||
|
|> Enum.reverse()
|
||||||
|
|> Enum.map(fn post ->
|
||||||
|
Regex.compile!("{{(.*)}}") |>
|
||||||
|
Regex.replace(post_feed_item_template, fn _, key -> Map.get(post, String.to_atom(key))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|> Enum.join("\n")
|
||||||
|
|> (fn items -> (Regex.compile!("{{items}}") |>
|
||||||
|
Regex.replace(post_feed_template, fn _, __ -> items end))
|
||||||
|
end).()
|
||||||
|
|
||||||
|
File.open!("./gen/rss.xml", [:write])
|
||||||
|
|> IO.binwrite(post_feed)
|
||||||
|
|> File.close()
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
<item>
|
||||||
|
<title>{{title}}</title>
|
||||||
|
<link>https://raymon.dev/posts/{{id}}.html</link>
|
||||||
|
<guid>https://raymon.dev/posts/{{id}}.html</guid>
|
||||||
|
<description>
|
||||||
|
<![CDATA[
|
||||||
|
{{content}}
|
||||||
|
]]>
|
||||||
|
</description>
|
||||||
|
<content type="html">
|
||||||
|
{{content}}
|
||||||
|
</content>
|
||||||
|
<pubDate>{{date}}</pubDate>
|
||||||
|
</item>
|
||||||
@@ -26,7 +26,7 @@
|
|||||||
<article>
|
<article>
|
||||||
<header>
|
<header>
|
||||||
<h1>{{title}}</h1>
|
<h1>{{title}}</h1>
|
||||||
<h3>For those using a RSS reader, subscribe here: <a href="/api/posts/rss.xml">rss.xml</a></h3>
|
<h3>For those using a RSS reader, subscribe here: <a href="/posts/rss.xml">rss.xml</a></h3>
|
||||||
</header>
|
</header>
|
||||||
<time datetime="{{date}}"></time>
|
<time datetime="{{date}}"></time>
|
||||||
{{content}}
|
{{content}}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
||||||
|
<channel>
|
||||||
|
<atom:link href="https://raymon.dev/api/posts/rss.xml" rel="self" type="application/rss+xml" />
|
||||||
|
<title>Posts</title>
|
||||||
|
<link>https://raymon.dev</link>
|
||||||
|
<description>Personal blog</description>
|
||||||
|
<language>en-us</language>
|
||||||
|
|
||||||
|
{{items}}
|
||||||
|
</channel>
|
||||||
|
</rss>
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
**/node_modules
|
|
||||||
**/dist
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
FROM node:latest
|
|
||||||
|
|
||||||
WORKDIR /usr/src/server
|
|
||||||
|
|
||||||
COPY package*.json ./
|
|
||||||
|
|
||||||
RUN npm ci --only=production
|
|
||||||
|
|
||||||
COPY . .
|
|
||||||
|
|
||||||
CMD [ "node", "index.js" ]
|
|
||||||
|
|
||||||
EXPOSE 5000
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
const fastify = require('fastify')()
|
|
||||||
|
|
||||||
fastify.register(require('fastify-cors'))
|
|
||||||
|
|
||||||
const posts = require('./routes/api/posts')
|
|
||||||
|
|
||||||
// Assuming they are all get requests
|
|
||||||
for (let [route, resolver] of posts.entries()) {
|
|
||||||
fastify.get('/posts' + route, resolver)
|
|
||||||
}
|
|
||||||
|
|
||||||
const port = process.env.PORT || 5000
|
|
||||||
|
|
||||||
const start = async () => {
|
|
||||||
try {
|
|
||||||
await fastify.listen(port, '0.0.0.0')
|
|
||||||
console.log(`Launched on port ${port} 🚀`)
|
|
||||||
} catch (err) {
|
|
||||||
fastify.log.error(err)
|
|
||||||
process.exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
start()
|
|
||||||
Generated
-1315
File diff suppressed because it is too large
Load Diff
@@ -1,20 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "server",
|
|
||||||
"version": "1.3.0",
|
|
||||||
"description": "The API for the website",
|
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
|
||||||
"start": "node ./index.js",
|
|
||||||
"dev": "nodemon server/index.js"
|
|
||||||
},
|
|
||||||
"keywords": [],
|
|
||||||
"author": "Raymonzut",
|
|
||||||
"license": "ISC",
|
|
||||||
"dependencies": {
|
|
||||||
"fastify": "^2.15.1",
|
|
||||||
"fastify-cors": "^3.0.3"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"nodemon": "^2.0.4"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
*.json
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
All the posts will be in here seperate in JSON format
|
|
||||||
|
|
||||||
post
|
|
||||||
- title
|
|
||||||
- date
|
|
||||||
- content
|
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
const fs = require('fs')
|
|
||||||
|
|
||||||
const posts_dir = 'posts/'
|
|
||||||
|
|
||||||
const routes = new Map();
|
|
||||||
|
|
||||||
if (!fs.existsSync(posts_dir)) throw Error(`Missing ${posts_dir}`)
|
|
||||||
|
|
||||||
let posts = readPosts();
|
|
||||||
|
|
||||||
setInterval(() => posts = readPosts(), 1000 * 60 * 60)
|
|
||||||
|
|
||||||
function readPosts() {
|
|
||||||
console.warn("reading all posts")
|
|
||||||
const files = fs.readdirSync(posts_dir)
|
|
||||||
|
|
||||||
if (files.length === 0) throw Error(`Could not find posts in ${posts_dir}`)
|
|
||||||
|
|
||||||
return files.filter(file_name => file_name.endsWith('.json'))
|
|
||||||
.map(file_name => `${posts_dir}${file_name}`)
|
|
||||||
.map(readJSONAsObject)
|
|
||||||
}
|
|
||||||
|
|
||||||
function readJSONAsObject(filename) {
|
|
||||||
return JSON.parse(fs.readFileSync(filename, 'utf8'))
|
|
||||||
}
|
|
||||||
|
|
||||||
function notFoundResponse(res) {
|
|
||||||
res.status(404).send('Sorry, can not find that')
|
|
||||||
}
|
|
||||||
|
|
||||||
routes.set('', async (req, res) => {
|
|
||||||
if (req.query.sort === '-1' || req.query.sort === '1') {
|
|
||||||
const posts_sorted = posts.sort((a, b) => {
|
|
||||||
return new Date(a.date).getTime() - new Date(b.date).getTime()
|
|
||||||
})
|
|
||||||
|
|
||||||
if (req.query.sort === '1') res.send(posts_sorted)
|
|
||||||
else res.send(posts_sorted.reverse())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// Default response when there are no interesting queries
|
|
||||||
res.send(posts)
|
|
||||||
})
|
|
||||||
|
|
||||||
routes.set('/:id', async (req, res) => {
|
|
||||||
const re = /[0-9A-Fa-f]{24}/g
|
|
||||||
|
|
||||||
if (!re.test(req.params.id)) {
|
|
||||||
notFoundResponse(res)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
const results = posts.filter(post => post._id === req.params.id)
|
|
||||||
|
|
||||||
if (!results.length) notFoundResponse(res)
|
|
||||||
else res.send(results[0])
|
|
||||||
})
|
|
||||||
|
|
||||||
// Dynamic RSS feed
|
|
||||||
routes.set('/rss.xml', async (req, res) => {
|
|
||||||
const re = /(\/api\/)?(.*)\/rss/g
|
|
||||||
const result = [...req.raw.originalUrl.matchAll(re)]
|
|
||||||
if (!result) {
|
|
||||||
notFoundResponse(res)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const endpoint = result [1] ? result[1][2] : result[0][2]
|
|
||||||
// Assumes https
|
|
||||||
const BASE_URL = 'https://' + req.raw.hostname + endpoint
|
|
||||||
const ITEMS = posts.map(post =>
|
|
||||||
`\r\n <item>
|
|
||||||
<title>${post.title}</title>
|
|
||||||
<link>https://${req.raw.hostname}/posts?post=${post._id}</link>
|
|
||||||
<guid>https://${req.raw.hostname}/posts?post=${post._id}</guid>
|
|
||||||
<description><![CDATA[${"\n" + post.content.split("\n").reduce((acc, v) => acc + "<p>" + v + "</p>")}]]></description>
|
|
||||||
<content type="html">CONTENT HERE: ${post.content}</content>
|
|
||||||
<pubDate>${post.date}</pubDate>
|
|
||||||
</item>`
|
|
||||||
).join("\n")
|
|
||||||
const xml = `<?xml version="1.0" encoding="UTF-8" ?>\n<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
|
||||||
<channel>
|
|
||||||
<atom:link href="https://${req.raw.hostname}/api/posts/rss.xml" rel="self" type="application/rss+xml" />
|
|
||||||
<title>Posts</title>
|
|
||||||
<link>https://${req.raw.hostname}</link>
|
|
||||||
<description>Personal blog</description>
|
|
||||||
<language>en-us</language>
|
|
||||||
${ITEMS}
|
|
||||||
</channel>\n</rss>`
|
|
||||||
|
|
||||||
res.header('Content-Type', 'application/xml')
|
|
||||||
res.status(200).send(xml)
|
|
||||||
})
|
|
||||||
|
|
||||||
module.exports = routes
|
|
||||||
Reference in New Issue
Block a user