Skip to content

Commit

Permalink
Merge pull request #1053 from autonomys/feat/add-api
Browse files Browse the repository at this point in the history
Add api
  • Loading branch information
marc-aurele-besner authored Jan 9, 2025
2 parents cfd6723 + ab0b14e commit 2c9c439
Show file tree
Hide file tree
Showing 59 changed files with 7,201 additions and 84 deletions.
6 changes: 4 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ HASURA_GRAPHQL_CORS_DOMAIN="*"
DICTIONARY_DIRECTORY=autonomys
# Testnet - Taurus
# DICTIONARY_DIRECTORY=autonomys-taurus
DICTIONARY_SUBQUERY_NODE_PORT=3000
DICTIONARY_URL="http://localhost:3000"
DICTIONARY_SUBQUERY_NODE_PORT=3010
DICTIONARY_URL="http://localhost:3010"

REDIS_PORT=6379

Expand All @@ -48,6 +48,8 @@ BULL_PASSWORD=board
BULL_PORT=3020
BULL_SESSION_SECRET=keyboardcat

API_PORT=3030

NR_API_KEY=""
NR_AGENT_IDENTIFIER=""

Expand Down
29 changes: 27 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ services:
environment:
ENDPOINT: ${RPC_URLS}
CHAIN_ID: ${CHAIN_ID}
DICTIONARY: http://localhost:3000
DICTIONARY: ${DICTIONARY_URL}
DB_USER: ${DB_USER}
DB_PASS: ${DB_PASSWORD}
DB_DATABASE: ${DB_DATABASE}
Expand Down Expand Up @@ -290,7 +290,7 @@ services:
environment:
ENDPOINT: ${RPC_URLS}
CHAIN_ID: ${CHAIN_ID}
DICTIONARY: http://localhost:3000
DICTIONARY: ${DICTIONARY_URL}
DB_USER: ${DB_USER}
DB_PASS: ${DB_PASSWORD}
DB_DATABASE: ${DB_DATABASE}
Expand Down Expand Up @@ -409,3 +409,28 @@ services:

SLACK_TOKEN: ${SLACK_TOKEN}
SLACK_CONVERSATION_ID: ${SLACK_CONVERSATION_ID}

# NestJS API Service
api:
build:
context: ./indexers/api
dockerfile: Dockerfile
depends_on:
postgres:
condition: service_healthy
hasura:
condition: service_started
restart: unless-stopped
ports:
- "${API_PORT}:3000"
environment:
NODE_ENV: ${NODE_ENV}
DB_HOST: ${DB_HOST}
DB_PORT: ${DB_PORT}
DB_USER: ${DB_USER}
DB_PASSWORD: ${DB_PASSWORD}
DB_DATABASE: ${DB_DATABASE}
hostname: api
volumes:
- ./indexers/api:/app
- /app/node_modules
25 changes: 25 additions & 0 deletions indexers/api/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
56 changes: 56 additions & 0 deletions indexers/api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# compiled output
/dist
/node_modules
/build

# Logs
logs
*.log
npm-debug.log*
pnpm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# OS
.DS_Store

# Tests
/coverage
/.nyc_output

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# temp directory
.temp
.tmp

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
4 changes: 4 additions & 0 deletions indexers/api/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
15 changes: 15 additions & 0 deletions indexers/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM node:18-alpine

WORKDIR /app

COPY package*.json ./

RUN yarn install

COPY . .

RUN yarn build

EXPOSE 3000

CMD ["yarn", "start:prod"]
64 changes: 64 additions & 0 deletions indexers/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Autonomys Network Astral API

![Autonomys Banner](https://github.com/autonomys/astral/blob/main/.github/images/autonomys-banner.webp)

[Astral](../README.md)

This is a public API for the Autonomys Network, built with NestJS and documented using Swagger. It provides endpoints for interacting with the network data.

## Table of content

- [🏄 Get Started](#get-started)
- [🔩 Requirements](#requirements)
- [🛳 Build](#build)
- [🔧 Environment Variables](#-environment-variables)
- [📜 API Documentation](#api-documentation)

## 🏄 Get Started

The application is built with NestJS and TypeScript. Follow these steps to set up your local development environment:

1. Clone the repository and navigate to the `api` directory:

```bash
git clone [email protected]:autonomys/astral.git
cd astral/api
```

2. If you use `nvm` to manage Node.js versions, ensure you're using the stable channel:

```bash
nvm use stable
```

3. Install the dependencies:

```bash
yarn install
```

4. To start the development server, run:

```bash
yarn start:dev
```

The server will run at [http://localhost:3000](http://localhost:3000).

## 🔩 Requirements

You will need to have the necessary environment variables set up to connect to the backend services.

## 🛳 Build

To create a production build, execute the following command:

```bash
yarn build
```

This will compile the project into an optimized build ready for deployment.

## 📜 API Documentation

The API is documented using Swagger. Once the server is running, you can access the API documentation at [http://localhost:3000/api-docs](http://localhost:3000/api-docs).
Binary file added indexers/api/assets/favicon.ico
Binary file not shown.
16 changes: 16 additions & 0 deletions indexers/api/nest-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true,
"assets": [
{
"include": "assets/**/*",
"outDir": "dist/assets",
"watchAssets": true
}
],
"watchAssets": true
}
}
90 changes: 90 additions & 0 deletions indexers/api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"name": "api",
"version": "0.0.1",
"description": "Autonomys Network Astral API",
"private": true,
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/autonomys/astral"
},
"author": {
"name": "Autonomys",
"url": "https://www.autonomys.xyz"
},
"bugs": {
"url": "https://github.com/autonomys/astral/issues"
},
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.3.0",
"@nestjs/core": "^10.0.0",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^8.1.0",
"@nestjs/typeorm": "^10.0.2",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"passport": "^0.7.0",
"passport-headerapikey": "^1.2.2",
"pg": "^8.13.1",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1",
"swagger-ui-express": "^5.0.1",
"typeorm": "^0.3.20"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/express": "^5.0.0",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@types/supertest": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^8.0.0",
"@typescript-eslint/parser": "^8.0.0",
"eslint": "^8.0.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.5.0",
"prettier": "^3.0.0",
"source-map-support": "^0.5.21",
"supertest": "^7.0.0",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.3",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
22 changes: 22 additions & 0 deletions indexers/api/src/app.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';

describe('AppController', () => {
let appController: AppController;

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();

appController = app.get<AppController>(AppController);
});

describe('root', () => {
it('should return "Hello Autonomys World!"', () => {
expect(appController.getHello()).toBe('Hello Autonomys World!');
});
});
});
12 changes: 12 additions & 0 deletions indexers/api/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}

@Get()
getHello(): string {
return this.appService.getHello();
}
}
Loading

0 comments on commit 2c9c439

Please sign in to comment.