comparison src/luan/modules/http/Server.luan @ 1716:b82767112d8e

add String.regex
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 24 Jul 2022 23:43:03 -0600
parents 46cf5137cb6b
children d3ea0380dfb6
comparison
equal deleted inserted replaced
1715:ad44e849c60c 1716:b82767112d8e
1 local Luan = require "luan:Luan.luan" 1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error 2 local error = Luan.error
3 local String = require "luan:String.luan" 3 local String = require "luan:String.luan"
4 local gsub = String.gsub or error() 4 local regex = String.regex or error()
5 local match = String.match or error() 5 local contains = String.contains or error()
6 local matches = String.matches or error()
7 local Io = require "luan:Io.luan" 6 local Io = require "luan:Io.luan"
8 local uri = Io.uri or error() 7 local uri = Io.uri or error()
9 local Package = require "luan:Package.luan" 8 local Package = require "luan:Package.luan"
10 local Rpc = require "luan:Rpc.luan" 9 local Rpc = require "luan:Rpc.luan"
11 local Thread = require "luan:Thread.luan" 10 local Thread = require "luan:Thread.luan"
29 28
30 29
31 local Server = {} 30 local Server = {}
32 31
33 function Server.init_dir(dir) 32 function Server.init_dir(dir)
34 if not matches(dir,":") then 33 if not contains(dir,":") then
35 dir = "file:"..dir 34 dir = "file:"..dir
36 end 35 end
37 dir = gsub(dir,"/$","") -- remove trailing '/' if any 36 dir = regex("/$").gsub(dir,"") -- remove trailing '/' if any
38 Http.dir = dir 37 Http.dir = dir
39 Http.is_serving = true 38 Http.is_serving = true
40 function Io.schemes.site(path) 39 function Io.schemes.site(path)
41 local u = uri( dir..path ) 40 local u = uri( dir..path )
42 if u ~= nil then 41 if u ~= nil then
61 return luan_handler.call_rpc(fn_name,...) 60 return luan_handler.call_rpc(fn_name,...)
62 end 61 end
63 Thread.fork(Rpc.serve) 62 Thread.fork(Rpc.serve)
64 end 63 end
65 64
65 local file_regex = regex("^file:(.*)$")
66
66 function Server.serve(dir,port) 67 function Server.serve(dir,port)
67 port = port or 8080 68 port = port or 8080
68 Server.init_dir(dir) 69 Server.init_dir(dir)
69 local dir_path = match(Http.dir,"^file:(.*)$") or error "server dir must be scheme 'file:'" 70 local dir_path = file_regex.match(Http.dir) or error "server dir must be scheme 'file:'"
70 local file_handler = FileHandler.new(dir_path) 71 local file_handler = FileHandler.new(dir_path)
71 local luan_handler = LuanHandler.new() 72 local luan_handler = LuanHandler.new()
72 local handler = ListHandler.new( luan_handler, file_handler ) 73 local handler = ListHandler.new( luan_handler, file_handler )
73 handler = ContentTypeHandler.new(handler) 74 handler = ContentTypeHandler.new(handler)
74 handler = IndexHandler.new(handler) 75 handler = IndexHandler.new(handler)