URL shortener: Difference between revisions
Content added Content deleted
(→{{header|Wren}}: Added libheader.) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 43: | Line 43: | ||
{{libheader| kemal}} |
{{libheader| kemal}} |
||
< |
<syntaxhighlight lang="ruby">require "kemal" |
||
CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".chars |
CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".chars |
||
Line 66: | Line 66: | ||
end |
end |
||
Kemal.run</ |
Kemal.run</syntaxhighlight> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
Line 78: | Line 78: | ||
{{libheader| Inifiles}} |
{{libheader| Inifiles}} |
||
Highly inspired in [[#Go]] |
Highly inspired in [[#Go]] |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program URLShortenerServer; |
program URLShortenerServer; |
||
Line 244: | Line 244: | ||
Manager.Free; |
Manager.Free; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 251: | Line 251: | ||
</pre> |
</pre> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">// shortener.go |
||
package main |
package main |
||
Line 319: | Line 319: | ||
db := make(database) |
db := make(database) |
||
log.Fatal(http.ListenAndServe(host, db)) |
log.Fatal(http.ListenAndServe(host, db)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 355: | Line 355: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
{{works with|Node.js}} |
{{works with|Node.js}} |
||
< |
<syntaxhighlight lang="javascript">#!/usr/bin/env node |
||
var mapping = new Map(); |
var mapping = new Map(); |
||
Line 402: | Line 402: | ||
res.writeHead(404); |
res.writeHead(404); |
||
res.end(); |
res.end(); |
||
}).listen(8080);</ |
}).listen(8080);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>$ curl -X POST http://localhost:8080/ -H "Content-Type: application/json" --data "{\"long\":\"https://www.example.com\"}" |
<pre>$ curl -X POST http://localhost:8080/ -H "Content-Type: application/json" --data "{\"long\":\"https://www.example.com\"}" |
||
Line 411: | Line 411: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Assumes an SQLite database containing a table called LONGNAMESHORTNAME (consisting of two string columns) already exists. |
Assumes an SQLite database containing a table called LONGNAMESHORTNAME (consisting of two string columns) already exists. |
||
< |
<syntaxhighlight lang="julia">using Base64, HTTP, JSON2, Sockets, SQLite, SHA |
||
function processpost(req::HTTP.Request, urilen=8) |
function processpost(req::HTTP.Request, urilen=8) |
||
Line 456: | Line 456: | ||
const localport = 3000 |
const localport = 3000 |
||
run_web_server(serveraddress, localport) |
run_web_server(serveraddress, localport) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck">use Collection.Generic; |
||
use Data.JSON; |
use Data.JSON; |
||
use Web.HTTP; |
use Web.HTTP; |
||
Line 520: | Line 520: | ||
return Response->New(200, response); |
return Response->New(200, response); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(notonline)--> |
||
<span style="color: #000080;font-style:italic;">-- |
<span style="color: #000080;font-style:italic;">-- |
||
-- demo\rosetta\URL_shortener.exw |
-- demo\rosetta\URL_shortener.exw |
||
Line 628: | Line 628: | ||
<span style="color: #000000;">sock</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">closesocket</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sock</span><span style="color: #0000FF;">)</span> |
<span style="color: #000000;">sock</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">closesocket</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sock</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #7060A8;">WSACleanup</span><span style="color: #0000FF;">()</span> |
<span style="color: #7060A8;">WSACleanup</span><span style="color: #0000FF;">()</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
Sample session output: |
Sample session output: |
||
Line 674: | Line 674: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(load "@lib/http.l") |
||
(allowed NIL "!short" u) |
(allowed NIL "!short" u) |
||
(pool "urls.db" (6)) |
(pool "urls.db" (6)) |
||
Line 685: | Line 685: | ||
(commit 'upd) |
(commit 'upd) |
||
(respond (pack "http://127.0.0.1:8080/?" K "\n")) ) ) ) |
(respond (pack "http://127.0.0.1:8080/?" K "\n")) ) ) ) |
||
(server 8080 "!short")</ |
(server 8080 "!short")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 719: | Line 719: | ||
===Flask=== |
===Flask=== |
||
{{libheader|Flask}} |
{{libheader|Flask}} |
||
< |
<syntaxhighlight lang="python"> |
||
"""A URL shortener using Flask. Requires Python >=3.5.""" |
"""A URL shortener using Flask. Requires Python >=3.5.""" |
||
Line 904: | Line 904: | ||
app.env = "development" |
app.env = "development" |
||
app.run(debug=True) |
app.run(debug=True) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 919: | Line 919: | ||
There is '''NO''' security or authentication on this minimal app. Not recommended to run this as-is on a public facing server. It would not be too difficult to ''add'' appropriate security, but it isn't a requirement of this task. Very minimal error checking and recovery. |
There is '''NO''' security or authentication on this minimal app. Not recommended to run this as-is on a public facing server. It would not be too difficult to ''add'' appropriate security, but it isn't a requirement of this task. Very minimal error checking and recovery. |
||
<lang |
<syntaxhighlight lang="raku" line># Persistent URL storage |
||
use JSON::Fast; |
use JSON::Fast; |
||
Line 975: | Line 975: | ||
react whenever signal(SIGINT) { $shorten.stop; exit; } |
react whenever signal(SIGINT) { $shorten.stop; exit; } |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Line 982: | Line 982: | ||
{{libheader|Wren-json}} |
{{libheader|Wren-json}} |
||
An embedded program with a Go host so we can use its net/http module. |
An embedded program with a Go host so we can use its net/http module. |
||
< |
<syntaxhighlight lang="ecmascript">/* url_shortener.wren */ |
||
import "./json" for JSON |
import "./json" for JSON |
||
Line 1,041: | Line 1,041: | ||
foreign static redirect(url, code) |
foreign static redirect(url, code) |
||
}</ |
}</syntaxhighlight> |
||
We now embed this script in the following Go program and build it. |
We now embed this script in the following Go program and build it. |
||
< |
<syntaxhighlight lang="go">/* go build url_shortener.go */ |
||
package main |
package main |
||
Line 1,154: | Line 1,154: | ||
log.Fatal(http.ListenAndServe(host, nil)) |
log.Fatal(http.ListenAndServe(host, nil)) |
||
vm.Free() |
vm.Free() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Sample output (abbreviated) including building and starting the server from Ubuntu 20.04 terminal and entering a valid and then an invalid shortened URL: |
Sample output (abbreviated) including building and starting the server from Ubuntu 20.04 terminal and entering a valid and then an invalid shortened URL: |