简体   繁体   中英

Clojure: How can I set up a specific url for a server?

I new with clojure and I am trying to host a server with a specific url using it.

Doing some research online I got something as below.

(ns rest-demo.core
  (:require [org.httpkit.server :as server]
            [compojure.core :refer :all]
            [compojure.route :as route]
            [ring.middleware.defaults :refer :all]
            [clojure.string :as str]
            [clojure.data.json :as json])
  (:gen-class))

(defn hello-name
  [req]
  {:status  200
   :headers {"Content-Type" "text/html"}
   :body (str "Hello " (:name (:params req)))})

(defroutes app-routes
  (GET "/hello" [] hello-name)
  (route/not-found "Error, page not found!"))

(defn -main
  "This is our main entry point"
  [& args]
  (let [port (Integer/parseInt (or (System/getenv "PORT") "3000"))]
    ; Run the server with Ring.defaults middleware
    (server/run-server (wrap-defaults #'app-routes site-defaults) {:port port})))

Running with lein run and accessing the 127.0.0.1:3000/hello I can access the API.

How would I host this with a url of my choice?

Thanks in advance!

A URL* consists of several parts, two of the most important ones are

  1. the hostname of the computer on which it's being hosted
  2. the path to the specific page.

It looks like you have taken care of #2. For solving #1 you can take two approaches:

  1. find a good place on the internet to host your program. Something like Heroku
  2. find a way to give you laptop a name on the internet. Something like https://portmap.io/

when you get these problems solved then we get back to the programming part of this question. httpskit will by default answer requests with any hostname. so you don't need to do anything and can accept the defaults.

'*' there are distinctions between URL vs. URI at play here and i'm glossing over them

Hosting this on a specific URL of your choice basically means, you would like your web application to be accessible on that particular URL. For that, you would first have to purchase the domain (for example.com) from an ICANN-accredited domain name registrar(like Namecheap, domain.com, Godaddy) for a fee and then configure it to redirect it to the server where you are sunning the service from. In this case, it would your machine's public IP address(or as @Arthur_Ulfeldt mentioned use something like portmap or ngrok).

This is certainly not recommended as your machine might not have a good uptime or reliable connectivity. Which is why you would host it on a cloud solution. Either something like Heroku(a PaaS which would be the best choice for you as it is really easy to deploy for Clojure) or some compute instances from any of the providers(AWS ec2 or GCP compute engine or Azure VM's) just clone your application there, run your server there and redirect all your requests there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM