diff --git a/back-end/server.py b/back-end/server.py new file mode 100644 index 0000000..4fb71e6 --- /dev/null +++ b/back-end/server.py @@ -0,0 +1,70 @@ +import json +from http.server import HTTPServer, BaseHTTPRequestHandler +from datetime import date +# from pymongo import MongoClient + + + +# mydatabase = client.TinyURLProject +# mycollection = mydatabase["URL"] +# print(client) +today = date.today().strftime("%m/%d/%Y") + +class MyServer(BaseHTTPRequestHandler): + def do_GET(self): + self.send_response(200) + self.send_header('Content-type', 'application/json') + self.send_header('Access-Control-Allow-Origin', '*') + self.end_headers() + + response = json.dumps({"name": "this is tiny url7"}) + self.wfile.write(response.encode('utf-8')) + + def do_OPTIONS(self): + self.send_response(200) + self.send_header('Content-type', 'application/json') + self.send_header('Access-Control-Allow-Origin', '*') + self.send_header("Access-Control-Allow-Methods", "GET,POST,OPTIONS") + self.send_header("Access-Control-Allow-Headers", "x-api-key,Content-Type") + self.end_headers() + + def do_POST(self): + print(self.path) + if (self.path == "/create-tiny-url"): + print("create-tiny-url get request!") + self.send_response(200) + self.send_header('Content-type', 'application/json') + self.send_header('Access-Control-Allow-Origin', '*') + self.send_header("Access-Control-Allow-Methods", "GET,POST,OPTIONS") + self.send_header("Access-Control-Allow-Headers", "x-api-key,Content-Type") + self.end_headers() + + data_string = self.rfile.read(int(self.headers['Content-Length'])) + print(data_string) + body = json.loads(data_string) + response = json.dumps({"tinyUrl": body.get("srcUrl"), "srcUrl": body.get("srcUrl"), + "creationDate": today}) # tinyurl: here need to call hash function for generate tinyUrl + self.wfile.write(response.encode('utf-8')) + if (self.path == "/get-original-url"): + print("get-original-url get request!") + self.send_response(200) + self.send_header('Content-type', 'application/json') + self.send_header('Access-Control-Allow-Origin', '*') + self.send_header("Access-Control-Allow-Methods", "GET,POST,OPTIONS") + self.send_header("Access-Control-Allow-Headers", "x-api-key,Content-Type") + self.end_headers() + + data_string = self.rfile.read(int(self.headers['Content-Length'])) + print(data_string) + body = json.loads(data_string) + response = json.dumps({"tinyUrl": body.get("tinyUrl"), "srcUrl": body.get("tinyUrl"), + "creationDate": today}) # tinyurl: here need to call hash function for generate tinyUrl + self.wfile.write(response.encode('utf-8')) + +def startServer(server_class=HTTPServer, handler_class=MyServer): + server_address = ('', 8000) + httpd = server_class(server_address, handler_class) + httpd.serve_forever(1.0) + + +startServer() diff --git a/tiny-url/src/html-request.js b/tiny-url/src/html-request.js new file mode 100644 index 0000000..2706922 --- /dev/null +++ b/tiny-url/src/html-request.js @@ -0,0 +1,53 @@ +export const getTinyUrl = async () => { + try { + const url = `http://localhost:8000/tiny`; + const response = await fetch(url) + const responseJson = await response.json(); + console.log(responseJson,"responseJson"); + // setTinyUrlObj(responseJson["name"]) + }catch (err){ + console.log(err) + } +} +export const postCreateTinyUrl = async (originalUrl,setTinyUrlObj) => { + console.log("postCreateTinyUrl") + try { + const url = `http://localhost:8000/create-tiny-url`; + const response = await fetch(url, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + srcUrl: originalUrl, + }) + }); + const responseJson = await response.json(); + console.log(responseJson,"responseJson"); + setTinyUrlObj(responseJson) + }catch (err){ + console.log(err) + } +} +export const postRedirectBySendTinyUrl = async (tinyUrl,setTinyUrlObj) => { + console.log("postRedirectBySendTinyUrl") + try { + const url = `http://localhost:8000/get-original-url`; + const response = await fetch(url, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + tinyUrl: tinyUrl, + }) + }); + const responseJson = await response.json(); + console.log(responseJson,"responseJson"); + setTinyUrlObj(responseJson) + }catch (err){ + console.log(err) + } +} diff --git a/tiny-url/src/pages/main-panel/MainPanel.css b/tiny-url/src/pages/main-panel/MainPanel.css index ab7c090..eac4bfb 100644 --- a/tiny-url/src/pages/main-panel/MainPanel.css +++ b/tiny-url/src/pages/main-panel/MainPanel.css @@ -3,4 +3,11 @@ flex-direction: column; justify-content: center; align-items: center; + padding-bottom: 30px; + +} +.forms-wrapper{ + width: 100%; + display: flex; + justify-content: space-around; } diff --git a/tiny-url/src/pages/main-panel/MainPanel.js b/tiny-url/src/pages/main-panel/MainPanel.js index 4fb9e39..ca91cb8 100644 --- a/tiny-url/src/pages/main-panel/MainPanel.js +++ b/tiny-url/src/pages/main-panel/MainPanel.js @@ -1,9 +1,16 @@ import {Header} from "./components/header/Header"; import {Form} from "./components/form/Form"; import './MainPanel.css' +import {postCreateTinyUrl, postRedirectBySendTinyUrl} from "../../html-request"; + export const MainPanel = () => { return