diff --git a/.gitignore b/.gitignore index f1e3d20e0..3ab113252 100644 --- a/.gitignore +++ b/.gitignore @@ -250,3 +250,4 @@ paket-files/ # JetBrains Rider .idea/ *.sln.iml +pub/ diff --git a/NuGet.config b/NuGet.config new file mode 100644 index 000000000..1fc25b9aa --- /dev/null +++ b/NuGet.config @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100755 index 000000000..7c127a31b --- /dev/null +++ b/build.sh @@ -0,0 +1,8 @@ +#!/bin/sh +#dotnet restore +rm -rf ./pub +dotnet publish "$(pwd)/src/Services/Catalog/Catalog.API/project.json" -o "$(pwd)/pub/catalog" +dotnet publish "$(pwd)/src/Web/Microsoft.eShopOnContainers.WebMVC/project.json" -o "$(pwd)/pub/webMVC" + +docker build -t eshop/web "$(pwd)/pub/webMVC" +docker build -t eshop/catalog.api "$(pwd)/pub/catalog" \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..72142bd45 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,21 @@ +version: '2' + +services: + webmvc: + image: eshop/web + environment: + - CatalogUrl=http://catalog.api + ports: + - "80:80" + depends_on: + - catalog.api + catalog.api: + image: eshop/catalog.api + environment: + - ConnectionString=Server=catalogdata;Port=5432;Database=postgres;username=postgres + expose: + - "80" + depends_on: + - catalogdata + catalogdata: + image: glennc/eshopdata \ No newline at end of file diff --git a/eShopOnContainers.sln b/eShopOnContainers.sln index 4f2b08979..15cfefba5 100644 --- a/eShopOnContainers.sln +++ b/eShopOnContainers.sln @@ -7,7 +7,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{932D8224-11F EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3AF739CD-81D8-428D-A08A-0A58372DEBF6}" ProjectSection(SolutionItems) = preProject + docker-compose.yml = docker-compose.yml global.json = global.json + NuGet.config = NuGet.config EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Services", "Services", "{91CF7717-08AB-4E65-B10E-0B426F01E2E8}" diff --git a/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs b/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs new file mode 100644 index 000000000..f703c2944 --- /dev/null +++ b/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.eShopOnContainers.Services.Catalog.API.Model; + +namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers +{ + [Route("/")] + public class CatalogController : ControllerBase + { + private CatalogContext _context; + + public CatalogController(CatalogContext context) + { + _context = context; + } + + // GET api/values + [HttpGet] + public IEnumerable Get() + { + return _context.CatalogItems.ToList(); + } + + // GET api/values/5 + [HttpGet("{id}")] + public IActionResult Get(Guid id) + { + var item = _context.CatalogItems.FirstOrDefault(x=> x.Id == id); + + if(item == null) + { + return NotFound(); + } + + return new OkObjectResult(item); + } + + // POST api/values + [HttpPost] + public IActionResult Post([FromBody]CatalogItem item) + { + try + { + _context.CatalogItems.Add(item); + _context.SaveChanges(); + return Ok(); + } + catch + { + return StatusCode(500, "Unable to add new catalog item"); + } + } + + // PUT api/values/5 + [HttpPut("{id}")] + public IActionResult Put(int id, [FromBody]CatalogItem item) + { + _context.CatalogItems.Update(item); + _context.SaveChanges(); + return Ok(); + } + + // DELETE api/values/5 + [HttpDelete("{id}")] + public IActionResult Delete(Guid id) + { + return Ok(); + } + } +} diff --git a/src/Services/Catalog/Catalog.API/Controllers/ValuesController.cs b/src/Services/Catalog/Catalog.API/Controllers/ValuesController.cs deleted file mode 100644 index e8b1a90c3..000000000 --- a/src/Services/Catalog/Catalog.API/Controllers/ValuesController.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; - -namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers -{ - [Route("api/[controller]")] - public class ValuesController : Controller - { - // GET api/values - [HttpGet] - public IEnumerable Get() - { - return new string[] { "value1", "value2" }; - } - - // GET api/values/5 - [HttpGet("{id}")] - public string Get(int id) - { - return "value"; - } - - // POST api/values - [HttpPost] - public void Post([FromBody]string value) - { - } - - // PUT api/values/5 - [HttpPut("{id}")] - public void Put(int id, [FromBody]string value) - { - } - - // DELETE api/values/5 - [HttpDelete("{id}")] - public void Delete(int id) - { - } - } -} diff --git a/src/Services/Catalog/Catalog.API/Dockerfile b/src/Services/Catalog/Catalog.API/Dockerfile new file mode 100644 index 000000000..99490a7c3 --- /dev/null +++ b/src/Services/Catalog/Catalog.API/Dockerfile @@ -0,0 +1,5 @@ +FROM microsoft/aspnetcore +WORKDIR /app +EXPOSE 80 +ADD . /app +ENTRYPOINT dotnet Catalog.API.dll \ No newline at end of file diff --git a/src/Services/Catalog/Catalog.API/Model/CatalogContext.cs b/src/Services/Catalog/Catalog.API/Model/CatalogContext.cs new file mode 100644 index 000000000..c8fa53cf6 --- /dev/null +++ b/src/Services/Catalog/Catalog.API/Model/CatalogContext.cs @@ -0,0 +1,23 @@ +using Microsoft.EntityFrameworkCore; +using Npgsql.EntityFrameworkCore.PostgreSQL; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Microsoft.eShopOnContainers.Services.Catalog.API.Model +{ + public class CatalogContext : DbContext + { + public CatalogContext(DbContextOptions options): base(options) + { + } + + public DbSet CatalogItems { get; set; } + + protected override void OnModelCreating(ModelBuilder builder) + { + builder.HasPostgresExtension("uuid-ossp"); + } + } +} diff --git a/src/Services/Catalog/Catalog.API/Model/CatalogItem.cs b/src/Services/Catalog/Catalog.API/Model/CatalogItem.cs new file mode 100644 index 000000000..abc3f91af --- /dev/null +++ b/src/Services/Catalog/Catalog.API/Model/CatalogItem.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Microsoft.eShopOnContainers.Services.Catalog.API.Model +{ + public class CatalogItem + { + public CatalogItem() + { + } + + public Guid Id { get; set; } + public string Name { get; set; } + public string Description { get; set; } + public decimal Price { get; set; } + public int ImageCount { get; set; } + } +} diff --git a/src/Services/Catalog/Catalog.API/Program.cs b/src/Services/Catalog/Catalog.API/Program.cs index 5e8b75301..82ca9383e 100644 --- a/src/Services/Catalog/Catalog.API/Program.cs +++ b/src/Services/Catalog/Catalog.API/Program.cs @@ -15,7 +15,6 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() .UseStartup() .Build(); diff --git a/src/Services/Catalog/Catalog.API/Project_Readme.html b/src/Services/Catalog/Catalog.API/Project_Readme.html deleted file mode 100644 index 1a0f5b51a..000000000 --- a/src/Services/Catalog/Catalog.API/Project_Readme.html +++ /dev/null @@ -1,187 +0,0 @@ - - - - - Welcome to ASP.NET Core - - - - - - - - - - diff --git a/src/Services/Catalog/Catalog.API/Properties/launchSettings.json b/src/Services/Catalog/Catalog.API/Properties/launchSettings.json index 25361b864..b123021c8 100644 --- a/src/Services/Catalog/Catalog.API/Properties/launchSettings.json +++ b/src/Services/Catalog/Catalog.API/Properties/launchSettings.json @@ -10,8 +10,6 @@ "profiles": { "IIS Express": { "commandName": "IISExpress", - "launchBrowser": true, - "launchUrl": "api/values", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } @@ -23,6 +21,10 @@ "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } + }, + "Docker": { + "launchBrowser": true, + "launchUrl": "http://localhost:{ServicePort}/api/values" } } } \ No newline at end of file diff --git a/src/Services/Catalog/Catalog.API/Startup.cs b/src/Services/Catalog/Catalog.API/Startup.cs index 773e0ef05..80485de5d 100644 --- a/src/Services/Catalog/Catalog.API/Startup.cs +++ b/src/Services/Catalog/Catalog.API/Startup.cs @@ -7,6 +7,8 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using Microsoft.eShopOnContainers.Services.Catalog.API.Model; +using Microsoft.EntityFrameworkCore; namespace Microsoft.eShopOnContainers.Services.Catalog.API { @@ -16,8 +18,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) + .AddJsonFile("settings.json") .AddEnvironmentVariables(); Configuration = builder.Build(); } @@ -27,8 +28,13 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + services.AddDbContext(c => { + c.UseNpgsql(Configuration["ConnectionString"]); + }); + // Add framework services. - services.AddMvc(); + services.AddMvcCore() + .AddJsonFormatters(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/src/Services/Catalog/Catalog.API/project.json b/src/Services/Catalog/Catalog.API/project.json index af6281beb..563e4d136 100644 --- a/src/Services/Catalog/Catalog.API/project.json +++ b/src/Services/Catalog/Catalog.API/project.json @@ -1,25 +1,21 @@ -{ +{ "dependencies": { "Microsoft.NETCore.App": { "version": "1.0.0", "type": "platform" }, "Microsoft.AspNetCore.Mvc": "1.0.0", - "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", "Microsoft.AspNetCore.Server.Kestrel": "1.0.0", + "Microsoft.EntityFrameworkCore": "1.0.0", "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0", - "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0", "Microsoft.Extensions.Configuration.Json": "1.0.0", "Microsoft.Extensions.Logging": "1.0.0", "Microsoft.Extensions.Logging.Console": "1.0.0", "Microsoft.Extensions.Logging.Debug": "1.0.0", - "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0" + "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0", + "Npgsql.EntityFrameworkCore.PostgreSQL": "1.0.1" }, - - "tools": { - "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final" - }, - + "tools": {}, "frameworks": { "netcoreapp1.0": { "imports": [ @@ -28,29 +24,26 @@ ] } }, - "buildOptions": { "emitEntryPoint": true, - "preserveCompilationContext": true + "preserveCompilationContext": true, + "debugType": "portable" }, - "runtimeOptions": { "configProperties": { "System.GC.Server": true } }, - "publishOptions": { "include": [ "wwwroot", "Views", "Areas/**/Views", - "appsettings.json", - "web.config" + "settings.json", + "web.config", + "project.json", + "Dockerfile" ] }, - - "scripts": { - "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] - } -} + "scripts": {} +} \ No newline at end of file diff --git a/src/Services/Catalog/Catalog.API/appsettings.json b/src/Services/Catalog/Catalog.API/settings.json similarity index 65% rename from src/Services/Catalog/Catalog.API/appsettings.json rename to src/Services/Catalog/Catalog.API/settings.json index fa8ce71a9..e4f7496f5 100644 --- a/src/Services/Catalog/Catalog.API/appsettings.json +++ b/src/Services/Catalog/Catalog.API/settings.json @@ -1,4 +1,5 @@ { + "ConnectionString": "Server=127.0.0.1;Port=5432;Database=postgres;username=postgres", "Logging": { "IncludeScopes": false, "LogLevel": { diff --git a/src/Services/Catalog/Catalog.API/web.config b/src/Services/Catalog/Catalog.API/web.config index dc0514fca..e04a0397b 100644 --- a/src/Services/Catalog/Catalog.API/web.config +++ b/src/Services/Catalog/Catalog.API/web.config @@ -1,14 +1,9 @@  - - - - + - + - + \ No newline at end of file diff --git a/src/Web/Microsoft.eShopOnContainers.WebMVC/AppSettings.cs b/src/Web/Microsoft.eShopOnContainers.WebMVC/AppSettings.cs new file mode 100644 index 000000000..f76d22dba --- /dev/null +++ b/src/Web/Microsoft.eShopOnContainers.WebMVC/AppSettings.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Microsoft.eShopOnContainers.WebMVC +{ + public class AppSettings + { + public Connectionstrings ConnectionStrings { get; set; } + public string CatalogUrl { get; set; } + public Logging Logging { get; set; } + } + + public class Connectionstrings + { + public string DefaultConnection { get; set; } + } + + public class Logging + { + public bool IncludeScopes { get; set; } + public Loglevel LogLevel { get; set; } + } + + public class Loglevel + { + public string Default { get; set; } + public string System { get; set; } + public string Microsoft { get; set; } + } +} diff --git a/src/Web/Microsoft.eShopOnContainers.WebMVC/Controllers/HomeController.cs b/src/Web/Microsoft.eShopOnContainers.WebMVC/Controllers/HomeController.cs index f2dc170d6..9aa982a23 100644 --- a/src/Web/Microsoft.eShopOnContainers.WebMVC/Controllers/HomeController.cs +++ b/src/Web/Microsoft.eShopOnContainers.WebMVC/Controllers/HomeController.cs @@ -2,15 +2,29 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using System.Net.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.eShopOnContainers.WebMVC.Models; +using Microsoft.Extensions.Options; +using Newtonsoft.Json; namespace Microsoft.eShopOnContainers.WebMVC.Controllers { public class HomeController : Controller { - public IActionResult Index() + private HttpClient _http; + private AppSettings _settings; + + public HomeController(IOptions options) { - return View(); + _http = new HttpClient(); + _settings = options.Value; + } + public async Task Index() + { + var dataString = await _http.GetStringAsync(_settings.CatalogUrl); + var items = JsonConvert.DeserializeObject>(dataString); + return View(items); } public IActionResult About() diff --git a/src/Web/Microsoft.eShopOnContainers.WebMVC/Dockerfile b/src/Web/Microsoft.eShopOnContainers.WebMVC/Dockerfile new file mode 100644 index 000000000..5d638a542 --- /dev/null +++ b/src/Web/Microsoft.eShopOnContainers.WebMVC/Dockerfile @@ -0,0 +1,7 @@ +FROM microsoft/dotnet:1.0.0-core +ARG source=. +WORKDIR /app +ENV ASPNETCORE_URLS http://*:80 +EXPOSE 80 +COPY $source . +ENTRYPOINT dotnet Microsoft.eShopOnContainers.WebMVC.dll diff --git a/src/Web/Microsoft.eShopOnContainers.WebMVC/Models/CatalogItem.cs b/src/Web/Microsoft.eShopOnContainers.WebMVC/Models/CatalogItem.cs new file mode 100644 index 000000000..8643fffe9 --- /dev/null +++ b/src/Web/Microsoft.eShopOnContainers.WebMVC/Models/CatalogItem.cs @@ -0,0 +1,12 @@ +using System; + +namespace Microsoft.eShopOnContainers.WebMVC.Models +{ + public class CatalogItem + { + public Guid Id { get; set; } + public string Name { get; set; } + public string Description { get; set; } + public decimal Price { get; set; } + } +} \ No newline at end of file diff --git a/src/Web/Microsoft.eShopOnContainers.WebMVC/Project_Readme.html b/src/Web/Microsoft.eShopOnContainers.WebMVC/Project_Readme.html deleted file mode 100644 index 1a0f5b51a..000000000 --- a/src/Web/Microsoft.eShopOnContainers.WebMVC/Project_Readme.html +++ /dev/null @@ -1,187 +0,0 @@ - - - - - Welcome to ASP.NET Core - - - - - - - - - - diff --git a/src/Web/Microsoft.eShopOnContainers.WebMVC/Properties/launchSettings.json b/src/Web/Microsoft.eShopOnContainers.WebMVC/Properties/launchSettings.json index 9cc22bcd1..1078a2bf0 100644 --- a/src/Web/Microsoft.eShopOnContainers.WebMVC/Properties/launchSettings.json +++ b/src/Web/Microsoft.eShopOnContainers.WebMVC/Properties/launchSettings.json @@ -22,6 +22,10 @@ "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } + }, + "Docker": { + "launchBrowser": true, + "launchUrl": "http://localhost:{ServicePort}" } } } \ No newline at end of file diff --git a/src/Web/Microsoft.eShopOnContainers.WebMVC/Startup.cs b/src/Web/Microsoft.eShopOnContainers.WebMVC/Startup.cs index 100aace69..e9ff825aa 100644 --- a/src/Web/Microsoft.eShopOnContainers.WebMVC/Startup.cs +++ b/src/Web/Microsoft.eShopOnContainers.WebMVC/Startup.cs @@ -52,6 +52,8 @@ namespace Microsoft.eShopOnContainers.WebMVC // Add application services. services.AddTransient(); services.AddTransient(); + + services.Configure(Configuration); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/src/Web/Microsoft.eShopOnContainers.WebMVC/Views/Home/Index.cshtml b/src/Web/Microsoft.eShopOnContainers.WebMVC/Views/Home/Index.cshtml index 3cd243dda..e2bf357e5 100644 --- a/src/Web/Microsoft.eShopOnContainers.WebMVC/Views/Home/Index.cshtml +++ b/src/Web/Microsoft.eShopOnContainers.WebMVC/Views/Home/Index.cshtml @@ -1,109 +1,16 @@ @{ ViewData["Title"] = "Home Page"; + @model IEnumerable } -