This is a Clojure application it uses Pedestal and clj-time as dependencies. The application simulates an ad server, it serve json ads to defined channels.
In Clojure are a lot of libraries that can be used for this purpose like ring + compojure + liberator or http-kit.
I choose pedestal because it uses ring under the hood and offers routing out of the box. But the feature I like most is that pedestal uses core async channels to serve non blocking micro-services, I think that asynchronous request handling is a huge benefit on performance.
- Clone or download the repository.
- Start the application:
lein run
inside the application folder - Go to localhost:8080 to see the application running
- Read your app's source code at src/ads_mockup/
- Run your app's tests with
lein test
. Read the tests at test/ads_mockup/service_test.clj.
The Pedestal website states that it can't be run in Windows but I run the service on windows without any problem. So be careful running on windows because is not yet officially supported by the current version of pedestal. I also run this application in my Debian 8 Linux box without any troubles. It should work well on OSX too but I didn't testes it. And any other platforms where the JVM is supported. My version of the JDK is:
java version "1.8.0_77"
Lein version:
Leiningen 2.6.1 on Java 1.8.0_77 Java HotSpot(TM) Server VM
To see the individual libraries version, refer to: project.clj
To configure logging see config/logback.xml. By default, the app logs to stdout and logs/. To learn more about configuring Logback, read its documentation.
- localhost:8080 is the root of the server.
- localhost:8080/api is the api end point.
- To get an ad you must pass query parameters to the application in the
api
end point.
The services can be viewed with: . curl, on the command line. . postman plug-in, on Chrome. . HttpRequester plug-in, on Firefox. . Or even the browser window can show the json content but if you want to examine the request/response in more detail you need to use one of these programs.
I Extensively use the logging service, if you need to view how the request is processed in detail I suggest to open the logs and check it on every request. The logs will help to figure out what is happening with the ads, also if you lost an ad check the date of the ad because some ads have five minutes of life.
The ads are being served to certain channels and to get an ad you must pass a channel parameter.
http://localhost:8080/api?channel=www.news.com
All defined channels and ads are in the in-memory data file ads_data.clj.
From here you can filter your ads using more query parameters:
Filter by country:
http://localhost:8080/api?channel=www.news.com&country=US
Filter by language:
http://localhost:8080/api?channel=www.news.com&lang=en
Filter by gender:
http://localhost:8080/api?channel=www.news.com&gender=F
Filter by age:
http://localhost:8080/api?channel=www.news.com&age=33
Or you can mix all the parameters and see what you can get, only the channel parameter is mandatory because you need to know who is requesting the ad
http://localhost:8080/api?channel=www.news.com&country=US&lang=en&gender=F&age=33
- Start a new REPL:
lein repl
- Start your service in dev-mode:
(def dev-serv (run-dev))
- Connect your editor to the running REPL session. Re-evaluated code will be seen immediately in the service.
Docker container support
- Build an uberjar of your service:
lein uberjar
- Build a Docker image:
sudo docker build -t ads-mockup .
- Run your Docker image:
docker run -p 8080:8080 ads-mockup
- Build and run your image:
capstan run -f "8080:8080"
Once the image it built, it's cached. To delete the image and build a new one:
capstan rmi ads-mockup; capstan build