Skip to content

Instantly share code, notes, and snippets.

View ThomasLocke's full-sized avatar

Thomas Løcke ThomasLocke

View GitHub Profile
@ThomasLocke
ThomasLocke / gist:491c357a21082078b2ab
Created October 12, 2014 15:07
shelf.middleware.routes.dart
library shelfmiddleware.routes;
import 'handlers.dart' as handler;
import 'package:shelf_route/shelf_route.dart';
final Router routes = new Router()
..get('/app{?token}', handler.app)
..get('/foo{?token}', handler.foo);
@ThomasLocke
ThomasLocke / gist:8157769
Created December 28, 2013 09:40
Trying to harden client.getUrl() in Dart, so it never crashes my HTTP server and never completes with an error.
Future getTag() {
final Completer c = new Completer();
final HttpClient client = new HttpClient();
client.getUrl(Uri.parse(tagUrl))
.then((HttpClientRequest request) {
return request.close();
})
.catchError((e) {
// If the tagUrl server is down, we catch and log it here.
@ThomasLocke
ThomasLocke / gist:7582314
Created November 21, 2013 14:21
How to access Google Drive from Dart.
// Add google_drive_v2_api to pubspec.yaml
import "package:google_oauth2_client/google_oauth2_console.dart" as oauth;
import "package:google_drive_v2_api/drive_v2_api_console.dart" as drivelib;
const _IDENTIFIER = "<YOUR CLIENT ID>";
const _SECRET = "<YOUR CLIENT SECRET>";
const _SCOPES = const ["https://www.googleapis.com/auth/drive.file"];
void main() {
@ThomasLocke
ThomasLocke / gist:6650607
Created September 21, 2013 13:28
Dead simple HTTP server in Dart - snippet 1
import 'dart:async';
import 'dart:io';
import 'package:route/server.dart';
import 'package:route/url_pattern.dart';
final Map<String, UrlPattern> Urls = {'/' : new UrlPattern('/'),
'ninja' : new UrlPattern('/ninja'),
'pirate' : new UrlPattern('/pirate'),
'samurai' : new UrlPattern('/samurai')};
@ThomasLocke
ThomasLocke / gist:6568446
Created September 15, 2013 06:18
Using the Ada Web Server (AWS), part 2 -snippet 11
function Hello_World_Template
(Request : in AWS.Status.Data)
return AWS.Response.Data
is (AWS.Response.File (AWS.MIME.Text_Plain,
"templates/hello_world.tmpl"));
@ThomasLocke
ThomasLocke / gist:6568434
Created September 15, 2013 06:15
Using the Ada Web Server (AWS), part 2 - snippet 9
@@--
@@-- First we define a macro.
@@--
@@MACRO(F)@@
<span style="font-style: italic;">F</span><span style="font-size: 60%;">@_$1_@</span>
@@END_MACRO@@
@@--
@@-- And then comes the actual HTML5 document
@@--
<!DOCTYPE html>
@ThomasLocke
ThomasLocke / gist:6568402
Created September 15, 2013 06:09
Using the Ada Web Server (AWS), part 2 - snippet 6
with AWS.Messages;
with AWS.Templates;
package body Hello_World is
-- Stuff...
------------------------
-- Generate_Content --
------------------------
@ThomasLocke
ThomasLocke / gist:6568439
Created September 15, 2013 06:17
Using the Ada Web Server (AWS), part 2 - snippet 10
Dispatcher.Register (URI => "/helloworld.tmpl",
Action => Hello_World.Hello_World_Template'Access);
@ThomasLocke
ThomasLocke / gist:6568413
Created September 15, 2013 06:10
Using the Ada Web Server (AWS), part 2 - snippet 8
procedure Append (T : in out Tag; Value : String);
procedure Append (T : in out Tag; Value : Character);
procedure Append (T : in out Tag; Value : Boolean);
procedure Append (T : in out Tag; Value : Unbounded_String);
procedure Append (T : in out Tag; Value : Integer);
procedure Append (T : in out Tag; Value : Tag);
@ThomasLocke
ThomasLocke / gist:6568410
Created September 15, 2013 06:10
Using the Ada Web Server (AWS), part 2 - snippet 7
for i in Fibs'Range loop
case i is
when 0 => Fibs (i) := 0;
when 1 => Fibs (i) := 1;
when others => Fibs (i) := Fibs (i - 1) + Fibs (i - 2);
end case;
Append (Position, i);
Append (Fibonacci, Fibs (i));
end loop;