Custom plug-ins :)

This commit is contained in:
thosakwe 2016-12-10 13:51:02 -05:00
parent 3655c7020e
commit 9a7a7d29e1
15 changed files with 81 additions and 61 deletions

View file

@ -0,0 +1,9 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Start Server (PRODUCTION)" type="DartCommandLineRunConfigurationType" factoryName="Dart Command Line Application" singleton="true">
<option name="envs">
<entry key="ANGEL_ENV" value="production" />
</option>
<option name="filePath" value="$PROJECT_DIR$/bin/server.dart" />
<method />
</configuration>
</component>

View file

@ -0,0 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Users Tests" type="DartTestRunConfigurationType" factoryName="Dart Test" singleton="true">
<option name="filePath" value="$PROJECT_DIR$/test/services/users_test.dart" />
<method />
</configuration>
</component>

0
analysis_options.yaml Normal file
View file

View file

@ -4,7 +4,6 @@ import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:angel/angel.dart'; import 'package:angel/angel.dart';
import 'package:angel_diagnostics/angel_diagnostics.dart'; import 'package:angel_diagnostics/angel_diagnostics.dart';
import 'package:angel_framework/angel_framework.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
main() async { main() async {

View file

@ -6,6 +6,7 @@ import 'package:angel_framework/angel_framework.dart';
import 'src/config/config.dart' as configuration; import 'src/config/config.dart' as configuration;
import 'src/routes/routes.dart' as routes; import 'src/routes/routes.dart' as routes;
import 'src/services/services.dart' as services; import 'src/services/services.dart' as services;
export 'src/services/services.dart';
/// Creates and configures the server instance. /// Creates and configures the server instance.
Future<Angel> createServer() async { Future<Angel> createServer() async {

View file

@ -5,9 +5,11 @@ import 'dart:io';
import 'package:angel_configuration/angel_configuration.dart'; import 'package:angel_configuration/angel_configuration.dart';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:angel_mustache/angel_mustache.dart'; import 'package:angel_mustache/angel_mustache.dart';
import 'plugins/plugins.dart' as plugins;
/// This is a perfect place to include configuration and load plug-ins. /// This is a perfect place to include configuration and load plug-ins.
configureServer(Angel app) async { configureServer(Angel app) async {
await app.configure(loadConfigurationFile()); await app.configure(loadConfigurationFile());
await app.configure(mustache(new Directory('views'))); await app.configure(mustache(new Directory('views')));
await plugins.configureServer(app);
} }

View file

@ -0,0 +1,9 @@
/// Custom plugins go here.
library angel.config.plugins;
import 'dart:async';
import 'package:angel_framework/angel_framework.dart';
Future configureServer(Angel app) async {
// Include any plugins you have made here.
}

View file

@ -26,18 +26,18 @@ class User extends Model {
factory User.fromMap(Map data) => new User( factory User.fromMap(Map data) => new User(
id: data['id'], id: data['id'],
email: data["email"], email: data['email'],
username: data["username"], username: data['username'],
password: data["password"], password: data['password'],
roles: data["roles"]); roles: data['roles']);
Map toJson() { Map toJson() {
return { return {
"id": id, 'id': id,
"email": email, 'email': email,
"username": username, 'username': username,
"password": password, 'password': password,
"roles": roles 'roles': roles
}; };
} }
} }

View file

@ -4,17 +4,17 @@ import 'package:angel_auth/angel_auth.dart';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import '../../services/user.dart'; import '../../services/user.dart';
@Expose("/api/auth") @Expose('/api/auth')
class AuthController extends Controller { class AuthController extends Controller {
final AngelAuth auth = new AngelAuth(); final AngelAuth auth = new AngelAuth();
deserializer(String id) async => app.service("api/users").read(id); deserializer(String id) async => app.service('api/users').read(id);
serializer(User user) async => user.id; serializer(User user) async => user.id;
/// Attempt to log a user in /// Attempt to log a user in
verifier(UserService Users) { verifier(UserService Users) {
return (String username, String password) async { return (String username, String password) async {
List<User> users = await Users.index({"username": username}); List<User> users = await Users.index({'username': username});
if (users.isNotEmpty) { if (users.isNotEmpty) {
var hash = hashPassword(password); var hash = hashPassword(password);
@ -36,14 +36,14 @@ class AuthController extends Controller {
await app.configure(auth); await app.configure(auth);
} }
bool loggedIn(RequestContext req) => req.session["userId"] != null; bool loggedIn(RequestContext req) => req.session['userId'] != null;
@Expose("/login", method: "POST") @Expose('/login', method: 'POST')
login(RequestContext req) async { login(RequestContext req) async {
// Include log-in logic here... // Include log-in logic here...
} }
@Expose("/register", method: "POST") @Expose('/register', method: 'POST')
register(RequestContext req, UserService Users) async { register(RequestContext req, UserService Users) async {
// And your registration logic... // And your registration logic...
} }

View file

@ -26,7 +26,7 @@ configureAfter(Angel app) async {
// Default error handler // Default error handler
app.onError( app.onError(
(e, req, res) async => res.render("error", {"message": e.message})); (e, req, res) async => res.render('error', {'message': e.message}));
} }
configureServer(Angel app) async { configureServer(Angel app) async {

View file

@ -7,7 +7,7 @@ import 'package:mongo_dart/mongo_dart.dart';
import 'user.dart' as User; import 'user.dart' as User;
configureServer(Angel app) async { configureServer(Angel app) async {
Db db = new Db(app.properties["mongo_db"]); Db db = new Db(app.properties['mongo_db']);
await db.open(); await db.open();
await app.configure(User.configureServer(db)); await app.configure(User.configureServer(db));

View file

@ -8,9 +8,9 @@ export '../models/user.dart';
configureServer(Db db) { configureServer(Db db) {
return (Angel app) async { return (Angel app) async {
app.use("/api/users", new UserService(db.collection("users"))); app.use('/api/users', new UserService(db.collection('users')));
HookedService service = app.service("api/users"); HookedService service = app.service('api/users');
app.container.singleton(service.inner); app.container.singleton(service.inner);
}; };
} }
@ -32,7 +32,7 @@ class UserService extends Service {
@override @override
index([Map params]) { index([Map params]) {
if (params != null && params.containsKey("provider")) { if (params != null && params.containsKey('provider')) {
// Nobody needs to see the entire user list except for the server. // Nobody needs to see the entire user list except for the server.
throw new AngelHttpException.Forbidden(); throw new AngelHttpException.Forbidden();
} }
@ -42,19 +42,19 @@ class UserService extends Service {
@override @override
create(data, [Map params]) { create(data, [Map params]) {
if (params != null && params.containsKey("provider")) { if (params != null && params.containsKey('provider')) {
// Deny creating users to the public - this should be done by the server only. // Deny creating users to the public - this should be done by the server only.
throw new AngelHttpException.Forbidden(); throw new AngelHttpException.Forbidden();
} }
try { try {
Validate.isKeyInMap("username", data); Validate.isKeyInMap('username', data);
Validate.isKeyInMap("password", data); Validate.isKeyInMap('password', data);
Validate.isEmail(data["email"]); Validate.isEmail(data['email']);
data["password"] = hashPassword(data["password"]); data['password'] = hashPassword(data['password']);
} catch (e) { } catch (e) {
throw new AngelHttpException.BadRequest( throw new AngelHttpException.BadRequest(
message: "User must have a username, e-mail address and password."); message: 'User must have a username, e-mail address and password.');
} }
return _inner.create(data, params); return _inner.create(data, params);

View file

@ -14,6 +14,7 @@ dependencies:
angel_mustache: ^1.0.0-dev angel_mustache: ^1.0.0-dev
angel_proxy: ^1.0.0-dev angel_proxy: ^1.0.0-dev
angel_static: ^1.1.0-dev angel_static: ^1.1.0-dev
angel_test: ^1.0.0-dev
mailer: ^1.1.0+4 mailer: ^1.1.0+4
validate: ^1.5.2 validate: ^1.5.2
dev_dependencies: dev_dependencies:

View file

@ -1,34 +0,0 @@
import 'dart:io';
import 'package:angel/angel.dart';
import 'package:angel_framework/angel_framework.dart';
import 'package:http/http.dart';
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));
});
});
}

View file

@ -0,0 +1,27 @@
import 'dart:io';
import 'package:angel/angel.dart';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_test/angel_test.dart';
import 'package:test/test.dart';
main() async {
Angel app;
TestClient client;
setUp(() async {
app = await createServer();
client = await connectTo(app, saveSession: false);
});
tearDown(() async {
await client.close();
app = null;
});
test('index users', () async {
final response = await client.get('/api/users');
// By default, we locked this away from the Internet...
expect(response, hasStatus(HttpStatus.FORBIDDEN));
});
}