fix logging, 404 handling
s-ol
10 months ago
27 | 27 | opts.host = 'localhost' unless opts.host |
28 | 28 | opts.port = 8000 unless opts.port |
29 | 29 | opts.onstream = @\stream |
30 | opts.onerror = @\error | |
31 | 30 | |
32 | 31 | @server = server.listen opts |
33 | 32 | |
107 | 106 | facet.type = base |
108 | 107 | @handle_debug fileder, facet |
109 | 108 | else if not fileder\has_facet facet.name |
110 | 404, "facet '#{facet.name}' not found in fileder '#{path}'" | |
109 | return 404, "facet '#{facet.name}' not found in fileder '#{path}'" | |
111 | 110 | else |
112 | 111 | fileder\get facet |
113 | 112 | |
123 | 122 | path = req\get ':path' |
124 | 123 | path = decodeURI path |
125 | 124 | |
126 | path_facet, type = path\match '(.*):(.*)' | |
125 | path_facet, typ = path\match '(.*):(.*)' | |
127 | 126 | path_facet or= path |
128 | 127 | path, facet = path_facet\match '(.*)/([^/]*)' |
129 | 128 | |
130 | facet = if facet == '' and (not type or type == '') and method ~= 'GET' and method ~= 'HEAD' | |
129 | facet = if facet == '' and (not typ or typ == '') and method ~= 'GET' and method ~= 'HEAD' | |
131 | 130 | nil |
132 | 131 | else |
133 | type or= '?' | |
134 | type = type\match '%s*(.*)' | |
135 | Key facet, type | |
132 | typ or= '?' | |
133 | typ = typ\match '%s*(.*)' | |
134 | Key facet, typ | |
136 | 135 | |
137 | 136 | value = stream\get_body_as_string! |
138 | 137 | ok, status, body = xpcall @.handle, err_and_trace, @, method, path, facet, value |
152 | 151 | stream\write_headers res, method == 'HEAD' |
153 | 152 | if method ~= 'HEAD' |
154 | 153 | stream\write_chunk body, true |
155 | ||
156 | error: (sv, ctx, op, err, errno) => | |
157 | msg = "#{op} on #{tostring ctx} failed" | |
158 | msg = "#{msg}: #{err}" if err | |
159 | 154 | |
160 | 155 | -- usage: |
161 | 156 | -- moon server.moon [FLAGS] [STORE] [host] [port] |