0

With the mount library, how do I reload (stop and start) an http-kit "mount state" on a -main function?

My current code is this:

(defstate server-config :start {:port 7890 :join? false})
(defn start-server [server-config]
  (when-let [server (run-server myserv-ring-handler server-config)]
    (println "Server has started!")
    server))

(defstate myserv-server :start (start-server server-config)
          :stop  (myserv-server :timeout 100))

(defn system-port [args]
  (Integer/parseInt
    (or (System/getenv "PORT")
        (first args)
        "7890")))

(defn -main [& args]
  (mount/start-with-states
    {#'myserv/server-config
     {:start #(array-map :port (system-port args)
                         :join? false)}}))

So when I "lein run" everything works, but whenever I change a file, and the http-kit server is stopped, the command stops. For the moment I'm doing "while true; do lein run; done" to work, so I've thought about adding an infinite loop to the -main function, but it doesn't feel like this is the right way.

How should I do this?

4

2 回答 2

0

所以我有几个单独的问题:

  • 我不明白现在我不需要使用 lein run,而是我可以只做 lein repl 并从那里启动服务器。这样就避免了重启问题。
  • 另一个是我滥用了 start-with-states 而不是配置状态。

你可以在这里看到与图书馆作者的讨论。

于 2017-02-04T18:29:19.130 回答
0

我建议向您的 http 服务器添加一些元数据defstate

从安装自述文件:

如果在重新加载/重新编译/重新定义时不需要对运行状态进行任何操作,请设置:on-reload:noop:.

所以尝试这样的事情:

(defstate ^{:on-reload :noop}
          myserv-server
          :start (start-server server-config)
          :stop  (my-stop-func myserv-server))

这意味着当您更改文件时,受影响的代码将被重新加载,但 http 服务器将继续运行。

我希望我已经正确理解了您的问题,并且这就是您想要的。

我还可以建议,如果您想快速启动并运行,那么 Leiningen 有各种模板化的 Web 应用程序项目。例如,Luminus 项目。您可以将+http-kit参数传递给lein new luminus myapp命令,这将为您正确连接应用程序。然后,您可以阅读生成的代码并了解它们是如何组合在一起的。

于 2016-12-23T14:54:55.063 回答