Skip to content

Commit

Permalink
implement controller and middleware for handing CORS
Browse files Browse the repository at this point in the history
  • Loading branch information
zamronypj committed Sep 3, 2019
1 parent 799e00e commit 1597882
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public/.htaccess
#----------------------------------------------
# Ignore generated binary executable
#----------------------------------------------
bin/app.cgi
public/app.cgi
cgi-bin/app.cgi

Expand Down Expand Up @@ -73,4 +74,3 @@ fp.ini
# Ignore Visual Studio Code generated files
#----------------------------------------------
.vscode/

4 changes: 2 additions & 2 deletions build.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ IF NOT DEFINED FANO_DIR (SET FANO_DIR="vendor/fano")
IF NOT DEFINED BUILD_TYPE (SET BUILD_TYPE="prod")
IF NOT DEFINED USER_APP_DIR (SET USER_APP_DIR="src")
IF NOT DEFINED UNIT_OUTPUT_DIR (SET UNIT_OUTPUT_DIR="bin\unit")
IF NOT DEFINED EXEC_OUTPUT_DIR (SET EXEC_OUTPUT_DIR="public")
IF NOT DEFINED EXEC_OUTPUT_DIR (SET EXEC_OUTPUT_DIR="bin")
IF NOT DEFINED EXEC_OUTPUT_NAME (SET EXEC_OUTPUT_NAME="app.cgi")
IF NOT DEFINED SOURCE_PROGRAM_NAME (SET SOURCE_PROGRAM_NAME="app.pas")
IF NOT DEFINED FPC_BIN (SET FPC_BIN="fpc")

%FPC_BIN% @vendor/fano/fano.cfg @build.cfg %USER_APP_DIR%\%SOURCE_PROGRAM_NAME%
%FPC_BIN% @vendor/fano/fano.cfg @build.cfg %USER_APP_DIR%\%SOURCE_PROGRAM_NAME%
4 changes: 2 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ if [[ -z "${UNIT_OUTPUT_DIR}" ]]; then
fi

if [[ -z "${EXEC_OUTPUT_DIR}" ]]; then
export EXEC_OUTPUT_DIR="public"
export EXEC_OUTPUT_DIR="bin"
fi

if [[ -z "${EXEC_OUTPUT_NAME}" ]]; then
Expand All @@ -44,4 +44,4 @@ if [[ -z "${FPC_BIN}" ]]; then
export FPC_BIN="fpc"
fi

${FPC_BIN} @vendor/fano/fano.cfg @build.cfg ${USER_APP_DIR}/${SOURCE_PROGRAM_NAME}
${FPC_BIN} @vendor/fano/fano.cfg @build.cfg ${USER_APP_DIR}/${SOURCE_PROGRAM_NAME}
6 changes: 2 additions & 4 deletions src/App/Home/Controllers/HomeController.pas
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface
*
* @author [[AUTHOR_NAME]] <[[AUTHOR_EMAIL]]>
*------------------------------------------------*)
THomeController = class(TRouteHandler, IDependency)
THomeController = class(TRouteHandler)
public
function handleRequest(
const request : IRequest;
Expand All @@ -38,9 +38,7 @@ implementation
const response : IResponse
) : IResponse;
begin
{---put your code here---}
//response.body().write('nice');
result := response;
result := TJsonResponse.create(response.headers(), '{ "hello" : "world" }');
end;

end.
23 changes: 18 additions & 5 deletions src/Dependencies/main.dependencies.inc
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,22 @@ container.add('appMiddlewares', TMiddlewareCollectionAwareFactory.create());
------------------------------------------------}
container.factory('routeMiddlewares', TNullMiddlewareCollectionAwareFactory.create());

container.add('cors', TCorsMiddlewareFactory.create());
container.add(
'cors',
(TCorsMiddlewareFactory.create())
.allowedOriginsPatterns(['http\:\/\/localhost\:[0-9]{1,5}'])
.allowedMethods(['GET', 'POST', 'OPTIONS'])
.allowedHeaders(['X-My-Custom-Header'])
);

container
(
container.get('appMiddlewares') as IMiddlewareCollectionAware
).addBefore(container.get('cors') as IMiddleware);
appMiddlewares := container.get('appMiddlewares') as IMiddlewareCollectionAware;
appMiddlewares.addBefore(container.get('cors') as IMiddleware);

container.add(
GuidToString(IDispatcher),
TDispatcherFactory.create(
appMiddlewares,
container.get(GuidToString(IRouteMatcher)) as IRouteMatcher,
TRequestResponseFactory.create()
)
);
3 changes: 2 additions & 1 deletion src/Routes/Home/routes.inc
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
* @license [[LICENSE_URL]] ([[LICENSE]])
*------------------------------------------------------------- *)

router.get('/home', container.get('homeController') as IRouteHandler);
router.get('/', container.get('homeController') as IRouteHandler);
router.options('/', container.get('homeController') as IRouteHandler);
6 changes: 3 additions & 3 deletions src/app.pas
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
appInstance : IWebApplication;

begin
writeln('Starting application at 127.0.0.1:20477');
writeln('Starting application at 127.0.0.1:4000');

(*!-----------------------------------------------
* Bootstrap SCGI application
*
* @author AUTHOR_NAME <[email protected]>
*------------------------------------------------*)
appInstance := TBootstrapApp.create('127.0.0.1', 20477);
appInstance := TBootstrapApp.create('127.0.0.1', 4000);
appInstance.run();
end.
end.
3 changes: 2 additions & 1 deletion src/bootstrap.pas
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ implementation


procedure TBootstrapApp.buildDependencies(const container : IDependencyContainer);
var appMiddlewares : IMiddlewareCollectionAware;
begin
{$INCLUDE Dependencies/dependencies.inc}
end;
Expand All @@ -49,4 +50,4 @@ implementation
router := nil;
end;
end;
end.
end.
2 changes: 1 addition & 1 deletion vendor/fano
Submodule fano updated 30 files
+9 −1 src/Http/Request/Contracts/RequestIntf.pas
+3 −1 src/Http/Request/Factories/RequestFactoryImpl.pas
+25 −2 src/Http/Request/RequestImpl.pas
+4 −1 src/Http/Response/JsonResponseImpl.pas
+61 −0 src/Http/Uri/Contracts/UriIntf.pas
+241 −0 src/Http/Uri/Implementations/UriImpl.pas
+1 −0 src/Includes/Http/Contracts/http.aliases.inc
+1 −0 src/Includes/Http/Contracts/interfaces.inc
+2 −0 src/Includes/Http/Implementations/http.aliases.inc
+2 −0 src/Includes/Http/Implementations/implementations.inc
+17 −0 src/Includes/Security/Contracts/cors.aliases.inc
+3 −0 src/Includes/Security/Contracts/interfaces.inc
+2 −0 src/Includes/Security/Contracts/security.aliases.inc
+22 −0 src/Includes/Security/Implementations/cors.aliases.inc
+8 −0 src/Includes/Security/Implementations/implementations.inc
+2 −0 src/Includes/Security/Implementations/security.aliases.inc
+3 −1 src/Libs/FastCGI/Request/Factories/FcgiRequestFactoryImpl.pas
+253 −3 src/Libs/FastCGI/Request/FcgiRequestImpl.pas
+3 −3 src/Middleware/Factories/MiddlewareChainFactoryImpl.pas
+1 −1 src/Middleware/MiddlewareCollectionImpl.pas
+48 −0 src/Middleware/NullMiddlewareImpl.pas
+8 −8 src/Security/Cors/Contracts/CorsIntf.pas
+123 −0 src/Security/Cors/Factories/BaseCorsMiddlewareFactoryImpl.pas
+62 −0 src/Security/Cors/Factories/CorsMiddlewareFactoryImpl.pas
+46 −0 src/Security/Cors/Factories/NullCorsMiddlewareFactoryImpl.pas
+53 −48 src/Security/Cors/Implementations/CorsConfigImpl.pas
+63 −39 src/Security/Cors/Implementations/CorsImpl.pas
+147 −0 src/Security/Cors/Implementations/NullCorsImpl.pas
+2 −3 src/Security/Cors/Middlewares/CorsMiddlewareImpl.pas
+4 −0 src/fano.cfg

0 comments on commit 1597882

Please sign in to comment.