Permalink
Browse files

Initial application layout

  • Loading branch information...
0 parents commit bb513bbd7aac5aa603262ffa0ff124ab7bee78cf @antirez committed Oct 15, 2011
Showing with 273 additions and 0 deletions.
  1. +71 −0 app.rb
  2. +4 −0 app_config.rb
  3. +117 −0 page.rb
  4. +59 −0 public/css/style.css
  5. BIN public/images/lama.gif
  6. BIN public/js/.app.js.swp
  7. +22 −0 public/js/app.js
@@ -0,0 +1,71 @@
+require 'rubygems'
+require 'redis'
+require 'page'
+require 'app_config'
+require 'sinatra'
+require 'json'
+
+before do
+ $r = Redis.new(:host => RedisHost, :port => RedisPort)
+ H = HTMLGen.new
+end
+
+get '/' do
+ H.set_title "Top News - #{SiteName}"
+ H.page {
+ "Hello World"
+ }
+end
+
+get '/login' do
+ H.set_title "Login - #{SiteName}"
+ H.page {
+ H.login {
+ H.form(:name=>"f") {
+ H.label(:for => "username") {"username"}+
+ H.inputtext(:name => "username")+
+ H.label(:for => "password") {"password"}+
+ H.inputtext(:name => "password")+H.br+
+ H.checkbox(:name => "register", :value => "1")+
+ "create account"+H.br+
+ H.button(:name => "do_login", :value => "Login")+H.br+
+ H.div(:id => "errormsg"){}
+ }
+ }+
+ H.script(:type=>"text/javascript") {'
+ $(document).ready(function() {
+ $("input[name=do_login]").click(login);
+ });
+ '}
+ }
+end
+
+get '/api/login' do
+ if params[:username] == 'antirez' and params[:password] == 'ANTIREZ'
+ return {:status => "ok", :token => "foobar"}.to_json
+ else
+ return {:status => "err", :error => "no such user/pass"}.to_json
+ end
+end
+
+def application_header
+ navitems = [ ["login / register","/login"],
+ ["top","/top"],
+ ["latest","/latest"]]
+ navbar = H.nav {
+ navitems.map{|ni|
+ H.a(:href=>ni[1]) {H.entities ni[0]}
+ }.inject{|a,b| a+"\n"+b}
+ }
+ H.header {
+ H.h1 {
+ H.entities SiteName
+ }+navbar
+ }
+end
+
+def application_footer
+ H.footer {
+ "Lamer News source code is located "+H.a(:href=>""){"here"}
+ }
+end
@@ -0,0 +1,4 @@
+SiteName = "Lamer News"
+RedisHost = "127.0.0.1"
+RedisPort = 6379
+PasswordSalt = "*LAMER*news*"
@@ -0,0 +1,117 @@
+require 'cgi'
+
+class HTMLGen
+ @@newlinetags = %w{html body div br ul hr title link head filedset label legend option table li select td tr meta}
+ @@metatags = {
+ "js" => {"tag"=>"script","type"=>"text/javascript"},
+ "inputtext" => {"tag"=>"input","type"=>"text"},
+ "inputpass" => {"tag"=>"input","type"=>"password"},
+ "inputfile" => {"tag"=>"input","type"=>"file"},
+ "inputhidden" => {"tag"=>"input","type"=>"hidden"},
+ "button" => {"tag"=>"input","type"=>"button"},
+ "submit" => {"tag"=>"input","type"=>"submit"},
+ "checkbox" => {"tag"=>"input","type"=>"checkbox"},
+ "radio" => {"tag"=>"input","type"=>"radio"}
+ }
+
+ def initialize
+ @title = "Default title"
+ end
+
+ def method_missing(m, attrhash={})
+ content = block_given? ? yield.to_s : nil
+ gentag(m,attrhash,content)
+ end
+
+ def gentag(m, attrhash, content)
+ m = m.to_s
+ if (@@metatags[m])
+ origm = m
+ m = @@metatags[m]['tag']
+ attrhash = @@metatags[origm].merge(attrhash)
+ attrhash.delete('tag')
+ if attrhash['!append']
+ content += attrhash['!append']
+ attrhash.delete('!append')
+ end
+ end
+ nl = (@@newlinetags.include? m) ? "\n" : ""
+ attribs = ""
+ if attrhash.length != 0
+ attrhash.each_pair {|k,v|
+ attribs += " #{k}=\"#{entities(v.to_s)}\"" if v
+ }
+ end
+ if content
+ content += nl if content[-1] != 10
+ content = nl+content if content[0] != 10
+ html = "<#{m}#{attribs}>"+content+"</#{m}>"+nl
+ else
+ html = "<#{m}#{attribs}>"+nl
+ end
+ return html
+ end
+
+ def list(l)
+ self.ul {
+ aux = ""
+ l.each {|x|
+ if block_given?
+ aux += self.li {yield x}
+ else
+ aux += self.li {x}
+ end
+ }
+ aux
+ }
+ end
+
+ def entities(s)
+ CGI::escapeHTML(s)
+ end
+
+ def unentities(s)
+ CGI::unescapeHTML(s)
+ end
+
+ def urlencode(s)
+ CGI::escape(s)
+ end
+
+ def urldecode(s)
+ CGI::unescape(s)
+ end
+
+ def _header()
+ application_header
+ end
+
+ def _footer()
+ application_footer
+ end
+
+ def set_title(t)
+ @title = t
+ end
+
+ def page()
+ "<!DOCTYPE html>"+
+ self.html {
+ H.head {
+ self.title{H.entities @title}+
+ self.meta(:charset => :utf8)+
+ self.link(:href => "/css/style.css", :rel => "stylesheet",
+ :type => "text/css")+
+ self.link(:href => "/images/favicon.png", :rel => "shortcut icon")+
+ self.script(:src =>
+ "http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"){}+
+ self.script(:src => "/js/app.js"){}
+ }+
+ self.body {
+ self.div(:class => "container") {
+ _header+H.content{yield}+_footer
+ }
+ }
+ }
+ end
+end
@@ -0,0 +1,59 @@
+html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td { margin: 0; padding: 0; border: 0; outline: 0; font-weight: inherit; font-style: inherit; font-size: 100%; font-family: inherit; vertical-align: baseline; }
+
+body {
+ font-family: "Helvetica Neue";
+ text-rendering: optimizeLegibility;
+}
+
+a {
+ text-decoration:none;
+ color:#666;
+}
+
+header {
+ display:block;
+ padding: 15px 0px 0px 15px;
+ background-color:#eee;
+ border-bottom: 1px #ccc solid;
+}
+
+header h1 {
+ display: inline;
+ font-size:2em;
+ color:#333;
+}
+
+content {
+ display:block;
+ margin-top:20px;
+ margin-bottom:20px;
+ padding: 15px;
+}
+
+footer {
+ display:block;
+ border-top: 1px #ccc solid;
+ text-align: center;
+ color:#999;
+ font-size:14px;
+}
+
+nav {
+ margin-bottom:15px;
+ display: inline;
+}
+
+nav a {
+ margin-left: 20px;
+}
+
+login {
+ display:block;
+ position:relative;
+ width:150px;
+}
+
+login label {
+ font-size:12px;
+ font-weight:bold;
+}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
@@ -0,0 +1,22 @@
+function login() {
+ var data = {
+ username: $("input[name=username]").val(),
+ password: $("input[name=password]").val(),
+ register: $("input[name=register]").val()
+ };
+ $.ajax({
+ type: "GET",
+ url: "/api/login",
+ data: data,
+ success: function(reply) {
+ r = jQuery.parseJSON(reply);
+ if (r.status == "ok") {
+ // Set the cookie
+ alert("Ok!");
+ } else {
+ $("#errormsg").html(r.error)
+ }
+ }
+ });
+ return false;
+}

0 comments on commit bb513bb

Please sign in to comment.