-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb_controller.cr
More file actions
234 lines (198 loc) · 8.41 KB
/
web_controller.cr
File metadata and controls
234 lines (198 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
require "kemal"
require "../services/tokei_service"
require "../models/analysis"
require "../views/renderer"
require "../views/contexts/layout_context"
module Tokei::Api::Controllers
# Controller for Web
module WebController
GITHUB_PATH_SAFE = /^[A-Za-z0-9._-]+$/
private def self.valid_github_param?(value : String) : Bool
value.matches?(GITHUB_PATH_SAFE)
end
# Detect common social preview bots (Twitter, Facebook, LinkedIn, Slack, Discord, etc.)
private def self.social_bot?(ua : String?) : Bool
return false unless ua
!!(ua =~ /Twitterbot|facebookexternalhit|LinkedInBot|Slackbot|Discordbot|WhatsApp|TelegramBot|Mastodon|Line|iMessage/i)
end
# Build absolute base URL from trusted configuration only.
private def self.base_url : String
ENV["BASE_URL"]? || "http://localhost:#{ENV["PORT"]?.try(&.to_i) || 3000}"
end
# Process analyze request (common logic for GET and POST)
private def self.process_analyze_request(env, repo_url)
# URL validation
unless Tokei::Api::Services::TokeiService.valid_repo_url?(repo_url)
env.response.status_code = 400
error_message = "Invalid repository URL"
return Tokei::Api::Views::Renderer.render_index(error_message)
end
# Search latest analysis result using lightweight summary query
recent_analysis = Tokei::Api::Models::Analysis.find_latest_by_repo_url(repo_url)
if recent_analysis && recent_analysis.analyzed_at.try(&.> Time.utc - 24.hours)
# Use recent analysis results if available
analysis = recent_analysis
else
# Analyze repository
result = Tokei::Api::Services::TokeiService.analyze_repo(repo_url)
# Save to database
analysis = Tokei::Api::Models::Analysis.new(repo_url: repo_url, result: result)
saved = analysis.save
raise "Failed to persist analysis result" unless saved && analysis.id
end
# Redirect to results page
env.redirect "/analyses/#{analysis.id}"
rescue ex
STDERR.puts "analyze error: #{ex.message}"
error_message = "An internal error occurred"
Tokei::Api::Views::Renderer.render_index(error_message)
end
# Process GitHub repository analyze request
private def self.process_github_analyze_request(env, owner, repo)
# Construct GitHub repository URL
repo_url = "https://github.com/#{owner}/#{repo}"
# Use the common analyze request processing
process_analyze_request(env, repo_url)
end
# Setup Web endpoints
def self.setup
# GET / endpoint (home page)
get "/" do |_env|
error_message = nil
Tokei::Api::Views::Renderer.render_index(error_message)
end
# GET /analyze endpoint (for badge links) - redirect to /analyses
get "/analyze" do |env|
# Get repository URL from query parameters
repo_url = env.params.query["url"]
process_analyze_request(env, repo_url)
end
# POST /analyze endpoint (form submission) - redirect to /analyses
post "/analyze" do |env|
# Get repository URL from form
repo_url = env.params.body["url"]
process_analyze_request(env, repo_url)
end
# POST /analyses endpoint (form submission - new API structure)
post "/analyses" do |env|
# Get repository URL from form
repo_url = env.params.body["url"]
process_analyze_request(env, repo_url)
end
# GET /result/:id endpoint (results display page) - redirect to /analyses/:id
get "/result/:id" do |env|
id = env.params.url["id"]
env.redirect "/analyses/#{id}"
end
# GET /analyses/:id endpoint (results display page - new API structure)
get "/analyses/:id" do |env|
begin
id = env.params.url["id"]
analysis = Tokei::Api::Models::Analysis.find(id)
if analysis.nil?
env.response.status_code = 404
error_message = "Analysis not found"
next Tokei::Api::Views::Renderer.render_index(error_message)
end
# Analysis results are already JSON::Any
result_json = analysis.result
Tokei::Api::Views::Renderer.render_result(analysis, result_json)
rescue ex
env.response.status_code = 500
STDERR.puts "analyses/:id error: #{ex.message}"
error_message = "An internal error occurred"
Tokei::Api::Views::Renderer.render_index(error_message)
end
end
# GET /repos/:id endpoint (results display page - redirect to /analyses/:id)
get "/repos/:id" do |env|
id = env.params.url["id"]
env.redirect "/analyses/#{id}"
end
# GET /analyses endpoint (analysis list/search page)
get "/analyses" do |env|
# This could be implemented in the future to show a list of recently analyzed repositories
# For now, redirect to home page
env.redirect "/"
end
# GET /github/:owner/:repo endpoint
# - Social bots: return minimal HTML with OG tags (no redirect)
# - Humans: run analysis then redirect to /analyses/:id
get "/github/:owner/:repo" do |env|
owner = env.params.url["owner"]
repo = env.params.url["repo"]
unless valid_github_param?(owner) && valid_github_param?(repo)
env.response.status_code = 400
next Tokei::Api::Views::Renderer.render_index("Invalid GitHub owner or repository")
end
if social_bot?(env.request.headers["User-Agent"]?)
base = base_url
safe_owner = HTML.escape(owner)
safe_repo = HTML.escape(repo)
image = "#{base}/og/github/#{owner}/#{repo}?format=png"
meta = String.build do |io|
io << %(<meta property="og:type" content="website">)
io << %(<meta property="og:title" content="#{safe_owner}/#{safe_repo}">)
io << %(<meta property="og:description" content="Language breakdown by tokei">)
io << %(<meta property="og:url" content="#{base}/github/#{owner}/#{repo}">)
io << %(<meta property="og:image" content="#{image}">)
io << %(<meta property="og:image:width" content="1200">)
io << %(<meta property="og:image:height" content="630">)
io << %(<meta name="twitter:card" content="summary_large_image">)
end
body = String.build do |io|
io << %(<h1>#{safe_owner}/#{safe_repo}</h1>)
io << %(<p>Share this URL on social networks to show a bar chart preview.</p>)
io << %(<img src="#{image}" alt="OG Preview" width="600" height="315">)
end
html = Tokei::Api::Views::Contexts::LayoutContext.new(body, nil, meta).to_s
env.response.content_type = "text/html; charset=utf-8"
next html
end
# Fallback for regular browsers: perform analysis and redirect
begin
process_github_analyze_request(env, owner, repo)
rescue ex
env.response.status_code = 500
STDERR.puts "github/:owner/:repo error: #{ex.message}"
error_message = "An internal error occurred"
Tokei::Api::Views::Renderer.render_index(error_message)
end
end
# POST /cleanup endpoint (delete old data)
# Disabled from public routes.
# post "/cleanup" do |env|
# begin
# deleted_count = Tokei::Api::Models::Analysis.cleanup_old_data
# env.response.status_code = 200
# env.response.print "Deleted #{deleted_count} old records."
# rescue ex
# env.response.status_code = 500
# STDERR.puts "cleanup error: #{ex.message}"
# env.response.print "An internal error occurred"
# end
# end
get "/api" do |_env|
error_message = nil
Tokei::Api::Views::Renderer.render_api(error_message)
end
# GET /badges endpoint (badges documentation page)
get "/badges" do |_env|
error_message = nil
Tokei::Api::Views::Renderer.render_badges(error_message)
end
# Error handling
error 404 do |env|
env.response.content_type = "text/html"
Tokei::Api::Views::Renderer.render_error("Page not found")
end
error 500 do |env, ex|
env.response.content_type = "text/html"
STDERR.puts "500 error: #{ex.message}"
Tokei::Api::Views::Renderer.render_error("Internal Server Error")
end
# Serve static files
public_folder "public"
end
end
end