Skip to content

Instantly share code, notes, and snippets.

@Syuparn
Created December 10, 2023 01:20
Show Gist options
  • Save Syuparn/f20e9c2c18f097f1205d6a7c62d61e72 to your computer and use it in GitHub Desktop.
Save Syuparn/f20e9c2c18f097f1205d6a7c62d61e72 to your computer and use it in GitHub Desktop.
Pangaea API server sample (with in-memory DB)
invite!("http")
idGen := <{|i| yield "user_#{i}"; recur(i+1)}>.new(1)
db := <{|users|
set := {|updated| recur(updated)}
yield [users, set]
}>.new({})
S.serve(
S.post("users") {|req|
db.next.{|users, set|
user := {id: idGen.next, name: req.body.decJSON.name}
set({user.id.S: user, **users})
Response.new(status: 201, body: user.S)
}
},
S.get("users") {|req|
db.next.{|users, _set|
{users: users.values}
}
},
S.delete("users/:id") {|req|
db.next.{|users, set|
set(users.exclude {|k, v| k == req.params.id}.O)
}
Response.new(status: 204)
},
url: ":8081",
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment