mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 19:25:23 +00:00
Moved completely webserver activities from index.js to server.js
Replaced `RequestHandler` with `generatePass`. Now `generatePass` function returns JSON objects containing the pass stream or, otherwise, the error structure.
This commit is contained in:
44
server.js
44
server.js
@@ -1,12 +1,14 @@
|
||||
const express = require("express");
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const passkit = require("./index");
|
||||
const Configuration = require("./config.json");
|
||||
|
||||
passkit.init("./config.json");
|
||||
passkit.init(Configuration);
|
||||
|
||||
const instance = express();
|
||||
|
||||
instance.use(express.json());
|
||||
|
||||
instance.listen(80, "0.0.0.0", function(request, response) {
|
||||
console.log("Listening on 80");
|
||||
});
|
||||
@@ -15,5 +17,39 @@ instance.get("/", function (request, response) {
|
||||
response.send("Hello there!");
|
||||
});
|
||||
|
||||
instance.get("/gen/:type/",passkit.RequestHandler);
|
||||
instance.post("/gen/:type/", passkit.RequestHandler);
|
||||
function manageRequest(request, response) {
|
||||
let passName = (request.query.name ||
|
||||
request.body.name ||
|
||||
request.params.name ||
|
||||
request.query.modelName ||
|
||||
request.body.modelName ||
|
||||
request.params.modelName) + "_" + (new Date()).toISOString().split('T')[0].replace(/-/ig, "");
|
||||
|
||||
response.set({
|
||||
"Content-type": "application/vnd.apple.pkpass",
|
||||
"Content-disposition": `attachment; filename=${passName}.pkpass`
|
||||
});
|
||||
|
||||
passkit.generatePass({
|
||||
modelName: request.params.modelName || request.query.modelName,
|
||||
overrides: {}
|
||||
})
|
||||
.then(function(result) {
|
||||
result.content.pipe(response);
|
||||
|
||||
// Writing to an output source
|
||||
if (Configuration.output.dir && Configuration.output.shouldWrite && !fs.accessSync(path.resolve(Configuration.output.dir))) {
|
||||
let wstreamOutputPass = fs.createWriteStream(path.resolve(Configuration.output.dir, `${passName}.pkpass`));
|
||||
result.content.pipe(wstreamOutputPass);
|
||||
}
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err);
|
||||
|
||||
response.set("Content-Type", "application/json");
|
||||
response.status(418).send(err);
|
||||
})
|
||||
}
|
||||
|
||||
instance.get("/gen/:modelName?", manageRequest);
|
||||
instance.post("/gen/:modelName?", manageRequest);
|
||||
|
||||
Reference in New Issue
Block a user