Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Make AppInstance available as mixing #134

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/pharaoh/lib/src/_next/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ part '_core/reflector.dart';

typedef RoutesResolver = List<RouteDefinition> Function();

/// This should really be a mixin but due to a bug in reflectable.dart#324
/// TODO:(codekeyz) make this a mixin when reflectable.dart#324 is fixed
abstract class AppInstance {
Application get app => Application.instance;
mixin class AppInstance {
Application get app => Application._instance;

AppConfig get config => app.config;
}

/// Use this to override the application exceptiosn handler
Expand All @@ -39,7 +39,7 @@ typedef ApplicationExceptionsHandler = FutureOr<Response> Function(
abstract interface class Application {
Application(AppConfig config);

static late final Application instance;
static late final Application _instance;

String get name;

Expand Down Expand Up @@ -101,7 +101,7 @@ abstract class ApplicationFactory {
}

Future<void> startServer() async {
final app = Application.instance as _PharaohNextImpl;
final app = Application._instance as _PharaohNextImpl;

await app
._createPharaohInstance(onException: onApplicationException)
Expand All @@ -110,7 +110,7 @@ abstract class ApplicationFactory {

Future<void> _bootstrapComponents(AppConfig config) async {
final spanner = Spanner()..addMiddleware('/', bodyParser);
Application.instance = _PharaohNextImpl(config, spanner);
Application._instance = _PharaohNextImpl(config, spanner);

final providerInstances = providers.map(createNewInstance<ServiceProvider>);

Expand Down Expand Up @@ -185,7 +185,7 @@ abstract class ApplicationFactory {

@visibleForTesting
Future<spookie.Spookie> get tester {
final app = (Application.instance as _PharaohNextImpl);
final app = (Application._instance as _PharaohNextImpl);
return spookie.request(
app._createPharaohInstance(onException: onApplicationException));
}
Expand Down
6 changes: 3 additions & 3 deletions packages/pharaoh/lib/src/_next/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import '../router/router_handler.dart';
import 'core.dart';

@inject
abstract class ClassMiddleware extends AppInstance {
abstract class ClassMiddleware with AppInstance {
handle(Request req, Response res, NextFunction next) {
next();
}
Expand All @@ -18,7 +18,7 @@ abstract class ClassMiddleware extends AppInstance {
}

@inject
abstract class ServiceProvider extends AppInstance {
abstract class ServiceProvider with AppInstance {
static List<Type> get defaultProviders => [];

void boot() {}
Expand All @@ -27,7 +27,7 @@ abstract class ServiceProvider extends AppInstance {
}

@inject
abstract class HTTPController extends AppInstance {
abstract class HTTPController with AppInstance {
late final Request request;

late final Response response;
Expand Down
5 changes: 3 additions & 2 deletions packages/pharaoh/lib/src/core_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ class $PharaohImpl extends RouterContract
..autoCompress = true;
_server.listen(handleRequest);

print(
'Server start on PORT: ${_server.port} -> ${uri.scheme}://localhost:${_server.port}');
stdout.writeln(
'Server started on PORT: ${_server.port} -> ${uri.scheme}://localhost:${_server.port}');
return this;
}

Expand Down Expand Up @@ -162,6 +162,7 @@ class $PharaohImpl extends RouterContract
if (!responseHeaders.containsKey(_XPoweredByHeader)) {
request.response.headers.add(_XPoweredByHeader, 'Pharaoh');
}

if (!responseHeaders.containsKey(HttpHeaders.dateHeader)) {
request.response.headers.add(
HttpHeaders.dateHeader,
Expand Down
7 changes: 2 additions & 5 deletions packages/pharaoh/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@ dependencies:
shelf: ^1.4.1

# framework
reflectable: 4.0.6
get_it: ^7.6.7
reflectable: ^4.0.12
get_it: ^8.0.2
grammer: ^1.0.3
dotenv: ^4.2.0
ez_validator_dart: ^0.3.1
spookie: ^1.0.2+3

dev_dependencies:
lints: ^3.0.0

dependency_overrides:
analyzer: 6.4.0
Loading