2013-05-21

raw

vim:set syntax=ruby:

require "msgpack"
require 'memcached'
require 'digest/md5'
$memcache = Memcached.new("localhost:11211")
class Page < ActiveRecord::Base
def subdomain request
if self.body.split("\n").last == "subdomain only"
return nil unless request.host == "ssig33.com"
return "http://#{self.name}/"
else
return nil
end
end

def blog str
text = <<EOS

ssig33.com

#{self.blogtitle}

EOS
str.sub(/# ssig33.com

app.rb

end

def blogtitle
if self.body.split("\n").first =~ /%blog%\ ./
self.body.split("\n").first.sub(/%blog%\ /, '')
else
self.name.sub(/^.?\//, '')
end
end

def html request
if self.body.split("\n").last == "subdomain only"
a = self.body.split("\n")
a.pop
self.body = a.join("\n")
end
case self.body.split("\n").first
when /^haml/
return self.haml request
when /^coffee/
return self.coffee
when /^ruby/
str = ""
when /^title/
str = ""
ary = self.body.split("\n")
str = ""
ary.shift
str = ary.join("\n")
else
str = self.body
end
str = self.blog(str) if str =~ /%blog%/
Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, fencedcodeblocks: true, tables: true).render(str)

end

def title
case self.body.split("\n").first
when /title/
str = self.body.split("\n").shift.split("title\ ").last
when /%blog%\ .*/
str = self.body.split("\n").first.sub(/%blog%\ /, '')
when /haml/
str = self.body.split("\n").shift.split("haml\ ").last
when /builder/
str = self.body.split("\n").shift.split("builder\ ").last
when /coffee/
str = self.body.split("\n").shift.split("coffee\ ").last
when /ruby/
str = self.body.split("\n").shift.split("ruby\ ").last
else
str = self.name
end
str.chomp!
str = self.name if str == nil or str == "" or str == "haml"
str.gsub(/=createdat=/, self.createdat.getutc.to_s)
end

def raw
ary = self.body.split("\n")
ary.pop if ary.last == 'subdomain only'
ary.shift
if ary.first =~ /^mime/
mime = ary.shift.split(" ").last
else
mime = "text"
end
return ary.join("\n"), mime
end

def haml request
ary = self.body.split("\n")
str = ""
ary.shift
str = ary.join("\n")
Haml::Engine.new(str).render
end

def ruby request, env
ary = self.body.split("\n")
str = ""
ary.shift
if ary.first =~ /^mime/
mime = ary.shift.split(" ").last
else
mime = :html
end

end

def builder request
ary = self.body.split("\n")
str = ""
ary.shift
str = ary.join("\n")
xml = ::Builder::XmlMarkup.new(:indent => 2)
eval str
end

def coffee
ary = self.body.split("\n")
str = ""
ary.shift
str = ary.join("\n")
CoffeeScript.compile(str)
end

def layout
ary = self.body.split("\n")
ary.shift
ary.join("\n")
end
end

class Store < ActiveRecord::Base
def value
MessagePack.unpack(self.body) rescue nil
end

def value= hash
self.body = hash.to_msgpack
end
end

def page id, request, env
file = open("public/#{id}/index.html").read rescue nil
return file if file
@page = Page.where(:name => id).order("createdat desc").first
if request.querystring
q = CGI.parse(request.querystring)
p q
if q['historyid'] and q['historyid'].first != nil and q['historyid'].first != ''
@page = Page.find(q['history_id'].first)
end
end

redirect "http://ssig33.com/edit/#{id}" unless @page

redirect @page.subdomain(request) if @page.subdomain(request)

return [@page.haml(request), :html] if @page.body.split("\n").first =~ /^haml/

case @page.body.split("\n").first
when /^raw/ , /^layout/
raw, mime = @page.raw
return [raw, :"#{mime}"]
when /^coffee/
return [@page.coffee, :js]
when /^html/
return [@page.raw, :html]
when /^builder/
return [@page.builder(request), :xml]
when /^ruby/
return @page.ruby(request, env)
else
if layout = Page.where(:name => "layout").order("created_at desc").first
return [haml(layout.layout), :html]
else
return [haml(:page), :html]
end
end
end

configure do
set :logging, false
set :appfile, _FILE__
use Rack::Session::Cookie, :secret => 'fsdjkfhsjkhr23f8fhsdjkvhnsdjhrfuiscflaaadn8or'
use Rack::Csrf, :raise => true
end

get '/' do
begin
cache = $memcache.get("site/#{CGI.escape request.pathinfo}")
c = JSON.parse cache
type = c['type']
body = c['body']
rescue Memcached::NotFound
id = 'index'
id.chop! if id.reverse[0] == "/"
body, type = page id, request, env
forcache = {type: type, body: body}.tojson
$memcache.set("site/#{CGI.escape request.pathinfo}", forcache, 60)
end
#response['Access-Control-Allow-Origin'] = ''
#response.headers["Access-Control-Allow-Origin"] = ""
contenttype type
body
end

get %r{/.*favicon.ico} do
content_type :png
open("public/favicon.ico").read
end

get %r{/edit/(.*)} do
@id = params[:captures].first.tos
@page = Page.where(:name => @id).order("createdat desc").first
@page = Page.new if @page == nil
ary = Page.where(:name => "source/edit.haml").order("created_at desc").first.body.split("\n")
ary.shift
status 404
haml ary.join("\n")
end

get %r{/(.)} do
if request.querystring.empty?
cachepath = "site/#{CGI.escape request.pathinfo}"
else
cachepath = "site/#{CGI.escape request.pathinfo+'?'+request.querystring}"
end
cachepath = Digest::MD5.hexdigest(cachepath).tos
begin
cache = $memcache.get(cachepath)
c = JSON.parse cache
type = c['type']
body = c['body']
rescue Memcached::NotFound
id = params[:captures].first.tos
id.chop! if id.reverse[0] == "/"
body, type = page id, request, env
forcache = {type: type, body: body}.tojson
if type == :js or type == :css
$memcache.set(cachepath, forcache, 80000)
else
$memcache.set(cachepath, for_cache, 30)
end
end
#response['Access-Control-Allow-Origin'] = ''
#response.headers["Access-Control-Allow-Origin"] = "*"
content_type type
body
end

post '/update' do
if Digest::MD5.hexdigest(params[:password]).tos != PASSWORD
return "ERROR"
else
begin
$memcache.delete Digest::MD5.hexdigest("site/#{CGI.escape "/"+CGI.unescape(params[:id])}").tos
rescue Memcached::NotFound
end
page = Page.new
page.name = params[:id]
page.body = params[:body]
page.save
key = Digest::MD5.hexdigest(page.name).tos
Groonga['Pages'].add key unless Groonga['Pages'][key]
Groonga['Pages'][key].name = page.name
Groonga['Pages'][key].text = page.body
Groonga['Pages'][key].pageid = page.id
redirect "http://ssig33.com/#{params[:id]}"
end
end

post '/destroy' do
raise if Digest::MD5.hexdigest(params[:password]).tos != PASSWORD
Page.where(:name => params[:id]).deleteall
redirect "http://ssig33.com/"
end

post '/usr2' do
raise if Digest::MD5.hexdigest(params[:password]).to_s != PASSWORD
system "rm site.pid.oldbin"
system "kill -QUIT cat site.pid"
redirect "http://ssig33.com/"
end

post %r{/(.*)} do
id = params[:captures].first.to_s
id.chop! if id.reverse[0] == "/"
page id, request, env
end

error do
"error... #{env['sinatra.error']}"
end

helpers do
def h str
CGI.escapeHTML str.to_s
rescue
""
end

def title
if request.pathinfo == "/" or request.pathinfo == "/index"
return "ssig33.com"
else
return "ssig33.com - #{@page.title}"
end
end
end

back to index of texts

Show more