Skip to content

Commit

Permalink
Add support to be installed as a dotnet tool (closes #56) +semver: fe…
Browse files Browse the repository at this point in the history
…ature
  • Loading branch information
natenho committed Jan 31, 2021
1 parent 787afd3 commit fecd4b7
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 20 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ You can run Mockaco from the official [Docker image](https://hub.docker.com/r/na
$ docker run -it --rm -p 5000:80 -v /your/folder:/app/Mocks natenho/mockaco
```

Or install an run as a dotnet tool:

```console
$ dotnet tool install -g mockaco
$ mockaco
```

Or using [.NET Core](https://dotnet.microsoft.com/download) dotnet CLI, you can run the [latest binaries](https://github.com/natenho/Mockaco/releases/latest/download/Mockaco.Web.Site.zip):

```console
Expand Down
2 changes: 1 addition & 1 deletion src/Mockaco/Docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ COPY ./Mocks/hello.json /app/Mocks/
COPY Settings /app/Settings
VOLUME /app/Mocks
VOLUME /app/Settings
ENTRYPOINT ["dotnet", "Mockaco.dll"]
ENTRYPOINT ["dotnet", "Mockaco.dll", "--path", "Mocks", "--urls", "http://+:5000"]
28 changes: 24 additions & 4 deletions src/Mockaco/Mockaco.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>latest</LangVersion>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..</DockerfileContext>
</PropertyGroup>


<PropertyGroup>
<IsPackable>true</IsPackable>
<PackAsTool>true</PackAsTool>
<ToolCommandName>mockaco</ToolCommandName>
<PackageOutputPath>./nupkg</PackageOutputPath>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<Authors>natenho</Authors>
<Description>HTTP mock server, useful to stub services and simulate dynamic API responses, leveraging ASP.NET Core features, built-in fake data generation and pure C# scripting</Description>
<PackageProjectUrl>https://github.com/natenho/Mockaco</PackageProjectUrl>
<RepositoryUrl>https://github.com/natenho/Mockaco</RepositoryUrl>
<PackageTags>mock http server</PackageTags>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageIcon>mockaco-icon.png</PackageIcon>
<Product>Mockaco</Product>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bogus" Version="29.0.2" />
<PackageReference Include="MAB.DotIgnore" Version="3.0.0" />
<PackageReference Include="MAB.DotIgnore" Version="3.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="3.6.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="3.1.6" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Polly" Version="7.2.1" />
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
Expand All @@ -29,6 +45,10 @@
<None Update="Plugins\Readme.md">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Resources\mockaco-icon.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Mockaco/Options/TemplateFileProviderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class TemplateFileProviderOptions

public TemplateFileProviderOptions()
{
Path = "Mocks";
Path = string.Empty;
}
}
}
17 changes: 16 additions & 1 deletion src/Mockaco/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;

namespace Mockaco
Expand All @@ -27,7 +31,18 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureAppConfiguration((_, configuration) => configuration.AddJsonFile("Settings/appsettings.json", optional: true, reloadOnChange: true))
webBuilder.ConfigureAppConfiguration((_, configuration) =>
{
var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
configuration.SetBasePath(Path.Combine(assemblyLocation, "Settings"));

var switchMappings = new Dictionary<string, string>() {
{"--path", "Mockaco:TemplateFileProvider:Path" },
{"--logs", "Serilog:WriteTo:0:Args:path" }
};

configuration.AddCommandLine(args, switchMappings);
})
.UseSerilog((context, loggerConfiguration) => loggerConfiguration.ReadFrom.Configuration(context.Configuration))
.UseStartup<Startup>();
});
Expand Down
Binary file added src/Mockaco/Resources/mockaco-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions src/Mockaco/Settings/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Mockaco": {
"TemplateFileProvider": {
"Path": "Mocks"
}
}
}
3 changes: 3 additions & 0 deletions src/Mockaco/Settings/appsettings.Production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Urls": "http://127.0.0.1:0"
}
5 changes: 1 addition & 4 deletions src/Mockaco/Settings/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
"ErrorHttpStatusCode": "NotImplemented",
"DefaultHttpContentType": "application/json",
"References": [],
"Imports": [],
"TemplateFileProvider": {
"Path": "Mocks"
}
"Imports": []
},
"AllowedHosts": "*",
"Serilog": {
Expand Down
2 changes: 1 addition & 1 deletion src/Mockaco/Startup.Banner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public partial class Startup
{
private const string _banner =
private const string _logo =
" .x000000000000000000000d. \n" +
" .kMW0OkdooooooooooooooookNWd. \n" +
" ... .kMNOdoc;;;;;;;;;;;;;;;;;;;:xNWx. \n" +
Expand Down
9 changes: 6 additions & 3 deletions src/Mockaco/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.IO;
using System.Reflection;

namespace Mockaco
Expand All @@ -12,15 +15,15 @@ public partial class Startup

public Startup(IConfiguration configuration)
{
_configuration = configuration;
_configuration = configuration;
}

public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache()
.AddHttpClient()
.AddCors()
.AddOptions()
.AddOptions()

.Configure<MockacoOptions>(_configuration.GetSection("Mockaco"))
.Configure<TemplateFileProviderOptions>(_configuration.GetSection("Mockaco:TemplateFileProvider"))
Expand Down Expand Up @@ -58,7 +61,7 @@ public void ConfigureServices(IServiceCollection services)
public void Configure(IApplicationBuilder app, ILogger<Startup> logger)
{
AssemblyName assemblyName = Assembly.GetExecutingAssembly().GetName();
logger.LogInformation($"{assemblyName.Name} v{assemblyName.Version} by Renato Lima [github.com/natenho]\n\n{_banner}");
logger.LogInformation("{assemblyName} v{assemblyVersion} by Renato Lima [github.com/natenho]\n\n{logo}", assemblyName.Name, assemblyName.Version, _logo);

app.UseCors(configurePolicy => configurePolicy.AllowAnyOrigin())
.UseMiddleware<ErrorHandlingMiddleware>()
Expand Down
12 changes: 7 additions & 5 deletions src/Mockaco/Templating/Providers/TemplateFileProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MAB.DotIgnore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.FileProviders.Physical;
Expand All @@ -22,12 +23,13 @@ public sealed class TemplateFileProvider : ITemplateProvider, IDisposable
private const string DefaultTemplateSearchPattern = "*.json";

private readonly ILogger<TemplateFileProvider> _logger;

private readonly IMemoryCache _memoryCache;
private PhysicalFileProvider _fileProvider;
private CancellationTokenSource _resetCacheToken = new CancellationTokenSource();

public TemplateFileProvider(IOptionsMonitor<TemplateFileProviderOptions> options, IMemoryCache memoryCache, ILogger<TemplateFileProvider> logger)
{
public TemplateFileProvider(IOptionsMonitor<TemplateFileProviderOptions> options, IWebHostEnvironment webHostEnvironment, IMemoryCache memoryCache, ILogger<TemplateFileProvider> logger)
{
_memoryCache = memoryCache;
_logger = logger;

Expand All @@ -52,14 +54,14 @@ private void SetMockRootPath(string path)
var fullPath = Path.IsPathRooted(path)
? path
: Path.Combine(Directory.GetCurrentDirectory(), path);

var fileProvider = new PhysicalFileProvider(fullPath, ExclusionFilters.Hidden | ExclusionFilters.System);

_fileProvider?.Dispose();
_fileProvider = fileProvider;

_logger.LogInformation("Mock root path: {fullPath}", fullPath);
}
_logger.LogInformation("Mock path: {fullPath}", fullPath);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error setting mock root path");
Expand Down

0 comments on commit fecd4b7

Please sign in to comment.