Skip to content

Commit

Permalink
feat: [sc-25514] split NameGuard code into separate package / app (#414)
Browse files Browse the repository at this point in the history
* move nameguard to packages

* update workflows

* update pnpm lock

* fix path

* fix path

* update pnpm lock

* update

* increase Lambda timeout

* increase timeout

* fix image paths for pypi

* rename workflow files

* Apply updated file name to docs

---------

Co-authored-by: djstrong <[email protected]>
Co-authored-by: lightwalker.eth <[email protected]>
  • Loading branch information
3 people authored Oct 23, 2024
1 parent 9f15fdf commit 17458b4
Show file tree
Hide file tree
Showing 140 changed files with 185 additions and 79 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/nameguard-api-lambda-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ on:
- main
- staging
paths:
- "api/**"
- "packages/nameguard-python/**"
- "apps/api.nameguard.io/**"
- ".github/workflows/nameguard-api-lambda-deploy.yml"
workflow_dispatch:

Expand Down Expand Up @@ -74,7 +75,7 @@ jobs:
echo "Deployment is only supported for main and staging branches"
exit 1
fi
working-directory: api
working-directory: apps/api.nameguard.io

- name: Delete old images from ECR
env:
Expand All @@ -85,7 +86,7 @@ jobs:
IMAGES_TO_DELETE=$(aws ecr describe-images --repository-name $ECR_REPO --query 'sort_by(imageDetails,& imagePushedAt)[*].imageDigest' --filter "tagStatus=UNTAGGED" --output json | jq '.[0]')
if [[ $len > 5 ]]; then aws ecr batch-delete-image --repository-name $ECR_REPO --image-ids imageDigest=$IMAGES_TO_DELETE; fi
working-directory: api
working-directory: apps/api.nameguard.io

notify:
name: Send Slack deployment event notification
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/nameguard-end-to-end-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ on:
- staging
paths:
- "packages/nameguard-sdk/**"
- "api/nameguard/**"
- "packages/nameguard-python/**"
- ".github/workflows/nameguard-end-to-end-tests.yml"
pull_request:
branches:
- main
- staging
paths:
- "packages/nameguard-sdk/**"
- "api/nameguard/**"
- "packages/nameguard-python/**"
- ".github/workflows/nameguard-end-to-end-tests.yml"

jobs:
Expand All @@ -31,7 +31,7 @@ jobs:
id: check
run: |
git fetch origin ${{ github.base_ref }}
API_CHANGED=$(git diff --name-only origin/${{ github.base_ref }} HEAD | grep "^api/nameguard/" | wc -l)
API_CHANGED=$(git diff --name-only origin/${{ github.base_ref }} HEAD | grep "^packages/nameguard-python/" | wc -l)
SDK_CHANGED=$(git diff --name-only origin/${{ github.base_ref }} HEAD | grep "^packages/nameguard-sdk/" | wc -l)
echo "api_changed=$API_CHANGED" >> $GITHUB_OUTPUT
echo "sdk_changed=$SDK_CHANGED" >> $GITHUB_OUTPUT
Expand Down Expand Up @@ -68,15 +68,15 @@ jobs:

- name: Install dependencies
if: needs.check_changes.outputs.api_changed > 0
working-directory: ./api
working-directory: ./packages/nameguard-python
run: poetry install --with dev

- name: Build the SDK
run: pnpm build

- name: Setup local nameguard api
if: needs.check_changes.outputs.api_changed > 0
working-directory: ./api
working-directory: ./packages/nameguard-python
env:
PROVIDER_URI_MAINNET: ${{ secrets.PROVIDER_URI_MAINNET }}
PROVIDER_URI_SEPOLIA: ${{ secrets.PROVIDER_URI_SEPOLIA }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ jobs:
python-version: "3.11"

- name: Install pip dependencies
working-directory: ./api
working-directory: ./packages/nameguard-python
run: |
python -m pip install --upgrade pip
pip install build
- name: Build NameGuard package
working-directory: ./api
working-directory: ./packages/nameguard-python
run: python -m build

- name: Publish NameGuard package
uses: pypa/gh-action-pypi-publish@release/v1.8
with:
packages-dir: api/dist/
packages-dir: packages/nameguard-python/dist/
user: __token__
password: ${{ secrets.PYPI_TOKEN }}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ on:
branches:
- main
paths:
- "api/**"
- "packages/nameguard-python/**"
pull_request:
branches:
- main
- staging
paths:
- "api/**"
- "packages/nameguard-python/**"

env:
PROVIDER_URI_MAINNET: ${{ secrets.PROVIDER_URI_MAINNET }}
Expand Down Expand Up @@ -42,15 +42,15 @@ jobs:
cache: "poetry"

- name: Install dependencies
working-directory: ./api
working-directory: ./packages/nameguard-python
run: poetry install

- name: Run tests mocked
working-directory: ./api
working-directory: ./packages/nameguard-python
run: poetry run pytest -vv --durations=0

- name: Run tests
working-directory: ./api
working-directory: ./packages/nameguard-python
run: MONKEYPATCH=0 poetry run pytest -vv --durations=0

add_badge:
Expand Down Expand Up @@ -78,15 +78,15 @@ jobs:
cache: "poetry"

- name: Install dependencies
working-directory: ./api
working-directory: ./packages/nameguard-python
run: poetry install

- name: Run tests mocked
working-directory: ./api
working-directory: ./packages/nameguard-python
run: poetry run pytest -vv --durations=0 --cov-report term-missing --cov=./

- name: Generate coverage badge
working-directory: ./api
working-directory: ./packages/nameguard-python
run: poetry run coverage-badge -fo coverage_badge.svg

- name: Commit changes
Expand All @@ -95,4 +95,4 @@ jobs:
author_name: github-actions
author_email: [email protected]
message: "Update coverage badge"
add: "api/coverage_badge.svg"
add: "packages/nameguard-python/coverage_badge.svg"
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ repos:
hooks:
- id: ruff
args: [ --fix ]
files: '^api/.*\.py'
files: '^packages/nameguard-python/.*\.py'
- id: ruff-format
files: '^api/.*\.py'
files: '^packages/nameguard-python/.*\.py'
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
<img src="https://img.shields.io/github/actions/workflow/status/namehash/namekit/typescript-packages-unit-tests.yml?logo=typescript&logoColor=ffffff&color=444444" alt="TypeScript Build Status">
</picture>
</a>
<a href="https://github.com/namehash/namekit/actions/workflows/nameguard-api-unit-tests.yml?query=branch%3Amain">
<a href="https://github.com/namehash/namekit/actions/workflows/nameguard-python-unit-tests.yml?query=branch%3Amain">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://img.shields.io/github/actions/workflow/status/namehash/namekit/nameguard-api-unit-tests.yml?logo=python&logoColor=ffffff&color=444444">
<img src="https://img.shields.io/github/actions/workflow/status/namehash/namekit/nameguard-api-unit-tests.yml?logo=python&logoColor=ffffff&color=444444" alt="Python Build Status">
<source media="(prefers-color-scheme: dark)" srcset="https://img.shields.io/github/actions/workflow/status/namehash/namekit/nameguard-python-unit-tests.yml?logo=python&logoColor=ffffff&color=444444">
<img src="https://img.shields.io/github/actions/workflow/status/namehash/namekit/nameguard-python-unit-tests.yml?logo=python&logoColor=ffffff&color=444444" alt="Python Build Status">
</picture>
</a>
<a href="#project-status">
Expand Down
File renamed without changes.
90 changes: 90 additions & 0 deletions apps/api.nameguard.io/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# NameGuard Python

![Tests](https://github.com/namehash/namekit/actions/workflows/ci_api.yml/badge.svg?branch=main)
![Coverage](coverage_badge.svg)

This repository contains the NameGuard AWS Lambda deployment.

## Getting Started

### Using the public API

NameGuard is hosted at <https://api.nameguard.io>

You can make a basic request to the API like this:

```bash
curl https://api.nameguard.io/inspect-name/mainnet/nick.eth
```

The API documentation is available at <https://api.nameguard.io/redoc> or <https://api.nameguard.io/docs>.

### Running your own NameGuard instance

#### Env variables

```bash
AWS_ROLE - AWS Role used by GitHub actions to create the CloudFormation infrastructure for deploying NameGuard as an AWS Lambda and pushing the latest build image to AWS ECR.
SLACK_WEBHOOK_URL - Slack webhook url used by GitHub actions to send notifications of deployment success or failure to the dev team's slack channel.
```
#### Installing the library
NameGuard is available as a Python library on [PyPI](https://pypi.org/project/nameguard/). You can install it with `pip`:
```bash
pip install nameguard
```
#### Setting Provider URIs
NameGuard uses the specified Provider endpoint (e.g. Alchemy, Infura, your own Ethereum node, etc...) for `secure-primary-name/`. Provider endpoints have to be set by environment variables, e.g.:
```bash
export PROVIDER_URI_MAINNET=https://eth-mainnet.g.alchemy.com/v2/[YOUR_ALCHEMY_API_KEY]
export PROVIDER_URI_SEPOLIA=https://eth-sepolia.g.alchemy.com/v2/[YOUR_ALCHEMY_API_KEY]
export ALCHEMY_URI_MAINNET=https://eth-mainnet.g.alchemy.com/v2/[YOUR_ALCHEMY_API_KEY]
export ALCHEMY_URI_SEPOLIA=https://eth-sepolia.g.alchemy.com/v2/[YOUR_ALCHEMY_API_KEY]
export ENS_SUBGRAPH_URL_MAINNET="https://gateway-arbitrum.network.thegraph.com/api/[YOUR_SUBGRAPH_API_KEY]/subgraphs/id/5XqPmWe6gjyrJtFn9cLy237i4cWw2j9HcUJEXsP5qGtH"
export ENS_SUBGRAPH_URL_SEPOLIA="https://gateway-arbitrum.network.thegraph.com/api/[YOUR_SUBGRAPH_API_KEY]/subgraphs/id/DmMXLtMZnGbQXASJ7p1jfzLUbBYnYUD9zNBTxpkjHYXV"
```
#### Starting the web server
A FastAPI application is included in the `nameguard.web_api` module. The default installation from PyPI does not include an ASGI server, so you will need to install one separately. For example, to install [uvicorn](https://www.uvicorn.org):
```bash
pip install 'uvicorn[standard]'
```
You can start the web server with:
```bash
uvicorn nameguard.web_api:app
```
Make an example request:
```bash
curl -d '{"name":"nick.eth", "network_name": "mainnet"}' -H "Content-Type: application/json" -X POST http://localhost:8000/inspect-name
# {
# "rating": "pass",
# "risk_count": 0,
# "highest_risk": null,
# "name": "nick.eth",
# "namehash": "0x05a67c0ee82964c4f7394cdd47fee7f4d9503a23c09c38341779ea012afe6e00",
# "normalization": "normalized",
# "checks": [...],
# "labels": [...],
# "canonical_name": "nick.eth",
# "title": "Looks Good",
# "subtitle": "All security checks passed!",
# "beautiful_name": "nick.eth"
# }
```
## License
Licensed under the MIT License, Copyright © 2023-present [NameHash Labs](https://namehashlabs.org).
See [LICENSE](./LICENSE) for more information.
4 changes: 2 additions & 2 deletions api/package.json → apps/api.nameguard.io/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"NameHash"
],
"scripts": {
"deploy:prod": "DOCKER_CONFIG=./.docker serverless deploy --stage prod",
"deploy:staging": "DOCKER_CONFIG=./.docker serverless deploy --stage staging"
"deploy:prod": "DOCKER_CONFIG=../../packages/nameguard-python/.docker serverless deploy --stage prod",
"deploy:staging": "DOCKER_CONFIG=../../packages/nameguard-python/.docker serverless deploy --stage staging"
},
"dependencies": {
"serverless": "3.39.0"
Expand Down
4 changes: 2 additions & 2 deletions api/serverless.yml → apps/api.nameguard.io/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ provider:
ecr:
images:
oss-nameguard:
path: ./
path: ../../packages/nameguard-python/
platform: linux/arm64

plugins:
Expand All @@ -39,7 +39,7 @@ functions:
name: oss-nameguard
name: oss-nameguard-${self:custom.stage}
memorySize: 1769
timeout: 10
timeout: 60
url: true
role: DefaultNameGuardRole
provisionedConcurrency: 1
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions packages/nameguard-python/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 NameHash

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 3 additions & 3 deletions api/README.md → packages/nameguard-python/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# NameGuard Python

![Tests](https://github.com/namehash/namekit/actions/workflows/ci_api.yml/badge.svg?branch=main)
![Coverage](coverage_badge.svg)
![Tests](https://github.com/namehash/namekit/actions/workflows/nameguard-python-unit-tests.yml/badge.svg?branch=main)
![Coverage](https://github.com/namehash/namekit/raw/packages/nameguard-python/coverage_badge.svg)

This repository contains the core logic for NameGuard, a python library, web API server, and AWS Lambda handler.
This repository contains the core logic for NameGuard, a python library and web API server.

## Getting Started

Expand Down
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ def test_bulk_inspect_name_post_long(test_client):
assert len(name) == MAX_INSPECTED_NAME_CHARACTERS

names = [name] * MAX_NUMBER_OF_NAMES_IN_BULK
response = test_client.post('/bulk-inspect-names', json={'names': names, 'network_name': 'mainnet'})
response = test_client.post('/bulk-inspect-names', json={'names': names, 'network_name': 'mainnet'}, timeout=60)
assert response.status_code == 200
res_json = response.json()
for x in res_json['results']:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 17458b4

Please sign in to comment.