【问题标题】:How can I run a command line tool in response to an HTTP Request in Vapor? [closed]如何运行命令行工具以响应 Vapor 中的 HTTP 请求? [关闭]
【发布时间】:2020-10-08 15:38:53
【问题描述】:

我有一个 node.js 命令行工具,我想运行它来生成资产以响应我在 Vapor 4 中收到的 HTTP 请求。是否可以这样做?

【问题讨论】:

    标签: node.js swift command-line-interface vapor vapor-cli


    【解决方案1】:

    我猜是这样的

    func requestHandler(_ req: Request) throws -> EventLoopFuture<HTTPStatus> {
        let promise = req.eventLoop.makePromise(of: HTTPStatus.self)
        let process = Process()
        // e.g. use `which node` to find path to `node`
        process.executableURL = URL(fileURLWithPath: "/path/to/binary") // e.g. /usr/bin/node
        
        // in which folder execute the command, it is optional
        process.currentDirectoryPath = "/path/to/folder"
        
        // optional arguments, e.g. if your arguments are -c release then it should be ["-c", "release"]
        process.arguments = ["arg1", "arg2", "argN"]
        
        // wait for termination in closure
        process.terminationHandler = { process in
            switch process.terminationStatus {
            // probably normal termination via SIGTERM or when process successfully finished
            case 0:
                promise.succeed(.ok)
            default:
                promise.fail(Abort(.failedDependency, reason: "Process finished with code \(process.terminationStatus)"))
            }
        }
        
        // don't forget to launch it
        try process.run()
        
        return promise.futureResult
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-01
      • 2017-06-30
      • 2011-02-28
      • 2012-03-09
      • 2014-09-27
      • 2021-09-11
      相关资源
      最近更新 更多