故障排除clojure web应用程序:连接html和css的heroku部署 [英] troubleshooting clojure web-app: connecting html and css for heroku deployment

查看:108
本文介绍了故障排除clojure web应用程序:连接html和css的heroku部署的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件,一个html和一个css。我试图把它们变成一个heroku应用程序,甚至使用lein命令创建一个heroku友好骨架和插入这两个文件,但不能得到它为我的生活工作。有一些非常基本的,我不知道如何协调一个视图与后端控制。和hello世界教程没有帮助我,因为他们不告诉我如何做不同的事情或解释什么需要改变在我的defroutes功能,例如,为了完成。总之,我的问题是这样:我如何协调这两个文件到一个Clojure项目,使html渲染作为webapp的首页,然后部署在heroku?

I have two files, one html and one css. I have tried to turn them into a heroku app and even used the lein command to create a heroku friendly skeleton and plug these two files in, but cannot get it to work for the life of me. There is something very basic that I don't yet understand about how to coordinate a view with the back-end control. And the hello world tutorials aren't helping me because they do not show me how to do different things or explain what needs to change in my defroutes function, for example, for that to be accomplished. In short, my question is this: How can I coordinate these two files into a Clojure project to make the html render as the front page of a webapp and then deploy it on heroku?

html:

<html>
    <head>
        <link rel="stylesheet" href="style.css" />
    </head>

    <body>
        <img id="sun" src="http://goo.gl/dEEssP">
        <div id='earth-orbit'>
            <img id="earth" src="http://goo.gl/o3YWu9">
        </div>
    </body>
</html>

web.clj文件:

web.clj file in "lein new heroku ..." project:

(ns solar_system.web
  (:require [compojure.core :refer [defroutes GET PUT POST DELETE ANY]]
            [compojure.handler :refer [site]]
            [compojure.route :as route]
            [clojure.java.io :as io]
            [ring.middleware.stacktrace :as trace]
            [ring.middleware.session :as session]
            [ring.middleware.session.cookie :as cookie]
            [ring.adapter.jetty :as jetty]
            [ring.middleware.basic-authentication :as basic]
            [cemerick.drawbridge :as drawbridge]
            [environ.core :refer [env]]))

(defn- authenticated? [user pass]
  ;; TODO: heroku config:add REPL_USER=[...] REPL_PASSWORD=[...]
  (= [user pass] [(env :repl-user false) (env :repl-password false)]))

(def ^:private drawbridge
  (-> (drawbridge/ring-handler)
      (session/wrap-session)
      (basic/wrap-basic-authentication authenticated?)))

(defroutes app
  (ANY "/repl" {:as req}
       (drawbridge req))
  (GET "/" []
       {:status 200
        :headers {"Content-Type" "text/plain"}
        :body (pr-str ["Hello" :from 'Heroku])})  ; <= Should I change this part here?
  (ANY "*" []
       (route/not-found (slurp (io/resource "404.html")))))

(defn wrap-error-page [handler]
  (fn [req]
    (try (handler req)
         (catch Exception e
           {:status 500
            :headers {"Content-Type" "text/html"}
            :body (slurp (io/resource "500.html"))}))))

(defn -main [& [port]]
  (let [port (Integer. (or port (env :port) 5000))
        ;; TODO: heroku config:add SESSION_SECRET=$RANDOM_16_CHARS
        store (cookie/cookie-store {:key (env :session-secret)})]
    (jetty/run-jetty (-> #'app
                         ((if (env :production)
                            wrap-error-page
                            trace/wrap-stacktrace))
                         (site {:session {:store store}}))
                     {:port port :join? false})))

;; For interactive development:
;; (.stop server)
;; (def server (-main)) 

project.clj文件

project.clj file

(defproject solar_system "1.0.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://solar_system.herokuapp.com"
  :license {:name "FIXME: choose"
            :url "http://example.com/FIXME"}            
  :dependencies [[org.clojure/clojure "1.4.0"]
                 [compojure "1.1.1"]
                 [ring/ring-jetty-adapter "1.1.0"]
                 [ring/ring-devel "1.1.0"]
                 [ring-basic-authentication "1.0.1"]
                 [environ "0.2.1"]
                 [com.cemerick/drawbridge "0.0.6"]]
  :min-lein-version "2.0.0"
  :plugins [[environ/environ.lein "0.2.1"]]
  :hooks [environ.leiningen.hooks]
  :profiles {:production {:env {:production true}}})

文本:

(ns hello-world.core
(:use ring.adapter.jetty))

(defn app [req]
    {:status 200
    :headers {"Content-Type" "text/plain"}
    :body "Hello, world"}) ; <= Could I just change this part to slurp in
                           ;    the html file and stick it in a file in my 
                           ;    root directory to get a successful 'git push heroku master'?


推荐答案

修改代码:

(defroutes app
  (ANY "/repl" {:as req}
       (drawbridge req))
  (GET "/" []
       {:status 200
        :headers {"Content-Type" "text/html"} ; change content type
        :body (slurp "resources/public/my-file.html")}) ; wherever your file is
  (ANY "*" []
       (route/not-found (slurp (io/resource "404.html")))))

如何写:

(defroutes app
  (ANY "/repl" {:as req} (drawbridge req))
  (GET "/" [] (slurp "resources/public/my-file.html")) ; wherever your file is
  (route/resources "/") ; special route for serving static files like css
                        ; default root directory is resources/public/
  (route/not-found (slurp (io/resource "404.html")))) ; IDK what io/resource does
                                                      ; you might not need it

这篇关于故障排除clojure web应用程序:连接html和css的heroku部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆