URL shortener: Difference between revisions

Content added Content deleted
(→‎{{header|Wren}}: Added libheader.)
m (syntax highlighting fixup automation)
Line 43: Line 43:
{{libheader| kemal}}
{{libheader| kemal}}


<lang ruby>require "kemal"
<syntaxhighlight lang="ruby">require "kemal"


CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".chars
CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".chars
Line 66: Line 66:
end
end


Kemal.run</lang>
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.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 251: Line 251:
</pre>
</pre>
=={{header|Go}}==
=={{header|Go}}==
<lang go>// shortener.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))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 355: Line 355:
=={{header|JavaScript}}==
=={{header|JavaScript}}==
{{works with|Node.js}}
{{works with|Node.js}}
<lang JavaScript>#!/usr/bin/env node
<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);</lang>
}).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.
<lang julia>using Base64, HTTP, JSON2, Sockets, SQLite, SHA
<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}}==
<lang objeck>use Collection.Generic;
<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);
}
}
}</lang>
}</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(notonline)-->
<!--<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
Sample session output:
Sample session output:
Line 674: Line 674:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(load "@lib/http.l")
<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")</lang>
(server 8080 "!short")</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 719: Line 719:
===Flask===
===Flask===
{{libheader|Flask}}
{{libheader|Flask}}
<lang python>
<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 perl6># Persistent URL storage
<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.
<lang ecmascript>/* url_shortener.wren */
<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)
}</lang>
}</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.
<lang go>/* go build url_shortener.go */
<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()
}</lang>
}</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: