Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Endpoint for getting all users online? #6

Closed
sunnydayoutside opened this issue Apr 6, 2024 · 6 comments
Closed

Endpoint for getting all users online? #6

sunnydayoutside opened this issue Apr 6, 2024 · 6 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@sunnydayoutside
Copy link

sunnydayoutside commented Apr 6, 2024

I'd like to create a frontend for my RAS server, but I think it would be really helpful if there was an endpoint that would report how much and who the people that are online.

@mk6i
Copy link
Owner

mk6i commented Apr 6, 2024

Sounds good to me. Are you interested in contributing this feature? If so, I can give you some direction on how to start. Otherwise, I can probably pick this up in a week or two.

@mk6i mk6i added enhancement New feature or request good first issue Good for newcomers labels Apr 6, 2024
@sunnydayoutside
Copy link
Author

sunnydayoutside commented Apr 6, 2024

Sounds good to me. Are you interested in contributing this feature? If so, I can give you some direction on how to start. Otherwise, I can probably pick this up in a week or two.

I'm not familiar with Go, I'll try to implement it on my own if I can. If you could give me some direction, that'd be great!

@mk6i
Copy link
Owner

mk6i commented Apr 7, 2024

Before you start, complete the development environment guide.

Now let's say you want to add a GET /user/online endpoint that returns all users currently online. InMemorySessionManager maintains the state of online users. All we need to do is have the endpoint dump the users returned by InMemorySessionManager.AllSessions().

Here's the gist of it:

  1. Add the new route and HTTP handler to server/http/mgmt_api.go. Use the existing handlers as examples.

    + mux.HandleFunc("/user/online", uh.getOnlineUsers)
    //...
    + func (uh userHandler) getOnlineUsers(w http.ResponseWriter, r *http.Request) {
    +     // ...
    + }
  2. Next, add the InMemorySessionManager dependency to user handler.

    type userHandler struct {
        UserManager
    +   state.InMemorySessionManager
        logger *slog.Logger
    }

You'll need to make make the corresponding change to StartManagementAPI() and in cmd/server/main.go, where the dependencies are injected.

  1. Finally, add logic to the new HTTP handler for pulling the list of users and sending it as a JSON response. (JSON response is not shown here).
    func (uh userHandler) getOnlineUsers(w http.ResponseWriter, r *http.Request) {
    // ...
    + onlineUsers := uh.InMemorySessionManager.AllSessions()
    + var screenNames []string
    + for _, sess := range onlineUsers {
    +     screenNames = append(screenNames, sess.ScreenName())
    + }
    // ...
    }

Let me know if/when you complete this, and then we can proceed to making the code tidy with interfaces, etc before the final commit. But this should guide you towards a working endpoint.

Again, if this is too overwhelming, I'll take it on in the next couple weekends :)

@sunnydayoutside
Copy link
Author

Before you start, complete the development environment guide.

Now let's say you want to add a GET /user/online endpoint that returns all users currently online. InMemorySessionManager maintains the state of online users. All we need to do is have the endpoint dump the users returned by InMemorySessionManager.AllSessions().

Here's the gist of it:

1. Add the new route and HTTP handler to [server/http/mgmt_api.go](https://github.com/mk6i/retro-aim-server/blob/main/server/http/mgmt_api.go). Use the existing handlers as examples.
   ```diff
   + mux.HandleFunc("/user/online", uh.getOnlineUsers)
   //...
   + func (uh userHandler) getOnlineUsers(w http.ResponseWriter, r *http.Request) {
   +     // ...
   + }
   ```

2. Next, add the `InMemorySessionManager` dependency to user handler.
   ```diff
   type userHandler struct {
       UserManager
   +   state.InMemorySessionManager
       logger *slog.Logger
   }
   ```

You'll need to make make the corresponding change to StartManagementAPI() and in cmd/server/main.go, where the dependencies are injected.

3. Finally, add logic to the new HTTP handler for pulling the list of users and sending it as a JSON response. (JSON response is not shown here).
   ```diff
   func (uh userHandler) getOnlineUsers(w http.ResponseWriter, r *http.Request) {
   // ...
   + onlineUsers := uh.InMemorySessionManager.AllSessions()
   + var screenNames []string
   + for _, sess := range onlineUsers {
   +     screenNames = append(screenNames, sess.ScreenName())
   + }
   // ...
   }
   ```

Let me know if/when you complete this, and then we can proceed to making the code tidy with interfaces, etc before the final commit. But this should guide you towards a working endpoint.

Again, if this is too overwhelming, I'll take it on in the next couple weekends :)

Unfortunately, some stuff IRL just happened and I have to deal with that over this weekend. Sorry about that, I hope everything goes well and I hope to see this implemented soon!

@mk6i
Copy link
Owner

mk6i commented Apr 9, 2024

No problem. I'm reopening this ticket to track the work.

@mk6i mk6i reopened this Apr 9, 2024
mk6i added a commit that referenced this issue Apr 13, 2024
@mk6i
Copy link
Owner

mk6i commented Apr 13, 2024

Implemented in v0.2.0. Please re-open this issue if you have questions or feedback.

@mk6i mk6i closed this as completed Apr 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants