Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 158 lines (120 sloc) 3.897 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
59f0c35 @pietern Set path to redis-doc repository via REDIS_DOC environment variable
pietern authored
27 def documentation_path
28 @documentation_path ||= ENV["REDIS_DOC"] || "redis-doc"
29 end
30
5a4f2a6 @soveran Reorder files.
soveran authored
31 def commands
59f0c35 @pietern Set path to redis-doc repository via REDIS_DOC environment variable
pietern authored
32 @commands ||= Reference.new(JSON.parse(File.read(documentation_path + "/commands.json")))
a76493e @djanowski Add formatting of reply types in Markdown.
djanowski authored
33 end
34
5a4f2a6 @soveran Reorder files.
soveran authored
35 def redis
36 @redis ||= Redis.connect(url: ENV["REDISTOGO_URL"])
a76493e @djanowski Add formatting of reply types in Markdown.
djanowski authored
37 end
38
5a4f2a6 @soveran Reorder files.
soveran authored
39 def user
40 @user ||= User[session[:user]]
8b6cc7b @djanowski Add basic pre-processing of Markdown files.
djanowski authored
41 end
42 end
43
36c6354 @djanowski Ohm needs to know about the connection to Redis.
djanowski authored
44 Ohm.redis = redis
45
83fcb59 App skeleton.
Michel Martens & Damian Janowski authored
46 Cuba.define do
1695e55 @djanowski Add topics and homepage Markdown.
djanowski authored
47 def render(path, locals = {})
59f0c35 @pietern Set path to redis-doc repository via REDIS_DOC environment variable
pietern authored
48 expanded = File.expand_path(path)
49 return unless expanded.start_with?(ROOT_PATH) || expanded.start_with?(documentation_path)
1695e55 @djanowski Add topics and homepage Markdown.
djanowski authored
50 super(path, locals)
51 end
52
13921cb Simple layout.
Michel Martens & Damian Janowski authored
53 def haml(template, locals = {})
755556e @djanowski Format comments.
djanowski authored
54 layout(partial(template, locals))
1695e55 @djanowski Add topics and homepage Markdown.
djanowski authored
55 end
56
755556e @djanowski Format comments.
djanowski authored
57 def partial(template, locals = {})
58 render("views/#{template}.haml", locals)
13921cb Simple layout.
Michel Martens & Damian Janowski authored
59 end
60
755556e @djanowski Format comments.
djanowski authored
61 def layout(content)
62 partial("layout", content: content)
1fc3530 @djanowski Add basic styling.
djanowski authored
63 end
64
fa5c38f @djanowski Add Download section.
djanowski authored
65 def topic(template)
66 # TODO: Relying on ivars for this is ugly.
67
68 @body = render(template)
69 @title = @body[%r{<h1>(.+?)</h1>}, 1] # Nokogiri may be overkill
70
71 haml("topics/name")
72 end
73
69a3458 @djanowski Do client-side login.
djanowski authored
74 def gravatar_hash(email)
75 Digest::MD5.hexdigest(email)
76 end
77
9d70924 @djanowski Add latest commits in the homepage.
djanowski authored
78 on get, path("") do
79 json = redis.get("commits")
80
81 @commits = json ? JSON.parse(json)["commits"] : []
82
83 res.write haml("home")
84 end
85
fa5c38f @djanowski Add Download section.
djanowski authored
86 on get, path("download") do
87 res.write topic("views/download.md")
88 end
89
0f637b4 @djanowski Add basic Community section.
djanowski authored
90 on get, path("community") do
91 res.write topic("views/community.md")
92 end
93
e7b6714 @djanowski Add basic Documentation section.
djanowski authored
94 on get, path("documentation") do
95 res.write topic("views/documentation.md")
96 end
97
83fcb59 App skeleton.
Michel Martens & Damian Janowski authored
98 on get, path("commands") do
99 on segment do |name|
274d5ba @djanowski Fix commands with spaces.
djanowski authored
100 @name = @title = name.upcase.gsub("-", " ")
e81763e @djanowski Simple auto-linking of commands.
djanowski authored
101 @command = commands[@name]
83fcb59 App skeleton.
Michel Martens & Damian Janowski authored
102
13921cb Simple layout.
Michel Martens & Damian Janowski authored
103 res.write haml("commands/name")
83fcb59 App skeleton.
Michel Martens & Damian Janowski authored
104 end
105
106 on default do
e81763e @djanowski Simple auto-linking of commands.
djanowski authored
107 @commands = commands
13921cb Simple layout.
Michel Martens & Damian Janowski authored
108 @title = "Command reference"
83fcb59 App skeleton.
Michel Martens & Damian Janowski authored
109
13921cb Simple layout.
Michel Martens & Damian Janowski authored
110 res.write haml("commands")
83fcb59 App skeleton.
Michel Martens & Damian Janowski authored
111 end
112 end
9d70924 @djanowski Add latest commits in the homepage.
djanowski authored
113
0512eb3 @djanowski Add clients.
djanowski authored
114 on get, path("clients") do
59f0c35 @pietern Set path to redis-doc repository via REDIS_DOC environment variable
pietern authored
115 @clients = JSON.parse(File.read(documentation_path + "/clients.json"))
0512eb3 @djanowski Add clients.
djanowski authored
116
117 @clients_by_language = @clients.group_by { |name, info| info["language"] }.sort_by { |name, _| name.downcase }
118
119 res.write haml("clients")
120 end
121
755556e @djanowski Format comments.
djanowski authored
122 on get, path("topics"), segment do |_, _, name|
59f0c35 @pietern Set path to redis-doc repository via REDIS_DOC environment variable
pietern authored
123 @body = render(documentation_path + "/topics/#{name}.md")
9584843 @djanowski Include topic title in the <title> tag.
djanowski authored
124 @title = @body[%r{<h1>(.+?)</h1>}, 1] # Nokogiri may be overkill
125
755556e @djanowski Format comments.
djanowski authored
126 res.write haml("topics/name")
127 end
ab91b7e @djanowski WIP comments.
djanowski authored
128
9d70924 @djanowski Add latest commits in the homepage.
djanowski authored
129 on post do
130 on path("commits"), param(:payload) do
131 if redis.setnx("commits:refresh", 1)
132 redis.pipelined do
83ec6e2 @pietern Use https for retrieving commit list
pietern authored
133 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
134 redis.expire("commits:refresh", 90)
135 end
136 end
137 end
138 end
755556e @djanowski Format comments.
djanowski authored
139
106b487 @soveran Serve commands and clients in JSON format.
soveran authored
140 on get, path(/\w+\.json/) do |_, file|
141 res.headers["Cache-Control"] = "public, max-age=29030400" if req.query_string =~ /[0-9]{10}/
142 res.headers["Content-Type"] = "application/json;charset=UTF-8"
59f0c35 @pietern Set path to redis-doc repository via REDIS_DOC environment variable
pietern authored
143 res.write File.read(documentation_path + "/#{file}")
106b487 @soveran Serve commands and clients in JSON format.
soveran authored
144 end
145
755556e @djanowski Format comments.
djanowski authored
146 on get, path("styles.css") do
147 res.headers["Cache-Control"] = "public, max-age=29030400" if req.query_string =~ /[0-9]{10}/
148 res.headers["Content-Type"] = "text/css; charset=utf-8"
149 res.write render("views/styles.sass")
150 end
151
152 on get, path("app.js") do |_, file|
153 res.headers["Cache-Control"] = "public, max-age=29030400" if req.query_string =~ /[0-9]{10}/
154 res.headers["Content-Type"] = "text/javascript; charset=utf-8"
155 res.write File.read("views/app.js")
156 end
83fcb59 App skeleton.
Michel Martens & Damian Janowski authored
157 end
Something went wrong with that request. Please try again.