changeset 596:6bb0c83116e9

add blog sample app
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 15 Sep 2015 21:30:33 -0600
parents 8370c4009cce
children cb6c628de583
files .hgignore blog/read_me.txt blog/serve.sh blog/src/edit.luan blog/src/index.html.luan blog/src/lib/Db.luan blog/src/lib/Db_mod.luan blog/src/lib/Post.luan blog/src/new.luan blog/src/site.css
diffstat 10 files changed, 217 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Mon Sep 14 14:20:52 2015 -0600
+++ b/.hgignore	Tue Sep 15 21:30:33 2015 -0600
@@ -9,3 +9,4 @@
 *.bck
 *.n
 go
+local
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blog/read_me.txt	Tue Sep 15 21:30:33 2015 -0600
@@ -0,0 +1,1 @@
+This is just a demo website not meant for real use.  It is just to demonstrate how to implement a simple website in Luan.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blog/serve.sh	Tue Sep 15 21:30:33 2015 -0600
@@ -0,0 +1,1 @@
+luan luan:http/serve file:src 2>&1 | tee err
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blog/src/edit.luan	Tue Sep 15 21:30:33 2015 -0600
@@ -0,0 +1,43 @@
+local Luan = require "luan:Luan"
+local error = Luan.error
+local String = require "luan:String"
+local to_number = String.to_number or error()
+local Io = require "luan:Io"
+local Http = require "luan:http/Http"
+local Post = require "site:/lib/Post"
+
+
+return function()
+	local post_id = to_number(Http.request.parameter.post) or error()
+	local post = Post.get_by_id(post_id) or error()
+	if Http.request.parameter.save ~= nil then
+		post.subject = Http.request.parameter.subject
+		post.content = Http.request.parameter.content
+		post.save()
+		Http.response.send_redirect("/#p"..post.id)
+		return
+	end
+
+	Io.stdout = Http.response.text_writer()
+%>
+<html>
+	<head>
+		<style>
+			@import "/site.css";
+		</style>
+	</head>
+	<body>
+		<h1>Make New Post</h1>
+
+		<form method=post>
+			<p>Subject: <input name=subject size=50 type=text value="<%= post.subject %>"></p>
+			<p><textarea name=content rows=20 cols=90><%= post.content %></textarea></p>
+			<p>
+				<input type=submit name=save value=Submit>
+			</p>
+		</form>
+
+	</body>
+</html>
+<%
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blog/src/index.html.luan	Tue Sep 15 21:30:33 2015 -0600
@@ -0,0 +1,39 @@
+local Luan = require "luan:Luan"
+local error = Luan.error
+local ipairs = Luan.ipairs or error()
+local Time = require "luan:Time"
+local Io = require "luan:Io"
+local Http = require "luan:http/Http"
+local Post = require "site:/lib/Post"
+
+
+return function()
+	Io.stdout = Http.response.text_writer()
+%>
+<html>
+	<head>
+		<style>
+			@import "/site.css";
+		</style>
+	</head>
+	<body>
+		<h1>Demo Blog App</h1>
+
+		<div><a href="new">Make New Post</a></div>
+
+		<%
+		for _, post in ipairs(Post.get_all()) do
+			%>
+			<a name="p<%= post.id %>">
+			<h2><%= post.subject %></h2>
+			<p><%= Time.format(post.date) %> - <a href="edit?post=<%= post.id %>">Edit</a></p>
+			<pre><%= post.content %></pre>
+			<hr>
+			<%
+		end
+		%>
+
+	</body>
+</html>
+<%
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blog/src/lib/Db.luan	Tue Sep 15 21:30:33 2015 -0600
@@ -0,0 +1,1 @@
+return require "site:/lib/Db_mod".new_db()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blog/src/lib/Db_mod.luan	Tue Sep 15 21:30:33 2015 -0600
@@ -0,0 +1,19 @@
+local Lucene = require "luan:lucene/Lucene"
+local Io = require "luan:Io"
+
+
+local M = {}
+
+M.lucene_dir = "site:/private/local/lucene"
+
+function M.new_db()
+	local dir = Io.uri(M.lucene_dir).to_string()
+	local db = Lucene.index(dir)
+	
+--	this is how you index a field
+--	db.indexed_fields.post_date = Lucene.type.long
+
+	return db
+end
+
+return M
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blog/src/lib/Post.luan	Tue Sep 15 21:30:33 2015 -0600
@@ -0,0 +1,55 @@
+local Luan = require "luan:Luan"
+local error = Luan.error
+local ipairs = Luan.ipairs or error()
+local assert_string = Luan.assert_string or error()
+local Time = require "luan:Time"
+local now = Time.now or error()
+local Db = require "site:/lib/Db"
+
+
+local M = {}
+
+local function from_doc(doc)
+	return M.new {
+		id = doc.id;
+		subject = doc.subject;
+		content = doc.content;
+		date = doc.date;
+	}
+end
+
+function M.new(this)
+	assert_string(this.subject)
+	assert_string(this.content)
+	this.date = this.date or now()
+
+	function this.save()
+		local doc = {
+			type = "post";
+			id = this.id;
+			subject = this.subject;
+			content = this.content;
+			date = this.date;
+		}
+		Db.save(doc)
+		this.id = doc.id
+	end
+
+	return this
+end
+
+function M.get_by_id(id)
+	local doc = Db.get_document("id:"..id)
+	return doc and from_doc(doc)
+end
+
+function M.get_all()
+	local docs = Db.search("type:post",1,1000,"id desc")
+	local posts = {}
+	for _, doc in ipairs(docs) do
+		posts[#posts+1] = from_doc(doc)
+	end
+	return posts
+end
+
+return M
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blog/src/new.luan	Tue Sep 15 21:30:33 2015 -0600
@@ -0,0 +1,40 @@
+local Luan = require "luan:Luan"
+local error = Luan.error
+local Io = require "luan:Io"
+local Http = require "luan:http/Http"
+local Post = require "site:/lib/Post"
+
+
+return function()
+	local subject = Http.request.parameter.subject
+	local content = Http.request.parameter.content
+	if Http.request.parameter.save ~= nil then
+		local post = Post.new{ subject=subject, content=content }
+		post.save()
+		Http.response.send_redirect("/")
+		return
+	end
+
+	Io.stdout = Http.response.text_writer()
+%>
+<html>
+	<head>
+		<style>
+			@import "/site.css";
+		</style>
+	</head>
+	<body>
+		<h1>Make New Post</h1>
+
+		<form method=post>
+			<p>Subject: <input name=subject size=50 type=text></p>
+			<p><textarea name=content rows=20 cols=90></textarea></p>
+			<p>
+				<input type=submit name=save value=Submit>
+			</p>
+		</form>
+
+	</body>
+</html>
+<%
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blog/src/site.css	Tue Sep 15 21:30:33 2015 -0600
@@ -0,0 +1,17 @@
+
+body {
+	font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
+	font-size: 14px;
+	margin: 1em 5%;
+}
+
+a[href] {
+	text-decoration: inherit;
+	color: #337ab7;
+}
+a[href]:visited {
+	color: #034a87;
+}
+a[href]:hover {
+	text-decoration: underline;
+}