+2
-2
gleam.toml
+2
-2
gleam.toml
···
1
name = "wisp"
2
-
version = "1.2.0"
3
gleam = ">= 1.1.0"
4
description = "A practical web framework for Gleam"
5
licences = ["Apache-2.0"]
···
13
gleam_erlang = ">= 0.21.0 and < 2.0.0"
14
gleam_http = ">= 3.5.0 and < 4.0.0"
15
gleam_json = ">= 0.6.0 and < 3.0.0"
16
-
gleam_stdlib = ">= 0.29.0 and < 2.0.0"
17
mist = ">= 1.2.0 and < 4.0.0"
18
simplifile = ">= 2.0.0 and < 3.0.0"
19
marceau = ">= 1.1.0 and < 2.0.0"
···
1
name = "wisp"
2
+
version = "1.2.1"
3
gleam = ">= 1.1.0"
4
description = "A practical web framework for Gleam"
5
licences = ["Apache-2.0"]
···
13
gleam_erlang = ">= 0.21.0 and < 2.0.0"
14
gleam_http = ">= 3.5.0 and < 4.0.0"
15
gleam_json = ">= 0.6.0 and < 3.0.0"
16
+
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
17
mist = ">= 1.2.0 and < 4.0.0"
18
simplifile = ">= 2.0.0 and < 3.0.0"
19
marceau = ">= 1.1.0 and < 2.0.0"
+10
-10
manifest.toml
+10
-10
manifest.toml
···
4
packages = [
5
{ name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" },
6
{ name = "directories", version = "1.1.0", build_tools = ["gleam"], requirements = ["envoy", "gleam_stdlib", "platform", "simplifile"], otp_app = "directories", source = "hex", outer_checksum = "BDA521A4EB9EE3A7894F0DC863797878E91FF5C7826F7084B2E731E208BDB076" },
7
-
{ name = "envoy", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "CFAACCCFC47654F7E8B75E614746ED924C65BD08B1DE21101548AC314A8B6A41" },
8
{ name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" },
9
-
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
10
-
{ name = "gleam_crypto", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "ADD058DEDE8F0341F1ADE3AAC492A224F15700829D9A3A3F9ADF370F875C51B7" },
11
-
{ name = "gleam_erlang", version = "0.26.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "3DF72F95F4716883FA51396FB0C550ED3D55195B541568CAF09745984FD37AD1" },
12
-
{ name = "gleam_http", version = "3.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "EA66440C2269F7CED0F6845E5BD0DB68095775D627FA709A841CA78A398D6D56" },
13
-
{ name = "gleam_json", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "CB10B0E7BF44282FB25162F1A24C1A025F6B93E777CCF238C4017E4EEF2CDE97" },
14
-
{ name = "gleam_otp", version = "0.12.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "CD5FC777E99673BDB390092DF85E34EAA6B8EE1882147496290AB3F45A4960B1" },
15
-
{ name = "gleam_stdlib", version = "0.40.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "86606B75A600BBD05E539EB59FABC6E307EEEA7B1E5865AFB6D980A93BCB2181" },
16
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
17
{ name = "glisten", version = "6.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib", "logging", "telemetry"], otp_app = "glisten", source = "hex", outer_checksum = "912132751031473CB38F454120124FFC96AF6B0EA33D92C9C90DB16327A2A972" },
18
{ name = "gramps", version = "2.0.3", build_tools = ["gleam"], requirements = ["gleam_crypto", "gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gramps", source = "hex", outer_checksum = "3CCAA6E081225180D95C79679D383BBF51C8D1FDC1B84DA1DA444F628C373793" },
19
{ name = "hpack_erl", version = "0.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "hpack", source = "hex", outer_checksum = "D6137D7079169D8C485C6962DFE261AF5B9EF60FBC557344511C1E65E3D95FB0" },
20
{ name = "logging", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "1098FBF10B54B44C2C7FDF0B01C1253CAFACDACABEFB4B0D027803246753E06D" },
21
-
{ name = "marceau", version = "1.2.0", build_tools = ["gleam"], requirements = [], otp_app = "marceau", source = "hex", outer_checksum = "5188D643C181EE350D8A20A3BDBD63AF7B6C505DE333CFBE05EF642ADD88A59B" },
22
{ name = "mist", version = "3.0.0", build_tools = ["gleam"], requirements = ["birl", "gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "glisten", "gramps", "hpack_erl", "logging"], otp_app = "mist", source = "hex", outer_checksum = "CDA1A74E768419235E16886463EC4722EFF4AB3F8D820A76EAD45D7C167D7282" },
23
{ name = "platform", version = "1.0.0", build_tools = ["gleam"], requirements = [], otp_app = "platform", source = "hex", outer_checksum = "8339420A95AD89AAC0F82F4C3DB8DD401041742D6C3F46132A8739F6AEB75391" },
24
{ name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" },
···
33
gleam_erlang = { version = ">= 0.21.0 and < 2.0.0" }
34
gleam_http = { version = ">= 3.5.0 and < 4.0.0" }
35
gleam_json = { version = ">= 0.6.0 and < 3.0.0" }
36
-
gleam_stdlib = { version = ">= 0.29.0 and < 2.0.0" }
37
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
38
logging = { version = ">= 1.2.0 and < 2.0.0" }
39
marceau = { version = ">= 1.1.0 and < 2.0.0" }
···
4
packages = [
5
{ name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" },
6
{ name = "directories", version = "1.1.0", build_tools = ["gleam"], requirements = ["envoy", "gleam_stdlib", "platform", "simplifile"], otp_app = "directories", source = "hex", outer_checksum = "BDA521A4EB9EE3A7894F0DC863797878E91FF5C7826F7084B2E731E208BDB076" },
7
+
{ name = "envoy", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "95FD059345AA982E89A0B6E2A3BF1CF43E17A7048DCD85B5B65D3B9E4E39D359" },
8
{ name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" },
9
+
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
10
+
{ name = "gleam_crypto", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "8AE56026B3E05EBB1F076778478A762E9EB62B31AEEB4285755452F397029D22" },
11
+
{ name = "gleam_erlang", version = "0.30.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "760618870AE4A497B10C73548E6E44F43B76292A54F0207B3771CBB599C675B4" },
12
+
{ name = "gleam_http", version = "3.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "A9EE0722106FCCAB8AD3BF9D0A3EFF92BFE8561D59B83BAE96EB0BE1938D4E0F" },
13
+
{ name = "gleam_json", version = "2.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "0A57FB5666E695FD2BEE74C0428A98B0FC11A395D2C7B4CDF5E22C5DD32C74C6" },
14
+
{ name = "gleam_otp", version = "0.14.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5A8CE8DBD01C29403390A7BD5C0A63D26F865C83173CF9708E6E827E53159C65" },
15
+
{ name = "gleam_stdlib", version = "0.43.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "69EF22E78FDCA9097CBE7DF91C05B2A8B5436826D9F66680D879182C0860A747" },
16
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
17
{ name = "glisten", version = "6.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib", "logging", "telemetry"], otp_app = "glisten", source = "hex", outer_checksum = "912132751031473CB38F454120124FFC96AF6B0EA33D92C9C90DB16327A2A972" },
18
{ name = "gramps", version = "2.0.3", build_tools = ["gleam"], requirements = ["gleam_crypto", "gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gramps", source = "hex", outer_checksum = "3CCAA6E081225180D95C79679D383BBF51C8D1FDC1B84DA1DA444F628C373793" },
19
{ name = "hpack_erl", version = "0.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "hpack", source = "hex", outer_checksum = "D6137D7079169D8C485C6962DFE261AF5B9EF60FBC557344511C1E65E3D95FB0" },
20
{ name = "logging", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "1098FBF10B54B44C2C7FDF0B01C1253CAFACDACABEFB4B0D027803246753E06D" },
21
+
{ name = "marceau", version = "1.3.0", build_tools = ["gleam"], requirements = [], otp_app = "marceau", source = "hex", outer_checksum = "2D1C27504BEF45005F5DFB18591F8610FB4BFA91744878210BDC464412EC44E9" },
22
{ name = "mist", version = "3.0.0", build_tools = ["gleam"], requirements = ["birl", "gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "glisten", "gramps", "hpack_erl", "logging"], otp_app = "mist", source = "hex", outer_checksum = "CDA1A74E768419235E16886463EC4722EFF4AB3F8D820A76EAD45D7C167D7282" },
23
{ name = "platform", version = "1.0.0", build_tools = ["gleam"], requirements = [], otp_app = "platform", source = "hex", outer_checksum = "8339420A95AD89AAC0F82F4C3DB8DD401041742D6C3F46132A8739F6AEB75391" },
24
{ name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" },
···
33
gleam_erlang = { version = ">= 0.21.0 and < 2.0.0" }
34
gleam_http = { version = ">= 3.5.0 and < 4.0.0" }
35
gleam_json = { version = ">= 0.6.0 and < 3.0.0" }
36
+
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
37
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
38
logging = { version = ">= 1.2.0 and < 2.0.0" }
39
marceau = { version = ">= 1.1.0 and < 2.0.0" }
+27
-29
src/wisp.gleam
+27
-29
src/wisp.gleam
···
1
import exception
2
import gleam/bit_array
3
import gleam/bool
4
-
import gleam/bytes_builder.{type BytesBuilder}
5
import gleam/crypto
6
import gleam/dict.{type Dict}
7
import gleam/dynamic.{type Dynamic}
···
19
import gleam/option.{type Option}
20
import gleam/result
21
import gleam/string
22
-
import gleam/string_builder.{type StringBuilder}
23
import gleam/uri
24
import logging
25
import marceau
···
35
pub type Body {
36
/// A body of unicode text.
37
///
38
-
/// The body is represented using a `StringBuilder`. If you have a `String`
39
-
/// you can use the `string_builder.from_string` function to convert it.
40
///
41
-
Text(StringBuilder)
42
/// A body of binary data.
43
///
44
-
/// The body is represented using a `BytesBuilder`. If you have a `BitArray`
45
-
/// you can use the `bytes_builder.from_bit_array` function to convert it.
46
///
47
-
Bytes(BytesBuilder)
48
/// A body of the contents of a file.
49
///
50
/// This will be sent efficiently using the `send_file` function of the
···
146
/// # Examples
147
///
148
/// ```gleam
149
/// response(200)
150
-
/// |> file_download_from_memory(named: "myfile.txt", containing: "Hello, Joe!")
151
/// // -> Response(
152
/// // 200,
153
/// // [#("content-disposition", "attachment; filename=\"myfile.txt\"")],
···
158
pub fn file_download_from_memory(
159
response: Response,
160
named name: String,
161
-
containing data: BytesBuilder,
162
) -> Response {
163
let name = uri.percent_encode(name)
164
response
···
177
/// # Examples
178
///
179
/// ```gleam
180
-
/// let body = string_builder.from_string("<h1>Hello, Joe!</h1>")
181
/// html_response(body, 200)
182
/// // -> Response(200, [#("content-type", "text/html")], Text(body))
183
/// ```
184
///
185
-
pub fn html_response(html: StringBuilder, status: Int) -> Response {
186
HttpResponse(
187
status,
188
[#("content-type", "text/html; charset=utf-8")],
···
198
/// # Examples
199
///
200
/// ```gleam
201
-
/// let body = string_builder.from_string("{\"name\": \"Joe\"}")
202
/// json_response(body, 200)
203
/// // -> Response(200, [#("content-type", "application/json")], Text(body))
204
/// ```
205
///
206
-
pub fn json_response(json: StringBuilder, status: Int) -> Response {
207
HttpResponse(
208
status,
209
[#("content-type", "application/json; charset=utf-8")],
···
219
/// # Examples
220
///
221
/// ```gleam
222
-
/// let body = string_builder.from_string("<h1>Hello, Joe!</h1>")
223
/// response(201)
224
/// |> html_body(body)
225
/// // -> Response(201, [#("content-type", "text/html; charset=utf-8")], Text(body))
226
/// ```
227
///
228
-
pub fn html_body(response: Response, html: StringBuilder) -> Response {
229
response
230
|> response.set_body(Text(html))
231
|> response.set_header("content-type", "text/html; charset=utf-8")
···
239
/// # Examples
240
///
241
/// ```gleam
242
-
/// let body = string_builder.from_string("{\"name\": \"Joe\"}")
243
/// response(201)
244
/// |> json_body(body)
245
/// // -> Response(201, [#("content-type", "application/json; charset=utf-8")], Text(body))
246
/// ```
247
///
248
-
pub fn json_body(response: Response, json: StringBuilder) -> Response {
249
response
250
|> response.set_body(Text(json))
251
|> response.set_header("content-type", "application/json; charset=utf-8")
252
}
253
254
-
/// Set the body of a response to a given string builder.
255
///
256
/// You likely want to also set the request `content-type` header to an
257
/// appropriate value for the format of the content.
···
259
/// # Examples
260
///
261
/// ```gleam
262
-
/// let body = string_builder.from_string("Hello, Joe!")
263
/// response(201)
264
-
/// |> string_builder_body(body)
265
/// // -> Response(201, [], Text(body))
266
/// ```
267
///
268
-
pub fn string_builder_body(
269
-
response: Response,
270
-
content: StringBuilder,
271
-
) -> Response {
272
response
273
|> response.set_body(Text(content))
274
}
275
276
-
/// Set the body of a response to a given string builder.
277
///
278
/// You likely want to also set the request `content-type` header to an
279
/// appropriate value for the format of the content.
···
287
/// // -> Response(
288
/// // 201,
289
/// // [],
290
-
/// // Text(string_builder.from_string("Hello, Joe"))
291
/// // )
292
/// ```
293
///
294
pub fn string_body(response: Response, content: String) -> Response {
295
response
296
-
|> response.set_body(Text(string_builder.from_string(content)))
297
}
298
299
/// Escape a string so that it can be safely included in a HTML document.
···
1475
http.Get, True -> {
1476
let path =
1477
path
1478
-
|> string.drop_left(string.length(prefix))
1479
|> string.replace(each: "..", with: "")
1480
|> internal.join_path(directory, _)
1481
···
1
import exception
2
import gleam/bit_array
3
import gleam/bool
4
+
import gleam/bytes_tree.{type BytesTree}
5
import gleam/crypto
6
import gleam/dict.{type Dict}
7
import gleam/dynamic.{type Dynamic}
···
19
import gleam/option.{type Option}
20
import gleam/result
21
import gleam/string
22
+
import gleam/string_tree.{type StringTree}
23
import gleam/uri
24
import logging
25
import marceau
···
35
pub type Body {
36
/// A body of unicode text.
37
///
38
+
/// The body is represented using a `StringTree`. If you have a `String`
39
+
/// you can use the `string_tree.from_string` function to convert it.
40
///
41
+
Text(StringTree)
42
/// A body of binary data.
43
///
44
+
/// The body is represented using a `BytesTree`. If you have a `BitArray`
45
+
/// you can use the `bytes_tree.from_bit_array` function to convert it.
46
///
47
+
Bytes(BytesTree)
48
/// A body of the contents of a file.
49
///
50
/// This will be sent efficiently using the `send_file` function of the
···
146
/// # Examples
147
///
148
/// ```gleam
149
+
/// let content = bytes_tree.from_string("Hello, Joe!")
150
/// response(200)
151
+
/// |> file_download_from_memory(named: "myfile.txt", containing: content)
152
/// // -> Response(
153
/// // 200,
154
/// // [#("content-disposition", "attachment; filename=\"myfile.txt\"")],
···
159
pub fn file_download_from_memory(
160
response: Response,
161
named name: String,
162
+
containing data: BytesTree,
163
) -> Response {
164
let name = uri.percent_encode(name)
165
response
···
178
/// # Examples
179
///
180
/// ```gleam
181
+
/// let body = string_tree.from_string("<h1>Hello, Joe!</h1>")
182
/// html_response(body, 200)
183
/// // -> Response(200, [#("content-type", "text/html")], Text(body))
184
/// ```
185
///
186
+
pub fn html_response(html: StringTree, status: Int) -> Response {
187
HttpResponse(
188
status,
189
[#("content-type", "text/html; charset=utf-8")],
···
199
/// # Examples
200
///
201
/// ```gleam
202
+
/// let body = string_tree.from_string("{\"name\": \"Joe\"}")
203
/// json_response(body, 200)
204
/// // -> Response(200, [#("content-type", "application/json")], Text(body))
205
/// ```
206
///
207
+
pub fn json_response(json: StringTree, status: Int) -> Response {
208
HttpResponse(
209
status,
210
[#("content-type", "application/json; charset=utf-8")],
···
220
/// # Examples
221
///
222
/// ```gleam
223
+
/// let body = string_tree.from_string("<h1>Hello, Joe!</h1>")
224
/// response(201)
225
/// |> html_body(body)
226
/// // -> Response(201, [#("content-type", "text/html; charset=utf-8")], Text(body))
227
/// ```
228
///
229
+
pub fn html_body(response: Response, html: StringTree) -> Response {
230
response
231
|> response.set_body(Text(html))
232
|> response.set_header("content-type", "text/html; charset=utf-8")
···
240
/// # Examples
241
///
242
/// ```gleam
243
+
/// let body = string_tree.from_string("{\"name\": \"Joe\"}")
244
/// response(201)
245
/// |> json_body(body)
246
/// // -> Response(201, [#("content-type", "application/json; charset=utf-8")], Text(body))
247
/// ```
248
///
249
+
pub fn json_body(response: Response, json: StringTree) -> Response {
250
response
251
|> response.set_body(Text(json))
252
|> response.set_header("content-type", "application/json; charset=utf-8")
253
}
254
255
+
/// Set the body of a response to a given string tree.
256
///
257
/// You likely want to also set the request `content-type` header to an
258
/// appropriate value for the format of the content.
···
260
/// # Examples
261
///
262
/// ```gleam
263
+
/// let body = string_tree.from_string("Hello, Joe!")
264
/// response(201)
265
+
/// |> string_tree_body(body)
266
/// // -> Response(201, [], Text(body))
267
/// ```
268
///
269
+
pub fn string_tree_body(response: Response, content: StringTree) -> Response {
270
response
271
|> response.set_body(Text(content))
272
}
273
274
+
/// Set the body of a response to a given string.
275
///
276
/// You likely want to also set the request `content-type` header to an
277
/// appropriate value for the format of the content.
···
285
/// // -> Response(
286
/// // 201,
287
/// // [],
288
+
/// // Text(string_tree.from_string("Hello, Joe"))
289
/// // )
290
/// ```
291
///
292
pub fn string_body(response: Response, content: String) -> Response {
293
response
294
+
|> response.set_body(Text(string_tree.from_string(content)))
295
}
296
297
/// Escape a string so that it can be safely included in a HTML document.
···
1473
http.Get, True -> {
1474
let path =
1475
path
1476
+
|> string.drop_start(string.length(prefix))
1477
|> string.replace(each: "..", with: "")
1478
|> internal.join_path(directory, _)
1479
+6
-7
src/wisp/testing.gleam
+6
-7
src/wisp/testing.gleam
···
1
import gleam/bit_array
2
-
import gleam/bytes_builder
3
import gleam/crypto
4
import gleam/http
5
import gleam/http/request
6
import gleam/json.{type Json}
7
import gleam/option.{None, Some}
8
import gleam/string
9
-
import gleam/string_builder
10
import gleam/uri
11
import simplifile
12
import wisp.{type Request, type Response, Bytes, Empty, File, Text}
···
227
pub fn string_body(response: Response) -> String {
228
case response.body {
229
Empty -> ""
230
-
Text(builder) -> string_builder.to_string(builder)
231
Bytes(bytes) -> {
232
-
let data = bytes_builder.to_bit_array(bytes)
233
let assert Ok(string) = bit_array.to_string(data)
234
string
235
}
···
250
pub fn bit_array_body(response: Response) -> BitArray {
251
case response.body {
252
Empty -> <<>>
253
-
Bytes(builder) -> bytes_builder.to_bit_array(builder)
254
-
Text(builder) ->
255
-
bytes_builder.to_bit_array(bytes_builder.from_string_builder(builder))
256
File(path) -> {
257
let assert Ok(contents) = simplifile.read_bits(path)
258
contents
···
1
import gleam/bit_array
2
+
import gleam/bytes_tree
3
import gleam/crypto
4
import gleam/http
5
import gleam/http/request
6
import gleam/json.{type Json}
7
import gleam/option.{None, Some}
8
import gleam/string
9
+
import gleam/string_tree
10
import gleam/uri
11
import simplifile
12
import wisp.{type Request, type Response, Bytes, Empty, File, Text}
···
227
pub fn string_body(response: Response) -> String {
228
case response.body {
229
Empty -> ""
230
+
Text(tree) -> string_tree.to_string(tree)
231
Bytes(bytes) -> {
232
+
let data = bytes_tree.to_bit_array(bytes)
233
let assert Ok(string) = bit_array.to_string(data)
234
string
235
}
···
250
pub fn bit_array_body(response: Response) -> BitArray {
251
case response.body {
252
Empty -> <<>>
253
+
Bytes(tree) -> bytes_tree.to_bit_array(tree)
254
+
Text(tree) -> bytes_tree.to_bit_array(bytes_tree.from_string_tree(tree))
255
File(path) -> {
256
let assert Ok(contents) = simplifile.read_bits(path)
257
contents
+5
-5
src/wisp/wisp_mist.gleam
+5
-5
src/wisp/wisp_mist.gleam
···
1
import exception
2
-
import gleam/bytes_builder
3
import gleam/http/request.{type Request as HttpRequest}
4
import gleam/http/response.{type Response as HttpResponse}
5
import gleam/option
···
63
chunk: Result(mist.Chunk, mist.ReadError),
64
) -> Result(internal.Read, Nil) {
65
chunk
66
-
|> result.nil_error
67
|> result.map(fn(chunk) {
68
case chunk {
69
mist.Done -> internal.ReadingFinished
···
75
76
fn mist_response(response: wisp.Response) -> HttpResponse(mist.ResponseData) {
77
let body = case response.body {
78
-
wisp.Empty -> mist.Bytes(bytes_builder.new())
79
-
wisp.Text(text) -> mist.Bytes(bytes_builder.from_string_builder(text))
80
wisp.Bytes(bytes) -> mist.Bytes(bytes)
81
wisp.File(path) -> mist_send_file(path)
82
}
···
90
Error(error) -> {
91
wisp.log_error(string.inspect(error))
92
// TODO: return 500
93
-
mist.Bytes(bytes_builder.new())
94
}
95
}
96
}
···
1
import exception
2
+
import gleam/bytes_tree
3
import gleam/http/request.{type Request as HttpRequest}
4
import gleam/http/response.{type Response as HttpResponse}
5
import gleam/option
···
63
chunk: Result(mist.Chunk, mist.ReadError),
64
) -> Result(internal.Read, Nil) {
65
chunk
66
+
|> result.replace_error(Nil)
67
|> result.map(fn(chunk) {
68
case chunk {
69
mist.Done -> internal.ReadingFinished
···
75
76
fn mist_response(response: wisp.Response) -> HttpResponse(mist.ResponseData) {
77
let body = case response.body {
78
+
wisp.Empty -> mist.Bytes(bytes_tree.new())
79
+
wisp.Text(text) -> mist.Bytes(bytes_tree.from_string_tree(text))
80
wisp.Bytes(bytes) -> mist.Bytes(bytes)
81
wisp.File(path) -> mist_send_file(path)
82
}
···
90
Error(error) -> {
91
wisp.log_error(string.inspect(error))
92
// TODO: return 500
93
+
mist.Bytes(bytes_tree.new())
94
}
95
}
96
}
+3
-3
test/wisp/testing_test.gleam
+3
-3
test/wisp/testing_test.gleam
···
2
import gleam/http/response
3
import gleam/json
4
import gleam/option.{None, Some}
5
-
import gleam/string_builder
6
import gleeunit/should
7
import wisp
8
import wisp/testing
···
502
503
pub fn string_body_text_test() {
504
wisp.ok()
505
-
|> response.set_body(wisp.Text(string_builder.from_string("Hello, Joe!")))
506
|> testing.string_body
507
|> should.equal("Hello, Joe!")
508
}
···
523
524
pub fn bit_array_body_text_test() {
525
wisp.ok()
526
-
|> response.set_body(wisp.Text(string_builder.from_string("Hello, Joe!")))
527
|> testing.bit_array_body
528
|> should.equal(<<"Hello, Joe!":utf8>>)
529
}
···
2
import gleam/http/response
3
import gleam/json
4
import gleam/option.{None, Some}
5
+
import gleam/string_tree
6
import gleeunit/should
7
import wisp
8
import wisp/testing
···
502
503
pub fn string_body_text_test() {
504
wisp.ok()
505
+
|> response.set_body(wisp.Text(string_tree.from_string("Hello, Joe!")))
506
|> testing.string_body
507
|> should.equal("Hello, Joe!")
508
}
···
523
524
pub fn bit_array_body_text_test() {
525
wisp.ok()
526
+
|> response.set_body(wisp.Text(string_tree.from_string("Hello, Joe!")))
527
|> testing.bit_array_body
528
|> should.equal(<<"Hello, Joe!":utf8>>)
529
}
+12
-12
test/wisp_test.gleam
+12
-12
test/wisp_test.gleam
···
11
import gleam/list
12
import gleam/set
13
import gleam/string
14
-
import gleam/string_builder
15
import gleeunit
16
import gleeunit/should
17
import simplifile
···
119
}
120
121
pub fn json_response_test() {
122
-
let body = string_builder.from_string("{\"one\":1,\"two\":2}")
123
let response = wisp.json_response(body, 201)
124
response.status
125
|> should.equal(201)
···
131
}
132
133
pub fn html_response_test() {
134
-
let body = string_builder.from_string("Hello, world!")
135
let response = wisp.html_response(body, 200)
136
response.status
137
|> should.equal(200)
···
143
}
144
145
pub fn html_body_test() {
146
-
let body = string_builder.from_string("Hello, world!")
147
let response =
148
wisp.method_not_allowed([http.Get])
149
|> wisp.html_body(body)
···
751
list.key_find(request.headers, "x-original-method")
752
|> should.equal(header)
753
754
-
string_builder.from_string("Hello!")
755
|> wisp.html_response(201)
756
}
757
···
761
|> should.equal(Response(
762
201,
763
[#("content-type", "text/html; charset=utf-8")],
764
-
wisp.Text(string_builder.from_string("Hello!")),
765
))
766
767
testing.get("/", [])
···
897
|> should.equal(Response(
898
200,
899
[],
900
-
wisp.Text(string_builder.from_string("Hello, world!")),
901
))
902
}
903
904
-
pub fn string_builder_body_test() {
905
wisp.ok()
906
-
|> wisp.string_builder_body(string_builder.from_string("Hello, world!"))
907
|> should.equal(Response(
908
200,
909
[],
910
-
wisp.Text(string_builder.from_string("Hello, world!")),
911
))
912
}
913
914
pub fn json_body_test() {
915
wisp.ok()
916
-
|> wisp.json_body(string_builder.from_string("{\"one\":1,\"two\":2}"))
917
|> should.equal(Response(
918
200,
919
[#("content-type", "application/json; charset=utf-8")],
920
-
wisp.Text(string_builder.from_string("{\"one\":1,\"two\":2}")),
921
))
922
}
923
···
11
import gleam/list
12
import gleam/set
13
import gleam/string
14
+
import gleam/string_tree
15
import gleeunit
16
import gleeunit/should
17
import simplifile
···
119
}
120
121
pub fn json_response_test() {
122
+
let body = string_tree.from_string("{\"one\":1,\"two\":2}")
123
let response = wisp.json_response(body, 201)
124
response.status
125
|> should.equal(201)
···
131
}
132
133
pub fn html_response_test() {
134
+
let body = string_tree.from_string("Hello, world!")
135
let response = wisp.html_response(body, 200)
136
response.status
137
|> should.equal(200)
···
143
}
144
145
pub fn html_body_test() {
146
+
let body = string_tree.from_string("Hello, world!")
147
let response =
148
wisp.method_not_allowed([http.Get])
149
|> wisp.html_body(body)
···
751
list.key_find(request.headers, "x-original-method")
752
|> should.equal(header)
753
754
+
string_tree.from_string("Hello!")
755
|> wisp.html_response(201)
756
}
757
···
761
|> should.equal(Response(
762
201,
763
[#("content-type", "text/html; charset=utf-8")],
764
+
wisp.Text(string_tree.from_string("Hello!")),
765
))
766
767
testing.get("/", [])
···
897
|> should.equal(Response(
898
200,
899
[],
900
+
wisp.Text(string_tree.from_string("Hello, world!")),
901
))
902
}
903
904
+
pub fn string_tree_body_test() {
905
wisp.ok()
906
+
|> wisp.string_tree_body(string_tree.from_string("Hello, world!"))
907
|> should.equal(Response(
908
200,
909
[],
910
+
wisp.Text(string_tree.from_string("Hello, world!")),
911
))
912
}
913
914
pub fn json_body_test() {
915
wisp.ok()
916
+
|> wisp.json_body(string_tree.from_string("{\"one\":1,\"two\":2}"))
917
|> should.equal(Response(
918
200,
919
[#("content-type", "application/json; charset=utf-8")],
920
+
wisp.Text(string_tree.from_string("{\"one\":1,\"two\":2}")),
921
))
922
}
923