Skip to content

Commit

Permalink
更新Configuration初始化过程
Browse files Browse the repository at this point in the history
  • Loading branch information
huanghaiyang committed Oct 1, 2019
1 parent 51bd1fc commit 0e238a2
Show file tree
Hide file tree
Showing 19 changed files with 150 additions and 51 deletions.
2 changes: 1 addition & 1 deletion lib/resources/configure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ application:
environment: prod<string>
configuration:
interceptor:
time: 10ms<timeunit>
timeout: 10ms<timeunit>
router:
defaultMapping: /<string>
request:
Expand Down
2 changes: 1 addition & 1 deletion lib/src/ApplicationEnvironmentResolver.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:Q/src/ApplicationEnvironment.dart';
import 'package:Q/src/aware/ApplicationEnvironmentResolverAware.dart';
import 'package:Q/src/configure/ApplicationConfigurationContants.dart';
import 'package:Q/src/configure/ApplicationConfigurationNames.dart';

class ApplicationEnvironmentResolver implements ApplicationEnvironmentResolverAware<Map<String, dynamic>, ApplicationEnvironment> {
ApplicationEnvironmentResolver._();
Expand Down
15 changes: 11 additions & 4 deletions lib/src/ApplicationInitializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import 'package:Q/src/resource/ApplicationConfigurationResource.dart';
abstract class ApplicationInitializer {
Application get application;

init();
void init();

createApplicationContext();
ApplicationContext createApplicationContext();

factory ApplicationInitializer(Application application) => _ApplicationInitializer(application);
}
Expand Down Expand Up @@ -72,8 +72,9 @@ class _ApplicationInitializer implements ApplicationInitializer {
List<ApplicationConfigurationResource> resources = await this.applicationConfigurationResourceResolver.resolve(environment);
await this.applicationConfigurationResourceValidator.check(resources);
List<ApplicationConfiguration> configurations = await this.applicationConfigurationLoader.load(resources);
ApplicationConfiguration officialConfiguration =
ApplicationConfiguration configuration =
await this.applicationConfigurationMixer.mix(configurations, defaultBootstrapConfiguration: defaultBootstrapConfiguration);
await this.initConfiguration(configuration);

this.initHandlers();
this.initConverters();
Expand Down Expand Up @@ -110,6 +111,12 @@ class _ApplicationInitializer implements ApplicationInitializer {

@override
createApplicationContext() {
this.application.applicationContext = ApplicationContext(this.application);
ApplicationContext applicationContext = ApplicationContext(this.application);
this.application.applicationContext = applicationContext;
return applicationContext;
}

Future<dynamic> initConfiguration(ApplicationConfiguration applicationConfiguration) async {
return this.application.applicationContext.configuration.init(applicationConfiguration);
}
}
61 changes: 31 additions & 30 deletions lib/src/Configuration.dart
Original file line number Diff line number Diff line change
@@ -1,72 +1,73 @@
import 'dart:io';

import 'package:Q/src/Method.dart';
import 'package:Q/src/ApplicationConfiguration.dart';
import 'package:Q/src/configure/HttpRequestConfigure.dart';
import 'package:Q/src/configure/HttpResponseConfigure.dart';
import 'package:Q/src/configure/InterceptorConfigure.dart';
import 'package:Q/src/configure/MultipartConfigure.dart';
import 'package:Q/src/configure/RouterMappingConfigure.dart';

// 应用程序配置
abstract class Configuration {
// 当前不支持的请求类型
List<ContentType> get unSupportedContentTypes;

// 当前支持的请求类型
List<HttpMethod> get unSupportedMethods;

// 默认返回结果的类型
ContentType get defaultProducedType;

MultipartConfigure get multipartConfigure;

RouterMappingConfigure get routerMappingConfigure;

InterceptorConfigure get interceptorConfigure;

HttpRequestConfigure get httpRequestConfigure;

HttpResponseConfigure get httpResponseConfigure;

Future<dynamic> init(ApplicationConfiguration applicationConfiguration);

factory Configuration() => _Configuration();
}

class _Configuration implements Configuration {
_Configuration();

List<ContentType> unSupportedContentTypes_ = List();

List<HttpMethod> unSupportedMethods_ = List();

ContentType defaultProducedType_ = ContentType.json;

MultipartConfigure _multipartConfigure = MultipartConfigure();

RouterMappingConfigure _routerMappingConfigure = RouterMappingConfigure();

InterceptorConfigure _interceptorConfigure = InterceptorConfigure();

HttpRequestConfigure _httpRequestConfigure = HttpRequestConfigure();

HttpResponseConfigure _httpResponseConfigure = HttpResponseConfigure();

@override
List<ContentType> get unSupportedContentTypes {
return this.unSupportedContentTypes_;
MultipartConfigure get multipartConfigure {
return this._multipartConfigure;
}

@override
ContentType get defaultProducedType {
return this.defaultProducedType_;
RouterMappingConfigure get routerMappingConfigure {
return this._routerMappingConfigure;
}

@override
List<HttpMethod> get unSupportedMethods {
return this.unSupportedMethods_;
InterceptorConfigure get interceptorConfigure {
return this._interceptorConfigure;
}

@override
MultipartConfigure get multipartConfigure {
return this._multipartConfigure;
HttpRequestConfigure get httpRequestConfigure {
return this._httpRequestConfigure;
}

@override
RouterMappingConfigure get routerMappingConfigure {
return this._routerMappingConfigure;
HttpResponseConfigure get httpResponseConfigure {
return this._httpResponseConfigure;
}

@override
InterceptorConfigure get interceptorConfigure {
return this._interceptorConfigure;
Future<dynamic> init(ApplicationConfiguration applicationConfiguration) async {
await Future.wait([
_multipartConfigure.init(),
_routerMappingConfigure.init(),
_httpResponseConfigure.init(),
_httpRequestConfigure.init(),
_interceptorConfigure.init()
]);
}
}
2 changes: 2 additions & 0 deletions lib/src/aware/ApplicationConfigurationMapperAware.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ abstract class ApplicationConfigurationMapperAware<R, S, T> {
S get values;

bool get isParsed;

dynamic get(String key);
}
3 changes: 3 additions & 0 deletions lib/src/configure/AbstractConfigure.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
abstract class AbstractConfigure {
Future<dynamic> init();
}
7 changes: 0 additions & 7 deletions lib/src/configure/ApplicationConfigurationContants.dart

This file was deleted.

5 changes: 5 additions & 0 deletions lib/src/configure/ApplicationConfigurationMapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ class ApplicationConfigurationMapper
bool get isParsed {
return _isParsed;
}

@override
dynamic get(String key) {
return this.values_[key];
}
}
12 changes: 12 additions & 0 deletions lib/src/configure/ApplicationConfigurationNames.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
final String APPLICATION_ENVIRONMENT_VARIABLE = 'application.environment';
final String APPLICATION_NAME = 'application.name';
final String APPLICATION_AUTHORS = 'application.author';
final String APPLICATION_CREATE_TIME = 'application.createTime';
final String APPLICATION_INTERCEPTOR_TIMEOUT = 'application.interceptor.timeout';
final String APPLICATION_ROUTER_DEFAULT_MAPPING = 'applixation.router.defaultMapping';
final String APPLICATION_REQUEST_UN_SUPPORTED_CONTENT_TYPES = 'application.request.unSpportedContentTypes';
final String APPLICATION_REQUEST_UN_SUPPORTED_METHODS = 'application.request.unSupportedMethods';
final String APPLICATION_MULTIPART_MAX_FILE_UPLOAD_SIZE = 'application.multipart.maxFileUploadSize';
final String APPLICATION_MULTIPART_FIX_NAME_SUFFIX_IF_ARRAY = 'application.multipart.fixNameSuffixIfArray';
final String APPLICATION_MULTIPART_DEFAULT_UPLOAD_TEMP_DIR_PATH = 'application.multipart.defaultUploadTempDirPath';
final String APPLICATION_RESPONSE_DEFAULT_PRODUCED_TYPE = 'application.response.defaultProducedType';
3 changes: 3 additions & 0 deletions lib/src/configure/Configure.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
library q_configure;

export 'package:Q/src/configure/AbstractConfigure.dart';
export 'package:Q/src/configure/ApplicationConfigurationMapper.dart';
export 'package:Q/src/configure/CustomYamlNode.dart';
export 'package:Q/src/configure/CustomYamlNodeConverter.dart';
export 'package:Q/src/configure/CustomYamlNodeValueType.dart';
export 'package:Q/src/configure/CustomYamlPaser.dart';
export 'package:Q/src/configure/HttpRequestConfigure.dart';
export 'package:Q/src/configure/HttpResponseConfigure.dart';
export 'package:Q/src/configure/InterceptorConfigure.dart';
export 'package:Q/src/configure/MultipartConfigure.dart';
export 'package:Q/src/configure/RouterMappingConfigure.dart';
33 changes: 33 additions & 0 deletions lib/src/configure/HttpRequestConfigure.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'dart:io';

import 'package:Q/src/Method.dart';
import 'package:Q/src/configure/AbstractConfigure.dart';

abstract class HttpRequestConfigure extends AbstractConfigure {
factory HttpRequestConfigure() => _HttpRequestConfigure();

// 当前不支持的请求类型
List<ContentType> get unSupportedContentTypes;

// 当前支持的请求类型
List<HttpMethod> get unSupportedMethods;
}

class _HttpRequestConfigure implements HttpRequestConfigure {
List<ContentType> unSupportedContentTypes_ = List();

List<HttpMethod> unSupportedMethods_ = List();

@override
List<ContentType> get unSupportedContentTypes {
return this.unSupportedContentTypes_;
}

@override
List<HttpMethod> get unSupportedMethods {
return this.unSupportedMethods_;
}

@override
Future<dynamic> init() async {}
}
24 changes: 24 additions & 0 deletions lib/src/configure/HttpResponseConfigure.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'dart:io';

import 'package:Q/src/configure/AbstractConfigure.dart';

abstract class HttpResponseConfigure extends AbstractConfigure {
factory HttpResponseConfigure() => _HttpResponseConfigure();

// 默认返回结果的类型
ContentType get defaultProducedType;
}

class _HttpResponseConfigure implements HttpResponseConfigure {
_HttpResponseConfigure();

ContentType defaultProducedType_ = ContentType.json;

@override
ContentType get defaultProducedType {
return this.defaultProducedType_;
}

@override
Future<dynamic> init() async {}
}
6 changes: 5 additions & 1 deletion lib/src/configure/InterceptorConfigure.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:Q/src/configure/AbstractConfigure.dart';
import 'package:Q/src/interceptor/InterceptorTimeout.dart';

abstract class InterceptorConfigure {
abstract class InterceptorConfigure extends AbstractConfigure {
InterceptorTimeout get timeout;

factory InterceptorConfigure() => _InterceptorConfigure();
Expand All @@ -13,4 +14,7 @@ class _InterceptorConfigure implements InterceptorConfigure {
InterceptorTimeout get timeout {
return this._timeout;
}

@override
Future<dynamic> init() async {}
}
7 changes: 6 additions & 1 deletion lib/src/configure/MultipartConfigure.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:io';

abstract class MultipartConfigure {
import 'package:Q/src/configure/AbstractConfigure.dart';

abstract class MultipartConfigure extends AbstractConfigure {
factory MultipartConfigure() => _MultipartConfigure();

bool get fixNameSuffixIfArray;
Expand Down Expand Up @@ -65,4 +67,7 @@ class _MultipartConfigure implements MultipartConfigure {
return size > this.maxUploadSize;
}
}

@override
Future<dynamic> init() async {}
}
7 changes: 6 additions & 1 deletion lib/src/configure/RouterMappingConfigure.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:Q/src/configure/AbstractConfigure.dart';

final String BASE_PATH = '/';
final Pattern BASE_PATH_PATTERN = RegExp('^$BASE_PATH');

abstract class RouterMappingConfigure {
abstract class RouterMappingConfigure extends AbstractConfigure {
factory RouterMappingConfigure() => _RouterMappingConfigure();

String get defaultMapping;
Expand All @@ -14,4 +16,7 @@ class _RouterMappingConfigure implements RouterMappingConfigure {
String get defaultMapping {
return BASE_PATH;
}

@override
Future<dynamic> init() async {}
}
5 changes: 3 additions & 2 deletions lib/src/delegate/ApplicationRouteDelegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class _ApplicationRouteDelegate implements ApplicationRouteDelegate {
void route(Router router) {
this.application_.routers.add(router);
// 查找路由的响应结果转换器
router.converter = this.application_.converters[
router?.produceType != null ? router.produceType : Application.getApplicationContext().configuration.defaultProducedType];
router.converter = this.application_.converters[router?.produceType != null
? router.produceType
: Application.getApplicationContext().configuration.httpResponseConfigure.defaultProducedType];
router.handlerAdapter = this.application_.handlers[HttpStatus.ok];
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/handler/OKHandler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class OKHandler implements HandlerAdapter {
httpResponse.statusCode = context.response.status;
httpResponse.headers.contentType = context?.router?.produceType != null
? context.router.produceType
: Application.getApplicationContext().configuration.defaultProducedType;
: Application.getApplicationContext().configuration.httpResponseConfigure.defaultProducedType;
httpResponse.write(context.response.responseEntry.convertedResult);
return context;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/src/helpers/UnSupportedContentTypeHelper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import 'package:Q/src/Application.dart';

class UnSupportedContentTypeHelper {
static bool checkSupported(HttpRequest req) {
List<ContentType> unSupportedContentTypes = Application.getApplicationContext().configuration.unSupportedContentTypes;
List<ContentType> unSupportedContentTypes =
Application.getApplicationContext().configuration.httpRequestConfigure.unSupportedContentTypes;
if (unSupportedContentTypes.isEmpty) return true;
return unSupportedContentTypes.indexWhere((contentType) => contentType.mimeType == req.headers.contentType.mimeType) == -1;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/helpers/UnSupportedMethodHelper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:Q/src/helpers/HttpMethodHelper.dart';

class UnSupportedMethodHelper {
static bool checkSupported(HttpRequest req) {
List<HttpMethod> unSupportedMethods = Application.getApplicationContext().configuration.unSupportedMethods;
List<HttpMethod> unSupportedMethods = Application.getApplicationContext().configuration.httpRequestConfigure.unSupportedMethods;
if (unSupportedMethods.isEmpty) return true;
return unSupportedMethods.indexWhere((method) => HttpMethodHelper.getMethodName(method) == req.method.toUpperCase()) == -1;
}
Expand Down

0 comments on commit 0e238a2

Please # to comment.