带有 Crystal 的类似 WEBrick 的服务器 [英] A WEBrick like server with Crystal

查看:24
本文介绍了带有 Crystal 的类似 WEBrick 的服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 Crystal 创建一个可以提供 HTML、CSS 和 JS 页面的简单 Web 服务器?

Is it possible to create a simple web server with Crystal that can serve HTML, CSS and JS pages?

我当前的代码是:

require "http/server"
Port = 8080
Mime = "text/html"

server = HTTP::Server.new([HTTP::ErrorHandler.new, HTTP::LogHandler.new]) do |context|
    req = context.request
    if req.method == "GET"
        filename = File.join(Dir.current, "index.html")
        context.response.content_type = Mime
        context.response.content_length = File.size(filename)
        File.open(filename) { |file| IO.copy(file, context.response) }
        next
    end
    context.response.content_type = Mime

end

puts "\e[1;33mStarted Listening on Port #{Port}\e[0m"
server.listen(Port)

当我运行编译并运行程序时,它会初始化服务器,但有几个问题:

When I run compile and run the program, it initializes the server, but there are a couple of problems:

  1. 在 Firefox 浏览器的Inspect Element"控制台中,我看到:

The stylesheet http://127.0.0.1:8080/styling.css was not loaded because its MIME type, "text/html", is not "text/css". 127.0.0.1:8080

The script from "http://127.0.0.1:8080/javascript.js" was loaded even though its MIME type ("text/html") is not a valid JavaScript MIME type. 127.0.0.1:8080

SyntaxError: expected expression, got '<' javascript.js:1

服务器只显示index.html的内容.

当我使用 WEBrick 运行或将 index.html 直接加载到浏览器时,HTML、CSS 和 JS 代码完全有效.

The codes HTML, CSS and JS is perfectly valid when I run using WEBrick or load up the index.html directly to the browser.

  1. 无法从本地网络上的任何其他设备访问我的服务器.

推荐答案

非常感谢@Johannes Müller 解决了我的问题.在这里,我正在分享我想要的代码.

Many thanks to @Johannes Müller which solved my problem. Here, I am sharing the code for what I wanted exactly.

#!/usr/bin/env crystal
require "http/server"

# Get the Address
ADDR = (ARGV.find { |x| x.split(".").size == 4 } || "0.0.0.0").tap { |x| ARGV.delete(x) }
        .split(".").map { |x| x.to_i { 0 } }.join(".")

# Get the Port
PORT = ARGV.find { |x| x.to_i { 0 } > 0 }.tap { |x| ARGV.delete(x) }.to_s.to_i { 8080 }

# Get the path
d = Dir.current
dir = ARGV[0] rescue d
path = Dir.exists?(dir) ? dir : Dir.exists?(File.join(d, dir)) ? File.join(d, dir) : d
listing = !!Dir.children(path).find { |x| x == "index.html" }
actual_path = listing ? File.join(path, "index.html") : path

server = HTTP::Server.new([
        HTTP::ErrorHandler.new,
        HTTP::LogHandler.new,
        HTTP::StaticFileHandler.new(path, directory_listing: !listing)
    ]) do |context|
        context.response.content_type = "text/html"
        File.open(actual_path) { |file| IO.copy(file, context.response) }
end

puts "\e[1;33m:: Starting Sharing \e[38;5;75m#{actual_path}\e[1;31m on \e[38;5;226mhttp://#{ADDR}:#{PORT}\e[0m"
server.listen(::ADDR, ::PORT)

此代码在提供的路径(默认为 Dir.current)中查找index.html"文件,如果找到,则将 index.html 文件共享到 IP 地址(默认为 0.0.0.0)和端口(默认为 8080)) 提供,否则它只是共享当前目录内容.

This code looks for a "index.html" file to the provided path (default Dir.current), if found, it shares the index.html file to the IP address (default 0.0.0.0) and port (default 8080) provided, else it just shares the current directory contents.

crystal code.cr /tmp/ 5020 127.0.0.1

选项可以洗牌.例如:

crystal code.cr 5020 /tmp/ 127.0.0.1

crystal code.cr 5020 127.0.0.1 /tmp

这将启动服务器并共享/tmp 目录.如果在/tmp/目录中找到 index.html 文件,则请求的浏览器将显示 index.html 内容,或者它的工作方式类似于 FTP(尽管不是).

This will start the server and share the /tmp direcotory. If the index.html file is found inside the /tmp/ directory, the requested browser will display the index.html content, or it will work similar to an FTP (although it's not).

crystal build code.cr
./code [options]

这篇关于带有 Crystal 的类似 WEBrick 的服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆