URL shortener: Difference between revisions

m
syntax highlighting fixup automation
(→‎{{header|Wren}}: Added libheader.)
m (syntax highlighting fixup automation)
Line 43:
{{libheader| kemal}}
 
<langsyntaxhighlight lang="ruby">require "kemal"
 
CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".chars
Line 66:
end
 
Kemal.run</langsyntaxhighlight>
 
=={{header|Delphi}}==
Line 78:
{{libheader| Inifiles}}
Highly inspired in [[#Go]]
<syntaxhighlight lang="delphi">
<lang Delphi>
program URLShortenerServer;
 
Line 244:
 
Manager.Free;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 251:
</pre>
=={{header|Go}}==
<langsyntaxhighlight lang="go">// shortener.go
package main
 
Line 319:
db := make(database)
log.Fatal(http.ListenAndServe(host, db))
}</langsyntaxhighlight>
 
{{out}}
Line 355:
=={{header|JavaScript}}==
{{works with|Node.js}}
<langsyntaxhighlight JavaScriptlang="javascript">#!/usr/bin/env node
 
var mapping = new Map();
Line 402:
res.writeHead(404);
res.end();
}).listen(8080);</langsyntaxhighlight>
{{out}}
<pre>$ curl -X POST http://localhost:8080/ -H "Content-Type: application/json" --data "{\"long\":\"https://www.example.com\"}"
Line 411:
=={{header|Julia}}==
Assumes an SQLite database containing a table called LONGNAMESHORTNAME (consisting of two string columns) already exists.
<langsyntaxhighlight lang="julia">using Base64, HTTP, JSON2, Sockets, SQLite, SHA
 
function processpost(req::HTTP.Request, urilen=8)
Line 456:
const localport = 3000
run_web_server(serveraddress, localport)
</syntaxhighlight>
</lang>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">use Collection.Generic;
use Data.JSON;
use Web.HTTP;
Line 520:
return Response->New(200, response);
}
}</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\URL_shortener.exw
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: #7060A8;">WSACleanup</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{out}}
Sample session output:
Line 674:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/http.l")
(allowed NIL "!short" u)
(pool "urls.db" (6))
Line 685:
(commit 'upd)
(respond (pack "http://127.0.0.1:8080/?" K "\n")) ) ) )
(server 8080 "!short")</langsyntaxhighlight>
{{out}}
<pre>
Line 719:
===Flask===
{{libheader|Flask}}
<langsyntaxhighlight lang="python">
"""A URL shortener using Flask. Requires Python >=3.5."""
 
Line 904:
app.env = "development"
app.run(debug=True)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
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.
 
<syntaxhighlight lang="raku" perl6line># Persistent URL storage
use JSON::Fast;
 
Line 975:
 
react whenever signal(SIGINT) { $shorten.stop; exit; }
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
Line 982:
{{libheader|Wren-json}}
An embedded program with a Go host so we can use its net/http module.
<langsyntaxhighlight lang="ecmascript">/* url_shortener.wren */
 
import "./json" for JSON
Line 1,041:
 
foreign static redirect(url, code)
}</langsyntaxhighlight>
We now embed this script in the following Go program and build it.
<langsyntaxhighlight lang="go">/* go build url_shortener.go */
 
package main
Line 1,154:
log.Fatal(http.ListenAndServe(host, nil))
vm.Free()
}</langsyntaxhighlight>
{{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:
10,327

edits