-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from AElfProject/feature/author
Feature/author
- Loading branch information
Showing
66 changed files
with
1,145 additions
and
11,427 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ dist-ssr | |
|
||
# lock | ||
*-lock.yarl | ||
*-lock.yaml | ||
*-lock.json | ||
|
||
apps/aelf-template/.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
apps/aelf-template/src/app/demos/api-all-in-one/author/csrfTokenProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
}; |
9 changes: 9 additions & 0 deletions
9
apps/aelf-template/src/app/demos/api-all-in-one/author/layout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
apps/aelf-template/src/app/demos/api-all-in-one/author/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
</>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.