Compare commits

17 Commits

Author SHA1 Message Date
fa30099991 update ui-assets.nix
All checks were successful
checks / test (push) Has been skipped
assets1 / test (push) Has been skipped
checks-impure / test (push) Has been skipped
2023-11-08 21:02:45 +00:00
b4c657501e Merge pull request 'README: Fixed missing direnv install step' (#13) from Luis-main into main
All checks were successful
checks-impure / test (push) Successful in 25s
checks / test (push) Successful in 1m32s
assets1 / test (push) Successful in 49s
Reviewed-on: #13
2023-11-08 21:47:57 +01:00
c4388733e5 Fix forgetting to delete api folder in UI 2023-11-08 21:47:57 +01:00
105b6f0b35 Merge pull request 'Improved README' (#12) from Luis-main into main
All checks were successful
assets1 / test (push) Successful in 22s
checks-impure / test (push) Successful in 24s
checks / test (push) Successful in 1m49s
Reviewed-on: #12
2023-11-01 16:11:31 +01:00
1f895a0668 README: Fixed missing direnv install step 2023-11-01 16:11:31 +01:00
adcca39dc9 Merge pull request 'Improved README' (#11) from Luis-main into main
All checks were successful
checks-impure / test (push) Successful in 26s
checks / test (push) Successful in 1m38s
assets1 / test (push) Successful in 24s
Reviewed-on: #11
2023-10-31 13:32:24 +01:00
637921e722 nix fmt
All checks were successful
checks-impure / test (pull_request) Successful in 26s
checks / test (pull_request) Successful in 1m31s
2023-10-31 13:24:10 +01:00
44f12945b8 Improved README 2023-10-31 13:24:10 +01:00
c0a5743502 update ui-assets.nix
All checks were successful
checks-impure / test (push) Has been skipped
checks / test (push) Has been skipped
assets1 / test (push) Has been skipped
2023-10-30 16:41:11 +00:00
48bc94a5de Merge pull request 'nix fmt' (#9) from Luis-main into main
All checks were successful
checks-impure / test (push) Successful in 24s
checks / test (push) Successful in 2m54s
assets1 / test (push) Successful in 48s
Reviewed-on: #9
2023-10-30 17:32:34 +01:00
1621b22c1c Fixed missing gnused in ui-assets.sh 2023-10-30 17:32:34 +01:00
1e9817dea2 Merge pull request 'nix fmt' (#8) from Luis-main into main
Some checks failed
checks-impure / test (push) Successful in 25s
checks / test (push) Successful in 2m43s
assets1 / test (push) Failing after 48s
Reviewed-on: #8
2023-10-30 17:26:29 +01:00
494067899e Generate ui assets package_name on the fly 2023-10-30 17:26:29 +01:00
b95194890d nix fmt 2023-10-30 17:26:29 +01:00
8c1c050ba3 Merge pull request 'Improved README' (#7) from Luis-main into main
Some checks failed
checks-impure / test (push) Successful in 26s
checks / test (push) Failing after 3m19s
assets1 / test (push) Successful in 22s
Reviewed-on: #7
2023-10-30 17:14:37 +01:00
55f252af92 update ui-assets.nix
All checks were successful
checks / test (push) Has been skipped
assets1 / test (push) Has been skipped
checks-impure / test (push) Has been skipped
2023-10-30 16:01:42 +00:00
81553a3bc6 Merge pull request 'Improved README and ui-asset workflow' (#6) from Luis-main into main
All checks were successful
checks-impure / test (push) Successful in 25s
checks / test (push) Successful in 1m28s
assets1 / test (push) Successful in 49s
Reviewed-on: #6
2023-10-30 16:59:11 +01:00
4 changed files with 185 additions and 95 deletions

270
README.md
View File

@@ -1,121 +1,211 @@
# Website Template
This repository is a template to build high quality websites as a team.
The frontend uses [React NextJS
](https://nextjs.org/) and the backend uses Python with the [Fastapi framework](https://fastapi.tiangolo.com/). To ensure API compatibility between frontend and backend an `openapi.json` file is generated from the Python backend code, which defines the REST API. This `openapi.json` file is then fed into [Orval](https://orval.dev/), which generates Typescript bindings for the Rest API. To ensure code correctness, we use [mypy](https://mypy-lang.org/) to ensure the Python code is correctly statically typed, and [pytest](https://docs.pytest.org/en/7.4.x/) for backend tests. A Continuos Integration (CI) Bot, verifies the code with previously mentioned Quality Assurance (QA) tools and blocks Pull requests if any errors arise.
For dependency management we use the [Nix package manager](https://nixos.org/) to ensure reproducibility.
Welcome to our website template repository! This template is designed to help you and your team build high-quality websites efficiently. We've carefully chosen the technologies to make development smooth and enjoyable. Here's what you can expect from this template:
## Getting Started: Development Environment
**Frontend**: Our frontend is powered by [React NextJS](https://nextjs.org/), a popular and versatile framework for building web applications.
1. Install the Nix package manager by [downloading the nix installer](https://github.com/DeterminateSystems/nix-installer/releases) or executing this command:
**Backend**: For the backend, we use Python along with the [FastAPI framework](https://fastapi.tiangolo.com/). To ensure seamless communication between the frontend and backend, we generate an `openapi.json` file from the Python code, which defines the REST API. This file is then used with [Orval](https://orval.dev/) to generate TypeScript bindings for the REST API. We're committed to code correctness, so we use [mypy](https://mypy-lang.org/) to ensure that our Python code is statically typed correctly. For backend testing, we rely on [pytest](https://docs.pytest.org/en/7.4.x/).
```bash
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
```
**Continuous Integration (CI)**: We've set up a CI bot that rigorously checks your code using the quality assurance (QA) tools mentioned above. If any errors are detected, it will block pull requests until they're resolved.
2. Install direnv by [downloading the direnv package](https://direnv.net/docs/installation.html) or executing this command:
**Dependency Management**: We use the [Nix package manager](https://nixos.org/) to manage dependencies and ensure reproducibility, making your development process more robust.
```bash
curl -sfL https://direnv.net/install.sh | bash
```
## Supported Operating Systems
3. Clone the repository and cd into it
4. You should see an error message reading like this:
- Linux
- macOS
```bash
direnv: error .envrc is blocked. Run `direnv allow` to approve its content
```
# Getting Started with the Development Environment
5. Execute `direnv allow` to allow automatically executing the shell script `.envrc` on entering the directory
6. Go to `pkgs/clan-cli` and execute
Let's get your development environment up and running:
```bash
direnv allow
```
1. **Install Nix Package Manager**:
Then wait for the backend to build
7. To start the backend server then execute:
- You can install the Nix package manager by either [downloading the Nix installer](https://github.com/DeterminateSystems/nix-installer/releases) or running this command:
```bash
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
```
```
clan webui --reload --no-open --log-level debug
```
2. **Install direnv**:
The server will automatically restart if any Python file changes.
8. In a different shell go to `pkgs/ui` and execute
- Download the direnv package from [here](https://direnv.net/docs/installation.html) or run the following command:
```bash
curl -sfL https://direnv.net/install.sh | bash
```
```bash
direnv allow
```
3. **Add direnv to your shell**:
Then wait for the frontend to build.
9. To start the frontend, execute:
```bash
npm run dev
```
Visit the website by going to [http://localhost:3000](http://localhost:3000)
## Getting started: Setup Git Workflow
1. Register your Gitea account locally by executing
```bash
tea login add
```
You will then see a prompt, please fill it out like outlined below:
```
? URL of Gitea instance: https://gitea.gchq.icu
? Name of new Login [gitea.gchq.icu]: gitea.gchq.icu:7171
? Do you have an access token? No
? Username: MyUserName
? Password: **********
? Set Optional settings: No
```
2. First add your changes to git:
1. `git add <file1> <file2>` your changes
2. Execute `nix fmt` to lint your files
3. `git commit -a -m "My descriptive commit message"`
4. Make sure your branch has the latest changes from upstream by executing:
- Direnv needs to [hook into your shell](https://direnv.net/docs/hook.html) to work.
You can do this by executing following command:
```bash
git fetch && git rebase origin/main --autostash
echo 'eval "$(direnv hook zsh)"' >> ~/.zshrc && echo 'eval "$(direnv hook bash)"' >> ~/.bashrc && eval "$SHELL"
```
5. Execute `git status` to see if you have a merge conflict.
6. If so edit the file and fix the conflict. Here is a tutorial how to do so in [vscode](https://code.visualstudio.com/docs/sourcecontrol/overview#_merge-conflicts)
7. Execute `git merge --continue` and repeat step 5 till there are no conflicts anymore
4. **Clone the Repository and Navigate**:
3. To automatically open up a pull request, that gets merged if all tests pass execute:
- Clone this repository and navigate to it.
5. **Allow .envrc**:
- When you enter the directory, you'll receive an error message like this:
```bash
direnv: error .envrc is blocked. Run `direnv allow` to approve its content
```
- Execute `direnv allow` to automatically execute the shell script `.envrc` when entering the directory.
6. **Build the Backend**:
- Go to the `pkgs/clan-cli` directory and execute:
```bash
direnv allow
```
- Wait for the backend to build.
7. **Start the Backend Server**:
- To start the backend server, execute:
```bash
clan webui --reload --no-open --log-level debug
```
- The server will automatically restart if any Python files change.
8. **Build the Frontend**:
- In a different shell, navigate to the `pkgs/ui` directory and execute:
```bash
direnv allow
```
- Wait for the frontend to build.
9. **Start the Frontend**:
- To start the frontend, execute:
```bash
npm run dev
```
- Access the website by going to [http://localhost:3000](http://localhost:3000).
# Setting Up Your Git Workflow
Let's set up your Git workflow to collaborate effectively:
1. **Register Your Gitea Account Locally**:
- Execute the following command to add your Gitea account locally:
```bash
tea login add
```
- Fill out the prompt as follows:
- URL of Gitea instance: `https://gitea.gchq.icu`
- Name of new Login [gitea.gchq.icu]: `gitea.gchq.icu:7171`
- Do you have an access token? No
- Username: YourUsername
- Password: YourPassword
- Set Optional settings: No
2. **Git Workflow**:
1. Add your changes to Git using `git add <file1> <file2>`.
2. Run `nix fmt` to lint your files.
3. Commit your changes with a descriptive message: `git commit -a -m "My descriptive commit message"`.
4. Make sure your branch has the latest changes from upstream by executing:
```bash
git fetch && git rebase origin/main --autostash
```
5. Use `git status` to check for merge conflicts.
6. If conflicts exist, resolve them. Here's a tutorial for resolving conflicts in [VSCode](https://code.visualstudio.com/docs/sourcecontrol/overview#_merge-conflicts).
7. After resolving conflicts, execute `git merge --continue` and repeat step 5 until there are no conflicts.
3. **Create a Pull Request**:
- To automatically open a pull request that gets merged if all tests pass, execute:
```bash
merge-after-ci
```
4. **Review Your Pull Request**:
- Visit https://gitea.gchq.icu and go to the project page. Check under "Pull Requests" for any issues with your pull request.
5. **Push Your Changes**:
- If there are issues, fix them and redo step 2. Afterward, execute:
```bash
git push origin HEAD:YourUsername-main
```
- This will directly push to your open pull request.
# Debugging
When working on the backend of your project, debugging is an essential part of the development process. Here are some methods for debugging and testing the backend of your application:
## Test Backend Locally in Devshell with Breakpoints
To test the backend locally in a development environment and set breakpoints for debugging, follow these steps:
1. Run the following command to execute your tests and allow for debugging with breakpoints:
```bash
pytest -n0 -s --maxfail=1
```
You can place `breakpoint()` in your Python code where you want to trigger a breakpoint for debugging.
## Test Backend Locally in a Nix Sandbox
To run your backend tests in a Nix sandbox, you have two options depending on whether your test functions have been marked as impure or not:
### Running Tests Marked as Impure
If your test functions need to execute `nix build` and have been marked as impure because you can't execute `nix build` inside a Nix sandbox, use the following command:
```bash
merge-after-ci
nix run .#impure-checks
```
4. Go to https://gitea.gchq.icu to the project page, and look under "Pull Requests" if there are any issues with it.
5. If there are issues, fix them and redo step 2. Afterwards execute
This command will run the impure test functions.
```
git push origin HEAD:MyUserName-main
### Running Pure Tests
For test functions that have not been marked as impure and don't require executing `nix build`, you can use the following command:
```bash
nix build .#checks.x86_64-linux.clan-pytest --rebuild
```
to directly push to your open pull request
This command will run all pure test functions.
## Using this template
### Inspecting the Nix Sandbox
Setup two new gitea accounts.
One named `ui-asset-bot`, generate an access token for it with all access permissions and set under `settings/actions/secrets` a secret called `BOT_ACCESS_TOKEN` with the token.
Also edit the file `.gitea/workflows/ui_assets.yaml` and change the `BOT_EMAIL` variable to the email you set for that account.
The second account is called `merge-bot` edit the file `pkgs/merge-after-ci/default.nix`
if the name should be different.
Under Branches set the main branch to protected and add `merge-bot` to whitelisted users for pushing.
Also set an unprotected file pattern to `**/ui-assets.nix`.
Also set the option `Enable Status Check` to `build / test (pull_request)`
Add `merge-bot` and `ui-asset-bot` as collaborators.
Also set the option `Delete pull request branch after merge by default`
Also the the default merge style to `Rebase then create merge commit`
If you need to inspect the Nix sandbox while running tests, follow these steps:
1. Insert an endless sleep into your test code where you want to pause the execution. For example:
```python
import time
time.sleep(3600) # Sleep for one hour
```
2. Use `cntr` and `psgrep` to attach to the Nix sandbox. This allows you to interactively debug your code while it's paused. For example:
```bash
cntr exec -w your_sandbox_name
psgrep -a -x your_python_process_name
```
These debugging and testing methods will help you identify and fix issues in your backend code efficiently, ensuring the reliability and robustness of your application.
# Using this Template
To make the most of this template:
1. Set up a new Gitea account named `ui-asset-bot`. Generate an access token with all access permissions and set it under `settings/actions/secrets` as a secret called `BOT_ACCESS_TOKEN`.
- Also, edit the file `.gitea/workflows/ui_assets.yaml` and change the `BOT_EMAIL` variable to match the email you set for that account. Gitea matches commits to accounts by their email address, so this step is essential.
2. Create a second Gitea account named `merge-bot`. Edit the file `pkgs/merge-after-ci/default.nix` if the name should be different. Under "Branches," set the main branch to be protected and add `merge-bot` to the whitelisted users for pushing. Set the unprotected file pattern to `**/ui-assets.nix`.
- Enable the status check for "build / test (pull_request)."
3. Add both `merge-bot` and `ui-asset-bot` as collaborators.
- Set the option to "Delete pull request branch after merge by default."
- Also, set the default merge style to "Rebase then create merge commit."
With this template, you're well-equipped to build and collaborate on high-quality websites efficiently. Happy coding!.

View File

@@ -1,5 +1,5 @@
{ fetchzip }:
fetchzip {
url = "https://gitea.gchq.icu/api/packages/ui-asset-bot/generic/ui/1inlxk1i9q87bw00zwqyvpm3m0hdgdffhxlymzgi5wvqn9syd0cm/assets.tar.gz";
sha256 = "1inlxk1i9q87bw00zwqyvpm3m0hdgdffhxlymzgi5wvqn9syd0cm";
url = "https://gitea.gchq.icu/api/packages/ui-asset-bot/generic/Luis-nextjs-python-web-template/0wq3lxsnsr9jcjqfhmxvjzxr4zz46aimwj1m23n7zzc835b47d70/assets.tar.gz";
sha256 = "0wq3lxsnsr9jcjqfhmxvjzxr4zz46aimwj1m23n7zzc835b47d70";
}

View File

@@ -17,7 +17,7 @@ if [[ -z "${GITHUB_SERVER_URL:-}" ]]; then
exit 1
fi
DEPS=$(nix shell --inputs-from '.#' "nixpkgs#gnutar" "nixpkgs#curl" "nixpkgs#gzip" -c bash -c "echo \$PATH")
DEPS=$(nix shell --inputs-from '.#' "nixpkgs#gnutar" "nixpkgs#gnused" "nixpkgs#curl" "nixpkgs#gzip" -c bash -c "echo \$PATH")
export PATH=$PATH:$DEPS
@@ -32,7 +32,7 @@ tar --transform 's,^\.,assets,' -czvf "$tmpdir/assets.tar.gz" -C "$tmpdir"/resul
NAR_HASH=$(nix-prefetch-url --unpack file://<(cat "$tmpdir/assets.tar.gz"))
owner=$BOT_NAME
package_name=ui
package_name=$(echo -n "$GITHUB_REPOSITORY" | sed 's/\//-/g')
package_version=$NAR_HASH
baseurl=$GITHUB_SERVER_URL

View File

@@ -25,7 +25,7 @@ pkgs.mkShell {
# re-generate the api code
rm -rf api openapi.json
rm -rf src/api openapi.json
cp ${clanPkgs.clan-openapi}/openapi.json .
orval
'';