Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add public dir process in crystal-kemal #258

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions spec/functional_test/fixtures/crystal_kemal/src/testapp.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ ws "/socket" do |socket|
socket.send "Hello from Kemal!"
end

public_folder "custom_public"

Kemal.run
4 changes: 3 additions & 1 deletion spec/functional_test/testers/crystal_kemal_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ extected_endpoints = [
Param.new("query", "", "form"),
Param.new("my_auth", "", "cookie"),
]),
Endpoint.new("/1.html", "GET"),
Endpoint.new("/2.html", "GET"),
]

FunctionalTester.new("fixtures/crystal_kemal/", {
:techs => 1,
:endpoints => 3,
:endpoints => 5,
}, extected_endpoints).test_all
46 changes: 46 additions & 0 deletions src/analyzer/analyzers/analyzer_crystal_kemal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ require "../../models/analyzer"

class AnalyzerCrystalKemal < Analyzer
def analyze
# Variables
is_public = true
public_folders = [] of String

# Source Analysis
begin
Dir.glob("#{@base_path}/**/*") do |path|
Expand All @@ -24,6 +28,25 @@ class AnalyzerCrystalKemal < Analyzer
last_endpoint.push_param(param)
end
end

if line.includes? "serve_static false" || "serve_static(false)"
is_public = false
end

if line.includes? "public_folder"
begin
splited = line.split("public_folder")
public_folder = ""

if splited.size > 1
public_folder = splited[1].gsub("(", "").gsub(")", "").gsub(" ", "").gsub("\"", "").gsub("'", "")
if public_folder != ""
public_folders << public_folder
end
end
rescue
end
end
end
end
end
Expand All @@ -32,6 +55,29 @@ class AnalyzerCrystalKemal < Analyzer
logger.debug e
end

# Public Dir Analysis
if is_public
begin
Dir.glob("#{@base_path}/public/**/*") do |file|
next if File.directory?(file)
real_path = "#{@base_path}/public/".gsub(/\/+/, '/')
relative_path = file.sub(real_path, "")
@result << Endpoint.new("/#{relative_path}", "GET")
end

public_folders.each do |folder|
Dir.glob("#{@base_path}/#{folder}/**/*") do |file|
next if File.directory?(file)
relative_path = get_relative_path(@base_path, file)
relative_path = get_relative_path(folder, relative_path)
@result << Endpoint.new("/#{relative_path}", "GET")
end
end
rescue e
logger.debug e
end
end

result
end

Expand Down
Loading