Class: Gemite::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/gemite/router.rb

Overview

リクエストパスから .gmt ファイル、または public/ 配下の静的ファイルを解決する。

仕様4: ファイルはURLへ直接マッピングされる(index.gmt -> /, login.gmt -> /login 等)。 ディレクトリ構成は自由なので、サーバ起動時のカレントディレクトリを プロジェクトルートとして扱い、リクエストパスをそのまま相対パスとして解決する。

Defined Under Namespace

Classes: Result

Constant Summary collapse

EXTENSION =
".gmt"
STATIC_DIR =
"public"
MIME_TYPES =
{
  ".html" => "text/html; charset=utf-8",
  ".htm" => "text/html; charset=utf-8",
  ".css" => "text/css; charset=utf-8",
  ".js" => "text/javascript; charset=utf-8",
  ".mjs" => "text/javascript; charset=utf-8",
  ".json" => "application/json; charset=utf-8",
  ".png" => "image/png",
  ".jpg" => "image/jpeg",
  ".jpeg" => "image/jpeg",
  ".gif" => "image/gif",
  ".svg" => "image/svg+xml",
  ".ico" => "image/x-icon",
  ".webp" => "image/webp",
  ".woff" => "font/woff",
  ".woff2" => "font/woff2",
  ".txt" => "text/plain; charset=utf-8",
  ".xml" => "application/xml; charset=utf-8",
  ".pdf" => "application/pdf"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Router

Returns a new instance of Router.



34
35
36
# File 'lib/gemite/router.rb', line 34

def initialize(root)
  @root = File.expand_path(root)
end

Instance Method Details

#mime_type_for(path) ⇒ Object



53
54
55
# File 'lib/gemite/router.rb', line 53

def mime_type_for(path)
  MIME_TYPES[File.extname(path).downcase] || "application/octet-stream"
end

#resolve(request_path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gemite/router.rb', line 40

def resolve(request_path)
  clean = normalize(request_path)
  return Result.new(:not_found, nil) unless clean

  gemite_path = find_gemite_file(clean)
  return Result.new(:gemite, gemite_path) if gemite_path

  static_path = find_static_file(clean)
  return Result.new(:static, static_path) if static_path

  Result.new(:not_found, nil)
end