每个环境的Clojure数据库设置 [英] Clojure database settings per environment

查看:110
本文介绍了每个环境的Clojure数据库设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用monger从我的Clojure简单的应用程序中获取和保存一些数据在MongoDb。我有强大的Ruby on Rails背景,所以我熟悉每个环境的数据库设置(开发,测试,生产)。我想在Clojure有类似的东西。如何将我的环境添加到我的代码?我想在Clojure方式,代码作为数据,没有任何yaml文件。

I am using monger to fetch and save some data in MongoDb from my Clojure simple app. I have strong Ruby on Rails background so I am familiar with database settings per environment (development, test, production). I want to have something similar in Clojure. How can I add the environment to my code? I want to do it in Clojure-way, code as data, without any yaml files. I am using Leiningen if it changes something.

推荐答案

您可以使用Leiningen profiles 功能。

You can use Leiningen profiles feature.

在您的project.clj中定义您的个人资料需要dev和prod)

In your project.clj define your profiles (most cases you need dev and prod)

:profiles {:dev {:resource-paths ["resource-dev"]}
           :prod {:resource-paths ["resource-prod"]}}

resource-dev resource-prod 并在其中创建config.clj文件,存储配置。例如:

Now create 2 directories resource-dev and resource-prod and create config.clj file in both of them which will have define a map to store configuration. Something like:

(ns myapp.config)
(def config {:database "dev"})

然后在您的应用程序代码中,您可以使用以下代码片段加载配置文件(仅一次),并访问配置map:

Then in your app code you can use below snippet to load the config file (only once) and access the config map:

(use 'clojure.java.io)
(def config (delay (load-file (.getFile (resource "config.clj")))))
(defn get-config []
  @(force config))

现在您可以使用 get-config 函数来访问配置映射。

Now you can use get-config function to access the config map.

这篇关于每个环境的Clojure数据库设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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