This is a template to set up a backend ASP.NET Core project with React frontend. You could check it out at https://template.mcc-soft.de (credentials: admin/BSjo1M8jU760).
Developers are encouraged to start with setting up the environment and proceed to development how-to.
Template contains (you could also check CHANGELOG for latest changes):
- ASP.NET Core backend (.NET 7)
- OpenIddict authentication (with support for Social Networks authentication)
- Guidance/example on how to structure backend
- Helpers for easy implementation of REST API for CRUD (with PATCH instead of UPDATE)
- Configured Domain events to apply DDD principles
- Localization (i18next), background jobs (Hangfire), etc.
- Audit logging for DB entities
- Logging via Serilog to:
- OpenSearch (credentials: backend-frontend-template/$a55IVHBhAJ@)
- Loggly (free up to 250MB/day). You could check out logs yourself (credentials: mcc.template.app@gmail.com/dBAeFm7_y5mq3)
- React frontend
- CRA-based template
- API client autogeneration (including react-query hooks)
- UI Kit to simplify app restyling
- Guidance/examples on setting up forms (react-hook-form), tables (with sorting/paging) and filters
- Support for injecting environment variables (without rebuilding the app)
- Redux with persistence
- Routing with react-router
- Error-boundaries which works with suspense/react-query/code splitting
- Pre-built authentication UI
- Build-script to build a single Docker container from backend and frontend
- package.json based build script (just run
yarn build
from repo root)
- package.json based build script (just run
- Azure-pipelines CI script which
- Builds Docker Image
- Uploads it to Container Registry (on Digital Ocean)
- Deploys Image to preconfigured agent
- Instructions about configuring an agent to deploy aforementioned Docker Image on any linux PC (Digital Ocean prefered :))
- Kubernetes deployment files and guidance regarding deploy to kubernetes (in progress)
Before using the template for your brand-new app run yarn rename -n YourProjectName
. Additionally you can use -c YourCompanyName
to change solution's root namespace. Use CamelCase for your project name. The script does the following (you could check the script and do this manually if you like to):
- Rename all files/folders from 'TemplateApp' to your project name.
- Change all namespaces, classnames, database names, dockerfiles and deployment scripts to suit your project
- Adjust SPA port number to something random, so that it doesn't clash with other projects (in package.json: 'start' script, PORT=3011; in main app .csproj: `http://localhost:3011/)
Create a new git repository and copy everything (except .git
folder) to it. Don't forget to change the favicons/tiles in frontend/public
folder to the logo of your project (use https://realfavicongenerator.net/ to create favicons in all required sizes)
- Check that connection string in appsettings is correct. Default connection string assumes that you have Postgres running on localhost on standard port with user/pass: postgres/postgres. Adjust connection string if the assumption is wrong.
- Make sure the target database exists (or that the postgres user has rights to create databases).
- Run backend (Open
webapi/MccSoft.TemplateApp.sln
in Rider/Visual Studio and hitF5
) - Run frontend (
yarn start
fromfrontend
folder) - Frontend will open up in browser. Use that url to test the app!
- (To authorize, you could use a preconfigured user. Login/Password is in
appsettings.json
underDefaultUser
section)
- First of all, you need to create an email for your project. It will be needed for registering in 3rd party systems that the project will use (e.g. Sentry and Loggly)
- Go to Azure and create Pipeline. Specify
.ci/azure-pipelines.yml
as the source of pipeline. - Give access for WRITING to Repo for your Pipeline. This is required for Tagging your sources when App is deployed. Instructions: https://elanderson.net/2020/04/azure-devops-pipelines-manual-tagging/
- You could skip this step if git tags are not needed. In this case remove
TagProdSources
andTagDevSources
from .ci/azure-pipelines.yml
- You could skip this step if git tags are not needed. In this case remove
- Set up Container Registry to push images to.
- You could use GitLab Container Registry (since it's private and free)
- Register a user in GitLab (using project email)
- Create a personal access token with read/write access to container registry at https://gitlab.com/-/profile/personal_access_tokens
- Add secret variable
DOCKER_TOKEN
to a pipeline containing created token - Adjust
DOCKER_REGISTRY
(e.g.registry.gitlab.com/mcctemplateapp1/main
) andDOCKER_USER
(e.g.mcc.template.app@gmail.com
) variables in pipeline.
- You could use GitLab Container Registry (since it's private and free)
- Run your pipeline. The first Stage (build) should pass.
- Disable Pipeline notifications in Azure (i.e. 'Run stage waiting for approval' and 'Manual validation Pending') https://dev.azure.com/mccsoft/TemplateApp/_settings/notifications. Also disable them in your personal profile: https://dev.azure.com/mccsoft/_usersSettings/notifications
- Pipeline contains 2 stages for deploying to DEV and PROD. You could add new deployment stages by copying existing once.
- Create a virtual machine that will host your project.
- The preferable way is to create a Droplet on Digital Ocean.
- Create an account in DO (or use your existing account, you don't need to use a project email here), then create a new Team for new project (or ask your manager to do so and invite you to the Team)
- Create new droplet on Digital ocean, use 'Docker' as image, generate random secure password.
- Alternatively, set up any linux machine with root access. Install
docker
anddocker-compose
.- To install
docker
follow instructions: - To install
docker-compose
(at least 1.29.0 is required to use profiles):sudo apt install pip && pip install docker-compose;
- To install
- Write down the passwords somewhere in WIKI
- The preferable way is to create a Droplet on Digital Ocean.
- SSH into droplet/VM and run the following (line by line):
apt install mc adduser azure (generate and remember the password) usermod -aG sudo azure sudo visudo (add the following as the last line: `azure ALL=(ALL) NOPASSWD: ALL`, click Ctrl+X, save the changes). It will disable password prompt for azure user. su -l azure
- Go to Azure DevOps, Pipelines -> Environment. Add Environment (name it 'DEV'), add virtual machine (Linux). Copy the script and run inside Droplet under
azure
user account (if you'd like to deploy more than one project to the same Droplet you could change $HOSTNAME in the script to something project-specific). - You should see your droplet in Environments in Azure.
- (If it's a PROD environment, you could add Approval Checks in Azure Dev Ops (so that only manager could deploy to PROD))
- Generate configuration (by running
./GenerateDotEnv.ps1
fromscripts
folder. File.env.base
will be generated for you - Copy generated
env.base
file to HOME directory ofazure
user. - Adjust the file (change the Email, VirtualHost, Sentry and Loggly URLs).
- (You could also grab Postgres password from this file to connect to Postgres later from your PC)
- Optional: ONLY if you used Digital Ocean container registry. Make sure that root user has access to Container Registry which stores the images.
- To do that (in case of Digital Ocean Container Registry) run the following:
sudo snap install doctl sudo snap connect doctl:dot-docker doctl auth init -t (INSERT DIGITAL OCEAN API TOKEN HERE) doctl registry login
- To do that (in case of Digital Ocean Container Registry) run the following: