forked from Shopify/app-bridge-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
70 lines (57 loc) · 1.84 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require("isomorphic-fetch");
const Koa = require("koa");
const path = require("path");
const static = require("koa-static");
const mount = require("koa-mount");
const { default: createShopifyAuth } = require("@shopify/koa-shopify-auth");
const { verifyRequest } = require("@shopify/koa-shopify-auth");
const session = require("koa-session");
const dotenv = require("dotenv");
dotenv.config();
const port = parseInt(process.env.PORT, 10) || 3000;
const { SHOPIFY_API_SECRET_KEY, SHOPIFY_API_KEY } = process.env;
buildServer();
async function buildServer() {
const server = new Koa();
server.use(session(server));
server.keys = [SHOPIFY_API_SECRET_KEY];
server.use(
createShopifyAuth({
apiKey: SHOPIFY_API_KEY,
secret: SHOPIFY_API_SECRET_KEY,
scopes: ["read_products", "read_orders"],
afterAuth(ctx) {
const { shop, accessToken } = ctx.session;
ctx.cookies.set("shopOrigin", shop, { httpOnly: false });
ctx.redirect("/");
}
})
);
server.use(verifyRequest());
if (process.env.NODE_ENV === "production") {
server.use(mount("/", static(__dirname + "/public")));
} else {
await webpackMiddleware(server);
}
server.listen(port, () => {
console.log(`> Ready on http://localhost:${port}`);
});
}
// serve files from webpack, in memory
async function webpackMiddleware(server) {
const koaWebpack = require("koa-webpack");
const config = require("./webpack.config.js");
const middleware = await koaWebpack({
config,
hotClient: false
});
server.use(middleware);
// to access in-memory filesystem provided by html-webpack-plugin
server.use(async ctx => {
const filename = path.resolve(config.output.path, "index.html");
ctx.response.type = "html";
ctx.response.body = middleware.devMiddleware.fileSystem.createReadStream(
filename
);
});
}