Write rest service 4 with clojure to read the configuration file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.