-
Notifications
You must be signed in to change notification settings - Fork 0
/
framework.r
73 lines (51 loc) · 2.11 KB
/
framework.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
Rebol [
Title: "Tiny Framework"
Documentation: http://www.rebol.net/cookbook/recipes/0057.html
]
;brings in the base FP functions
do %base/functional.r
do %base/helpers.r
config: context load %config.r
routing: context load %base/routing.r
templater: context load %base/templater.r
do %base/request.r
do %base/response.r
;stops the framework if a test fails
do %tests.r
serverPort: open/lines append tcp://: config/port ; port used for web connections
print rejoin ["^/listening on port " config/port]
; set up the routes
routeFilesLocations: copy f_map lambda [append copy config/routingDir ?] config/routeFiles
routing/setRoutes routeFilesLocations
routing/printRoutes
; holds the request information which is printed out as connections are made
buffer: make string! 1024 ; will auto-expand if needed
; processes each HTTP request from a web browser. The first step is to wait for a connection on the serverPort. When a connection is made, the connectionPort variable is set to the TCP port connection and is then used to get the HTTP request from the browser and send the result back to the browser.
forever [
print rejoin [newline "#####"]
print "waiting for request"
connectionPort: first wait serverPort
clear buffer
print "waiting over"
if error? error: try [
buffer: makeBufferFromConnectionPort buffer connectionPort
request: makeRequest config buffer
print rejoin ["request: [" newline request "]" newline]
response: handleRequest config routing request
sendResponse response connectionPort
; block must return something so we can 'try it
none
] [
error: disarm error
str_error: errorToString error
print rejoin [newline "#####" newline "error: " str_error]
response: make response_obj compose [
status: 500
data: (str_error)
]
sendResponse response connectionPort
]
; makes sure that the connection from the browser is closed, now that the requested web data has been returned.
print "port closed"
close connectionPort
]