Skip to content

Commit e854671

Browse files
authored
Create model-server.js
1 parent 6f699b4 commit e854671

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

model-server.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// https://docs.deno.com/runtime/tutorials/how_to_with_npm/express
2+
3+
import * as util from 'https://code.agentscript.org/src/utils.js'
4+
import Model from 'https://code.agentscript.org/models/HelloModel.js'
5+
import express from "npm:[email protected]"
6+
import cors from "npm:[email protected]" // You might need to adjust the version or import method for Deno
7+
8+
const app = express() // https://expressjs.com/en/api.html#app.use
9+
app.use(express.json()) // Make sure Express can parse JSON body data
10+
app.use(cors()) // Use CORS with default settings: allows all origins
11+
12+
const model = new Model()
13+
model.setup()
14+
util.repeat(500, model.step)
15+
16+
const props = `Props: /props /sample /step /turtles /links /patches
17+
/createturtles/number /storedata /getdata`
18+
19+
app.get("/", (req, res) => {
20+
res.send(props)
21+
})
22+
app.get("/sample", (req, res) => {
23+
res.send(JSON.stringify(util.sampleModel(model)))
24+
})
25+
app.get("/step", (req, res) => {
26+
model.step()
27+
res.send(JSON.stringify(util.sampleModel(model)))
28+
})
29+
app.get("/turtles", (req, res) => {
30+
const ts = model.turtles.map(t => ({
31+
id: t.id, x: t.x, y: t.y, z: t.z, theta: t.theta
32+
}))
33+
res.send(JSON.stringify(ts))
34+
})
35+
app.get("/links", (req, res) => {
36+
const ls = model.links.map(l => ({
37+
end0: l.end0.id, end1: l.end1.id
38+
}))
39+
res.send(JSON.stringify(ls))
40+
})
41+
app.get("/patches", (req, res) => {
42+
const ps = model.patches.map(p => ({
43+
id: p.id,
44+
neighbors: p.neighbors.map(n => n.id),
45+
neighbors4: p.neighbors4.map(n4 => n4.id)
46+
}))
47+
res.send(JSON.stringify(ps))
48+
})
49+
app.get("/props", (req, res) => {
50+
const props = `Props: /step /turtles /links /patches
51+
/createturtles/number /storedata /getdata`
52+
res.send(props)
53+
})
54+
app.get('/createturtles/:number', (req, res) => {
55+
const number = parseInt(req.params.number, 10)
56+
if (isNaN(number) || number < 1) {
57+
res.status(400).send('Invalid number of turtles')
58+
} else {
59+
const turtles = model.turtles.create(number)
60+
res.json(turtles)
61+
}
62+
})
63+
64+
const kv = await Deno.openKv()
65+
kv.set(['mydata'], {}) // debug: reset every deploy
66+
app.put('/storedata', async (req, res) => {
67+
const data = req.body
68+
console.log('data', data)
69+
try {
70+
await kv.set(['mydata'], data)
71+
res.status(200).send('Data stored successfully')
72+
} catch (error) {
73+
res.status(500).send('Failed to store data')
74+
}
75+
})
76+
app.get("/getdata", async (req, res) => {
77+
const result = await kv.get(['mydata'])
78+
console.log('result', result.value)
79+
res.send(result.value)
80+
})
81+
82+
app.listen(8000)

0 commit comments

Comments
 (0)