Source and resources
This commit is contained in:
parent
4e58a426c6
commit
195c96eb54
13 changed files with 279 additions and 0 deletions
55
app/src/main/java/krusty/Database.java
Normal file
55
app/src/main/java/krusty/Database.java
Normal file
|
@ -0,0 +1,55 @@
|
|||
package krusty;
|
||||
|
||||
import spark.Request;
|
||||
import spark.Response;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import static krusty.Jsonizer.toJson;
|
||||
|
||||
public class Database {
|
||||
/**
|
||||
* Modify it to fit your environment and then use this string when connecting to your database!
|
||||
*/
|
||||
private static final String jdbcString = "jdbc:mysql://localhost/krusty";
|
||||
|
||||
// For use with MySQL or PostgreSQL
|
||||
private static final String jdbcUsername = "<CHANGE ME>";
|
||||
private static final String jdbcPassword = "<CHANGE ME>";
|
||||
|
||||
public void connect() {
|
||||
// Connect to database here
|
||||
}
|
||||
|
||||
// TODO: Implement and change output in all methods below!
|
||||
|
||||
public String getCustomers(Request req, Response res) {
|
||||
return "{}";
|
||||
}
|
||||
|
||||
public String getRawMaterials(Request req, Response res) {
|
||||
return "{}";
|
||||
}
|
||||
|
||||
public String getCookies(Request req, Response res) {
|
||||
return "{\"cookies\":[]}";
|
||||
}
|
||||
|
||||
public String getRecipes(Request req, Response res) {
|
||||
return "{}";
|
||||
}
|
||||
|
||||
public String getPallets(Request req, Response res) {
|
||||
return "{\"pallets\":[]}";
|
||||
}
|
||||
|
||||
public String reset(Request req, Response res) {
|
||||
return "{}";
|
||||
}
|
||||
|
||||
public String createPallet(Request req, Response res) {
|
||||
return "{}";
|
||||
}
|
||||
}
|
78
app/src/main/java/krusty/Jsonizer.java
Normal file
78
app/src/main/java/krusty/Jsonizer.java
Normal file
|
@ -0,0 +1,78 @@
|
|||
package krusty;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.io.IOError;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Auxiliary class for automatically translating a ResultSet to JSON
|
||||
*/
|
||||
public class Jsonizer {
|
||||
private static final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
static {
|
||||
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
mapper.setDateFormat(df);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert any POJO or plain Java data to JSON supported by Jackson
|
||||
*
|
||||
* @param name name of returned data
|
||||
* @return {name: jackson serialized json representation}
|
||||
*/
|
||||
public static String anythingToJson(Object data, String name) {
|
||||
try {
|
||||
Map<String, Object> entries = new HashMap<>();
|
||||
entries.put(name, data);
|
||||
return mapper.writeValueAsString(entries);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new IOError(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert JDBC Result to JSON
|
||||
*
|
||||
* @param rs open and unused ResultSet
|
||||
* @param name name of the resultset
|
||||
* @return JSON object with one entry: {name: result of ResultSet}
|
||||
* @throws SQLException
|
||||
*/
|
||||
public static String toJson(ResultSet rs, String name) throws SQLException {
|
||||
try {
|
||||
ResultSetMetaData meta = rs.getMetaData();
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
JsonGenerator writer = mapper.getFactory().createGenerator(sw);
|
||||
|
||||
writer.writeStartObject();
|
||||
writer.writeFieldName(name);
|
||||
writer.writeStartArray();
|
||||
while (rs.next()) {
|
||||
writer.writeStartObject();
|
||||
for (int i = 1; i <= meta.getColumnCount(); i++) {
|
||||
writer.writeFieldName(meta.getColumnLabel(i));
|
||||
writer.writeObject(rs.getObject(i));
|
||||
}
|
||||
writer.writeEndObject();
|
||||
}
|
||||
writer.writeEndArray();
|
||||
writer.writeEndObject();
|
||||
writer.flush();
|
||||
return sw.toString();
|
||||
} catch (IOException e) {
|
||||
throw new IOError(e);
|
||||
}
|
||||
}
|
||||
}
|
83
app/src/main/java/krusty/ServerMain.java
Normal file
83
app/src/main/java/krusty/ServerMain.java
Normal file
|
@ -0,0 +1,83 @@
|
|||
package krusty;
|
||||
|
||||
import java.io.IOError;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static spark.Spark.*;
|
||||
|
||||
public class ServerMain {
|
||||
public static int PORT = 8888;
|
||||
public static String API_ENTRYPOINT = "/api/v1";
|
||||
|
||||
private Database db;
|
||||
|
||||
public void startServer() {
|
||||
staticFiles.location("/public");
|
||||
|
||||
db = new Database();
|
||||
db.connect();
|
||||
|
||||
port(PORT);
|
||||
|
||||
enableCORS();
|
||||
|
||||
initIndex();
|
||||
initRoutes();
|
||||
}
|
||||
|
||||
private void initIndex() {
|
||||
try {
|
||||
byte[] indexData = getClass().getResource("/public/index.html").openStream().readAllBytes();
|
||||
final String index = new String(indexData, StandardCharsets.UTF_8);
|
||||
|
||||
get("/", (req, res) -> index);
|
||||
} catch (IOException e) {
|
||||
throw new IOError(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void initRoutes() {
|
||||
get(API_ENTRYPOINT + "/customers", (req, res) -> db.getCustomers(req, res));
|
||||
get(API_ENTRYPOINT + "/raw-materials", (req, res) -> db.getRawMaterials(req, res));
|
||||
get(API_ENTRYPOINT + "/cookies", (req, res) -> db.getCookies(req, res));
|
||||
get(API_ENTRYPOINT + "/recipes", (req, res) -> db.getRecipes(req, res));
|
||||
get(API_ENTRYPOINT + "/pallets", (req, res) -> db.getPallets(req, res));
|
||||
|
||||
post(API_ENTRYPOINT + "/reset", (req, res) -> db.reset(req, res));
|
||||
post(API_ENTRYPOINT + "/pallets", (req, res) -> db.createPallet(req, res));
|
||||
}
|
||||
|
||||
public void stopServer() {
|
||||
stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup CORS, see:
|
||||
* - https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
|
||||
* - http://sparkjava.com/tutorials/cors
|
||||
*/
|
||||
private void enableCORS() {
|
||||
options("/*", (request, response) -> {
|
||||
String accessControlRequestHeaders = request.headers("Access-Control-Request-Headers");
|
||||
if (accessControlRequestHeaders != null) {
|
||||
response.header("Access-Control-Allow-Headers", accessControlRequestHeaders);
|
||||
}
|
||||
String accessControlRequestMethod = request.headers("Access-Control-Request-Method");
|
||||
if (accessControlRequestMethod != null) {
|
||||
response.header("Access-Control-Allow-Methods", accessControlRequestMethod);
|
||||
}
|
||||
return "OK";
|
||||
});
|
||||
|
||||
before((request, response) -> {
|
||||
response.header("Access-Control-Allow-Origin", "*");
|
||||
response.header("Access-Control-Allow-Headers", "Content-Type, Accept");
|
||||
response.type("application/json");
|
||||
});
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
new ServerMain().startServer();
|
||||
}
|
||||
}
|
1
app/src/main/resources/public/index.html
Normal file
1
app/src/main/resources/public/index.html
Normal file
|
@ -0,0 +1 @@
|
|||
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500"><title>Krusty</title><link href="/static/css/main.59f83d58.chunk.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script>!function(e){function r(r){for(var n,f,l=r[0],i=r[1],a=r[2],c=0,s=[];c<l.length;c++)f=l[c],Object.prototype.hasOwnProperty.call(o,f)&&o[f]&&s.push(o[f][0]),o[f]=0;for(n in i)Object.prototype.hasOwnProperty.call(i,n)&&(e[n]=i[n]);for(p&&p(r);s.length;)s.shift()();return u.push.apply(u,a||[]),t()}function t(){for(var e,r=0;r<u.length;r++){for(var t=u[r],n=!0,l=1;l<t.length;l++){var i=t[l];0!==o[i]&&(n=!1)}n&&(u.splice(r--,1),e=f(f.s=t[0]))}return e}var n={},o={1:0},u=[];function f(r){if(n[r])return n[r].exports;var t=n[r]={i:r,l:!1,exports:{}};return e[r].call(t.exports,t,t.exports,f),t.l=!0,t.exports}f.m=e,f.c=n,f.d=function(e,r,t){f.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},f.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},f.t=function(e,r){if(1&r&&(e=f(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var t=Object.create(null);if(f.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var n in e)f.d(t,n,function(r){return e[r]}.bind(null,n));return t},f.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return f.d(r,"a",r),r},f.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},f.p="/";var l=this.webpackJsonpfrontend=this.webpackJsonpfrontend||[],i=l.push.bind(l);l.push=r,l=l.slice();for(var a=0;a<l.length;a++)r(l[a]);var p=i;t()}([])</script><script src="/static/js/2.0a7bcc05.chunk.js"></script><script src="/static/js/main.4ee489f9.chunk.js"></script></body></html>
|
|
@ -0,0 +1,2 @@
|
|||
div.App{width:800px;margin:0 auto}@media screen and (max-width:800px){div.App{width:100%}}body{margin:10px;padding:0;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","Roboto","Oxygen","Ubuntu","Cantarell","Fira Sans","Droid Sans","Helvetica Neue",sans-serif;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}code{font-family:source-code-pro,Menlo,Monaco,Consolas,"Courier New",monospace}
|
||||
/*# sourceMappingURL=main.59f83d58.chunk.css.map */
|
|
@ -0,0 +1 @@
|
|||
{"version":3,"sources":["index.css"],"names":[],"mappings":"AAAA,QACC,WAAY,CACZ,aACD,CAEA,oCACE,QACE,UACF,CACF,CAEA,KACE,WAAY,CACZ,SAAU,CACV,mJAEY,CACZ,kCAAmC,CACnC,iCACF,CAEA,KACE,yEAEF","file":"main.59f83d58.chunk.css","sourcesContent":["div.App {\n\twidth: 800px;\n\tmargin: 0 auto;\n}\n\n@media screen and (max-width: 800px) {\n div.App {\n width: 100%;\n }\n}\n\nbody {\n margin: 10px;\n padding: 0;\n font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", \"Roboto\", \"Oxygen\",\n \"Ubuntu\", \"Cantarell\", \"Fira Sans\", \"Droid Sans\", \"Helvetica Neue\",\n sans-serif;\n -webkit-font-smoothing: antialiased;\n -moz-osx-font-smoothing: grayscale;\n}\n\ncode {\n font-family: source-code-pro, Menlo, Monaco, Consolas, \"Courier New\",\n monospace;\n}\n"]}
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
object-assign
|
||||
(c) Sindre Sorhus
|
||||
@license MIT
|
||||
*/
|
||||
|
||||
/**
|
||||
* A better abstraction over CSS.
|
||||
*
|
||||
* @copyright Oleg Isonen (Slobodskoi) / Isonen 2014-present
|
||||
* @website https://github.com/cssinjs/jss
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
/** @license React v0.18.0
|
||||
* scheduler.production.min.js
|
||||
*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
/** @license React v16.12.0
|
||||
* react-dom.production.min.js
|
||||
*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
/** @license React v16.12.0
|
||||
* react-is.production.min.js
|
||||
*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
/** @license React v16.12.0
|
||||
* react.production.min.js
|
||||
*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,2 @@
|
|||
!function(e){function r(r){for(var n,f,l=r[0],i=r[1],a=r[2],c=0,s=[];c<l.length;c++)f=l[c],Object.prototype.hasOwnProperty.call(o,f)&&o[f]&&s.push(o[f][0]),o[f]=0;for(n in i)Object.prototype.hasOwnProperty.call(i,n)&&(e[n]=i[n]);for(p&&p(r);s.length;)s.shift()();return u.push.apply(u,a||[]),t()}function t(){for(var e,r=0;r<u.length;r++){for(var t=u[r],n=!0,l=1;l<t.length;l++){var i=t[l];0!==o[i]&&(n=!1)}n&&(u.splice(r--,1),e=f(f.s=t[0]))}return e}var n={},o={1:0},u=[];function f(r){if(n[r])return n[r].exports;var t=n[r]={i:r,l:!1,exports:{}};return e[r].call(t.exports,t,t.exports,f),t.l=!0,t.exports}f.m=e,f.c=n,f.d=function(e,r,t){f.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},f.r=function(e){"undefined"!==typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},f.t=function(e,r){if(1&r&&(e=f(e)),8&r)return e;if(4&r&&"object"===typeof e&&e&&e.__esModule)return e;var t=Object.create(null);if(f.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var n in e)f.d(t,n,function(r){return e[r]}.bind(null,n));return t},f.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return f.d(r,"a",r),r},f.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},f.p="/";var l=this.webpackJsonpfrontend=this.webpackJsonpfrontend||[],i=l.push.bind(l);l.push=r,l=l.slice();for(var a=0;a<l.length;a++)r(l[a]);var p=i;t()}([]);
|
||||
//# sourceMappingURL=runtime-main.1c2d59f4.js.map
|
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue