Skip to content

Commit b30dbfc

Browse files
committed
add compojure router example to quick start page
1 parent fd82141 commit b30dbfc

File tree

3 files changed

+193
-91
lines changed

3 files changed

+193
-91
lines changed

QuickStart.md

+67-33
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,25 @@ Configuration
1919
### auto or a real path, e,g /usr/lib/jvm/java-7-oracle/jre/lib/amd64/server/libjvm.so
2020
jvm_path auto;
2121

22-
### my app jars e.g. clojure-1.5.1.jar , groovy-2.3.4.jar ,etc.
22+
### my app jars e.g. clojure-1.5.1.jar , groovy-2.3.4.jar,etc.
2323
### if we only use Java handler we need not place clojure-xxx.jar here.
2424
jvm_var my_other_jars 'my_jar_dir/clojure-1.5.1.jar';
2525
2626
### my app classpath, windows user should use ';' as the separator
27+
### for clojure devs the simplest way is to use the result jar file from `lein uberjar` here
2728
jvm_options "-Djava.class.path=jars/nginx-clojure-0.4.2.jar:#{my_other_jars}";
2829

2930
###or we can put jars in some directories, e.g. jars-dir1, jars-dir2
3031
###so that all jars or direct sub directories from these directories will be appended to the jvm classpath
3132
##Note: if you use build-in extensions (e.g. SUN JCE) from jdk please append these dirs, e.g. the value of
3233
## System.getProperty("java.ext.dirs")
33-
jvm_options "-Djava.ext.dirs=jars-dir1:jars-dir2"
34+
jvm_options "-Djava.ext.dirs=jars-dir1:jars-dir2";
3435
```
35-
1. Setting inline Http Service Handler
36+
1. Setting Inline Http Service Handler
3637

3738
For Clojure:
3839
```nginx
39-
40+
##Within `server {` block in nginx.conf
4041
location /clojure {
4142
content_handler_type 'clojure';
4243
content_handler_code '
@@ -52,7 +53,7 @@ Configuration
5253

5354
For Groovy:
5455
```nginx
55-
56+
##Within `server {` block in nginx.conf
5657
location /groovy {
5758
content_handler_type 'groovy';
5859
content_handler_code '
@@ -69,53 +70,82 @@ Configuration
6970
}
7071
```
7172

72-
For Java:
73+
For Java:
74+
> **Note:**
75+
So far nginx-clojure has not supported inline java handler, please see the next section to learn how to use an external java handler.
76+
77+
1. Setting Compojure Router/External Http Service Handler
7378

74-
```nginx
79+
For Clojure:
80+
```nginx
81+
##Within `server {` block in nginx.conf
82+
location / {
83+
content_handler_type clojure;
84+
content_handler_name 'example/my-app';
85+
}
86+
```
7587

76-
location /groovy {
77-
content_handler_type 'java';
78-
content_handler_name 'mytest.HelloService';
79-
}
88+
Make sure that the below source is in the classpath.
8089

90+
```clojure
91+
;;;my_app.clj
92+
(ns example
93+
(:require [compojure.core :refer :all]
94+
[compojure.route :as route]))
95+
96+
(defroutes my-app
97+
(GET "/" [] "<h1>Hello World</h1>")
98+
(route/not-found "<h1>Page not found</h1>"))
99+
```
81100

82-
```
83101

84-
Make sure that the class of the below source is in the classpath.
85-
```java
86-
package mytest;
102+
For Java:
87103

88-
import java.util.Map;
104+
```nginx
105+
##Within `server {` block in nginx.conf
106+
location /java {
107+
content_handler_type 'java';
108+
content_handler_name 'mytest.HelloService';
109+
}
89110

90-
import nginx.clojure.java.ArrayMap;
91-
import nginx.clojure.java.NginxJavaRingHandler;
92-
import static nginx.clojure.MiniConstants.*;
93111

94-
public class HelloService implements NginxJavaRingHandler {
112+
```
95113

96-
@Override
97-
public Object[] invoke(Map<String, Object> request) {
98-
return new Object[] {
99-
NGX_HTTP_OK, //http status 200
100-
ArrayMap.create(CONTENT_TYPE, "text/plain"), //headers map
101-
"Hello, Java & Nginx!" //response body can be string, File or Array/Collection of string or File
102-
};
103-
}
104-
}
105-
```
114+
Make sure that the class of the below source is in the classpath.
115+
```java
116+
package mytest;
117+
118+
import java.util.Map;
119+
120+
import nginx.clojure.java.ArrayMap;
121+
import nginx.clojure.java.NginxJavaRingHandler;
122+
import static nginx.clojure.MiniConstants.*;
123+
124+
public class HelloService implements NginxJavaRingHandler {
125+
126+
@Override
127+
public Object[] invoke(Map<String, Object> request) {
128+
return new Object[] {
129+
NGX_HTTP_OK, //http status 200
130+
ArrayMap.create(CONTENT_TYPE, "text/plain"), //headers map
131+
"Hello, Java & Nginx!" //response body can be string, File or Array/Collection of string or File
132+
};
133+
}
134+
}
135+
```
106136

107137
-----------------------------------
108138

109139
> **Note:**
110-
> For more advanced configurations such as external Http Service Handler, enable coroutine based socket, thread pool etc. Please check them from [HERE](configuration.html).
140+
> For more advanced configurations such as enable coroutine based socket, thread pool etc. Please check them from [HERE](configuration.html).
111141
112142
Start up
113143
--------------
114144

115145

116146
```nginx
117147
118-
$ cd nginx-clojure-0.3.0/nginx-1.6.2
148+
$ cd nginx-clojure-0.4.2/nginx-1.8.0
119149
$ ./nginx
120150
```
121151
If everything is ok, we can access our first http service by this url
@@ -124,6 +154,10 @@ If everything is ok, we can access our first http service by this url
124154
### For Clojure
125155
http://localhost:8080/clojure
126156
157+
### For Clojure Compojure Router
158+
http://localhost:8080
159+
160+
127161
### For Groovy
128162
http://localhost:8080/groovy
129163
@@ -136,7 +170,7 @@ We can check the logs/error.log to see error information.
136170
Reload
137171
--------------
138172

139-
If we change some settings we can reload the settings without stoping our services.
173+
If we change some settings we can reload the settings without stopping our services.
140174

141175
```nginx
142176
$ ./nginx -s reload

QuickStart.md.html

+63-29
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,26 @@ <h2>
2626
<span class="pl-c">### auto or a real path, e,g /usr/lib/jvm/java-7-oracle/jre/lib/amd64/server/libjvm.so</span>
2727
<span class="pl-k">jvm_path</span> auto;
2828

29-
<span class="pl-c">### my app jars e.g. clojure-1.5.1.jar , groovy-2.3.4.jar ,etc.</span>
29+
<span class="pl-c">### my app jars e.g. clojure-1.5.1.jar , groovy-2.3.4.jar,etc.</span>
3030
<span class="pl-c">### if we only use Java handler we need not place clojure-xxx.jar here.</span>
3131
<span class="pl-k">jvm_var</span> my_other_jars <span class="pl-s">'my_jar_dir/clojure-1.5.1.jar'</span>;
3232

3333
<span class="pl-c">### my app classpath, windows user should use ';' as the separator</span>
34+
<span class="pl-c">### for clojure devs the simplest way is to use the result jar file from `lein uberjar` here</span>
3435
<span class="pl-k">jvm_options</span> <span class="pl-s">"-Djava.class.path=jars/nginx-clojure-0.4.2.jar:#{my_other_jars}"</span>;
3536

3637
<span class="pl-c">###or we can put jars in some directories, e.g. jars-dir1, jars-dir2</span>
3738
<span class="pl-c">###so that all jars or direct sub directories from these directories will be appended to the jvm classpath</span>
3839
<span class="pl-c">##Note: if you use build-in extensions (e.g. SUN JCE) from jdk please append these dirs, e.g. the value of </span>
3940
<span class="pl-c">## System.getProperty("java.ext.dirs")</span>
40-
<span class="pl-k">jvm_options</span> <span class="pl-s">"-Djava.ext.dirs=jars-dir1:jars-dir2"</span> </pre></div>
41+
<span class="pl-k">jvm_options</span> <span class="pl-s">"-Djava.ext.dirs=jars-dir1:jars-dir2"</span>;</pre></div>
4142
</li>
4243
<li>
43-
<p>Setting inline Http Service Handler</p>
44+
<p>Setting Inline Http Service Handler</p>
4445

4546
<p>For Clojure:</p>
4647

47-
<div class="highlight highlight-nginx"><pre>
48+
<div class="highlight highlight-nginx"><pre> <span class="pl-c">##Within `server {` block in nginx.conf</span>
4849
<span class="pl-k">location</span> <span class="pl-en">/clojure </span>{
4950
<span class="pl-k">content_handler_type</span> <span class="pl-s">'clojure'</span>;
5051
<span class="pl-k">content_handler_code</span> <span class="pl-s">' </span>
@@ -59,7 +60,7 @@ <h2>
5960

6061
<p>For Groovy:</p>
6162

62-
<div class="highlight highlight-nginx"><pre>
63+
<div class="highlight highlight-nginx"><pre> <span class="pl-c">##Within `server {` block in nginx.conf</span>
6364
<span class="pl-k">location</span> <span class="pl-en">/groovy </span>{
6465
<span class="pl-k">content_handler_type</span> <span class="pl-s">'groovy'</span>;
6566
<span class="pl-k">content_handler_code</span> <span class="pl-s">' </span>
@@ -74,60 +75,93 @@ <h2>
7475
<span class="pl-s"> }</span>
7576
<span class="pl-s"> '</span>;
7677
}</pre></div>
78+
79+
<p>For Java:</p>
80+
81+
<blockquote>
82+
<p><strong>Note:</strong>
83+
So far nginx-clojure has not supported inline java handler, please see the next section to learn how to use an external java handler.</p>
84+
</blockquote>
7785
</li>
78-
</ol>
86+
<li>
87+
<p>Setting Compojure Router/External Http Service Handler</p>
88+
89+
<p>For Clojure:</p>
90+
91+
<div class="highlight highlight-nginx"><pre><span class="pl-c">##Within `server {` block in nginx.conf</span>
92+
<span class="pl-k">location</span> <span class="pl-en">/ </span>{
93+
<span class="pl-k">content_handler_type</span> clojure;
94+
<span class="pl-k">content_handler_name</span> <span class="pl-s">'example/my-app'</span>;
95+
}</pre></div>
96+
97+
<p>Make sure that the below source is in the classpath.</p>
98+
99+
<div class="highlight highlight-clojure"><pre><span class="pl-c">;;;my_app.clj</span>
100+
(<span class="pl-k">ns</span> <span class="pl-e">example</span>
101+
(<span class="pl-c1">:require</span> [compojure.core <span class="pl-c1">:refer</span> <span class="pl-c1">:all</span>]
102+
[compojure.route <span class="pl-c1">:as</span> route]))
103+
104+
(<span class="pl-k">defroutes</span> my-app
105+
(<span class="pl-en">GET</span> <span class="pl-s"><span class="pl-pds">"</span>/<span class="pl-pds">"</span></span> [] <span class="pl-s"><span class="pl-pds">"</span>&lt;h1&gt;Hello World&lt;/h1&gt;<span class="pl-pds">"</span></span>)
106+
(<span class="pl-en">route/not-found</span> <span class="pl-s"><span class="pl-pds">"</span>&lt;h1&gt;Page not found&lt;/h1&gt;<span class="pl-pds">"</span></span>))</pre></div>
79107

80108
<p>For Java:</p>
81109

82-
<div class="highlight highlight-nginx"><pre>
83-
<span class="pl-k">location</span> <span class="pl-en">/groovy </span>{
84-
<span class="pl-k">content_handler_type</span> <span class="pl-s">'java'</span>;
85-
<span class="pl-k">content_handler_name</span> <span class="pl-s">'mytest.HelloService'</span>;
86-
}
110+
<div class="highlight highlight-nginx"><pre> <span class="pl-c">##Within `server {` block in nginx.conf</span>
111+
<span class="pl-k">location</span> <span class="pl-en">/java </span>{
112+
<span class="pl-k">content_handler_type</span> <span class="pl-s">'java'</span>;
113+
<span class="pl-k">content_handler_name</span> <span class="pl-s">'mytest.HelloService'</span>;
114+
}
87115

88116
</pre></div>
89117

90118
<p>Make sure that the class of the below source is in the classpath.</p>
91119

92-
<div class="highlight highlight-java"><pre><span class="pl-k">package</span> <span class="pl-smi">mytest</span>;
120+
<div class="highlight highlight-java"><pre> <span class="pl-k">package</span> <span class="pl-smi">mytest</span>;
93121

94-
<span class="pl-k">import</span> <span class="pl-smi">java.util.Map</span>;
122+
<span class="pl-k">import</span> <span class="pl-smi">java.util.Map</span>;
95123

96-
<span class="pl-k">import</span> <span class="pl-smi">nginx.clojure.java.ArrayMap</span>;
97-
<span class="pl-k">import</span> <span class="pl-smi">nginx.clojure.java.NginxJavaRingHandler</span>;
98-
<span class="pl-k">import static</span> <span class="pl-smi">nginx.clojure.MiniConstants.*</span>;
124+
<span class="pl-k">import</span> <span class="pl-smi">nginx.clojure.java.ArrayMap</span>;
125+
<span class="pl-k">import</span> <span class="pl-smi">nginx.clojure.java.NginxJavaRingHandler</span>;
126+
<span class="pl-k">import static</span> <span class="pl-smi">nginx.clojure.MiniConstants.*</span>;
99127

100-
<span class="pl-k">public</span> <span class="pl-k">class</span> <span class="pl-en">HelloService</span> <span class="pl-k">implements</span> <span class="pl-e">NginxJavaRingHandler</span> {
128+
<span class="pl-k">public</span> <span class="pl-k">class</span> <span class="pl-en">HelloService</span> <span class="pl-k">implements</span> <span class="pl-e">NginxJavaRingHandler</span> {
101129

102-
<span class="pl-k">@Override</span>
103-
<span class="pl-k">public</span> <span class="pl-k">Object</span>[] <span class="pl-en">invoke</span>(<span class="pl-k">Map&lt;<span class="pl-smi">String</span>, <span class="pl-smi">Object</span>&gt;</span> <span class="pl-v">request</span>) {
104-
<span class="pl-k">return</span> <span class="pl-k">new</span> <span class="pl-smi">Object</span>[] {
105-
<span class="pl-c1">NGX_HTTP_OK</span>, <span class="pl-c">//http status 200</span>
106-
<span class="pl-smi">ArrayMap</span><span class="pl-k">.</span>create(<span class="pl-c1">CONTENT_TYPE</span>, <span class="pl-s"><span class="pl-pds">"</span>text/plain<span class="pl-pds">"</span></span>), <span class="pl-c">//headers map</span>
107-
<span class="pl-s"><span class="pl-pds">"</span>Hello, Java &amp; Nginx!<span class="pl-pds">"</span></span> <span class="pl-c">//response body can be string, File or Array/Collection of string or File</span>
108-
};
109-
}
110-
}</pre></div>
130+
<span class="pl-k">@Override</span>
131+
<span class="pl-k">public</span> <span class="pl-k">Object</span>[] <span class="pl-en">invoke</span>(<span class="pl-k">Map&lt;<span class="pl-smi">String</span>, <span class="pl-smi">Object</span>&gt;</span> <span class="pl-v">request</span>) {
132+
<span class="pl-k">return</span> <span class="pl-k">new</span> <span class="pl-smi">Object</span>[] {
133+
<span class="pl-c1">NGX_HTTP_OK</span>, <span class="pl-c">//http status 200</span>
134+
<span class="pl-smi">ArrayMap</span><span class="pl-k">.</span>create(<span class="pl-c1">CONTENT_TYPE</span>, <span class="pl-s"><span class="pl-pds">"</span>text/plain<span class="pl-pds">"</span></span>), <span class="pl-c">//headers map</span>
135+
<span class="pl-s"><span class="pl-pds">"</span>Hello, Java &amp; Nginx!<span class="pl-pds">"</span></span> <span class="pl-c">//response body can be string, File or Array/Collection of string or File</span>
136+
};
137+
}
138+
}</pre></div>
139+
</li>
140+
</ol>
111141

112142
<hr>
113143

114144
<blockquote>
115145
<p><strong>Note:</strong>
116-
For more advanced configurations such as external Http Service Handler, enable coroutine based socket, thread pool etc. Please check them from <a href="configuration.html">HERE</a>.</p>
146+
For more advanced configurations such as enable coroutine based socket, thread pool etc. Please check them from <a href="configuration.html">HERE</a>.</p>
117147
</blockquote>
118148

119149
<h2>
120150
<a id="user-content-start-up" class="anchor" href="#start-up" aria-hidden="true"><span class="octicon octicon-link"></span></a>Start up</h2>
121151

122152
<div class="highlight highlight-nginx"><pre>
123-
$ <span class="pl-k">cd</span> nginx-clojure-0.3.0/nginx-1.6.2
153+
$ <span class="pl-k">cd</span> nginx-clojure-0.4.2/nginx-1.8.0
124154
$ ./<span class="pl-k">nginx</span></pre></div>
125155

126156
<p>If everything is ok, we can access our first http service by this url</p>
127157

128158
<div class="highlight highlight-nginx"><pre><span class="pl-c">### For Clojure</span>
129159
http://localhost:8080/<span class="pl-k">clojure</span>
130160

161+
<span class="pl-c">### For Clojure Compojure Router</span>
162+
http://localhost:<span class="pl-k">8080</span>
163+
164+
131165
<span class="pl-c">### For Groovy</span>
132166
http://localhost:8080/<span class="pl-k">groovy</span>
133167

@@ -139,7 +173,7 @@ <h2>
139173
<h2>
140174
<a id="user-content-reload" class="anchor" href="#reload" aria-hidden="true"><span class="octicon octicon-link"></span></a>Reload</h2>
141175

142-
<p>If we change some settings we can reload the settings without stoping our services.</p>
176+
<p>If we change some settings we can reload the settings without stopping our services.</p>
143177

144178
<div class="highlight highlight-nginx"><pre>$ ./<span class="pl-k">nginx</span> -s reload</pre></div>
145179

0 commit comments

Comments
 (0)