Web server and DB integration(2)

Artificial Intelligence Technology   Semantic Web Technology   Search Technology     DataBase Technology   Ontology Technology   Digital Transformation Technology   User Interface and DataVisualization   Workflow & ServicesProgramming  Clojure

In the previous article, we first set up a server for the purpose of using machine learning results in a database. In this article, we will discuss routing as the next step.

In this article, I will introduce compojure, which is responsible for routing, one of the essential functions of web applications. Routing is defined as “the process of linking the contents of a client request with the contents of a server process.

The contents of a client request are (1) the HTTP method, (2) the request URL, and (3) the data that the client sends to the server. HTTP method” refers to the four instructions to the server: GET (to retrieve data), POST (to send data (mainly to create new data)), PUT (to send data (mainly to update existing data)), and DELETE (to delete data). An example of compojure code is shown below.

(defroutes handler
  (GET "/" req home)
  (GET "/my-page" req my-page))

The above code shows that when “req” data comes to a web page address (e.g., http:localhost:8080/ or http:localhost:8080/my-page), the variable or function represented by home or my-page is sent (to be retrieved when GET comes). The code flow is as follows.

As a concrete code flow, add the following library specification to the dependencies of project.clj.

:dependencies [[org.clojure/clojure "1.10.1"]
                           [ring "1.8.2"]
                           [compojure "1.6.2"]]

After adding the above code, the library will be automatically added from the web when the REPL is running, but before that, you can download the library by running “lein deps” in the terminal from the top of the folder where the application is located. This command will download the library from an external repository and store it in a local repository on your PC.

Next, rewrite ns in the core.clj file in src as follows

(ns todo-clj.core
  (:require [compojure.core :refer [defroutes context GET]]
            [compojure.route :as route]
            [ring.adapter.jetty :as server]
            [ring.util.response :as res]))

This will be the function that imports the library you specified earlier (downloaded to the local repository) into the code.

Next, add the following code as a handler function.

(defroutes handler
  (GET "/" req home)
  (GET "/my-page" req my-page)
  (route/not-found "<h1>404 page not found</h1>"))

In addition, add a function to specify the header part of the response data.

(defn html [res]
  (res/content-type res "text/html; charset=utf-8"))

The above uses the ring.util.response/content-type function in the ring.util.response library. This takes the response map and the value to be set in the Context-Type of the header, and sets the received value in the “Context-Type” key of the response map header.

In addition, we define the home and my-page functions defined in the handler.

(defn home [req]
  (-> (home-view req)
      res/response
      html))

(defn my-page [req]
  (-> (my-page-view req)
      res/response
      html))

The ring.util.response/response used above will be the function that receives the body and generates the response map. Next, we define home-view and my-page-view as follows.

(defn home-view [req]
  "<h1>ホーム画面</h1>
   <a href="/home">ホーム 一覧</a>")
(defn my-page-view [req]
  "<h1>マイページ画面</h1>
   <a href="/my-page">マイページ 一覧</a>")

The above is a direct description of HTML data as view. There are also ways to express these data using Clojure functions (hiccup, etc.), but we will discuss those later. Finally, if we add the server setting code mentioned above, we can start up the server and do the routing.

(defonce server (atom nil))

(def app
  (routes
   handler))

(defn start-server []
  (when-not @server
    (reset! server (server/run-jetty #'app {:port 3000 :join? false}))))

(defn stop-server []
  (when @server
    (.stop @server)
    (reset! server nil)))

(defn restart-server []
  (when @server
    (stop-server)
    (start-server)))

As before, you can see the results by REPLing on spacemacs and accessing localhost:3000/. In the next article, we will discuss setting up and linking databases.

コメント

Exit mobile version
タイトルとURLをコピーしました