-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault_controller.e
204 lines (159 loc) · 3.84 KB
/
default_controller.e
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
note
description: "Summary description for {DEFAULT_CONTROLLER}."
author: ""
date: "$Date$"
revision: "$Revision$"
deferred class
DEFAULT_CONTROLLER
feature
-- Handlers
db: DATABASE_HELPER
sess: SESSION_HELPER
json_h: JSON_HELPER
feature
-- Access
layout: STRING
content_type: STRING
feature {NONE}
-- Config
view_root: STRING = "www/view/"
feature {NONE}
-- Creation
make(db_conn: DATABASE_HELPER; sess_h: SESSION_HELPER)
require
db_con_exists: db_conn /= Void
db_avaliable: NOT db_conn.is_closed AND db_conn.is_accessible
do
db := db_conn
sess := sess_h
create json_h.default_create
create layout.make_empty
create content_type.make_empty
initialize
end
initialize
do
end
feature
-- Helpers
readView(short_path: STRING; custom_root: detachable STRING): STRING
require
path_not_void: short_path /= Void
local
full_path: PATH
root_folder: STRING
do
if attached custom_root then
root_folder := custom_root
else
root_folder := view_root
end
create full_path.make_from_string (root_folder + short_path)
Result := readFile(getFile(full_path))
end
getFile(path: PATH): PLAIN_TEXT_FILE
require
path_set: path /= Void AND NOT path.is_empty
local
file: PLAIN_TEXT_FILE
other_path: detachable PATH
do
create file.make_with_path (path.absolute_path)
if NOT file.exists then
Io.put_string ("ERROR: file not found " + path.out)
create other_path.make_from_string (view_root + "error/404.html")
create file.make_with_path (other_path.absolute_path)
end
Result := file
end
readFile(file: PLAIN_TEXT_FILE): STRING
do
--Io.put_string (file.path.absolute_path.out + "%N")
file.open_read
from
Result := ""
until
file.end_of_file
loop
file.read_line
Result := Result + file.last_string + "%N "
end
file.close
end
jsonEncode(data: HASH_TABLE[ANY, HASHABLE]): STRING
do
Result := json_h.encode(data)
end
convertPostData(req: WSF_REQUEST): HASH_TABLE[ANY, STRING]
local
field: WSF_VALUE
do
create Result.make (2)
across
req.form_parameters as param
loop
field := param.item
Result.put(field.string_representation, field.name)
end
end
feature
-- Features
renderHtml(path: STRING; opts: detachable HASH_TABLE[STRING, STRING]): STRING
require
path_not_void: path /= Void
local
layout_html: STRING
content_html: STRING
do
layout_html := readView("layout/" + layout + ".html", Void)
content_html := readView(layout + "/" + path + ".html", Void)
Result := layout_html
Result.replace_substring_all ("{{insert_content}}", content_html)
if attached opts as params then
across
params as param
loop
Result.replace_substring_all ("{{" + param.key + "}}", param.item)
end
end
end
renderJson(data: HASH_TABLE[ANY, HASHABLE]): STRING
require
not_void: data /= Void
do
Result := jsonEncode(data)
end
output(res: WSF_RESPONSE; content: STRING)
local
header: HTTP_HEADER
do
create header.make
if content_type ~ "html" then
header.put_content_type_text_html
elseif content_type ~ "json" then
header.put_content_type_text_json
else
header.put_content_type_text_plain
end
header.put_content_length (content.count)
res.set_status_code(200)
res.put_header_lines(header)
res.put_string(content)
res.flush
end
output404(req: WSF_REQUEST; res: WSF_RESPONSE)
local
layout_html: STRING
content_html: STRING
do
Io.put_string ("Error 404: " + req.path_info)
--res.send (create {WSF_NOT_FOUND_RESPONSE}.make (req))
layout_html := readView("layout/error.html", Void)
content_html := readView("error/404.html", Void)
layout_html.replace_substring_all ("{{insert_content}}", content_html)
output(res, layout_html)
end
invariant
content_type /= Void
layout /= Void
end