Module Router
	EnableExplicit
	
	Procedure _MethodNotAllowed(*Request)
		General::RespondJSON(*Request, ~"{\"error\":\"Method not allowed\"}", "405 Method Not Allowed")
	EndProcedure
	
	Procedure Route(*Request)
		Protected URI.s    = FastCGI::GetParameter(*Request, "REQUEST_URI")
		Protected Method.s = FastCGI::GetParameter(*Request, "REQUEST_METHOD")
		
		Protected QPos.i = FindString(URI, "?")
		If QPos > 0 : URI = Left(URI, QPos - 1) : EndIf
		
		If Left(URI, 5) <> "/api/"
			General::ServeStatic(*Request, URI)
			ProcedureReturn
		EndIf
		
		If Left(URI, 10) = "/api/apps/" And
		   URI <> "/api/apps/list"      And
		   URI <> "/api/apps/install"   And
		   URI <> "/api/apps/uninstall"
			
			; URI looks like /api/apps/<app_id>/<filepath>
			Protected AppSegment.s = Mid(URI, 11)
			Protected SlashPos.i   = FindString(AppSegment, "/")
			
			If SlashPos > 1
				Protected AppID.s    = Left(AppSegment, SlashPos - 1)
				Protected FilePath.s = Mid(AppSegment, SlashPos + 1)
				If FilePath <> "" And Method = "GET"
					AppStore::HandleServeFile(*Request, AppID, FilePath)
				Else
					General::RespondJSON(*Request, ~"{\"error\":\"Not found\"}", "404 Not Found")
				EndIf
			Else
				General::RespondJSON(*Request, ~"{\"error\":\"Not found\"}", "404 Not Found")
			EndIf
			
			ProcedureReturn
		EndIf
		
		
		Select URI
				
				; Auth
			Case "/api/auth/login"
				If Method = "POST" : Auth::HandleLogin(*Request)
					Else : _MethodNotAllowed(*Request) : EndIf
				
			Case "/api/auth/logout"
				If Method = "POST" : Auth::HandleLogout(*Request)
					Else : _MethodNotAllowed(*Request) : EndIf
				
			Case "/api/auth/check"
				Auth::HandleCheck(*Request)
				
			Case "/api/auth/password"
				If Method = "POST" : Auth::HandleChangePassword(*Request)
					Else : _MethodNotAllowed(*Request) : EndIf
				
				; Filesystem
			Case "/api/fs/list"
				If Method = "GET" : FileSystem::HandleList(*Request)
					Else : _MethodNotAllowed(*Request) : EndIf
				
			Case "/api/fs/stat"
				If Method = "GET" : FileSystem::HandleStat(*Request)
					Else : _MethodNotAllowed(*Request) : EndIf
				
			Case "/api/fs/read"
				If Method = "GET" : FileSystem::HandleRead(*Request)
					Else : _MethodNotAllowed(*Request) : EndIf
				
			Case "/api/fs/write"
				If Method = "POST" : FileSystem::HandleWrite(*Request)
					Else : _MethodNotAllowed(*Request) : EndIf
				
			Case "/api/fs/mkdir"
				If Method = "POST" : FileSystem::HandleMkdir(*Request)
					Else : _MethodNotAllowed(*Request) : EndIf
				
			Case "/api/fs/delete"
				If Method = "POST" : FileSystem::HandleDelete(*Request)
					Else : _MethodNotAllowed(*Request) : EndIf
				
			Case "/api/fs/move"
				If Method = "POST" : FileSystem::HandleMove(*Request)
					Else : _MethodNotAllowed(*Request) : EndIf
				
				; App store
			Case "/api/apps/list"
				If Method = "GET" : AppStore::HandleList(*Request)
					Else : _MethodNotAllowed(*Request) : EndIf
				
			Case "/api/apps/install"
				If Method = "POST" : AppStore::HandleInstall(*Request)
					Else : _MethodNotAllowed(*Request) : EndIf
				
			Case "/api/apps/uninstall"
				If Method = "POST" : AppStore::HandleUninstall(*Request)
					Else : _MethodNotAllowed(*Request) : EndIf
				
			Default
				General::RespondJSON(*Request, ~"{\"error\":\"Not found\"}", "404 Not Found")
				
		EndSelect
	EndProcedure
	
EndModule

; IDE Options = PureBasic 6.30 (Windows - x64)
; CursorPosition = 111
; Folding = 6
; EnableXP
; DPIAware