Skip to content

Commit

Permalink
sync backend code
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangcheng870518 committed May 31, 2023
1 parent 4cfb49b commit e9764de
Show file tree
Hide file tree
Showing 31 changed files with 353 additions and 39 deletions.
2 changes: 2 additions & 0 deletions packages/server/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ node_modules/

# PipyJS
config/pipy-codebases/
config/pjs/
config/website/
*.yaml

# Temp
Expand Down
6 changes: 3 additions & 3 deletions packages/server/config/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
const k8s = require('@kubernetes/client-node');

async function syncRegistries () {
async function syncRegistries() {
if (process.env.DISABLE_SYNCREGISTRIES) return;
const all = await strapi.db.query('api::registry.registry').findMany();
for (const data of all) {
Expand Down Expand Up @@ -83,7 +83,7 @@ async function syncOsm () {
if (!meshName) continue;
let ns = await strapi.db
.query('api::namespace.namespace')
.findOne({ where: { name: item.metadata.name, registry: registry.id }, populate: true});
.findOne({ where: { name: item.metadata.name, registry: registry.id }, populate: true });
if (!ns) {
ns = await strapi.db
.query('api::namespace.namespace')
Expand All @@ -101,7 +101,7 @@ async function syncOsm () {
if (mesh.bindNamespaces.indexOf(ns.id) == -1) {
mesh.bindNamespaces.push(ns.id);
await strapi.db.query('api::mesh.mesh')
.update({where: {id: mesh.id}, data: {bindNamespaces: mesh.bindNamespaces}})
.update({ where: { id: mesh.id }, data: { bindNamespaces: mesh.bindNamespaces } })
}
}
}
Expand Down
14 changes: 0 additions & 14 deletions packages/server/config/pipy-codebases/tunnelExternal/ip-list.json

This file was deleted.

Empty file.
Binary file not shown.
Empty file.
Binary file not shown.
Empty file.
Binary file not shown.
32 changes: 32 additions & 0 deletions packages/server/config/website/template/bin/init-repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
sleep 1

curl -X POST http://localhost:6060/api/v1/repo/website

curl -X POST http://localhost:6060/api/v1/repo-files/website/main.js --data-binary "@../pjs/main.js"
curl -X POST http://localhost:6060/api/v1/repo-files/website/config.js --data-binary "@../pjs/config.js"
curl -X POST http://localhost:6060/api/v1/repo-files/website/config.json --data-binary "@../pjs/config.json"
curl -X POST http://localhost:6060/api/v1/repo-files/website/plugins/balancer.js --data-binary "@../pjs/plugins/balancer.js"
curl -X POST http://localhost:6060/api/v1/repo-files/website/plugins/default.js --data-binary "@../pjs/plugins/default.js"
curl -X POST http://localhost:6060/api/v1/repo-files/website/plugins/router.js --data-binary "@../pjs/plugins/router.js"
curl -X POST http://localhost:6060/api/v1/repo-files/website/plugins/serve-files.js --data-binary "@../pjs/plugins/serve-files.js"
curl -X PATCH http://localhost:6060/api/v1/repo/website --data-raw '{"version":"1"}'

function getdir(){
for element in `ls $1`
do
dir_or_file=$1"/"$element
if [ -d $dir_or_file ]
then
getdir $dir_or_file
else
file=${dir_or_file/../}
echo added $file
curl -X POST "http://localhost:6060/api/v1/repo-files/website"$file --data-binary "@"$dir_or_file
fi
done
}
root_dir="../www"
getdir $root_dir

curl -X PATCH http://localhost:6060/api/v1/repo/website --data-raw '{"version":"2"}'
4 changes: 4 additions & 0 deletions packages/server/config/website/template/bin/start-repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
nohup sh init-repo.sh >/dev/null 2>&1 &

./pipy
2 changes: 2 additions & 0 deletions packages/server/config/website/template/bin/start-worker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
./pipy http://localhost:6060/repo/website/
2 changes: 2 additions & 0 deletions packages/server/config/website/template/bin/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
./pipy ../pjs/main.js
1 change: 1 addition & 0 deletions packages/server/config/website/template/pjs/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
JSON.decode(pipy.load('config.json'))
Empty file.
38 changes: 38 additions & 0 deletions packages/server/config/website/template/pjs/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
((
config = JSON.decode(pipy.load('config.json')),
) => pipy({
_certificates: Object.fromEntries(
Object.entries(config.certificates).map(
([k, v]) => [
k, {
cert: new crypto.CertificateChain(os.readFile(v.cert)),
key: new crypto.PrivateKey(os.readFile(v.key)),
}
]
)
),

})

.export('main', {
__route: undefined,
__isTLS: false,
})

.listen(config.listen)
.link('inbound-http')

.listen(config.listenTLS || 0 )
.onStart(() => void (__isTLS = true))
.acceptTLS({
certificate: (sni, cert) => (
sni && Object.entries(_certificates).find(([k, v]) => new RegExp(k).test(sni))?.[1]
)
}).to('inbound-http')

.pipeline('inbound-http')
.demuxHTTP().to(
$ => $.chain(config.plugins)
)

)()
37 changes: 37 additions & 0 deletions packages/server/config/website/template/pjs/plugins/balancer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
((
config = pipy.solve('config.js'),
policies = {
'RoundRobinLoadBalancer': algo.RoundRobinLoadBalancer,
'LeastWorkLoadBalancer': algo.LeastWorkLoadBalancer,
'HashingLoadBalancer': algo.HashingLoadBalancer,
},
balancers = (
Object.fromEntries(
Object.entries(config.upstreams).map(
([k, v]) => [
k, new policies[v.policy](v.targets)
]
)
)
),

) => pipy({
_target: undefined,
})

.import({
__route: 'main',
})

.pipeline()
.branch(
() => Boolean(_target = balancers[__route]?.next?.()), (
$ => $.muxHTTP(() => _target).to(
$ => $.connect(() => _target.id)
)
), (
$ => $.chain()
)
)

)()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(config =>
pipy()
.pipeline()
.replaceMessage(
new Message({ status: 404 }, 'No handler')
)
)()
40 changes: 40 additions & 0 deletions packages/server/config/website/template/pjs/plugins/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
((
// config = JSON.decode(pipy.load('config.json')),
config = pipy.solve('config.js'),

router = new algo.URLRouter(
Object.fromEntries(
Object.entries(config.endpoints).map(
([k, { route, rewrite }]) => [
k, { route, rewrite: rewrite && [new RegExp(rewrite[0]), rewrite[1]] }
]
)
)
),

) => pipy()

.import({
__route: 'main',
})

.pipeline()
.handleMessageStart(
msg => (
((
r = router.find(
msg.head.headers.host,
msg.head.path,
)
) => (
__route = r?.route,
r?.rewrite && (
msg.head.path = msg.head.path.replace(r.rewrite[0], r.rewrite[1])
)

))()
)
)
.chain()

)()
53 changes: 53 additions & 0 deletions packages/server/config/website/template/pjs/plugins/serve-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
((
config = pipy.solve('config.js'),

sites = config['serve-files'],

mimeTypes = config['mime'].types,

) => pipy({
_file: null,
_reqPathName: null
})

.import({
__route: 'main',
})

.pipeline()
.handleMessageStart(
msg => (
((
root = sites[__route]?.root,
) => (
_reqPathName = new URL(msg.head.path).pathname,
root && (
_file = http.File.from(root + _reqPathName)
)

))()
)
)
.branch(
() => Boolean(_file), (
$ => $
.replaceData()
.replaceMessage(
msg => _file.toMessage(msg.head.headers['accept-encoding'])
)
.handleMessageStart(
(msg, ext) => (
_reqPathName
&& _reqPathName.indexOf(".") > -1
&& (ext = _reqPathName.split(".")[_reqPathName.split(".").length - 1]),
ext
&& mimeTypes[ext]
&& (msg.head.headers["content-type"] = mimeTypes[ext])
)
)
),
$ => $.chain()
)


)()
1 change: 1 addition & 0 deletions packages/server/config/website/template/scerets/tls.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions packages/server/config/website/template/scerets/tls.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Empty file.
1 change: 1 addition & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"async-lock": "^1.3.0",
"atomic-sleep": "^1.0.0",
"axios": "^0.26.0",
"compressing": "^1.9.0",
"ejs": "^3.1.6",
"get-ip-range": "^4.0.1",
"ip-address": "^8.1.0",
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ CREATE TABLE healthcheckLog
`serviceId` UInt64 DEFAULT JSONExtractInt(message,
'serviceId'),

`serviceName` UInt64 DEFAULT JSONExtractString(message,
`serviceName` String DEFAULT JSONExtractString(message,
'serviceName'),

`message` String,
Expand Down
10 changes: 7 additions & 3 deletions packages/server/src/api/fleet/content-types/fleet/lifecycles.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ resourceTypes.add('tunnelInternal');
resourceTypes.add('tunnelExternal');

module.exports = {
beforeCreate: async (event) => {
const data = event.params.data;
data.version = 2;
},
afterCreate: async (event) => {

if (!event || !event.result.type) return;
Expand All @@ -32,14 +36,14 @@ module.exports = {
if ((event.result.type === 'clickhouse' || event.result.type === 'log' || event.result.type === 'prometheus') && !event.result.apply) {
const fleets = await strapi.db.query('api::fleet.fleet').findMany({ where: { type: event.result.type } });
if (fleets.length == 1) {
strapi.db.query('api::fleet.fleet').update({where: { id: fleets[0].id }, data: { apply: false }});
strapi.db.query('api::fleet.fleet').update({ where: { id: fleets[0].id }, data: { apply: false } });
}
}
if (event.result.type === 'log' || event.result.type === 'clickhouse') {
try {
await strapi.service("api::clickhouse.clickhouse").createTable(event.result);
await strapi.service("api::clickhouse.clickhouse").createTable(event.result,"healthcheckLog");
await strapi.service("api::clickhouse.clickhouse").createTable(event.result,"bgpLog");
await strapi.service("api::clickhouse.clickhouse").createTable(event.result, "healthcheckLog");
await strapi.service("api::clickhouse.clickhouse").createTable(event.result, "bgpLog");
} catch (error) {
console.error(error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,27 @@ module.exports = {
},
afterUpdate: async (event) => {
const { result } = event;
// 获取修改的templateFile信息
const templateFile = await strapi.db.query('api::templatefile.templatefile').findOne({
where: { id: result.id },
populate: {
template: true
}
});
// 修改templates表中对应的base repo的version属性
const template = await strapi.db.query('api::template.template').update({
where: { id: templateFile.template.id },
data: {
version: templateFile.template.version + 1,
}
});
// 根据type类型拼接base repo的路径
const path = `/flomesh/bases/${template.type}/${template.name}`;
await strapi.db.query('api::template.template').update({
where: { id: template.id },
data: {
version: template.version + 1,
},
});
// 部署base repo
strapi.service('api::resource.resource').deployBaseRepo(
path,
Object.fromEntries([[result.path, result.content]]),
template.version + 1
template.version
);

},
Expand Down
Loading

0 comments on commit e9764de

Please sign in to comment.