Difference between revisions of "Havard's Crappy Blog"

From AOLserver Wiki
Jump to navigation Jump to search
(New page: I whipped up a quick bloggish app in php, but decided to port it over to AOLserver for good measure. It's not very complete, has little to no sanity checking, was written in a little unde...)
 
Line 1: Line 1:
I whipped up a quick bloggish app in php, but decided to port it over to AOLserver for good measure.  It's not very complete, has little to no sanity checking, was written in a little under an hour without any thought or planning, and is barely useful, but it does work.
+
Havard's Crappy Blog is an aptly-named piece of software written by [[User:Havard|John Havard]] to power one of his web sites.  It's a simple blog-style web app that runs under our favorite web application server.  It was initially a couple of <tt>.adp</tt> scripts, but with a little prodding from [[User:Dossy|Dossy]], it was whipped into shape to be insanely fast.  Since most blogs are relatively small, and RAM is cheap, everything is cached.
  
First thing's first, you'll need to create a pool for your PostgreSQL database.  Let's assume for this project the name of the pool is <tt>pool1</tt>.
+
All the code is kept in the [http://aolserver.cvs.sourceforge.net/aolserver/ AOLserver CVS repository] as the <tt>havardblog</tt> packageBe warned that the code is likely full of WTF-isms.
 
 
== schema.pgsql ==
 
 
 
<code>
 
CREATE DOMAIN AUTHOR_ID AS VARCHAR(16) NOT NULL;
 
CREATE DOMAIN EMAIL AS VARCHAR(128) NOT NULL;
 
CREATE DOMAIN SLUG AS VARCHAR(32) NOT NULL;
 
 
CREATE TABLE AUTHOR (
 
        ID AUTHOR_ID PRIMARY KEY,
 
        PERSONAL_NAME VARCHAR(64) NOT NULL,
 
        DISPLAY_EMAIL EMAIL,
 
        REAL_EMAIL EMAIL,
 
        PASSWORD VARCHAR(64) NOT NULL
 
);
 
 
CREATE TABLE ARTICLE (
 
        SLUG SLUG NOT NULL,
 
        PUBDATE DATE NOT NULL,
 
        UPDATED TIMESTAMP NOT NULL,
 
        AUTHOR AUTHOR_ID REFERENCES AUTHOR(ID) NOT NULL,
 
        LEAD TEXT NOT NULL,
 
        BODY TEXT NOT NULL,
 
        TITLE VARCHAR(128) NOT NULL,
 
        PRIMARY KEY (SLUG, PUBDATE)
 
);
 
</code>
 
 
 
== init.tcl ==
 
 
 
<code>
 
proc do_article {} {
 
        set mainStuff ""
 
        # /article/YYYY/MM/DD/slug/junk
 
        set urlv [ns_conn urlv]
 
        set qslug [lindex $urlv 4]
 
        set qpubdate [lindex $urlv 1]-[lindex $urlv 2]-[lindex $urlv 3]
 
        set qry "SELECT ARTICLE.*,AUTHOR.PERSONAL_NAME FROM ARTICLE,AUTHOR WHERE AUTHOR.ID=ARTICLE.AUTHOR AND PUBDATE =  '${qpubdate}' AND SLUG = '${qslug}'"
 
        catch {
 
                set hdl [ns_db gethandle pool1]
 
                set res [ns_db 1row $hdl $qry]
 
 
                #[ns_db getrow $hdl $res]
 
                set slug    [subst [ns_set get $res slug  ]]
 
                set pubdate [subst [ns_set get $res pubdate]]
 
                set updated [subst [ns_set get $res updated]]
 
                set author  [subst [ns_set get $res author ]]
 
                set title  [subst [ns_set get $res title  ]]
 
                set lead    [subst [ns_set get $res lead  ]]
 
                set body    [subst [ns_set get $res body  ]]
 
                ns_db releasehandle $hdl
 
        }
 
 
        if { [string compare $slug $qslug] == 0 } {
 
                set mainStuff [ns_adp_parse -file template/article.adp]
 
                set rPole [ns_adp_parse -file template/rpole.adp]
 
                ns_return 200 "text/html" [ns_adp_parse -file template/template.adp]
 
        } else {
 
                ns_returnfile 404 "text/html" "global/file-not-found.html"
 
        }
 
}
 
 
ns_register_proc GET /article do_article
 
 
ns_log warning "We processed init.tcl!"
 
</code>
 
 
 
== index.adp ==
 
<%
 
        set mainStuff ""
 
        set qry "SELECT ARTICLE.*,AUTHOR.PERSONAL_NAME FROM ARTICLE,AUTHOR WHERE AUTHOR.ID=ARTICLE.AUTHOR ORDER BY PUBDATE DESC, UPDATED DESC LIMIT 10"
 
        catch {
 
                set hdl [ns_db gethandle pool1]
 
                set res [ns_db select $hdl $qry]
 
 
                while {[ns_db getrow $hdl $res]} {
 
                        set slug    [subst [ns_set get $res slug  ]]
 
                        set pubdate [subst [ns_set get $res pubdate]]
 
                        set updated [subst [ns_set get $res updated]]
 
                        set author  [subst [ns_set get $res author ]]
 
                        set title  [subst [ns_set get $res title  ]]
 
                        set lead    [subst [ns_set get $res lead  ]]
 
                        set body    [subst [ns_set get $res body  ]]
 
                        set pubdate [string map {- /} $pubdate]
 
                        set mainStuff "$mainStuff [ns_adp_parse -file template/entry.adp]"
 
                }
 
        }
 
ns_adp_puts [ns_adp_parse -file template/template.adp]
 
%>
 
 
 
== /admin/post.adp ==
 
<code>
 
<html>
 
<head>
 
<title>Post an article</title>
 
</head>
 
<body>
 
&lt;table&gt;
 
&lt;form method="POST" action="do_post.adp"&gt;
 
<tr>
 
        <td>Title</td>
 
        <td><input name="title" size="80" value=""/></td>
 
</tr>
 
<tr>
 
        <td>username</td>
 
        <td><input name="username" value=""/></td>
 
</tr>
 
<tr>
 
        <td>password</td>
 
        <td><input type="password" name="password" value=""/></td>
 
</tr>
 
<tr>
 
        <td>date (YYYY/MM/DD)</td>
 
        <td><input name="date" value=""/></td>
 
</tr>
 
<tr>
 
        <td>slug</td>
 
        <td><input name="slug" value=""/></td>
 
</tr>
 
<tr>
 
        <td>lead</td>
 
        <td><textarea name="lead" cols="80" rows="5"></textarea>
 
</tr>
 
<tr>
 
        <td>body</td>
 
        <td><textarea name="body" cols="80" rows="12"></textarea>
 
</tr>
 
<tr>
 
        <td><input type="submit"></td>
 
        <td><input type="reset"></td>
 
</tr>
 
</form>
 
</table>
 
</body>
 
</html>
 
</code>
 
 
 
== /admin/do_post.adp ==
 
<code>
 
<%
 
set title [ns_dbquotevalue [ns_queryget title]]
 
set user  [ns_dbquotevalue [ns_queryget username]]
 
set pass  [ns_dbquotevalue [ns_queryget password]]
 
set date  [ns_dbquotevalue [ns_queryget date]]
 
set slug  [ns_dbquotevalue [ns_queryget slug]]
 
set lead  [ns_dbquotevalue [ns_queryget lead]]
 
set body  [ns_dbquotevalue [ns_queryget body]]
 
 
set qry "INSERT INTO ARTICLE VALUES ($slug, $date, now(), (SELECT ID FROM AUTHOR WHERE ID = $user and PASSWORD = $pass), $lead, $body, $title)"
 
 
        set hdl [ns_db gethandle pool1]
 
 
        if {[catch {ns_db dml $hdl $qry} res]} {
 
                ns_adp_puts "post probably failed, hit back and try again"
 
                ns_adp_puts "$res"
 
        } else {
 
                ns_adp_puts "post was a success!"
 
                ns_adp_puts "$res"
 
        }
 
%>
 
</code>
 
== notes ==
 
 
 
You'll need a <tt>template</tt> directory off your pagerootThe files <tt>entry.adp</tt> and <tt>article.adp</tt> are virtually the same except that <tt>entry.adp</tt> is used for the home page listing, while <tt>article.adp</tt> is used for the full article page.  You'll generally want to use the lead in <tt>entry.adp</tt> and body in <tt>article.adp</tt>.  The main template is <tt>template.adp</tt>.  The main content section is filled with the <tt>mainStuff</tt> variable.
 
 
 
Your links to articles will be in the form of <tt>/article/YYYY/MM/DD/slug/whatever.html</tt>.  If you'll notice, pubdate has dashes substituted for slashes.  This makes forming the URL nice and easy.  What I use to form the <em>Read More</em> link is:
 
 
 
<code>
 
<a href="/article/<%= $pubdate %>/<%= $slug %>/article.html">Read More</a>
 
</code>
 

Revision as of 20:12, 15 September 2008

Havard's Crappy Blog is an aptly-named piece of software written by John Havard to power one of his web sites. It's a simple blog-style web app that runs under our favorite web application server. It was initially a couple of .adp scripts, but with a little prodding from Dossy, it was whipped into shape to be insanely fast. Since most blogs are relatively small, and RAM is cheap, everything is cached.

All the code is kept in the AOLserver CVS repository as the havardblog package. Be warned that the code is likely full of WTF-isms.