Skip to content

Commit

Permalink
multithreaded server works
Browse files Browse the repository at this point in the history
  • Loading branch information
LimeGradient committed Aug 5, 2024
1 parent 0153c21 commit be354a0
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ file(GLOB_RECURSE SOURCES

add_library(${PROJECT_NAME} SHARED ${SOURCES})

if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
add_compile_definitions(WIN32_LEAN_AND_MEAN=1) # geode moment
endif()

if (NOT DEFINED ENV{GEODE_SDK})
message(FATAL_ERROR "Unable to find Geode SDK! Please define GEODE_SDK environment variable to point to Geode")
else()
Expand Down
9 changes: 6 additions & 3 deletions src/Spotify/AuthWebserver.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "AuthWebserver.hpp"

void Webserver::createServer() {
this->m_spotify->m_isWebserverCreated = true;

crow::SimpleApp app;
CROW_ROUTE(app, "/")([]() {
return "Hello from SpotiDash!";

CROW_ROUTE(app, "/")([](){
return "Hello world";
});

app.port(18080).multithreaded().run();
app.port(18080).multithreaded().run_async();
}
10 changes: 9 additions & 1 deletion src/Spotify/AuthWebserver.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
#pragma once
#define _WINSOCK2API_
#undef _WINSOCKAPI_
#include <crow.h>

#include "Spotify.hpp"

class Webserver {
public:
Webserver(Spotify* spotify) {
this->m_spotify = spotify;
}

void createServer();

Spotify* m_spotify;
};
12 changes: 9 additions & 3 deletions src/Spotify/Spotify.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
#include "AuthWebserver.hpp"
#include <Geode/Geode.hpp>
#include "Spotify.hpp"

#include "../lib/base64.hpp"
#include "AuthWebserver.hpp"

void Spotify::init() {
if (Mod::get()->getSavedValue<std::string>("spotify-code").empty()) {
Mod::get()->setSavedValue<std::string>("spotify-code", "MDRkNTcxMWFjZWRkNGE2NWE1N2YzYjI5YmYwZWNmNjU=");
}

this->spotifyToken = Mod::get()->getSavedValue<std::string>("spotify-code");
Webserver* server = new Webserver();
server->createServer();

if (!this->m_isWebserverCreated) {
this->m_webserverThread = std::thread([=, this] {
Webserver* server = new Webserver(this);
server->createServer();
});
this->m_webserverThread.detach();
}
/* this->m_webListener.bind([=, this] (web::WebTask::Event* e) {
if (web::WebResponse* res = e->getValue()) {
if (res->ok()) {
Expand Down
6 changes: 6 additions & 0 deletions src/Spotify/Spotify.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#pragma once

#include <future>

#include <Geode/Geode.hpp>
#include <Geode/utils/web.hpp>
#include <Geode/loader/Event.hpp>
Expand All @@ -13,8 +17,10 @@ class Spotify {
void init();
Playback* getCurrentPlayback();

bool m_isWebserverCreated = false;
protected:
std::string spotifyToken;
std::string accessToken;
std::thread m_webserverThread;
EventListener<web::WebTask> m_webListener;
};
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using namespace geode::prelude;

#include <Geode/modify/MenuLayer.hpp>
class $modify(MyMenuLayer, MenuLayer) {

bool init() {
if (!MenuLayer::init()) {
return false;
Expand Down

0 comments on commit be354a0

Please sign in to comment.