From 71ef377ea03a9d1afb1d1bb543538d3e8a725de9 Mon Sep 17 00:00:00 2001 From: necessarylion Date: Sat, 16 Dec 2023 20:34:15 +0630 Subject: [PATCH] update dox app --- packages/dox-app/.env | 6 +- packages/dox-app/bin/server.dart | 11 ++- .../http/controllers/api.controller.dart | 0 .../http/controllers/auth.controller.dart | 12 +-- .../http/controllers/blog.controller.dart | 4 +- .../http/controllers/web.controller.dart | 0 packages/dox-app/lib/app/http/handler.dart | 8 ++ .../http/middleware/custom.middleware.dart | 4 +- .../http/middleware/function.middleware.dart | 0 .../{ => app}/http/requests/blog.request.dart | 0 .../app/http/serializers/user.serializer.dart | 12 +++ .../{ => app}/http/services/blog.service.dart | 6 +- .../lib/{ => app}/models/blog/blog.model.dart | 4 +- .../{ => app}/models/blog/blog.model.g.dart | 2 +- .../models/category/category.model.dart | 2 +- .../models/category/category.model.g.dart | 2 +- .../lib/{ => app}/models/user/user.model.dart | 0 .../{ => app}/models/user/user.model.g.dart | 2 +- .../lib/app/ws/controllers/ws_controller.dart | 7 ++ packages/dox-app/lib/config/app.dart | 75 +++++++++++++------ packages/dox-app/lib/config/cors.dart | 60 ++++++++++++++- packages/dox-app/lib/config/logger.dart | 24 ++++++ packages/dox-app/lib/config/postgres.dart | 34 +++++++++ packages/dox-app/lib/config/redis.dart | 16 ++++ packages/dox-app/lib/config/services.dart | 9 ++- packages/dox-app/lib/http/handler.dart | 14 ---- packages/dox-app/lib/routes/api.dart | 8 +- packages/dox-app/lib/routes/web.dart | 12 +-- packages/dox-app/lib/routes/websocket.dart | 15 +++- .../dox-app/lib/services/auth_service.dart | 2 +- .../lib/services/database_service.dart | 51 ------------- .../dox-app/lib/services/orm_service.dart | 22 ++++++ .../lib/services/websocket_service.dart | 3 +- packages/dox-app/pubspec.lock | 6 +- 34 files changed, 298 insertions(+), 135 deletions(-) rename packages/dox-app/lib/{ => app}/http/controllers/api.controller.dart (100%) rename packages/dox-app/lib/{ => app}/http/controllers/auth.controller.dart (71%) rename packages/dox-app/lib/{ => app}/http/controllers/blog.controller.dart (89%) rename packages/dox-app/lib/{ => app}/http/controllers/web.controller.dart (100%) create mode 100644 packages/dox-app/lib/app/http/handler.dart rename packages/dox-app/lib/{ => app}/http/middleware/custom.middleware.dart (54%) rename packages/dox-app/lib/{ => app}/http/middleware/function.middleware.dart (100%) rename packages/dox-app/lib/{ => app}/http/requests/blog.request.dart (100%) create mode 100644 packages/dox-app/lib/app/http/serializers/user.serializer.dart rename packages/dox-app/lib/{ => app}/http/services/blog.service.dart (86%) rename packages/dox-app/lib/{ => app}/models/blog/blog.model.dart (88%) rename packages/dox-app/lib/{ => app}/models/blog/blog.model.g.dart (99%) rename packages/dox-app/lib/{ => app}/models/category/category.model.dart (90%) rename packages/dox-app/lib/{ => app}/models/category/category.model.g.dart (98%) rename packages/dox-app/lib/{ => app}/models/user/user.model.dart (100%) rename packages/dox-app/lib/{ => app}/models/user/user.model.g.dart (98%) create mode 100644 packages/dox-app/lib/app/ws/controllers/ws_controller.dart create mode 100644 packages/dox-app/lib/config/logger.dart create mode 100644 packages/dox-app/lib/config/postgres.dart create mode 100644 packages/dox-app/lib/config/redis.dart delete mode 100644 packages/dox-app/lib/http/handler.dart delete mode 100644 packages/dox-app/lib/services/database_service.dart create mode 100644 packages/dox-app/lib/services/orm_service.dart diff --git a/packages/dox-app/.env b/packages/dox-app/.env index 3527a26..a597a12 100644 --- a/packages/dox-app/.env +++ b/packages/dox-app/.env @@ -1,8 +1,10 @@ +APP_NAME=dox +APP_ENV=development APP_PORT=3003 -APP_KEY=KKep5qC6uHlAgrwaQTQewgMRSIqQnoj0 +APP_KEY=ZrNbOZJAjC2MDzqehhsGV2DzFFI4pz5R DB_HOST=localhost -DB_PORT=5432 +DB_PORT=3002 DB_NAME=postgres DB_USERNAME=postgres DB_PASSWORD=postgres diff --git a/packages/dox-app/bin/server.dart b/packages/dox-app/bin/server.dart index 2944b2a..31c25fa 100644 --- a/packages/dox-app/bin/server.dart +++ b/packages/dox-app/bin/server.dart @@ -1,14 +1,17 @@ import 'package:dox_app/config/app.dart'; -import 'package:dox_app/services/database_service.dart'; +import 'package:dox_app/config/postgres.dart'; import 'package:dox_core/dox_core.dart'; +import 'package:dox_migration/dox_migration.dart'; void main() async { /// Initialize Dox Dox().initialize(appConfig); - /// run database migration before starting server - await DatabaseService().migrate(); + /// Run database migration before starting server. + /// Since Migration need to process only once, + /// it do not required to register in services. + await Migration().migrate(postgresEndpoint); - /// start dox http server + /// Start dox http server await Dox().startServer(); } diff --git a/packages/dox-app/lib/http/controllers/api.controller.dart b/packages/dox-app/lib/app/http/controllers/api.controller.dart similarity index 100% rename from packages/dox-app/lib/http/controllers/api.controller.dart rename to packages/dox-app/lib/app/http/controllers/api.controller.dart diff --git a/packages/dox-app/lib/http/controllers/auth.controller.dart b/packages/dox-app/lib/app/http/controllers/auth.controller.dart similarity index 71% rename from packages/dox-app/lib/http/controllers/auth.controller.dart rename to packages/dox-app/lib/app/http/controllers/auth.controller.dart index 16dc302..27ecebb 100644 --- a/packages/dox-app/lib/http/controllers/auth.controller.dart +++ b/packages/dox-app/lib/app/http/controllers/auth.controller.dart @@ -1,6 +1,8 @@ -import 'package:dox_app/models/user/user.model.dart'; +import 'package:dox_app/app/http/serializers/user.serializer.dart'; +import 'package:dox_app/app/models/user/user.model.dart'; import 'package:dox_auth/dox_auth.dart'; import 'package:dox_core/dox_core.dart'; +import 'package:dox_core/utils/logger.dart'; class AuthController { Future login(DoxRequest req) async { @@ -25,7 +27,6 @@ class AuthController { Future register(DoxRequest req) async { User user = User(); - user.debug(true); user.name = 'AJ'; user.email = 'aj@mail.com'; user.password = Hash.make('password'); @@ -34,9 +35,10 @@ class AuthController { } Future user(DoxRequest req) async { - if (req.auth?.isLoggedIn() == true) { - return req.auth?.user(); + IAuth? auth = req.auth; + if (auth?.isLoggedIn() == true) { + Logger.info('${auth?.user()?.name} is logged in'); + return UserSerializer(auth?.user()); } - throw UnAuthorizedException(message: 'reach to controller'); } } diff --git a/packages/dox-app/lib/http/controllers/blog.controller.dart b/packages/dox-app/lib/app/http/controllers/blog.controller.dart similarity index 89% rename from packages/dox-app/lib/http/controllers/blog.controller.dart rename to packages/dox-app/lib/app/http/controllers/blog.controller.dart index 2a7d049..3a4bcd7 100644 --- a/packages/dox-app/lib/http/controllers/blog.controller.dart +++ b/packages/dox-app/lib/app/http/controllers/blog.controller.dart @@ -1,5 +1,5 @@ -import 'package:dox_app/http/requests/blog.request.dart'; -import 'package:dox_app/http/services/blog.service.dart'; +import 'package:dox_app/app/http/requests/blog.request.dart'; +import 'package:dox_app/app/http/services/blog.service.dart'; import 'package:dox_core/dox_core.dart'; class BlogController { diff --git a/packages/dox-app/lib/http/controllers/web.controller.dart b/packages/dox-app/lib/app/http/controllers/web.controller.dart similarity index 100% rename from packages/dox-app/lib/http/controllers/web.controller.dart rename to packages/dox-app/lib/app/http/controllers/web.controller.dart diff --git a/packages/dox-app/lib/app/http/handler.dart b/packages/dox-app/lib/app/http/handler.dart new file mode 100644 index 0000000..38653b1 --- /dev/null +++ b/packages/dox-app/lib/app/http/handler.dart @@ -0,0 +1,8 @@ +import 'package:dox_core/dox_core.dart'; + +class ResponseHandler extends ResponseHandlerInterface { + @override + DoxResponse handle(DoxResponse res) { + return res; + } +} diff --git a/packages/dox-app/lib/http/middleware/custom.middleware.dart b/packages/dox-app/lib/app/http/middleware/custom.middleware.dart similarity index 54% rename from packages/dox-app/lib/http/middleware/custom.middleware.dart rename to packages/dox-app/lib/app/http/middleware/custom.middleware.dart index 406b9fe..a3ba20b 100644 --- a/packages/dox-app/lib/http/middleware/custom.middleware.dart +++ b/packages/dox-app/lib/app/http/middleware/custom.middleware.dart @@ -1,8 +1,8 @@ import 'package:dox_core/dox_core.dart'; -class CustomMiddleware extends DoxMiddleware { +class CustomMiddleware extends IDoxMiddleware { @override - DoxRequest handle(DoxRequest req) { + IDoxRequest handle(IDoxRequest req) { /// write your logic here return req; } diff --git a/packages/dox-app/lib/http/middleware/function.middleware.dart b/packages/dox-app/lib/app/http/middleware/function.middleware.dart similarity index 100% rename from packages/dox-app/lib/http/middleware/function.middleware.dart rename to packages/dox-app/lib/app/http/middleware/function.middleware.dart diff --git a/packages/dox-app/lib/http/requests/blog.request.dart b/packages/dox-app/lib/app/http/requests/blog.request.dart similarity index 100% rename from packages/dox-app/lib/http/requests/blog.request.dart rename to packages/dox-app/lib/app/http/requests/blog.request.dart diff --git a/packages/dox-app/lib/app/http/serializers/user.serializer.dart b/packages/dox-app/lib/app/http/serializers/user.serializer.dart new file mode 100644 index 0000000..cccfd52 --- /dev/null +++ b/packages/dox-app/lib/app/http/serializers/user.serializer.dart @@ -0,0 +1,12 @@ +import 'package:dox_core/dox_core.dart'; + +import '../../../app/models/user/user.model.dart'; + +class UserSerializer extends Serializer { + UserSerializer(super.data); + + @override + Map convert(User m) { + return {}; + } +} diff --git a/packages/dox-app/lib/http/services/blog.service.dart b/packages/dox-app/lib/app/http/services/blog.service.dart similarity index 86% rename from packages/dox-app/lib/http/services/blog.service.dart rename to packages/dox-app/lib/app/http/services/blog.service.dart index 06128b3..5325065 100644 --- a/packages/dox-app/lib/http/services/blog.service.dart +++ b/packages/dox-app/lib/app/http/services/blog.service.dart @@ -1,6 +1,6 @@ -import 'package:dox_app/http/requests/blog.request.dart'; -import 'package:dox_app/models/blog/blog.model.dart'; -import 'package:dox_app/models/category/category.model.dart'; +import 'package:dox_app/app/http/requests/blog.request.dart'; +import 'package:dox_app/app/models/blog/blog.model.dart'; +import 'package:dox_app/app/models/category/category.model.dart'; import 'package:dox_query_builder/dox_query_builder.dart'; class BlogService { diff --git a/packages/dox-app/lib/models/blog/blog.model.dart b/packages/dox-app/lib/app/models/blog/blog.model.dart similarity index 88% rename from packages/dox-app/lib/models/blog/blog.model.dart rename to packages/dox-app/lib/app/models/blog/blog.model.dart index 73f7af9..aadbe2e 100644 --- a/packages/dox-app/lib/models/blog/blog.model.dart +++ b/packages/dox-app/lib/app/models/blog/blog.model.dart @@ -1,5 +1,5 @@ -import 'package:dox_app/models/category/category.model.dart'; -import 'package:dox_app/models/user/user.model.dart'; +import 'package:dox_app/app/models/category/category.model.dart'; +import 'package:dox_app/app/models/user/user.model.dart'; import 'package:dox_app/utils/extensions.dart'; import 'package:dox_query_builder/dox_query_builder.dart'; diff --git a/packages/dox-app/lib/models/blog/blog.model.g.dart b/packages/dox-app/lib/app/models/blog/blog.model.g.dart similarity index 99% rename from packages/dox-app/lib/models/blog/blog.model.g.dart rename to packages/dox-app/lib/app/models/blog/blog.model.g.dart index 5cf046c..ce63fc2 100644 --- a/packages/dox-app/lib/models/blog/blog.model.g.dart +++ b/packages/dox-app/lib/app/models/blog/blog.model.g.dart @@ -22,7 +22,7 @@ class BlogGenerator extends Model with SoftDeletes { set id(dynamic val) => tempIdValue = val; - Blog get newQuery => Blog(); + static Blog query() => Blog(); @override List get preloadList => []; diff --git a/packages/dox-app/lib/models/category/category.model.dart b/packages/dox-app/lib/app/models/category/category.model.dart similarity index 90% rename from packages/dox-app/lib/models/category/category.model.dart rename to packages/dox-app/lib/app/models/category/category.model.dart index 76a5f1f..753b090 100644 --- a/packages/dox-app/lib/models/category/category.model.dart +++ b/packages/dox-app/lib/app/models/category/category.model.dart @@ -1,4 +1,4 @@ -import 'package:dox_app/models/blog/blog.model.dart'; +import 'package:dox_app/app/models/blog/blog.model.dart'; import 'package:dox_query_builder/dox_query_builder.dart'; part 'category.model.g.dart'; diff --git a/packages/dox-app/lib/models/category/category.model.g.dart b/packages/dox-app/lib/app/models/category/category.model.g.dart similarity index 98% rename from packages/dox-app/lib/models/category/category.model.g.dart rename to packages/dox-app/lib/app/models/category/category.model.g.dart index 4b2407d..d7c8448 100644 --- a/packages/dox-app/lib/models/category/category.model.g.dart +++ b/packages/dox-app/lib/app/models/category/category.model.g.dart @@ -22,7 +22,7 @@ class CategoryGenerator extends Model { set id(dynamic val) => tempIdValue = val; - Category get newQuery => Category(); + static Category query() => Category(); @override List get preloadList => []; diff --git a/packages/dox-app/lib/models/user/user.model.dart b/packages/dox-app/lib/app/models/user/user.model.dart similarity index 100% rename from packages/dox-app/lib/models/user/user.model.dart rename to packages/dox-app/lib/app/models/user/user.model.dart diff --git a/packages/dox-app/lib/models/user/user.model.g.dart b/packages/dox-app/lib/app/models/user/user.model.g.dart similarity index 98% rename from packages/dox-app/lib/models/user/user.model.g.dart rename to packages/dox-app/lib/app/models/user/user.model.g.dart index 8427b37..24b04bb 100644 --- a/packages/dox-app/lib/models/user/user.model.g.dart +++ b/packages/dox-app/lib/app/models/user/user.model.g.dart @@ -25,7 +25,7 @@ class UserGenerator extends Model with SoftDeletes { set id(dynamic val) => tempIdValue = val; - User get newQuery => User(); + static User query() => User(); @override List get preloadList => []; diff --git a/packages/dox-app/lib/app/ws/controllers/ws_controller.dart b/packages/dox-app/lib/app/ws/controllers/ws_controller.dart new file mode 100644 index 0000000..8b11eb5 --- /dev/null +++ b/packages/dox-app/lib/app/ws/controllers/ws_controller.dart @@ -0,0 +1,7 @@ +import 'package:dox_websocket/dox_websocket.dart'; + +class BlogController { + void index(WebsocketEmitter emitter, dynamic message) async { + /// write your logic here + } +} diff --git a/packages/dox-app/lib/config/app.dart b/packages/dox-app/lib/config/app.dart index 4739467..27d151b 100644 --- a/packages/dox-app/lib/config/app.dart +++ b/packages/dox-app/lib/config/app.dart @@ -1,51 +1,80 @@ +import 'package:dox_app/app/http/handler.dart'; import 'package:dox_app/config/cache.dart'; import 'package:dox_app/config/cors.dart'; +import 'package:dox_app/config/logger.dart'; import 'package:dox_app/config/services.dart'; import 'package:dox_app/config/storage.dart'; -import 'package:dox_app/http/handler.dart'; -import 'package:dox_app/http/requests/blog.request.dart'; import 'package:dox_app/routes/api.dart'; import 'package:dox_app/routes/web.dart'; import 'package:dox_app/routes/websocket.dart'; import 'package:dox_core/dox_core.dart'; AppConfig appConfig = AppConfig( - /// application key - appKey: Env.get('APP_KEY'), + /// Application secret key + /// ------------------------------- + /// This key is use to encrypt/decrypt the data such as cache and cookies. + /// Make sure to keep the `APP_KEY` as an environment variable and secure. + /// + /// Note: Changing the application key for an existing app will make all + /// the cookies and cache invalid and also the existing encrypted data + /// will not be decrypted. + appKey: Env.get('APP_KEY'), - /// application server port - serverPort: int.parse(Env.get('APP_PORT', 3000)), + /// Server port + /// ------------------------------- + /// App will be running on this port. + serverPort: Env.get('APP_PORT', 3000), - /// total multi-thread isolate to run - totalIsolate: 6, + /// Total isolate (multi thread) + /// ------------------------------- + /// Total isolate to run the application. + /// Depending on your machine CPU and RAM, you can adjust the the number + /// of isolates. The more isolate application have, the more it can handle + /// concurrency requests. + totalIsolate: Env.get('APP_TOTAL_ISOLATE', 6), - /// global middleware - globalMiddleware: [], + /// Form requests + /// ------------------------------- + /// Register form request to dox in order to inject into the controller. + /// Example, + /// { + /// BlogRequest : () => BlogRequest(), + /// UserRequest : () => UserRequest(), + /// } + formRequests: {}, - /// form requests - formRequests: { - BlogRequest: () => BlogRequest(), - }, + /// Global middleware + /// ------------------------------- + /// By registering global middleware here, + /// middleware will be applied to all the routes. + globalMiddleware: [ + LogMiddleware(enabled: true), + ], - /// routers + /// Routers + /// ------------------------------- + /// Register routers to create route to dox framework. routers: [ WebRouter(), ApiRouter(), WebsocketRouter(), ], - /// response handler + /// Response handler responseHandler: ResponseHandler(), - /// service to run on multithread server - services: services, + /// Cache driver configuration + cache: cache, + + /// File storage driver configuration + fileStorage: storage, - /// cors configuration + /// Cors configuration cors: cors, - /// cache driver configuration - cache: cache, + /// Service to run on multithread server + services: services, - /// file storage driver configuration - fileStorage: storage, + /// logger configuration + logger: logger, ); diff --git a/packages/dox-app/lib/config/cors.dart b/packages/dox-app/lib/config/cors.dart index cbaaf74..312c462 100644 --- a/packages/dox-app/lib/config/cors.dart +++ b/packages/dox-app/lib/config/cors.dart @@ -1,3 +1,61 @@ import 'package:dox_core/dox_core.dart'; -CORSConfig cors = CORSConfig(); +CORSConfig cors = CORSConfig( + /// Enabled + /// ------------------------------- + /// A boolean to enable or disable CORS integration. + /// Setting to true will enable the CORS for all HTTP request. + enabled: false, + + /// Origin + /// ------------------------------- + /// Set a list of origins to be allowed for `Access-Control-Allow-Origin`. + /// The value can be one of the following: + /// Array : An array of allowed origins. + /// String : Comma separated list of allowed origins. + /// String (*) : A wildcard (*) to allow all request origins. + origin: '*', + + /// Methods + /// ------------------------------- + /// Set a list of origins to be allowed for `Access-Control-Request-Method`. + /// The value can be one of the following: + /// Array : An array of request methods. + /// String : Comma separated list of request methods. + /// String (*) : A wildcard (*) to allow all request methods. + methods: '*', + + /// Headers + /// ------------------------------- + /// Set a list of origins to be allowed for `Access-Control-Allow-Headers`. + /// The value can be one of the following: + /// Array : An array of allowed headers. + /// String : Comma separated list of allowed headers. + /// String (*) : A wildcard (*) to allow all request headers. + headers: '*', + + /// Expose Headers + /// ------------------------------- + /// Set a list of origins to be allowed for `Access-Control-Expose-Headers`. + /// The value can be one of the following: + /// Array : An array of expose headers. + /// String : Comma separated list of expose headers. + exposeHeaders: [ + 'cache-control', + 'content-language', + 'content-type', + 'expires', + 'last-modified', + 'pragma', + ], + + /// Credentials + /// ------------------------------- + /// Toggle `Access-Control-Allow-Credentials` header. + credentials: true, + + /// MaxAge + /// ------------------------------- + /// Define `Access-Control-Max-Age` header in seconds. + maxAge: 90, +); diff --git a/packages/dox-app/lib/config/logger.dart b/packages/dox-app/lib/config/logger.dart new file mode 100644 index 0000000..4f5d4ce --- /dev/null +++ b/packages/dox-app/lib/config/logger.dart @@ -0,0 +1,24 @@ +import 'package:dox_core/dox_core.dart'; + +LoggerConfig logger = LoggerConfig( + /// Application name + /// ------------------------------- + /// The name of the application you want to add to the log. + name: Env.get('APP_NAME'), + + /// Toggle logger + /// ------------------------------- + /// Enable or disable logger in application. + enabled: true, + + /// Logging level + /// ------------------------------- + /// The level from which you want the logger to flush logs. + level: Env.get('LOG_LEVEL', 'info'), + + /// Pretty print + /// ------------------------------- + /// It is highly advised NOT to use `prettyPrint` in production, since it + /// can have huge impact on performance. + prettyPrint: Env.get('APP_ENV') == 'development', +); diff --git a/packages/dox-app/lib/config/postgres.dart b/packages/dox-app/lib/config/postgres.dart new file mode 100644 index 0000000..2ecd619 --- /dev/null +++ b/packages/dox-app/lib/config/postgres.dart @@ -0,0 +1,34 @@ +import 'package:dox_core/dox_core.dart'; +import 'package:postgres_pool/postgres_pool.dart'; + +PgEndpoint postgresEndpoint = PgEndpoint( + /// database host + host: Env.get('DB_HOST', 'localhost'), + + /// database port + port: Env.get('DB_PORT', 5432), + + /// database name + database: Env.get('DB_NAME', 'dox'), + + /// database username + username: Env.get('DB_USERNAME', 'postgres'), + + /// database password + password: Env.get('DB_PASSWORD', 'postgres'), +); + +/// postgres pool connection configuration +PgPool pool = PgPool( + postgresEndpoint, + + /// postgres setting + settings: PgPoolSettings() + + /// The maximum duration a connection is kept open. + /// New sessions won't be scheduled after this limit is reached. + ..maxConnectionAge = Duration(hours: 1) + + /// The maximum number of concurrent sessions. + ..concurrency = 10, +); diff --git a/packages/dox-app/lib/config/redis.dart b/packages/dox-app/lib/config/redis.dart new file mode 100644 index 0000000..77d1d49 --- /dev/null +++ b/packages/dox-app/lib/config/redis.dart @@ -0,0 +1,16 @@ +import 'package:dox_core/dox_core.dart'; +import 'package:ioredis/ioredis.dart'; + +Redis redis = Redis( + /// Redis configuration + /// ------------------------------- + /// Following is the configuration used by the Redis provider to connect to + /// the redis server and execute redis commands. + RedisOptions( + host: Env.get('REDIS_HOST', '127.0.0.1'), + port: Env.get('REDIS_PORT', 6379), + db: Env.get('REDIS_DB', 0), + username: Env.get('REDIS_USERNAME', ''), + password: Env.get('REDIS_PASSWORD', ''), + ), +); diff --git a/packages/dox-app/lib/config/services.dart b/packages/dox-app/lib/config/services.dart index 81c618b..485eadd 100644 --- a/packages/dox-app/lib/config/services.dart +++ b/packages/dox-app/lib/config/services.dart @@ -1,10 +1,15 @@ import 'package:dox_app/services/auth_service.dart'; -import 'package:dox_app/services/database_service.dart'; +import 'package:dox_app/services/orm_service.dart'; import 'package:dox_app/services/websocket_service.dart'; import 'package:dox_core/dox_core.dart'; +/// Services to register on dox +/// ------------------------------- +/// Since dox run on multi thread isolate, we need to register +/// below extra services to dox. +/// So that dox can register again on new isolate. List services = [ - DatabaseService(), + ORMService(), AuthService(), WebsocketService(), ]; diff --git a/packages/dox-app/lib/http/handler.dart b/packages/dox-app/lib/http/handler.dart deleted file mode 100644 index 429dbad..0000000 --- a/packages/dox-app/lib/http/handler.dart +++ /dev/null @@ -1,14 +0,0 @@ -import 'package:dox_core/dox_core.dart'; - -class ResponseHandler extends ResponseHandlerInterface { - @override - dynamic handle(DoxResponse res) { - if (res.content is IHttpException) { - return { - 'code': res.content.code, - 'status': 'failed', - 'message': res.content.toResponse(), - }; - } - } -} diff --git a/packages/dox-app/lib/routes/api.dart b/packages/dox-app/lib/routes/api.dart index c62e789..d874587 100644 --- a/packages/dox-app/lib/routes/api.dart +++ b/packages/dox-app/lib/routes/api.dart @@ -1,6 +1,6 @@ -import 'package:dox_app/http/controllers/api.controller.dart'; -import 'package:dox_app/http/controllers/auth.controller.dart'; -import 'package:dox_app/http/controllers/blog.controller.dart'; +import 'package:dox_app/app/http/controllers/api.controller.dart'; +import 'package:dox_app/app/http/controllers/auth.controller.dart'; +import 'package:dox_app/app/http/controllers/blog.controller.dart'; import 'package:dox_auth/dox_auth.dart'; import 'package:dox_core/dox_core.dart'; @@ -24,7 +24,7 @@ class ApiRouter extends Router { Route.post('/auth/login', authController.login); Route.post('/auth/register', authController.register); Route.get('/auth/user', [ - AuthMiddleware.handle, + AuthMiddleware(), authController.user, ]); } diff --git a/packages/dox-app/lib/routes/web.dart b/packages/dox-app/lib/routes/web.dart index 4c7ef36..44827c8 100644 --- a/packages/dox-app/lib/routes/web.dart +++ b/packages/dox-app/lib/routes/web.dart @@ -1,5 +1,4 @@ -import 'package:dox_app/services/database_service.dart'; -import 'package:dox_auth/dox_auth.dart'; +import 'package:dox_app/config/postgres.dart'; import 'package:dox_core/dox_core.dart'; class WebRouter extends Router { @@ -8,13 +7,6 @@ class WebRouter extends Router { @override void register() { - Route.get('/auth', [ - AuthMiddleware.handle, - (DoxRequest req) { - return 'hello'; - } - ]); - Route.get('/ping', (DoxRequest req) async { return 'pong'; }); @@ -49,7 +41,7 @@ class WebRouter extends Router { }); Route.put('/ping', (DoxRequest req) async { - return await DatabaseService().pool.query('SELECT * from blog'); + return await pool.query('SELECT * from blog'); }); } } diff --git a/packages/dox-app/lib/routes/websocket.dart b/packages/dox-app/lib/routes/websocket.dart index bbafbf5..2184c23 100644 --- a/packages/dox-app/lib/routes/websocket.dart +++ b/packages/dox-app/lib/routes/websocket.dart @@ -9,9 +9,22 @@ class WebsocketRouter extends Router { void register() { Route.websocket('ws', (WebsocketEvent event) { event.on('intro', (WebsocketEmitter emitter, dynamic message) { - print(message); emitter.emit('intro', message); }); + + event.on('json', (WebsocketEmitter emitter, dynamic message) { + emitter.emit('json_response', message); + }); + }); + + Route.websocket('chat', (WebsocketEvent event) { + event.on('intro', (WebsocketEmitter emitter, dynamic message) { + emitter.emit('intro', message); + }); + + event.on('json', (WebsocketEmitter emitter, dynamic message) { + emitter.emit('json_response', message); + }); }); } } diff --git a/packages/dox-app/lib/services/auth_service.dart b/packages/dox-app/lib/services/auth_service.dart index 7728f23..13a33d2 100644 --- a/packages/dox-app/lib/services/auth_service.dart +++ b/packages/dox-app/lib/services/auth_service.dart @@ -1,4 +1,4 @@ -import 'package:dox_app/models/user/user.model.dart'; +import 'package:dox_app/app/models/user/user.model.dart'; import 'package:dox_auth/dox_auth.dart'; import 'package:dox_core/dox_core.dart'; diff --git a/packages/dox-app/lib/services/database_service.dart b/packages/dox-app/lib/services/database_service.dart deleted file mode 100644 index 42ba5c1..0000000 --- a/packages/dox-app/lib/services/database_service.dart +++ /dev/null @@ -1,51 +0,0 @@ -import 'package:dox_core/dox_core.dart'; -import 'package:dox_migration/dox_migration.dart' as mi; -import 'package:dox_query_builder/dox_query_builder.dart'; -import 'package:postgres_pool/postgres_pool.dart'; - -class DatabaseService implements DoxService { - /// Declare as Singleton to use connection pool - static final DatabaseService _i = DatabaseService._internal(); - factory DatabaseService() => _i; - DatabaseService._internal(); - - late PgPool pool; - - late PgEndpoint endPoint; - - bool debug = true; - - int concurrency = 10; - - @override - Future setup() async { - endPoint = PgEndpoint( - host: Env.get('DB_HOST', 'localhost'), - port: int.parse(Env.get('DB_PORT', '5432')), - database: Env.get('DB_NAME', 'dox'), - username: Env.get('DB_USERNAME', 'postgres'), - password: Env.get('DB_PASSWORD', 'postgres'), - ); - - pool = PgPool( - endPoint, - settings: PgPoolSettings() - ..maxConnectionAge = Duration(hours: 1) - ..concurrency = concurrency, - ); - - /// this will create connection pool initially when server started - /// so prevent connecting to connection pool when calling API for first time - await pool.query('SELECT 1'); - - /// Initialize Sql QueryBuilder - SqlQueryBuilder.initialize( - database: pool, - debug: debug, - ); - } - - Future migrate() async { - await mi.Migration().migrate(); - } -} diff --git a/packages/dox-app/lib/services/orm_service.dart b/packages/dox-app/lib/services/orm_service.dart new file mode 100644 index 0000000..642110a --- /dev/null +++ b/packages/dox-app/lib/services/orm_service.dart @@ -0,0 +1,22 @@ +import 'package:dox_app/config/postgres.dart'; +import 'package:dox_core/dox_core.dart'; +import 'package:dox_query_builder/dox_query_builder.dart'; + +/// Query builder service +/// -------------------------- +/// Initializing to setup query builder so that this project can use ORM. +/// If this project do not require database, you can simply delete this file +/// and remove from config/services.dart list. +class ORMService implements DoxService { + @override + Future setup() async { + /// Initialize Sql QueryBuilder + SqlQueryBuilder.initialize( + database: pool, + debug: true, + printer: Env.get('APP_ENV') == 'development' + ? PrettyQueryPrinter() + : ConsoleQueryPrinter(), + ); + } +} diff --git a/packages/dox-app/lib/services/websocket_service.dart b/packages/dox-app/lib/services/websocket_service.dart index f3f2bc3..c946a0b 100644 --- a/packages/dox-app/lib/services/websocket_service.dart +++ b/packages/dox-app/lib/services/websocket_service.dart @@ -1,3 +1,4 @@ +import 'package:dox_app/config/redis.dart'; import 'package:dox_core/dox_core.dart'; import 'package:dox_websocket/dox_websocket.dart'; import 'package:ioredis/ioredis.dart'; @@ -5,7 +6,7 @@ import 'package:ioredis/ioredis.dart'; class WebsocketService implements DoxService { @override void setup() { - Redis sub = Redis(); + Redis sub = redis; Redis pub = sub.duplicate(); WebsocketServer io = WebsocketServer(Dox()); diff --git a/packages/dox-app/pubspec.lock b/packages/dox-app/pubspec.lock index b815aaf..89c2cc3 100644 --- a/packages/dox-app/pubspec.lock +++ b/packages/dox-app/pubspec.lock @@ -231,14 +231,14 @@ packages: path: "../dox-annotation" relative: true source: path - version: "1.0.5-alpha1.9" + version: "1.0.5-alpha2.0" dox_auth: dependency: "direct main" description: path: "../dox-auth" relative: true source: path - version: "2.0.0-alpha.2" + version: "2.0.0-alpha.3" dox_builder: dependency: "direct dev" description: @@ -259,7 +259,7 @@ packages: path: "../dox-migration" relative: true source: path - version: "1.0.4" + version: "2.0.0-alpha.1" dox_query_builder: dependency: "direct main" description: