The best way to write a configuration file is to use the CLJ file. Next, replace the. xml file with the. CLJ file. The content is as follows:
{:mongodb "localhost" :listen_port 7777 :login_timeout 200 :check_timeout 200}
A map is defined here. Note that localhost is a string and must be enclosed in double quotation marks.
Here we use spaces to separate the pair composed of each key/value.
Add a line of code to the test. CLJ file:
(ns my-website.rest.test (:require [noir.response :as response]) (:use [noir.core :only [defpage]]))(defpage "/rest/:id" {:keys [id]} (response/json {:userId id}))(defpage "/rest/file/:name" {:keys [name]} (str (load-file (str "/opt/" name))))
The last line allows you to enter a file name, read the content of the file under/opt/, and return it to the client as a string.
Enter the URL: http: // localhost: 8080/rest/file/a. CLJ in the browser.
The returned result is:
{: MongoDB "localhost",: check_timeout 200,: listen_port 7777,: login_timeout 200}
Note: It is automatically added as the separator.
Continue to improve. I hope to read the configuration file a. CLJ in the Web service request, and then retrieve the MongoDB Host IP address,
The modification code is as follows:
(ns my-website.rest.test (:require [noir.response :as response]) (:use [noir.core :only [defpage]]))(defpage "/rest/:id" {:keys [id]} (response/json {:userId id}))(def load_config (fn [name] (load-file (str "/opt/" name))))(def host (fn [name] (:mongodb (load_config name))))(defpage "/rest/file/:name" {:keys [name]} (str "mongodb host: " (host name) ))
The request result is:
MongoDB HOST: localhost
For more information about FN usage, see my article:
Http://blog.csdn.net/sheismylife/article/details/8457872