add init demos
This commit is contained in:
24
server/code/root/light-code-demo/main.ts
Normal file
24
server/code/root/light-code-demo/main.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { QueryRouterServer } from "@kevisual/router";
|
||||
|
||||
const app = new QueryRouterServer();
|
||||
|
||||
app.route({
|
||||
path: 'main'
|
||||
}).define(async (ctx) => {
|
||||
ctx.body = {
|
||||
message: 'this is main. filename: root/light-code-demo/main.ts',
|
||||
params: ctx.query
|
||||
}
|
||||
}).addTo(app)
|
||||
|
||||
app.route({
|
||||
path: 'main2'
|
||||
}).define(async (ctx) => {
|
||||
ctx.body = {
|
||||
message: 'this is main2. filename: root/light-code-demo/main.ts',
|
||||
params: ctx.query
|
||||
}
|
||||
}).addTo(app)
|
||||
|
||||
|
||||
app.wait()
|
||||
46
server/code/root/light-code-demo/sign.ts
Normal file
46
server/code/root/light-code-demo/sign.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { QueryRouterServer as Mini } from "@kevisual/router";
|
||||
import { NocoApi } from "@kevisual/noco";
|
||||
const config = {
|
||||
NOCODB_URL: process.env.NOCODB_URL || 'https://nocodb.xiongxiao.me',
|
||||
NOCODB_API_KEY: process.env.NOCODB_API_KEY || 'uca1Zx3p_0pnNUBV6ot9mBP6JCPqQ0X1TF3N3R7s'
|
||||
}
|
||||
const table = 'mcby44q8zrayvn9'
|
||||
const nocoAPi = new NocoApi({
|
||||
baseURL: config.NOCODB_URL,
|
||||
token: config.NOCODB_API_KEY,
|
||||
table,
|
||||
});
|
||||
console.log('nocoAPi', await nocoAPi.record.list())
|
||||
const app = new Mini();
|
||||
|
||||
|
||||
app.route({
|
||||
path: 'sign'
|
||||
}).define(async (ctx) => {
|
||||
const { Title, Description } = ctx.query
|
||||
// 这里可以处理签到
|
||||
await nocoAPi.record.create({ Title, Description })
|
||||
const list = await nocoAPi.record.list({ sort: '-CreatedAt' })
|
||||
ctx.body = { message: '签到成功', list }
|
||||
}).addTo(app)
|
||||
|
||||
app.route({
|
||||
path: 'sign',
|
||||
key: 'list'
|
||||
}).define(async (ctx) => {
|
||||
// 这里可以处理签到
|
||||
ctx.body = await nocoAPi.record.list()
|
||||
}).addTo(app)
|
||||
|
||||
app.route({
|
||||
path: 'sign',
|
||||
key: 'delete'
|
||||
}).define(async (ctx) => {
|
||||
const { id } = ctx.query
|
||||
// 这里可以处理签到
|
||||
await nocoAPi.record.delete({ Id: id })
|
||||
const list = await nocoAPi.record.list({ sort: '-CreatedAt' })
|
||||
ctx.body = { message: '删除成功', list }
|
||||
}).addTo(app)
|
||||
|
||||
app.wait()
|
||||
56
server/code/root/light-code-demo/weather.ts
Normal file
56
server/code/root/light-code-demo/weather.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
const weatherHost = 'n65khufe5n.re.qweatherapi.com';
|
||||
const token = 'fdad5aeb2ba54949a8a1df2a0f3d1efb'
|
||||
const xihu = '101210113'; // 西湖
|
||||
export const getWeather = async (location: string = xihu) => {
|
||||
const url = `https://${weatherHost}/v7/weather/3d?location=${location}`;
|
||||
const headers = {
|
||||
'Authorization': `Bearer ${token}`
|
||||
};
|
||||
const res = await fetch(url, { headers });
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP error! status: ${res.status}`);
|
||||
}
|
||||
const data = await res.json();
|
||||
return data;
|
||||
}
|
||||
// getWeather().then(console.log).catch(console.error);
|
||||
|
||||
// https://dev.qweather.com/
|
||||
class Weather {
|
||||
host: string;
|
||||
token: string;
|
||||
constructor(opts: { host: string; token: string }) {
|
||||
this.host = opts.host;
|
||||
this.token = opts.token;
|
||||
console.log(this.host, this.token);
|
||||
}
|
||||
getWeather(location: string) {
|
||||
return fetch(`https://${this.host}/v7/weather/now?location=${location}`, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-QW-Api-Key': '<KEY>'.replace('<KEY>', this.token),
|
||||
},
|
||||
}).then((res) => res.json());
|
||||
}
|
||||
}
|
||||
const newWeather = new Weather({
|
||||
host: process.env?.QWEATHER_HOST || weatherHost,
|
||||
token: process.env?.QWEATHER_TOKEN || token,
|
||||
});
|
||||
|
||||
|
||||
// newWeather.getWeather(xihu).then(console.log).catch(console.error);
|
||||
|
||||
|
||||
import { QueryRouterServer as Mini } from "@kevisual/router";
|
||||
|
||||
const app = new Mini();
|
||||
|
||||
app.route({
|
||||
path: 'main'
|
||||
}).define(async (ctx) => {
|
||||
ctx.body = await newWeather.getWeather(xihu);
|
||||
}).addTo(app)
|
||||
|
||||
|
||||
app.wait()
|
||||
Reference in New Issue
Block a user