Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 153 lines (116 sloc) 3.689 kb
5a4f2a6 @soveran Reorder files.
soveran authored
1 ROOT_PATH = File.expand_path(File.dirname(__FILE__))
2
83fcb59 App skeleton.
Michel Martens & Damian Janowski authored
3 require "cuba"
4 require "haml"
5 require "rdiscount"
6 require "json"
1fc3530 @djanowski Add basic styling.
djanowski authored
7 require "compass"
9d70924 @djanowski Add latest commits in the homepage.
djanowski authored
8 require "open-uri"
9 require "digest/md5"
10 require "redis"
ab91b7e @djanowski WIP comments.
djanowski authored
11 require "rack/openid"
12 require "ohm"
fa5c38f @djanowski Add Download section.
djanowski authored
13 require "rack/static"
83fcb59 App skeleton.
Michel Martens & Damian Janowski authored
14
5a4f2a6 @soveran Reorder files.
soveran authored
15 require File.expand_path("lib/reference", ROOT_PATH)
16 require File.expand_path("lib/template", ROOT_PATH)
1695e55 @djanowski Add topics and homepage Markdown.
djanowski authored
17
cf829ba @soveran Let Cuba handle middlewares.
soveran authored
18 Cuba.use Rack::Session::Cookie
19 Cuba.use Rack::OpenID
58c123f @djanowski One would've thought Heroku picks up ./public by default.
djanowski authored
20 Cuba.use Rack::Static, urls: ["/images"], root: File.join(ROOT_PATH, "public")
ab91b7e @djanowski WIP comments.
djanowski authored
21
f52f46d @djanowski Force UTF-8.
djanowski authored
22 Encoding.default_external = Encoding::UTF_8
23
5a4f2a6 @soveran Reorder files.
soveran authored
24 module Kernel
25 private
e81763e @djanowski Simple auto-linking of commands.
djanowski authored
26
5a4f2a6 @soveran Reorder files.
soveran authored
27 def commands
28 @commands ||= Reference.new(JSON.parse(File.read("redis-doc/commands.json")))
a76493e @djanowski Add formatting of reply types in Markdown.
djanowski authored
29 end
30
5a4f2a6 @soveran Reorder files.
soveran authored
31 def redis
32 @redis ||= Redis.connect(url: ENV["REDISTOGO_URL"])
a76493e @djanowski Add formatting of reply types in Markdown.
djanowski authored
33 end
34
5a4f2a6 @soveran Reorder files.
soveran authored
35 def user
36 @user ||= User[session[:user]]
8b6cc7b @djanowski Add basic pre-processing of Markdown files.
djanowski authored
37 end
38 end
39
36c6354 @djanowski Ohm needs to know about the connection to Redis.
djanowski authored
40 Ohm.redis = redis
41
83fcb59 App skeleton.
Michel Martens & Damian Janowski authored
42 Cuba.define do
1695e55 @djanowski Add topics and homepage Markdown.
djanowski authored
43 def render(path, locals = {})
44 return unless File.expand_path(path).start_with?(ROOT_PATH)
45 super(path, locals)
46 end
47
13921cb Simple layout.
Michel Martens & Damian Janowski authored
48 def haml(template, locals = {})
755556e @djanowski Format comments.
djanowski authored
49 layout(partial(template, locals))
1695e55 @djanowski Add topics and homepage Markdown.
djanowski authored
50 end
51
755556e @djanowski Format comments.
djanowski authored
52 def partial(template, locals = {})
53 render("views/#{template}.haml", locals)
13921cb Simple layout.
Michel Martens & Damian Janowski authored
54 end
55
755556e @djanowski Format comments.
djanowski authored
56 def layout(content)
57 partial("layout", content: content)
1fc3530 @djanowski Add basic styling.
djanowski authored
58 end
59
fa5c38f @djanowski Add Download section.
djanowski authored
60 def topic(template)
61 # TODO: Relying on ivars for this is ugly.
62
63 @body = render(template)
64 @title = @body[%r{<h1>(.+?)</h1>}, 1] # Nokogiri may be overkill
65
66 haml("topics/name")
67 end
68
69a3458 @djanowski Do client-side login.
djanowski authored
69 def gravatar_hash(email)
70 Digest::MD5.hexdigest(email)
71 end
72
9d70924 @djanowski Add latest commits in the homepage.
djanowski authored
73 on get, path("") do
74 json = redis.get("commits")
75
76 @commits = json ? JSON.parse(json)["commits"] : []
77
78 res.write haml("home")
79 end
80
fa5c38f @djanowski Add Download section.
djanowski authored
81 on get, path("download") do
82 res.write topic("views/download.md")
83 end
84
0f637b4 @djanowski Add basic Community section.
djanowski authored
85 on get, path("community") do
86 res.write topic("views/community.md")
87 end
88
e7b6714 @djanowski Add basic Documentation section.
djanowski authored
89 on get, path("documentation") do
90 res.write topic("views/documentation.md")
91 end
92
83fcb59 App skeleton.
Michel Martens & Damian Janowski authored
93 on get, path("commands") do
94 on segment do |name|
274d5ba @djanowski Fix commands with spaces.
djanowski authored
95 @name = @title = name.upcase.gsub("-", " ")
e81763e @djanowski Simple auto-linking of commands.
djanowski authored
96 @command = commands[@name]
83fcb59 App skeleton.
Michel Martens & Damian Janowski authored
97
13921cb Simple layout.
Michel Martens & Damian Janowski authored
98 res.write haml("commands/name")
83fcb59 App skeleton.
Michel Martens & Damian Janowski authored
99 end
100
101 on default do
e81763e @djanowski Simple auto-linking of commands.
djanowski authored
102 @commands = commands
13921cb Simple layout.
Michel Martens & Damian Janowski authored
103 @title = "Command reference"
83fcb59 App skeleton.
Michel Martens & Damian Janowski authored
104
13921cb Simple layout.
Michel Martens & Damian Janowski authored
105 res.write haml("commands")
83fcb59 App skeleton.
Michel Martens & Damian Janowski authored
106 end
107 end
9d70924 @djanowski Add latest commits in the homepage.
djanowski authored
108
0512eb3 @djanowski Add clients.
djanowski authored
109 on get, path("clients") do
110 @clients = JSON.parse(File.read("redis-doc/clients.json"))
111
112 @clients_by_language = @clients.group_by { |name, info| info["language"] }.sort_by { |name, _| name.downcase }
113
114 res.write haml("clients")
115 end
116
755556e @djanowski Format comments.
djanowski authored
117 on get, path("topics"), segment do |_, _, name|
9584843 @djanowski Include topic title in the <title> tag.
djanowski authored
118 @body = render("redis-doc/topics/#{name}.md")
119 @title = @body[%r{<h1>(.+?)</h1>}, 1] # Nokogiri may be overkill
120
755556e @djanowski Format comments.
djanowski authored
121 res.write haml("topics/name")
122 end
ab91b7e @djanowski WIP comments.
djanowski authored
123
9d70924 @djanowski Add latest commits in the homepage.
djanowski authored
124 on post do
125 on path("commits"), param(:payload) do
126 if redis.setnx("commits:refresh", 1)
127 redis.pipelined do
83ec6e2 @pietern Use https for retrieving commit list
pietern authored
128 redis.set("commits", open("https://github.com/api/v2/json/commits/list/antirez/redis/master").read)
9d70924 @djanowski Add latest commits in the homepage.
djanowski authored
129 redis.expire("commits:refresh", 90)
130 end
131 end
132 end
133 end
755556e @djanowski Format comments.
djanowski authored
134
106b487 @soveran Serve commands and clients in JSON format.
soveran authored
135 on get, path(/\w+\.json/) do |_, file|
136 res.headers["Cache-Control"] = "public, max-age=29030400" if req.query_string =~ /[0-9]{10}/
137 res.headers["Content-Type"] = "application/json;charset=UTF-8"
138 res.write File.read("redis-doc/#{file}")
139 end
140
755556e @djanowski Format comments.
djanowski authored
141 on get, path("styles.css") do
142 res.headers["Cache-Control"] = "public, max-age=29030400" if req.query_string =~ /[0-9]{10}/
143 res.headers["Content-Type"] = "text/css; charset=utf-8"
144 res.write render("views/styles.sass")
145 end
146
147 on get, path("app.js") do |_, file|
148 res.headers["Cache-Control"] = "public, max-age=29030400" if req.query_string =~ /[0-9]{10}/
149 res.headers["Content-Type"] = "text/javascript; charset=utf-8"
150 res.write File.read("views/app.js")
151 end
83fcb59 App skeleton.
Michel Martens & Damian Janowski authored
152 end
Something went wrong with that request. Please try again.