Skip to content

Commit

Permalink
feat: added fastify to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
AngeloAyranji committed Mar 26, 2024
1 parent 919c2d2 commit 24a962e
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/backend/fastify/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
JUSTANAME_API_KEY=your_api_key
JUSTANAME_CHAIN_ID=your_chain_id
JUSTANAME_DOMAIN=your-ens-domain.eth
JUSTANAME_ORIGIN=yoursite.com
132 changes: 132 additions & 0 deletions examples/backend/fastify/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
### Node template
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

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

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

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

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

16 changes: 16 additions & 0 deletions examples/backend/fastify/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
'import', // Add this line
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:import/recommended', // Add this line
],
rules: {
// Add any additional rules or overrides here
}
};
1 change: 1 addition & 0 deletions examples/backend/fastify/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
21 changes: 21 additions & 0 deletions examples/backend/fastify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "justaname-fastify-example",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"fastify": "^3.0.0",
"@fastify/cors": "^3.0.0",
"@justaname.id/sdk": "^0.0.7"
},
"devDependencies": {
"@types/eslint": "^8",
"@typescript-eslint/eslint-plugin": "^7.3.1",
"@typescript-eslint/parser": "^7.3.1",
"concurrently": "^8.2.2",
"dotenv": "^16.4.5",
"eslint": "^8.57.0",
"eslint-plugin-import": "^2.29.1",
"nodemon": "^3.1.0"
}
}
37 changes: 37 additions & 0 deletions examples/backend/fastify/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "justaname-fastify",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "examples/backend/fastify/src",
"projectType": "application",
"targets": {
"build": {
"executor": "nx:run-commands",
"options": {
"command": "yarn build",
"cwd": "examples/backend/fastify"
}
},
"serve": {
"executor": "nx:run-commands",
"options": {
"command": "yarn serve",
"cwd": "examples/backend/fastify"
}
},
"lint": {
"executor": "nx:run-commands",
"options": {
"command": "yarn lint",
"cwd": "examples/backend/fastify"
}
},
"install": {
"executor": "nx:run-commands",
"options": {
"command": "yarn",
"cwd": "examples/backend/fastify"
}
}
},
"tags": []
}
103 changes: 103 additions & 0 deletions examples/backend/fastify/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import Fastify, { FastifyRequest, FastifyReply } from 'fastify';
import cors from '@fastify/cors';
import { JustaName } from '@justaname.id/sdk';
import dotenv from 'dotenv';

dotenv.config();

const fastify = Fastify({
logger: true,
});

fastify.register(cors);

interface RequestChallenge {
address: string;
}

interface SubnameAdd {
username: string;
address: string;
signature: string;
message: string;
}

const chainId = parseInt(process.env.JUSTANAME_CHAIN_ID ?? '');
const domain = process.env.JUSTANAME_DOMAIN ?? '';
const origin = process.env.JUSTANAME_ORIGIN ?? '';

if (!origin || !chainId || !domain || (chainId !== 1 && chainId !== 11155111)) {
console.error('Environment configuration is invalid');
process.exit(1);
}

let justaname: JustaName;

fastify.get('/api/request-challenge', async (request: FastifyRequest<{ Querystring: RequestChallenge }>, reply: FastifyReply) => {
const { address } = request.query;

if (!address) {
reply.send({ message: 'Address is required' });
return;
}

try {
const challenge = await justaname.siwe.requestChallenge({
chainId,
origin,
address,
domain,
});

reply.status(200).send(challenge);
} catch (error: any) {
reply.status(500).send({ error: error.message });
}
});

fastify.post<{ Body: SubnameAdd }>('/api/subnames/add', async (request: FastifyRequest<{ Body: SubnameAdd }>, reply: FastifyReply) => {
const { username, signature, address, message } = request.body;

if (!username) {
reply.send({ message: 'Username is required' });
return;
}

try {
const add = await justaname.subnames.addSubname(
{
username,
ensDomain: domain,
chainId,
},
{
xSignature: signature,
xAddress: address,
xMessage: message,
}
);

reply.status(201).send(add);
} catch (error: any) {
reply.status(500).send({ error: error.message });
}
});

fastify.get('/api', async (request: any, reply: FastifyReply) => {
reply.send({ message: 'Welcome to the server!' });
});

const start = async () => {
try {
justaname = await JustaName.init({
apiKey: process.env.JUSTANAME_API_KEY ?? '',
});

await fastify.listen({ port: parseInt(process.env.PORT ?? '3333'), host: '0.0.0.0' });
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};

start();
17 changes: 17 additions & 0 deletions examples/backend/fastify/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"outDir": "./dist",
"rootDir": "./src",
"skipLibCheck": true
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}

0 comments on commit 24a962e

Please sign in to comment.