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

Update dox example app #83

Merged
merged 1 commit into from
Dec 16, 2023
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
6 changes: 4 additions & 2 deletions packages/dox-app/.env
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 7 additions & 4 deletions packages/dox-app/bin/server.dart
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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<dynamic> login(DoxRequest req) async {
Expand All @@ -25,7 +27,6 @@ class AuthController {

Future<dynamic> register(DoxRequest req) async {
User user = User();
user.debug(true);
user.name = 'AJ';
user.email = '[email protected]';
user.password = Hash.make('password');
Expand All @@ -34,9 +35,10 @@ class AuthController {
}

Future<dynamic> 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<User>()?.name} is logged in');
return UserSerializer(auth?.user());
}
throw UnAuthorizedException(message: 'reach to controller');
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions packages/dox-app/lib/app/http/handler.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:dox_core/dox_core.dart';

class ResponseHandler extends ResponseHandlerInterface {
@override
DoxResponse handle(DoxResponse res) {
return res;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
12 changes: 12 additions & 0 deletions packages/dox-app/lib/app/http/serializers/user.serializer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:dox_core/dox_core.dart';

import '../../../app/models/user/user.model.dart';

class UserSerializer extends Serializer<User> {
UserSerializer(super.data);

@override
Map<String, dynamic> convert(User m) {
return <String, dynamic>{};
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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';
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/dox-app/lib/app/ws/controllers/ws_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:dox_websocket/dox_websocket.dart';

class BlogController {
void index(WebsocketEmitter emitter, dynamic message) async {
/// write your logic here
}
}
75 changes: 52 additions & 23 deletions packages/dox-app/lib/config/app.dart
Original file line number Diff line number Diff line change
@@ -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<String>('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<int>('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<int>('APP_TOTAL_ISOLATE', 6),

/// global middleware
globalMiddleware: <dynamic>[],
/// Form requests
/// -------------------------------
/// Register form request to dox in order to inject into the controller.
/// Example,
/// {
/// BlogRequest : () => BlogRequest(),
/// UserRequest : () => UserRequest(),
/// }
formRequests: <Type, FormRequest Function()>{},

/// form requests
formRequests: <Type, FormRequest Function()>{
BlogRequest: () => BlogRequest(),
},
/// Global middleware
/// -------------------------------
/// By registering global middleware here,
/// middleware will be applied to all the routes.
globalMiddleware: <dynamic>[
LogMiddleware(enabled: true),
],

/// routers
/// Routers
/// -------------------------------
/// Register routers to create route to dox framework.
routers: <Router>[
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,
);
60 changes: 59 additions & 1 deletion packages/dox-app/lib/config/cors.dart
Original file line number Diff line number Diff line change
@@ -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: <String>[
'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,
);
24 changes: 24 additions & 0 deletions packages/dox-app/lib/config/logger.dart
Original file line number Diff line number Diff line change
@@ -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',
);
34 changes: 34 additions & 0 deletions packages/dox-app/lib/config/postgres.dart
Original file line number Diff line number Diff line change
@@ -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<int>('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,
);
Loading