about summary refs log tree commit diff
pre { line-height: 125%; } td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } .highlight .hll { background-color: #0000ff } .highlight .c { color: #0F0 } /* Comment */ .highlight .err { color: #DDD } /* Error */ .highlight .esc { color: #DDD } /* Escape */ .highlight .g { color: #DDD } /* Generic */ .highlight .k { color: #F00 } /* Keyword */ .highlight .l { color: #DDD } /* Literal */ .highlight .n { color: #DDD } /* Name */ .highlight .o { color: #DDD } /* Operator */ .highlight .x { color: #DDD } /* Other */ .highlight .p { color: #DDD } /* Punctuation */ .highlight .ch { color: #0F0 } /* Comment.Hashbang */ .highlight .cm { color: #0F0 } /* Comment.Multiline */ .highlight .cp { color: #E5E5E5 } /* Comment.Preproc */ .highlight .cpf { color: #0F0 } /* Comment.PreprocFile */ .highlight .c1 { color: #0F0 } /* Comment.Single */ .highlight .cs { color: #0F0 } /* Comment.Special */ .highlight .gd { color: #DDD } /* Generic.Deleted */ .highlight .ge { color: #DDD } /* Generic.Emph */ .highlight .ges { color: #DDD } /* Generic.EmphStrong */ .highlight .gr { color: #DDD } /* Generic.Error */ .highlight .gh { color: #DDD } /* Generic.Heading */ .highlight .gi { color: #DDD } /* Generic.Inserted */ .highlight .go { color: #DDD } /* Generic.Output */ .highlight .gp { color: #DDD } /* Generic.Prompt */ .highlight .gs { color: #DDD } /* Generic.Strong */ .highlight .gu { color: #DDD } /* Generic.Subheading */ .highlight .gt { color: #DDD } /* Generic.Traceback */ .highlight .kc { color: #F00 } /* Keyword.Constant */ .highlight .kd { color: #F00 } /* Keyword.Declaration */ .highlight .kn { color: #F00 } /* Keyword.Namespace */ .highlight .kp { color: #F00 } /* Keyword.Pseudo */ .highlight .kr { color: #F00 } /* Keyword.Reserved */ .highlight .kt { color: #EE82EE } /* Keyword.Type */ .highlight .ld { color: #DDD } /* Literal.Date */ .highlight .m { color: #F0F } /* Literal.Number */ .highlight .s { color: #87CEEB } /* Literal.String */ .highlight .na { color: #DDD } /* Name.Attribute */ .highlight .nb { color: #DDD } /* Name.Builtin */ .highlight .nc { color: #DDD } /* Name.Class */ .highlight .no { color: #7FFFD4 } /* Name.Constant */ .highlight .nd { color: #DDD } /* Name.Decorator */ .highlight .ni { color: #DDD } /* Name.Entity */ .highlight .ne { color: #DDD } /* Name.Exception */ .highlight .nf { color: #FF0 } /* Name.Function */ .highlight .nl { color: #DDD } /* Name.Label */ .highlight .nn { color: #DDD } /* Name.Namespace */ .highlight .nx { color: #DDD } /* Name.Other */ .highlight .py { color: #DDD } /* Name.Property */ .highlight .nt { color: #DDD } /* Name.Tag */ .highlight .nv { color: #EEDD82 } /* Name.Variable */ .highlight .ow { color: #F00 } /* Operator.Word */ .highlight .pm { color: #DDD } /* Punctuation.Marker */ .highlight .w { color: #DDD } /* Text.Whitespace */ .highlight .mb { color: #F0F } /* Literal.Number.Bin */ .highlight .mf { color: #F0F } /* Literal.Number.Float */ .highlight .mh { color: #F0F } /* Literal.Number.Hex */ .highlight .mi { color: #F0F } /* Literal.Number.Integer */ .highlight .mo { color: #F0F } /* Literal.Number.Oct */ .highlight .sa { color: #87CEEB } /* Literal.String.Affix */ .highlight .sb { color: #87CEEB } /* Literal.String.Backtick */ .highlight .sc { color: #87CEEB } /* Literal.String.Char */ .highlight .dl { color: #87CEEB } /* Literal.String.Delimiter */ .highlight .sd { color: #87CEEB } /* Literal.String.Doc */ .highlight .s2 { color: #87CEEB } /* Literal.String.Double */ .highlight .se { color: #87CEEB } /* Literal.String.Escape */ .highlight .sh { color: #87CEEB } /* Literal.String.Heredoc */ .highlight .si { color: #87CEEB } /* Literal.String.Interpol */ .highlight .sx { color: #87CEEB } /* Literal.String.Other */ .highlight .sr { color: #87CEEB } /* Literal.String.Regex */ .highlight .s1 { color: #87CEEB } /* Literal.String.Single */ .highlight .ss { color: #87CEEB } /* Literal.String.Symbol */ .highlight .bp { color: #DDD } /* Name.Builtin.Pseudo */ .highlight .fm { color: #FF0 } /* Name.Function.Magic */ .highlight .vc { color: #EEDD82 } /* Name.Variable.Class */ .highlight .vg { color: #EEDD82 } /* Name.Variable.Global */ .highlight .vi { color: #EEDD82 } /* Name.Variable.Instance */ .highlight .vm { color: #EEDD82 } /* Name.Variable.Magic */ .highlight .il { color: #F0F } /* Literal.Number.Integer.Long */

th: 0.0%;'/> -rw-r--r--src/main.rs16
6 files changed, 48 insertions, 25 deletions
diff --git a/Cargo.toml b/Cargo.toml
index c717f19..db5fa3f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -17,5 +17,6 @@ crc32fast = "1.2.0"
 base64 = "0.12.0"
 url = "2.1.1"
 diesel = { version = "1.4.4", features = ["postgres", "r2d2"] }
+actix-files = "0.2.1"
 
 
diff --git a/client/index.html b/client/index.html
index a8aa831..5b8459e 100644
--- a/client/index.html
+++ b/client/index.html
@@ -2,9 +2,11 @@
 <html lang="en">
 <head>
     <meta charset="UTF-8">
-    <title>Shor</title>
-    <link rel="stylesheet" href="main.css">
-    <script src="main.js"></script>
+    <title>Shorest</title>
+    <link rel="stylesheet" href="static/main.css">
+    <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
+    <script src="https://cdnjs.cloudflare.com/ajax/libs/validate.js/0.13.1/validate.min.js"></script>
+    <script src="static/main.js"></script>
 </head>
 <body>
 <div class="active">
@@ -12,11 +14,11 @@
         <a><b>sho.rest</b><br></a>
         <a>Made with ❤ by <b>Mel</b></a>
     </div>
-    <form class="form-inline" onsubmit="onFormSubmit(); return false;">
+    <form id="form">
         <div class="input-group" id="form-group">
             <div class="input-container">
                 <a class="input-field-text">https://</a>
-                <input oninput="inputUpdate()" pattern="(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?" class="input-field" id="url" required>
+                <input class="input-field" id="url" required>
             </div>
             <div class="btn-container">
                 <input type="submit" value="→" class="btn">
diff --git a/client/main.js b/client/main.js
deleted file mode 100644
index 652776a..0000000
--- a/client/main.js
+++ /dev/null
@@ -1,13 +0,0 @@
-function onFormSubmit() {
-    return false;
-}
-
-function inputUpdate() {
-    const pattern = /^(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?&/;
-    const userInput = document.getElementById('url').value;
-    if (pattern.test(userInput)) {
-        document.getElementById('form-group').style.borderColor = '#E0E0E0';
-    } else {
-        document.getElementById('form-group').style.borderColor = '#FFBCBC';
-    }
-}
\ No newline at end of file
diff --git a/client/main.css b/client/static/main.css
index 8f84f71..1726881 100644
--- a/client/main.css
+++ b/client/static/main.css
@@ -86,6 +86,7 @@ html * {
     touch-action: manipulation;
     cursor: pointer;
     background-image: none;
+    background-color: #fff;
     padding: 4px 5vw;
     height: 10vh;
     -webkit-user-select: none;
diff --git a/client/static/main.js b/client/static/main.js
new file mode 100644
index 0000000..aa823c0
--- /dev/null
+++ b/client/static/main.js
@@ -0,0 +1,30 @@
+$(document).ready(function() {
+    $('#form').on('submit', onFormSubmit);
+    $('#url').on({'input': inputUpdate, 'paste': pasteTrim});
+});
+
+function onFormSubmit() {
+    const urlField = document.getElementById('url');
+    const data = JSON.stringify({'url': 'https://' + urlField.value});
+    $.ajax('/', {method: 'POST', data: data, contentType: 'application/json'}).then(function (r) {
+        urlField.value = 'sho.rest/' + r.hash;
+    })
+    return false;
+}
+
+function inputUpdate() {
+    const userInput = document.getElementById('url').value;
+    if (!validate({website: 'https://' + userInput}, {website: {url: true}})) {
+        $('#form-group').css('border-color', '#E0E0E0');
+    } else {
+        $('#form-group').css('border-color', '#FFBCBC');
+    }
+}
+
+function pasteTrim() {
+    const pattern = /^https?:\/\//;
+    setTimeout(() => {
+        const element = $('#url');
+        element.value = element.value.replace(pattern, '');
+    }, 0);
+}
\ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
index ab4c044..12c1356 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,11 +7,12 @@ use schema::links;
 mod types;
 use types::*;
 
-use actix_web::{middleware, web, HttpServer, App, HttpResponse, Result};
+use actix_web::{middleware, web, HttpServer, App, HttpResponse, Result, HttpRequest};
 use actix_web::web::{Json, Path, Data};
 use diesel::{PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, QueryResult};
 use dotenv::dotenv;
 use diesel::r2d2::{ConnectionManager, Pool};
+use actix_files::{Files, NamedFile};
 
 fn establish_connection() -> Pool<ConnectionManager<PgConnection>> {
     dotenv().ok();
@@ -58,16 +59,14 @@ fn add_to_database_safely(mut hash: String, user_url: String, connection: &PgCon
             }
         }
         Err(_) => {
-            if add_entry_to_database(Entry { hash: hash.clone() , url: user_url }, connection).is_err() {
-
-            }
+            add_entry_to_database(Entry { hash: hash.clone() , url: user_url }, connection).unwrap();
         }
     };
     hash
 }
 
-async fn root() -> HttpResponse {
-    HttpResponse::Ok().body("Please make a POST request to / in the format {'url': '<your_url>'} with the URL you want to shorten!")
+async fn root(req: HttpRequest) -> HttpResponse {
+    NamedFile::open("./client/index.html").unwrap().into_response(&req).unwrap()
 }
 
 async fn shorten(params: Json<UserData>, state: Data<PoolState>) -> HttpResponse {
@@ -77,7 +76,7 @@ async fn shorten(params: Json<UserData>, state: Data<PoolState>) -> HttpResponse
             return HttpResponse::BadRequest().body("The URL you entered does not follow the proper URL format.");
         },
     };
-    let hash= add_to_database_safely(get_hash_from_string(&user_url), user_url, &state.get().expect("Could not get a connection from pool"));
+    let hash = add_to_database_safely(get_hash_from_string(&user_url), user_url, &state.get().expect("Could not get a connection from pool"));
 
     HttpResponse::Ok().json(UserResponse{ hash })
 }
@@ -109,6 +108,9 @@ async fn main() -> std::io::Result<()> {
                 web::resource("/{hash}")
                     .route(web::get().to(redirect))
             )
+            .service(
+                Files::new("/static/", "./client/static/")
+            )
     })
         .bind("localhost:3000")?
         .run()