Skip to content

Commit

Permalink
Merge pull request #28 from AElfProject/feature/author
Browse files Browse the repository at this point in the history
Feature/author
  • Loading branch information
hzz780 authored Aug 12, 2024
2 parents 32d22a6 + 91a8050 commit a45d1ba
Show file tree
Hide file tree
Showing 66 changed files with 1,145 additions and 11,427 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dist-ssr

# lock
*-lock.yarl
*-lock.yaml
*-lock.json

apps/aelf-template/.next
7 changes: 7 additions & 0 deletions apps/aelf-template/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ yarn-error.log*

# local env files
.env*.local
.env

# vercel
.vercel
Expand All @@ -39,3 +40,9 @@ next-env.d.ts

# Sentry Config File
.env.sentry-build-plugin

# npm pack local test
package

# git ls-files > filelist.txt
filelist.txt
2 changes: 1 addition & 1 deletion apps/aelf-template/appsettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const APP_SETTINGS: IAppSettings = {
"serviceName": "create-aelf-dapp",
"serviceVersion": "v1.0",
// https://opentelemetry.io/docs/languages/sdk-configuration/otlp-exporter/
"collectorEndpoint": "http://localhost:8093/v1/traces",
"collectorEndpoint": "/v1/traces",
"tracerName": "create-aelf-dapp-tracer",
ignoreUrls: [
/localhost:8093\/sockjs-node/,
Expand Down
2 changes: 1 addition & 1 deletion apps/aelf-template/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ disableLogger: true,
// https://docs.sentry.io/product/crons/
// https://vercel.com/docs/cron-jobs
automaticVercelMonitors: true,
});
});
83 changes: 83 additions & 0 deletions apps/aelf-template/nginx.template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# 1. Prepare
```bash
brew install nginx

# turn to the directory of nginx
mkdir https
openssl req -nodes -new -x509 -keyout server.key -out server.crt
```

# 2. Config
```nginx
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /opt/homebrew/etc/nginx/https/server.crt;
ssl_certificate_key /opt/homebrew/etc/nginx/https/server.key;
# template
location / {
proxy_pass http://localhost:3005;
proxy_ssl_verify off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# https://github.com/AElfProject/backend-templates
location /v1/token {
proxy_pass https://localhost:44376/token;
proxy_ssl_verify off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# https://github.com/AElfProject/backend-templates
location /v1/api {
proxy_set_header host $host;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-forward-for $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_ssl_verify off;
proxy_pass https://localhost:44376/api;
}
# https://github.com/AElfProject/aelf-observability-setup
location /v1/traces {
proxy_set_header host $host;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-forward-for $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:4316/v1/traces;
}
# packages/server-socket-io
location /socket-io/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://localhost:3006;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
# packages/server-chaingpt
location /api/chaingpt {
proxy_set_header host $host;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-forward-for $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:3007/chaingpt;
}
}
```

# 3. Run
```bash
nginx -t
nginx -s reload
```
2 changes: 1 addition & 1 deletion apps/aelf-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"license": "MIT",
"scripts": {
"dev": "next dev -p 3005",
"dev": "NODE_OPTIONS='--trace-warnings' next dev -p 3005",
"build": "next build",
"start": "next start",
"lint": "next lint",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"use client"
import {createContext, ReactNode, useEffect, useState} from 'react';
import {RequestAllInOne} from 'request-all-in-one';
interface Props {
readonly children: ReactNode;
}
export const CSRFTokenContext = createContext<string>('');

export const CSRFTokenProvider = ({ children }: Props) => {
const [token, setToken] = useState<string>();
useEffect(() => {
const token = localStorage.getItem('csrfToken');
if (token) {
setToken(token);
}
const fetchToken = async () => {
const client = new RequestAllInOne({});
const response = await client.post('/v1/token');
if (response.code && response.code === '20000') {
localStorage.setItem('csrfToken', response.data);
setToken(response.data);
return response.data;
}
throw Error('fetch token error');
}
fetchToken().then(token => {
console.log('fetch token done', token);
}).catch(error => {
console.log('fetch token error: ', error);
});
}, []);

return <CSRFTokenContext.Provider value={token}>{children}</CSRFTokenContext.Provider>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { CSRFTokenProvider } from '@/app/demos/api-all-in-one/author/csrfTokenProvider';

export default function Layout({
children, // will be a page or nested layout
}: {
children: React.ReactNode
}) {
return <CSRFTokenProvider>{children}</CSRFTokenProvider>;
}
42 changes: 42 additions & 0 deletions apps/aelf-template/src/app/demos/api-all-in-one/author/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use client';
import {RequestAllInOne} from 'request-all-in-one';
import {useContext, useState} from 'react';
import {Button} from 'antd';
import {CSRFTokenContext} from '@/app/demos/api-all-in-one/author/csrfTokenProvider';

const client = new RequestAllInOne({});
export default function Author() {
const csrfToken = useContext(CSRFTokenContext);
console.log('csrfToken: ', csrfToken);
const [data, setData] = useState<any>('');

return <>
<div>
<Button onClick={async () => {
const response = await client.post('/v1/api/app/author', {
body: {
"name": "string",
"birthDate": "2024-08-08T10:55:44.335Z",
"shortBio": "string"
},
headers: {
RequestVerificationToken: csrfToken
}
});
setData(JSON.stringify(response));
}}>Post Author</Button>
<Button onClick={async () => {
const response = await client.get('/v1/api/app/author/620a13a3-0d9e-e900-2b28-3a143e37d66d');
setData(JSON.stringify(response));
}}>Get Author Info</Button>
<div>{data}</div>
</div>
<div>
<div>Request token and storage in local by default</div>
<Button onClick={async () => {
const response = await client.post('/token');
setData(JSON.stringify(response));
}}>Post to Get Token</Button>
</div>
</>;
}
7 changes: 4 additions & 3 deletions apps/aelf-template/src/app/demos/api-all-in-one/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Menu } from 'antd';
import Link from 'next/link';
import type { MenuProps } from 'antd';
// import type { MenuProps } from 'antd';

type MenuItem = Required<MenuProps>['items'][number];
// type MenuItem = Required<MenuProps>['items'][number];

const items: MenuItem[] = [
const items: ({ label: JSX.Element; key: string })[] = [
{ key: '1', label: <Link href="/demos/api-all-in-one/fetch">Fetch</Link> },
{ key: '2', label: <Link href="/demos/api-all-in-one/graphql">Graphql</Link> },
{ key: '3', label: <Link href="/demos/api-all-in-one/socket">Socket</Link> },
{ key: '4', label: <Link href="/demos/api-all-in-one/stream">Stream</Link> },
{ key: '5', label: <Link href="/demos/api-all-in-one/author">Author</Link> },
];
export default function APIAllInOneLayout({
children, // will be a page or nested layout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export default function Fetch() {
<div>
<Button
onClick={async () => {
await fetchEventSource('http://localhost:3100/api/chaingpt', {
// await fetchEventSource('http://localhost:3100/api/chaingpt', {
await fetchEventSource('/api/chaingpt', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const getData = (url: string) => new Promise((resolve, reject) => {
req.send();
});
// const URL_TEST = 'https://httpbin.org/get?trace=233333';
const URL_TEST = 'http://localhost:8093/sharp/api/app/book';
const URL_TEST = '/v1/sharp/api/app/book';
export default function Page() {
return <>
<Button onClick={() => {
Expand Down
4 changes: 3 additions & 1 deletion apps/aelf-template/src/app/demos/opentelemetry/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ const getData = (url: string) => new Promise((resolve, reject) => {
req.send();
});
// const URL_TEST = 'https://httpbin.org/get?trace=233333';
const URL_TEST = 'http://localhost:8093/sharp/api/app/book';
const URL_TEST = '/v1/api/app/book';
export default function Page() {
// Solution1: use useContext
// const webTracerWithZone = useContext(OpentelemetryContext);
// Solution2: use useOpentelemetry
const webTracerWithZone = useOpentelemetry();

return <>
Expand Down
2 changes: 1 addition & 1 deletion apps/aelf-template/src/app/demos/sentry/page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const getData = (url) => new Promise((resolve, reject) => {
req.send();
});
// const URL_TEST = 'https://httpbin.org/get';
const URL_TEST = 'http://localhost:8093/sharp/api/app/book';
const URL_TEST = '/v1/api/app/book';

export default function Page() {
return (
Expand Down
24 changes: 12 additions & 12 deletions apps/aelf-template/src/config/demo/configMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ export const menuList: IMenuItemData[] = [
label: 'ChainGPT',
href: '/demos/chaingpt',
},
{
label: 'socket',
href: '/demos/socket',
},
{
label: 'Graphql',
href: '/demos/graphql/server',
},
// {
// label: 'socket',
// href: '/demos/socket',
// },
// {
// label: 'Graphql',
// href: '/demos/graphql/server',
// },
{
label: 'API All in one',
href: '/demos/api-all-in-one',
Expand All @@ -69,8 +69,8 @@ export const menuList: IMenuItemData[] = [
label: 'opentelemetry',
href: '/demos/opentelemetry',
},
{
label: 'opentelemetry-request',
href: '/demos/opentelemetry-request',
},
// {
// label: 'opentelemetry-request',
// href: '/demos/opentelemetry-request',
// },
];
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v1
uses: actions/setup-node@v4
with:
node-version: '20' # Your node version, default 20 in 2024

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v1
uses: actions/setup-node@v4
with:
node-version: '20' # Your node version, default 20 in 2024

- name: Cache node modules
uses: actions/cache@v2
uses: actions/cache@v4
env:
cache-name: cache-node-modules
with:
Expand Down
9 changes: 9 additions & 0 deletions apps/create-aelf-dapp/lib/generator/templates/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
/.next/
/out/

/.idea/

# production
/build

Expand All @@ -27,6 +29,7 @@ yarn-error.log*

# local env files
.env*.local
.env

# vercel
.vercel
Expand All @@ -37,3 +40,9 @@ next-env.d.ts

# Sentry Config File
.env.sentry-build-plugin

# npm pack local test
package

# git ls-files > filelist.txt
filelist.txt
3 changes: 0 additions & 3 deletions apps/create-aelf-dapp/lib/generator/templates/.yarnrc.yml

This file was deleted.

Loading

0 comments on commit a45d1ba

Please sign in to comment.