root
to executeTo be safe, it is recommended to run this program with its own username:
sudo file_server -u nitcorn:www
FileServer
action, which is a standard and minimal file server
HttpRequest
class and services to create it
Serializable::inspect
to show more useful information
more_collections :: more_collections
Highly specific, but useful, collections-related classes.serialization :: serialization_core
Abstract services to serialize Nit objects to different formatscore :: union_find
union–find algorithm using an efficient disjoint-set data structure
# Basic file server on port 80 by default, may require `root` to execute
#
# To be safe, it is recommended to run this program with its own username:
# `sudo file_server -u nitcorn:www`
module simple_file_server is example
import nitcorn
import privileges
# Prepare options
var opts = new OptionContext
var opt_drop = new OptionUserAndGroup.for_dropping_privileges
var opt_port = new OptionInt("Server port", 80, "--port", "-p")
var opt_help = new OptionBool("Print this message", "--help", "-h")
opts.add_option(opt_drop, opt_port, opt_help)
opts.parse args
# Check options errors and help
if not opts.errors.is_empty or opt_help.value then
print opts.errors.join("\n")
print "Usage: file_server [Options]"
opts.usage
exit 1
end
# Serve everything with a standard FilesHandler
var vh = new VirtualHost("localhost:{opt_port.value}")
vh.routes.add new Route(null, new FileServer("www/hello_world/"))
var factory = new HttpFactory.and_libevent
factory.config.virtual_hosts.add vh
# Drop to a low-privileged user
var user_group = opt_drop.value
if user_group != null then user_group.drop_privileges
factory.run
lib/nitcorn/examples/src/simple_file_server.nit:17,1--52,11