Class: Pindo::Command::Web::Run::WebGLPageHandler

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/pindo/command/web/run.rb

Overview

自定义前端页面处理器,处理Unity WebGL的特殊文件加载需求

Instance Method Summary collapse

Constructor Details

#initialize(server, root_dir, debug = false) ⇒ WebGLPageHandler

Returns a new instance of WebGLPageHandler.



113
114
115
116
117
# File 'lib/pindo/command/web/run.rb', line 113

def initialize(server, root_dir, debug=false)
    super(server)
    @root_dir = root_dir
    @debug = debug
end

Instance Method Details

#do_GET(req, res) ⇒ Object



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
# File 'lib/pindo/command/web/run.rb', line 119

def do_GET(req, res)
    path = req.path
    path = "/index.html" if path == "/"
    
    file_path = File.join(@root_dir, path[1..-1])
    
    if File.exist?(file_path)
        # 确定内容类型
        ext = File.extname(file_path)
        content_type = case ext
            when ".html" then "text/html"
            when ".js" then "application/javascript"
            when ".css" then "text/css"
            when ".png" then "image/png"
            when ".jpg", ".jpeg" then "image/jpeg"
            when ".gif" then "image/gif"
            when ".ico" then "image/x-icon"
            else "application/octet-stream"
        end
        
        res.content_type = content_type
        res.body = File.read(file_path)
        res.status = 200
    else
        res.status = 404
    end
end