0

I have an angular project that I want to deploy using swifter in swiftUI. I have tried the below code.

import Foundation
import Swifter

class WebServer {
    let server = HttpServer()
    let path: String

    init(path: String) {
        self.path = path
    }
    func startServer() {
            if let webAppPath = Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "/Users/user/Downloads/dirFile") {

                // Serve the Flutter web build files
                server["/"] = shareFilesFromDirectory(webAppPath)

                do {
                    try server.start(8081)
                    print("Web server started on port 8081")
                } catch {
                    print("Failed to start the web server: \(error)")
                }
            } else {
                print("Web app path not found.")
            }
        }
    func stopServer() {
        server.stop()
        print("Web server stopped")
    }
}

When trying this, I am getting the following error:

Web app path not found.

I have tried giving path of the directory where my web application is from both, separate directory, and copied angular project directory in the Xcode project (Both gives the same result). Can someone help me out?

HangarRash
  • 7,314
  • 5
  • 5
  • 32
Deeshant
  • 1
  • 2

1 Answers1

0

I could imagine it's because of sandboxing. That your app simply cannot access your downloads folder?!? (I assume you have a macOS app, right? If it's an iOS app than you cannot access files like that at all)

Check if that's the case. To test simply give your app full access to everything for testing: https://developer.apple.com/documentation/security/app_sandbox/accessing_files_from_the_macos_app_sandbox#4144040

If that's the problem, then depending on your use-case embed the index.html file in your apps bundle.

Georg
  • 3,664
  • 3
  • 34
  • 75
  • Hi @Georg, Thanks for the answer 1- I am using this for an IOS app. I have tried giving the path of web application which I have copied inside the swiftUI project already. I have even tried giving absolute and related path. 2- If I give the path of downloads directory, will it not work on simulator? – Deeshant Jul 20 '23 at 10:55
  • No, the app will not really be able to leave it's sandbox. So you need to embed your files in your app. Try removing the `inDirectory:` part for the sake of testing it. I am quite sure this is your problem... – Georg Jul 20 '23 at 17:21
  • I embedded the files in the app and it started the server but when trying on localhost, it gives 404. I am sure all the files related to web are there. I have tried using npm http-server in the same directory, and it works that way. Do you have any idea why that can happen. Also if you can point out to any repo where same thing is achieved, may be with some different package. Let me know if you need any additional questions. – Deeshant Jul 21 '23 at 11:30