A very basic "clone" of r/place written in Rust using warp.
Instead of rendering the image on the client side, the image is encoded as PNG and sent to the user. This doesn't scale well as that image is constructed every time someone sends a GET requests to /image
. Ideally, you would render the image client-side and e.g. using WebSockets, send them notifications when a pixel was edited.
There aren't that many:
- When the server shuts down, the image data is stored in a file
image
and loaded back up when the server starts again.
GET
/
basic welcoming message
HTTP code | content-type | response |
---|---|---|
200 |
application/json |
{"code":200,"message":"Welcome to r/place but in Rust!"} |
$ curl -X GET http://localhost:3030/
GET
/image
sends the current canvas
HTTP code | content-type | response |
---|---|---|
200 |
image/png |
$ curl -X GET http://localhost:3030/image > image.png
PUT
/set-pixel/:x/:y
set the color of a pixel
name | type | data type | description |
---|---|---|---|
x |
required | usize |
The x position of the pixel |
y |
required | usize |
The y position of the pixel |
- max.
Content-Length
is 32 bytes hex_color
has to be string of length 6
{
"color": hex_color
}
Example:
{"color":"FFFFFF"}
name | type | data type | description |
---|---|---|---|
X-Token |
required | String |
The "auth" token (has to be set to abc ) |
HTTP code | content-type | response | description |
---|---|---|---|
200 |
application/json |
{"code":200,"message":"Successfully edited pixel"} |
|
400 |
application/json |
{"code":400,"message":"MISSING_HEADER: X-Token"} |
|
401 |
application/json |
{"code":401,"message":"BAD_AUTH"} |
X-Token wasn't abc |
411 |
application/json |
{"code":411,"message":"MISSING_CONTENT_LENGTH"} |
Missing body |
413 |
application/json |
{"code":413,"message":"PAYLOAD_TOO_LARGE"} |
Body too big (max. 32 bytes) |
422 |
application/json |
{"code":422,"message":"MALFORMED_BODY"} |
Body couldn't be parsed to JSON |
422 |
application/json |
{"code":422,"message":"OUT_OF_BOUNDS"} |
Pixel out of bounds |
422 |
application/json |
{"code":422,"message":"INVALID_COLOR"} |
Invalid hex color |
$ curl -X PUT -H "X-Token: abc" -H "Content-Type: application/json" -d "{\"color\":\"ffffff\"}" http://localhost:3030/set-pixel/10/10
- "proper" authentication i.e. creating accounts with unique auth tokens
- rate limits to prevent spamming