Skip to content

Commit

Permalink
fix(main): improve ngrok integration and error handling
Browse files Browse the repository at this point in the history
- Added proper import for pyngrok
- Updated index method to be static
- Enhanced error handling for ngrok start/stop functions
  • Loading branch information
IRedDragonICY committed Oct 8, 2024
1 parent 8691671 commit 5e77f61
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import numpy as np
import uvicorn

# Perbaikan import pyngrok
from pyngrok import ngrok

logging.disable(logging.CRITICAL)


Expand Down Expand Up @@ -42,7 +45,6 @@ def setup_routes_and_middlewares(self):
for mount_point, directory in directories.items():
self.app.mount(mount_point, StaticFiles(directory=directory), name=mount_point.strip("/"))

# Define routes
self.app.get("/")(self.index)
self.app.get("/api/audio_status")(self.get_audio_status)
self.app.post("/api/reset_audio_status")(self.reset_audio_status)
Expand All @@ -51,7 +53,8 @@ def setup_routes_and_middlewares(self):
self.app.post("/api/start_ngrok")(self.start_ngrok)
self.app.post("/api/stop_ngrok")(self.stop_ngrok)

async def index(self, ngrok_api_key: str = Cookie(default=None)):
@staticmethod
async def index(ngrok_api_key: str = Cookie(default=None)):
with open('app/index.html', 'r') as f:
html_content = f.read()
headers = {"ngrok_api_key": ngrok_api_key} if ngrok_api_key else {}
Expand Down Expand Up @@ -91,22 +94,29 @@ async def start_ngrok(self, api_key: str = Form(...)):
return JSONResponse(content={"message": "Ngrok sudah berjalan.", "public_url": self.public_url},
status_code=200)

import ngrok
ngrok.set_auth_token(api_key)
self.public_url = ngrok.connect(8000).public_url
self.ngrok_process = ngrok.get_ngrok_process()
threading.Thread(target=self.ngrok_process.proc.wait).start()
try:
ngrok.set_auth_token(api_key)
tunnel = ngrok.connect(8000)
self.public_url = tunnel.public_url
self.ngrok_process = ngrok.get_ngrok_process()
threading.Thread(target=self.ngrok_process.proc.wait).start()

return JSONResponse(content={"message": "Ngrok berhasil dimulai.", "public_url": self.public_url},
status_code=200)
return JSONResponse(content={"message": "Ngrok berhasil dimulai.", "public_url": self.public_url},
status_code=200)
except Exception as e:
logging.error(f"Error starting ngrok: {e}")
return JSONResponse(content={"message": f"Error starting ngrok: {str(e)}"}, status_code=500)

async def stop_ngrok(self):
if self.ngrok_process:
import ngrok
ngrok.kill()
self.ngrok_process = None
self.public_url = None
return JSONResponse(content={"message": "Ngrok berhasil dihentikan."}, status_code=200)
try:
ngrok.kill()
self.ngrok_process = None
self.public_url = None
return JSONResponse(content={"message": "Ngrok berhasil dihentikan."}, status_code=200)
except Exception as e:
logging.error(f"Error stopping ngrok: {e}")
return JSONResponse(content={"message": f"Error stopping ngrok: {str(e)}"}, status_code=500)

return JSONResponse(content={"message": "Ngrok tidak berjalan."}, status_code=400)

Expand All @@ -129,4 +139,4 @@ def open_browser():

if __name__ == "__main__":
server_app = ServerApp()
server_app.run()
server_app.run()

0 comments on commit 5e77f61

Please sign in to comment.