JustaPenguin / assetto-server-manager

A web interface to manage an Assetto Corsa Server.
https://github.com/JustaPenguin/assetto-server-manager/releases
MIT License
276 stars 70 forks source link

Basic REST API #516

Open cheesegrits opened 4 years ago

cheesegrits commented 4 years ago

Big ask, I know, but I'd be happy to do as much of the work as I could.

I'd love to have a basic API, with post/get/patch for creating and scheduling custom races, updating entry lists, and for grabbing results.

At the moment, I'm using ASM for our league's "ad hoc" races. Off season stuff, workshops, etc. For our main series, I have to use our existing (and aging) provisioning, as we have some specific needs that ASM can't currently meet. We do pre-qualifying, collecting stracker results for a week, which we then use external scripting to bust up into multiple splits on race day - anything from 2 to 5 servers worth, depending on the series. We also do non-standard race formats, like grid reversal but with different length races (feature and sprint), which requires rebuilding entry lists from results and restarting the server with a modified config.

I would love to use ASM as the backend for all our AC management ... but that would require a basic API, which I could use to push race configs and grab results. I haven't looked at the code in much depth, but I suspect that the existing JSON database format and associated handling would lend itself to a JSON based API.

cj123 commented 4 years ago

This sounds like a big job. A lot of server manager is kinda pluggable anyway, you just have to delve a bit deeper into the interactions that take place over the web. Everything a user does triggers a GET or POST request at some point, e.g. 'creating a custom race' is just a form post with some data. We don't do anything such as CSRF protection so there's nothing stopping another app making those requests itself.

As for reading data, you could set server manager to use the JSON store and read direct from there.

Building an entire API would require a lot of duplication, and be more difficult to maintain - especially because the only decent way to maintain an API is to be a consumer of it yourself (otherwise things get left behind). We're talking about a big architectural change here. My views are that if you have things you want to automate, have a look at how the requests are working and use it yourself.

Your reasons for wanting a REST API are interesting though, because we do have an open ticket that will likely fix a bunch of these problems (#308) :)

sgohl commented 4 years ago

I am building a discord bot (nodejs/discord.js) for mods to start a predefined custom race from discord. Can someone perhaps provide a simple example how a request via curl would look like to start a predefined custom race stored in json?

I'm stuck at this:

$ curl -u admin:servermanager http://localhost:8772/custom/load/b4a1eb15-8d34-4526-8dfe-560d1e6c5d5d
<a href="/login">Found</a>.

If custom/load is even the correct route to start a custom race. And I'm not sure how to correctly do the auth. Is there a static auth token available? I have set the session storage to filesystem so the bot should not need cookie handling.

Hecrer commented 4 years ago

Looking at this I don't see why you shouldn't use cookie handling, I just had a go and this works fine for me:

Login:

curl -X POST -c /tmp/cookies --data 'Username=admin' --data 'Password=servermanager' --header 'Content-Type: application/x-www-form-urlencoded' http://localhost:8772/login

Start Event:

curl -b /tmp/cookies http://localhost:8772/custom/load/b4a1eb15-8d34-4526-8dfe-560d1e6c5d5d

Also, we could expand the existing Discord bot that @cheesegrits developed to allow it to start/stop races etc. without needing external commands at all. There would be a few issues though, you need the event ID from server manager anyway, and you'd have to somehow lock the bot to admins.

We probably won't focus on this though - using bots to control server manager when you can just use the server manager UI isn't something we expect many people to want.

sgohl commented 4 years ago

That worked, I switched back to cookies now, thanks !!

lock the bot to admins

I think that's possible by discord roles. Else, you can accept commands from users which are admin in server-manager. Or both

probably won't focus on this though

no problem, I want to do more things with my discord bot people may want. And actually I prefer it living outside the app.

isn't something we expect many people to want

When you're playing in VR, you have to look between the little spot over the nose and navigate through the webinterface, or remove the HMD completely. That's utterly annoying. Typing simplest commands to start/stop/next a race is just even simpler when you do not need to watch at keyboard while typing. Must-have for me.