From 2ba2a42c2fd45ab344c1c8f824bdf5d0cab13691 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Tue, 19 May 2020 00:25:12 +0200 Subject: [PATCH] First commit --- .gitignore | 1 + README.md | 14 +++++ config.yaml | 3 + project.clj | 42 +++++++++++++ resources/public/css/main.css | 63 +++++++++++++++++++ resources/public/img/rabbit.svg | 82 +++++++++++++++++++++++++ resources/public/js/main.js | 3 + resources/templates/layouts/base.html | 29 +++++++++ resources/templates/public/404.html | 9 +++ resources/templates/public/welcome.html | 12 ++++ src/api2smtp/config.clj | 5 ++ src/api2smtp/core.clj | 24 ++++++++ src/api2smtp/urls.clj | 20 ++++++ src/api2smtp/views/public.clj | 20 ++++++ 14 files changed, 327 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 config.yaml create mode 100644 project.clj create mode 100644 resources/public/css/main.css create mode 100644 resources/public/img/rabbit.svg create mode 100644 resources/public/js/main.js create mode 100644 resources/templates/layouts/base.html create mode 100644 resources/templates/public/404.html create mode 100644 resources/templates/public/welcome.html create mode 100644 src/api2smtp/config.clj create mode 100644 src/api2smtp/core.clj create mode 100644 src/api2smtp/urls.clj create mode 100644 src/api2smtp/views/public.clj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..b23143d --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# api2smtp + +## Usage + +```sh +lein run +``` + +## Testing + +``` sh +lein check-idiomatic +lein check-format +``` diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..2c60228 --- /dev/null +++ b/config.yaml @@ -0,0 +1,3 @@ +domain: "http://localhost" +debug: true +port: 7404 diff --git a/project.clj b/project.clj new file mode 100644 index 0000000..fa48e05 --- /dev/null +++ b/project.clj @@ -0,0 +1,42 @@ +(defproject api2smtp "0.1.0" + :description "api2smtp" + :url "" + :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0" + :url "https://www.eclipse.org/legal/epl-2.0/"} + :dependencies [;; Clojure + [org.clojure/clojure "1.10.1"] + ;; Tadam core + [tadam-core "0.3.2"] + ;; HTTP Server + [ring "1.8.0"] + ;; Ring middleware that prevents CSRF attacks + [ring/ring-defaults "0.3.2"] + [ring/ring-anti-forgery "1.3.0"] + ;; Routing + [compojure "1.6.1"] + ;; Cors + [ring-cors "0.1.13"] + ;; Templates + [selmer "1.12.12"] + ;; Validations + [jkkramer/verily "0.6.0"] + ;; Yaml + [clj-yaml "0.4.0"] + ;; JSON encoding + [cheshire "5.9.0"]] + :plugins [;; DEV TOOLS + ;;; Check idiomatic bug + [lein-kibit "0.1.7"] + ;;; Check format + [lein-cljfmt "0.6.4"] + ;;; Generate documentation + [lein-codox "0.10.7"]] + ;; Map configuration for Ring + :ring {:handler api2smtp.core.wrapped-handler} + ;; ALIAS + :aliases {"check-idiomatic" ["kibit" "src"] + "check-format" ["cljfmt" "check"]} + ;; LEIN + :main ^:skip-aot api2smtp.core + :aot [api2smtp.core] + :repl-options {:init-ns api2smtp.core}) diff --git a/resources/public/css/main.css b/resources/public/css/main.css new file mode 100644 index 0000000..35c1338 --- /dev/null +++ b/resources/public/css/main.css @@ -0,0 +1,63 @@ +@import url(https://fonts.googleapis.com/css?family=Raleway); +body { + font-family: Raleway, arial; + margin: 0; + background: yellow; + height: 100vh; + margin-top: 8rem; +} +header { + display: flex; + justify-content: space-between; + align-items: center; + background-color: #3fa46a; + position: fixed; + top: 0; + left: 0; + right: 0; +} +header > a { + margin: 1rem; +} +header > a > img { + height: 3rem; +} +header ul { + display: flex; + justify-content: flex-end; + list-style: none; + padding: 0; + margin: 0; +} +header ul a { + display: block; + margin: 1rem; + padding: 1rem; + color: white; + text-decoration: initial; +} +header ul a:hover { + background-color: white; + color: #3fa46a; +} +main { + max-width: 800px; + margin: 3rem auto; +} +footer { + text-align: center; +} + +.title-welcome { + text-align: center; + color: #3fa46a; +} + +.logo-welcome { + text-align: center; +} + +.logo-welcome img { + width: 20rem; + margin: 4rem auto; +} diff --git a/resources/public/img/rabbit.svg b/resources/public/img/rabbit.svg new file mode 100644 index 0000000..2c138ba --- /dev/null +++ b/resources/public/img/rabbit.svg @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/resources/public/js/main.js b/resources/public/js/main.js new file mode 100644 index 0000000..b082f22 --- /dev/null +++ b/resources/public/js/main.js @@ -0,0 +1,3 @@ +document.addEventListener('DOMContentLoaded', () => { + +}); diff --git a/resources/templates/layouts/base.html b/resources/templates/layouts/base.html new file mode 100644 index 0000000..4c2c462 --- /dev/null +++ b/resources/templates/layouts/base.html @@ -0,0 +1,29 @@ + + + + + + + + + {% block title %}{% endblock %} | Tadam Framework + + +
+ + brand + + +
+
+ {% block content %}{% endblock %} +
+ + + diff --git a/resources/templates/public/404.html b/resources/templates/public/404.html new file mode 100644 index 0000000..3c7952a --- /dev/null +++ b/resources/templates/public/404.html @@ -0,0 +1,9 @@ +{% extends "layouts/base.html" %} + +{% block title %} +404 +{% endblock %} + +{% block content %} +

404

+{% endblock %} diff --git a/resources/templates/public/welcome.html b/resources/templates/public/welcome.html new file mode 100644 index 0000000..aeb4490 --- /dev/null +++ b/resources/templates/public/welcome.html @@ -0,0 +1,12 @@ +{% extends "layouts/base.html" %} + +{% block title %} +Welcome to Tadam +{% endblock %} + +{% block content %} +

Welcome to Tadam Framework

+

+ rabbit +

+{% endblock %} diff --git a/src/api2smtp/config.clj b/src/api2smtp/config.clj new file mode 100644 index 0000000..8a2a111 --- /dev/null +++ b/src/api2smtp/config.clj @@ -0,0 +1,5 @@ +(ns api2smtp.config + (:require + [clj-yaml.core :as yaml])) + +(def config (yaml/parse-string (slurp "config.yaml"))) diff --git a/src/api2smtp/core.clj b/src/api2smtp/core.clj new file mode 100644 index 0000000..ea66664 --- /dev/null +++ b/src/api2smtp/core.clj @@ -0,0 +1,24 @@ +(ns api2smtp.core + (:require + [api2smtp.config :refer [config]] + [ring.middleware.defaults :refer [site-defaults wrap-defaults]] + [ring.middleware.reload :refer [wrap-reload]] + [ring.middleware.cors :refer [wrap-cors]] + [api2smtp.urls :refer [all-routes]] + [ring.adapter.jetty :refer [run-jetty]]) (:gen-class)) + +(def wrapped-handler + ;; Handler middlewares + (-> all-routes + (wrap-defaults (assoc-in site-defaults [:security :anti-forgery] false)) + (wrap-cors + :access-control-allow-origin [(re-pattern (if (config :debug) ".*" (config :domain)))] + :access-control-allow-methods [:get]) + (#(if (config :debug) (wrap-reload %) %)))) + +(defn -main [& args] + ;; Main + ;; Welcome + (prn (str "Open " (config :domain) ":" (config :port))) + ;; Run web server + (run-jetty wrapped-handler {:port (config :port)})) diff --git a/src/api2smtp/urls.clj b/src/api2smtp/urls.clj new file mode 100644 index 0000000..31760d3 --- /dev/null +++ b/src/api2smtp/urls.clj @@ -0,0 +1,20 @@ +(ns api2smtp.urls + (:require + [compojure.core :refer [defroutes GET]] + [compojure.route :as route] + [api2smtp.views.public :as view-public])) + +(defroutes public + ;; Urls public pages + (GET "/" [] view-public/index) + (GET "/api" [] view-public/api)) + + +(defroutes resources-routes + ;; Resources (statics) + (route/resources "/") + (route/not-found view-public/page-404)) + +(def all-routes + ;; Wrap routers. "resources-routes" should always be the last. + (compojure.core/routes public resources-routes)) diff --git a/src/api2smtp/views/public.clj b/src/api2smtp/views/public.clj new file mode 100644 index 0000000..e551611 --- /dev/null +++ b/src/api2smtp/views/public.clj @@ -0,0 +1,20 @@ +;;;; Views public web +(ns api2smtp.views.public + (:require + [tadam.templates :refer [render-HTML render-JSON render-404]] + )) + +(defn index + ;; View HTML + [req] + (render-HTML req "public/welcome.html" {})) + +(defn api + ;; View JSON + [req] + (render-JSON req {:result true})) + +(defn page-404 + ;; View page 404 + [req] + (render-404 req "public/404.html" {}))