Skip to content

Commit

Permalink
Updated Mustache demo to use IMARSResponse and IMARSRequest. Enabled …
Browse files Browse the repository at this point in the history
…CORS and added StatusCode for swagger endpoint. (#70)
  • Loading branch information
larsenbjorn authored and andrea-magni committed May 14, 2019
1 parent dccdfe7 commit 1ce811e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
13 changes: 10 additions & 3 deletions Demos/Mustache/Server.Forms.Main.pas
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ implementation

uses
StrUtils, Web.HttpApp
, MARS.Core.URL, MARS.Core.Engine
, MARS.Core.URL, MARS.Core.Engine, MARS.Core.RequestAndResponse.Interfaces
, Server.Ignition;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
Expand All @@ -59,7 +59,7 @@ procedure TMainForm.FormCreate(Sender: TObject);
// skip favicon requests (browser)
TServerEngine.Default.BeforeHandleRequest :=
function (const AEngine: TMARSEngine;
const AURL: TMARSURL; const ARequest: TWebRequest; const AResponse: TWebResponse;
const AURL: TMARSURL; const ARequest: IMARSRequest; const AResponse: IMARSResponse;
var Handled: Boolean
): Boolean
begin
Expand All @@ -68,7 +68,14 @@ procedure TMainForm.FormCreate(Sender: TObject);
begin
Result := False;
Handled := True;
end
end;

// Handle CORS and PreFlight
if SameText(ARequest.Method, 'OPTIONS') then
begin
Handled := True;
Result := False;
end;
end;

StartServerAction.Execute;
Expand Down
1 change: 1 addition & 0 deletions Demos/Mustache/Server.Ignition.pas
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ implementation
try
// Engine configuration
FEngine.Parameters.LoadFromIniFile;
FEngine.Parameters.Values['CORS.Enabled']:= True;

// Application configuration
FEngine.AddApplication('DefaultApp', '/default', [ 'Server.Resources.*']);
Expand Down
3 changes: 2 additions & 1 deletion Demos/Mustache/Server.Resources.pas
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ THelloWorldResource = class
[GET, Path('metadata/bootstrap'), Produces(TMediaType.TEXT_HTML)]
function MetadataBootstrap([Context] Metadata: TMARSApplicationMetadata): string;

[GET, Path('metadata/swagger'), Produces(TMediaType.APPLICATION_JSON)]
[GET, Path('metadata/swagger'), Produces(TMediaType.APPLICATION_JSON), MetaVisible(False)]
function MetadataSwagger([Context] Metadata: TMARSEngineMetadata): TMARSResponse;

[GET, Path('metadata/json'), Produces(TMediaType.APPLICATION_JSON)]
Expand Down Expand Up @@ -71,6 +71,7 @@ function THelloWorldResource.MetadataSwagger(
Result := TMARSResponse.Create;
Result.ContentType := 'application/json';
Result.Content := mustache.RenderTemplateWithJSON('swagger.json', Metadata.ToJSON, True);
Result.StatusCode := 200;
end;

function THelloWorldResource.MetadataBootstrap(
Expand Down

0 comments on commit 1ce811e

Please sign in to comment.