Skip to content

Work in progress c# Net Core Microservices application for learning purpose

Notifications You must be signed in to change notification settings

openddd-com/MyMicroservicesApplication

 
 

Repository files navigation

ASP NET Core / Docker / SPA - Microservices Example application


Work in progress

alt text

How to run the solution:

$ docker-compose -c docker-compose.dev.yml build
$ docker-compose -c docker-compose.dev.yml up

or build run and detach

$ docker-compose -c docker-compose.dev.yml up --build -d

Navigate to http://your-docker-host-name-or-ip/

Authentication:

I used Json Web Token with public/private key signature (RSA256) to keep the users authenticated RFC doc.

First, SPA retrieve from the authentication service an access token. It'll be expire in one week (7 days). Every hour and every time the user open the web application a new token will be retrieved from the token renew endpoint. This strategy seems acceptable for a web application.

Initial incpit from this discussion.

Database:

I use MySQL to keep users informations running on the 'db' container with a mapping volume on the host machine.

dotnet-ef migrations to database versioning.

Requirements:

For each aspnet core service you may need to restore packages:

$ dotnet restore

And build the project:

$ dotnet publish

You have to generate a private/public keys for JWT:

openssl genrsa -des3 -out private.pem 2048
openssl rsa -in private.pem -outform PEM -pubout -out public.pem

See this link

Place the private and public keys in the 'keys' folder into the services base folder. The private key is needed only by the authorization service.

And generate the HTTPS certificate:

openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt

See this link

Install this certificate in your dev computer.

Setup AWS credentials into a file (MyMicroservicesApplication\Services\Orders\Orders.Application\aws.dev\credentials): Guide

For Authorization, Orders and Catalog services you need to initialize the mySQL schema. Use the dotnet utility for each service, type the following commands from the project folders:

dotnet ef database -v update

Notes about the frontend:

The frontend is a single page application built by angular-cli, I changed the ng serve command in the package.json file to accept two configuration:

.1 Run the webpack-dev-server with a local backend services (ex. if you want to debug a service locally with the frontend as client). Configure the proxy with the desired redirects for the routes, for example I want to debug the login page using the authorization service run locally:

  {
    "/api/token": {
        "target": "https://localhost:443",
        "secure": false,
        "changeOrigin": true,
        "logLevel": "debug"
    }
  }

(proxy.config.local.json)

$ npm run start

.2 Or run the webpack-dev-server with the backend run on the docker host

$ npm run start-docker

(see proxy.config.docker.json)

Others options are the same from angular-cli docs

How to debug a remote container:

For example, to debug the auth_service: look at the Dockerfile.debug version. I added the sshd support. Then you can attach remotely over a ssh tunnel with your ide. Notes the port mapping '2222:22' to avoid conflicts with the host's ssh server.

Use the correct version of docker-compose file to overwrite the configurations, like:

$ docker-compose -c docker-compose.dev.yml -c docker-compose.debug.yml up --build -d

Execute, on the docker host the ssh server of the container

$ docker exec -it <container-id> "/usr/sbin/sshd"  

Copy the ssh key to the docker container, look into the 'scripts' folder. Remember that you can reach the ssh server of the container through the docker-host port mapped (2222 in this case).

$ ssh-copy-id -p 2222 -i your_public_key.pub -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" root@<docker-host-ip>

If you are using vs code, you can now use the launch.json settings to attach to the remote container and select the correct process (example configuration).

{
    "version": "0.2.0",
    "configurations": [  
    {
        "name": ".NET Core Remote Attach",
        "type": "coreclr",
        "request": "attach",
        "processId": "${command:pickRemoteProcess}",
        "pipeTransport": {
            "pipeCwd": "${workspaceRoot}",
            "pipeProgram": "ssh",
            "pipeArgs": ["-p", "2222", "-i", "<your-.ssh-path>/id_rsa_clrdbg", "-T", "root@<docker-host-ip>"],
            "debuggerPath": "/root/vsdbg/vsdbg",
            "quoteArgs": true
        },
        "sourceFileMap": {
                "<your-solution-folder>": "${workspaceRoot}"
        }
    }]
}

About

Work in progress c# Net Core Microservices application for learning purpose

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • CSS 74.2%
  • C# 21.7%
  • TypeScript 3.2%
  • Other 0.9%