Class: Pindo::WebServer::WebGLPageHandler

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/pindo/module/webserver/webgl_page_handler.rb

Overview

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of WebGLPageHandler.



9
10
11
12
13
# File 'lib/pindo/module/webserver/webgl_page_handler.rb', line 9

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

Instance Method Details

#do_GET(req, res) ⇒ Object



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
# File 'lib/pindo/module/webserver/webgl_page_handler.rb', line 15

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