-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tcl
108 lines (86 loc) · 2.47 KB
/
main.tcl
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
if {$tcl_version < 8.6} {
puts stderr {This application requires Tcl version 8.6 or higher}
exit 1
}
package require tanzer 0.1
package require tanzer::file::handler
package require ncgi 1.4
source ./service/repository.tcl
source ./service/render.tcl
source ./service/form.tcl
source ./service/file.tcl
::repo::create
set server [::tanzer::server new]
$server route GET /api {localhost:8080} apply {
{event session args} {
if {$event ne "write"} {
return
}
set response [::tanzer::response new 200 {
Content-Type "application/json"
}]
$response buffer {{"json":true}}
$session send $response
$session nextRequest
}
}
$server route POST /api/file {localhost:8080} apply {
{event session {data ""}} {
switch -- $event "read" {
append currentdata [$session store data] $data
$session store data $currentdata
if {[$session requestBodyFinished]} {
set contentType [dict get [[$session request] headers] Content-Type]
set upload [::form::parseUpload $contentType [$session store data]]
set id [::repo::insert uploads $upload]
$session store newId $id
}
return
} "write" {
set response [::tanzer::response new 302 [
list Location /file-details/[$session store newId]
]]
$session send $response
$session nextRequest
}
}
}
$server route GET /api/file/:id {localhost:8080} apply {
{event session args} {
if {$event ne "write"} return
set id [[$session request] param id]
set data [fileservice::getfile $id]
set response [::tanzer::response new 200 [
list Content-Disposition "attachment; filename=[dict get $data filename]"
]]
$response buffer [dict get $data contents]
$session send $response
$session nextRequest
}
}
$server route GET /file-details/:file-id {localhost:8080} apply {
{event session args} {
if {$event ne "write"} return
set response [::tanzer::response new 200 {
Content-Type "text/html"
}]
set fileId [[$session request] param {file-id}]
$response buffer [::render::fileDetails $fileId]
$session send $response
$session nextRequest
}
}
$server route GET / {localhost:8080} apply {
{event session args} {
if {$event ne "write"} return
set response [::tanzer::response new 200 {
Content-Type "text/html"
}]
$response buffer [::render::index]
$session send $response
$session nextRequest
}
}
$server route GET /static/* {localhost:8080} [::tanzer::file::handler new [list root ./public listings 1]]
puts {about to listen on http://localhost:8080}
$server listen 8080