From e5a84268cc33d64716bbcfd5c59f918c1f3d15a7 Mon Sep 17 00:00:00 2001 From: Gnarwhal Date: Fri, 29 Jan 2021 03:30:05 -0500 Subject: [PATCH] Completed first revision of front end. Enabled CORS in spring. Allowed path variable to be optional. --- backend/.gitignore | 3 + .../main/java/achievements/Application.java | 15 + .../achievements/controllers/Controller.java | 8 +- frontend/static_server.js | 52 +--- frontend/webpage/index.html | 273 ++---------------- frontend/webpage/scripts/index.js | 38 ++- frontend/webpage/scripts/template.js | 145 ++++++++++ frontend/webpage/styles/index.css | 22 +- .../templates/achievements_page.html.template | 40 +++ .../templates/games_page.html.template | 53 ++++ .../templates/profile_page.html.template | 135 +++++++++ 11 files changed, 456 insertions(+), 328 deletions(-) create mode 100644 frontend/webpage/scripts/template.js create mode 100644 frontend/webpage/templates/achievements_page.html.template create mode 100644 frontend/webpage/templates/games_page.html.template create mode 100644 frontend/webpage/templates/profile_page.html.template diff --git a/backend/.gitignore b/backend/.gitignore index 8f8894d..eaa6b6f 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -19,3 +19,6 @@ gradlew.bat # Local property file src/main/resources/application-*.properties + +# Server Keystore +src/main/resources/achievements-ssl-key.p12 diff --git a/backend/src/main/java/achievements/Application.java b/backend/src/main/java/achievements/Application.java index 44b5d01..b622eff 100644 --- a/backend/src/main/java/achievements/Application.java +++ b/backend/src/main/java/achievements/Application.java @@ -3,6 +3,9 @@ package achievements; import achievements.services.DbConnectionService; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @SpringBootApplication public class Application { @@ -16,4 +19,16 @@ public class Application { SpringApplication.exit(context, () -> 0); } } + + @Bean + public WebMvcConfigurer configurer() { + return new WebMvcConfigurer() { + @Override + public void addCorsMappings(CorsRegistry registry) { + registry + .addMapping("/*") + .allowedOrigins("*"); + } + }; + } } \ No newline at end of file diff --git a/backend/src/main/java/achievements/controllers/Controller.java b/backend/src/main/java/achievements/controllers/Controller.java index 4cc455f..7595ba5 100644 --- a/backend/src/main/java/achievements/controllers/Controller.java +++ b/backend/src/main/java/achievements/controllers/Controller.java @@ -21,8 +21,8 @@ public class Controller { public Controller() {} - @RequestMapping(value = "/achievements/{Name}", method = GET, produces = "application/json") - public ResponseEntity fetchAchievements(@PathVariable("Name") String getName) { + @RequestMapping(value = { "/achievements", "/achievements/{Name}" }, method = GET, produces = "application/json") + public ResponseEntity fetchAchievements(@PathVariable(value = "Name", required = false) String getName) { var achievements = (Achievements) null; if (getName == null) { achievements = db.getAchievements("%"); @@ -42,8 +42,8 @@ public class Controller { } } - @RequestMapping(value = "/games/{Name}", method = GET, produces = "text/html") - public ResponseEntity fetchGames(@PathVariable("Name") String getName) { + @RequestMapping(value = { "/games", "/games/{Name}" }, method = GET, produces = "application/json") + public ResponseEntity fetchGames(@PathVariable(value = "Name", required = false) String getName) { var games = (Games) null; if (getName == null) { games = db.getGames("%"); diff --git a/frontend/static_server.js b/frontend/static_server.js index 0f05bed..75509c5 100644 --- a/frontend/static_server.js +++ b/frontend/static_server.js @@ -1,58 +1,16 @@ const express = require('express'); const morgan = require('morgan' ); const fs = require('fs' ); -const http = require('http' ); +const https = require('https' ); const config = require('./config.js').load(process.argv[2]); +if (config.build === 'debug') { + process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0; +} + const app = express(); app.use("/", morgan("dev")); -app.use("/", (request, response, next) => { - /*if (request.headers['accept'].split(',')[0] === 'text/html') { - let path = request.url; - if (path === '/') { - path = "/index.html"; - } - path = 'webpage' + path; - fs.readFile(path, 'utf8', (err, fileData) => { - if (err) { - response.statusCode = 500; - response.statusMessage = 'Unkown file read error'; - console.log(err); - } - - if (path === 'webpage/index.html') { - console.log("ummmm"); - dbRequest = http.get("http://localhost:4730/achievements", dbResponse => { - console.log("Yesting?"); - dbResponse.on('data', achievementData => { - console.log("Hello?"); - achievementData = JSON.parse(achievementData); - let list = ""; - for (const achievement of achievementData.achievements) { - list = list + - `
- Achievement Icon.png -

${achievement.name}

-

${achievement.description}

-

${achievement.gameID}

-
`; - } - - response.statusCode = 200; - response.statusMessage = 'OK'; - response.write(Buffer.from(fileDate.replace("\${list}", list), 'utf8')); - response.end(); - }); - }); - } - - }); - } else { - next(); - }*/ - next(); -}); app.use("/", express.static("webpage")); app.listen(config.port); \ No newline at end of file diff --git a/frontend/webpage/index.html b/frontend/webpage/index.html index e731f36..4035170 100644 --- a/frontend/webpage/index.html +++ b/frontend/webpage/index.html @@ -1,272 +1,35 @@ - Achievements Project + Achievements Project -