Working on crypto

This commit is contained in:
thosakwe 2016-06-23 17:54:10 -04:00
parent 24da44f6a2
commit e67d7adb77
12 changed files with 153 additions and 22 deletions

1
bin/packages Symbolic link
View file

@ -0,0 +1 @@
../packages

View file

@ -4,17 +4,21 @@ import 'package:angel/angel.dart';
import 'package:angel_framework/angel_framework.dart';
main() async {
Angel app = await createServer();
runZoned(() async {
await app.startServer(
new InternetAddress(app.properties['host']), app.properties['port']);
print("Angel server listening on ${app.httpServer.address.host}:${app
.httpServer.port}");
}, onError: (error, [StackTrace stackTrace]) {
stderr.writeln("Unhandled error occurred: $error");
if (stackTrace != null) {
stderr.writeln(stackTrace);
}
});
runZoned(startServer, onError: onError);
}
startServer() async {
Angel app = await createServer();
InternetAddress host = new InternetAddress(app.properties['host']);
int port = app.properties['port'];
await app.startServer(host, port);
print("Angel server listening on ${host.address}:${port}");
}
onError(error, [StackTrace stackTrace]) {
stderr.writeln("Unhandled error occurred: $error");
if (stackTrace != null) {
stderr.writeln(stackTrace);
}
}

View file

@ -1,2 +1,3 @@
# Default server configuration.
host: 127.0.0.1
mongo_db: mongodb://localhost:27017/angel

View file

@ -3,13 +3,17 @@ library angel;
import 'dart:async';
import 'package:angel_framework/angel_framework.dart';
import 'src/config/config.dart' show configureServer;
import 'src/config/config.dart' as configuration;
import 'src/routes/routes.dart' as routes;
import 'src/services/services.dart' as services;
/// Creates and configures the server instance.
Future<Angel> createServer() async {
Angel app = new Angel();
await app.configure(configureServer);
await app.configure(configuration.configureServer);
await app.configure(services.configureServer);
await app.configure(routes.configureServer);
return app;
}

View file

@ -2,13 +2,31 @@
library angel.config;
import 'dart:io';
import 'package:angel_auth/angel_auth.dart';
import 'package:angel_configuration/angel_configuration.dart';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_mustache/angel_mustache.dart';
import 'routes.dart';
/// This is a perfect place to include configuration and load plug-ins.
configureServer(Angel app) async {
AngelAuthOptions localOpts = new AngelAuthOptions(
failureRedirect: '/failure', successRedirect: '/success');
Map sampleUser = {'hello': 'world'};
verifier(username, password) async {
if (username == 'username' && password == 'password') {
return sampleUser;
} else
return false;
}
wireAuth(Angel app) async {
Auth.serializer = (user) async => 1337;
Auth.deserializer = (id) async => sampleUser;
Auth.strategies.add(new LocalAuthStrategy(verifier));
await app.configure(AngelAuth);
}
await app.configure(loadConfigurationFile());
await app.configure(mustache(new Directory('views')));
await app.configure(configureRoutes);
}

View file

@ -4,18 +4,28 @@ library angel.routes;
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_static/angel_static.dart';
configureBefore(Angel app) async {}
/// Put your app routes here!
configureRoutes(Angel app) async {
app.get('/', (req, ResponseContext res) => res.render('hello'));
app.all('*', serveStatic());
}
configureAfter(Angel app) async {
// 404 handler
app.after.add((RequestContext req, ResponseContext res) async {
res.willCloseItself = true;
res.status(404);
res.header('Content-Type', 'text/html');
res.underlyingResponse.write(
await app.viewGenerator('404', {'path': req.path}));
res.underlyingResponse
.write(await app.viewGenerator('404', {'path': req.path}));
await res.underlyingResponse.close();
});
}
configureServer(Angel app) async {
await configureBefore(app);
await configureRoutes(app);
await configureAfter(app);
}

View file

@ -0,0 +1,14 @@
/// Declare services here!
library angel.services;
import 'package:angel_framework/angel_framework.dart';
import 'package:mongo_dart/mongo_dart.dart';
import 'users/users.dart' as Users;
configureServer(Angel app) async {
Db db = new Db(app.properties["mongo_db"]);
await db.open();
await app.configure(Users.configureServer(db));
}

View file

@ -0,0 +1 @@
const Map UserSchema = const {};

View file

@ -0,0 +1,40 @@
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_mongo/angel_mongo.dart';
import 'package:crypto/crypto.dart' show sha256;
import 'package:json_god/json_god.dart' as god;
import 'package:mongo_dart/mongo_dart.dart';
import 'schema.dart';
@god.WithSchema(UserSchema)
class User extends Model {
String email;
String username;
String password;
List<String> roles;
User(
{String this.email,
String this.username,
String this.password,
List<String> roles}) {
this.roles = roles ?? [];
}
}
hashPassword(HookedServiceEvent event) {
if (event.data.password != null) {
event.data.password =
sha256.convert(event.data.password.codeUnits).toString();
}
}
configureServer(Db db) {
return (Angel app) async {
app.use("/api/users", new MongoTypedService<User>(db.collection("users")));
HookedService service = app.service("api/users");
// Place your hooks here!
service.beforeCreated.listen(hashPassword);
};
}

View file

@ -4,8 +4,10 @@ version: 0.0.0-dev.0
author: thosakwe <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel
dependencies:
angel_auth: ">=1.0.0-dev <2.0.0"
angel_configuration: ">=1.0.0-dev <2.0.0"
angel_framework: ">=1.0.0-dev <2.0.0"
angel_mongo: ">=1.0.0-dev <2.0.0"
angel_mustache: ">=1.0.0-dev <2.0.0"
angel_static: ">=1.0.0 <1.1.0"
json_god: ">=2.0.0-beta <3.0.0"

35
test/services/users.dart Normal file
View file

@ -0,0 +1,35 @@
import 'dart:io';
import 'package:angel/angel.dart';
import 'package:angel_framework/angel_framework.dart';
import 'package:http/http.dart';
import 'package:json_god/json_god.dart' as god;
import 'package:test/test.dart';
main() async {
group('services.users', () {
Angel app;
Client client = new Client();
Map headers = {
HttpHeaders.ACCEPT: ContentType.JSON.mimeType,
HttpHeaders.CONTENT_TYPE: ContentType.JSON.mimeType
};
HttpServer server;
String url;
setUp(() async {
Angel app = await createServer();
server = await app.startServer(InternetAddress.LOOPBACK_IP_V4, 3000);
url = "http://localhost:3000";
});
tearDown(() async {
await server.close(force: true);
client.close();
});
test('index users', () async {
Response response = await client.get("$url/api/users");
expect(response.statusCode, equals(HttpStatus.OK));
});
});
}

1
web/packages Symbolic link
View file

@ -0,0 +1 @@
../packages