forked from ohmygod481999/izzi-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
162 lines (138 loc) · 3.83 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const { Liquid } = require("liquidjs");
const {
registCustomFilter,
registCustomTag,
preprocessLiquid,
} = require("./liquid/index");
const utils = require("./utils/utils");
const api = require("./utils/api");
const redis = require("redis");
const port = 5002;
const REDIS_PORT = 6379;
const app = express();
const redisClient = redis.createClient(REDIS_PORT);
if (process.argv.length < 3) {
return console.log("Pls tell me the path to build folder you want to run");
}
let reload = false;
if (process.argv.length > 3) {
reload = true;
}
const inputFolderPath = process.argv[2];
let globalObject;
const gloablObjectKey = `GLOBAL-OBJECT-${inputFolderPath}`;
redisClient.get(gloablObjectKey, (err, data) => {
if (err) throw err;
if (!data || reload) {
console.log("getting global object");
utils.getGlobalObject(inputFolderPath).then((value) => {
globalObject = value;
redisClient.setex(gloablObjectKey, 3600, JSON.stringify(value));
console.log("init done!");
});
} else {
globalObject = JSON.parse(data);
}
});
const engine = new Liquid({
extname: ".liquid",
root: [
`./${inputFolderPath}/layout`,
`./${inputFolderPath}/sections`,
`./${inputFolderPath}/snippets`,
`./${inputFolderPath}/templates`,
],
});
registCustomTag(engine, inputFolderPath);
registCustomFilter(engine, inputFolderPath);
app.set("view engine", "liquid"); // set liquid to default
app.use("/public", express.static(`${inputFolderPath}/assets`));
// app.use("/public", express.static(path.join(__dirname, "public")));
// app.use(bodyParser.json());
app.get("/", async (req, res) => {
const html = await utils.getPage(
engine,
inputFolderPath,
"index",
{},
globalObject
);
res.send(html);
});
app.get("/products/:productId", async (req, res) => {
const productId = req.params.productId;
const product = await api.getProductParseDataById(productId);
const html = await utils.getPage(
engine,
inputFolderPath,
"product",
product,
globalObject
);
res.send(html);
});
app.get("/cart", async (req, res) => {
const html = await utils.getPage(
engine,
inputFolderPath,
"cart",
{},
globalObject
);
res.send(html);
});
app.get("/search", async (req, res) => {
const terms = req.query.q;
const searchData = await api.getSearchData(terms);
const html = await utils.getPage(
engine,
inputFolderPath,
"search",
searchData,
globalObject
);
res.send(html);
});
app.get("/collections/:id", async (req, res) => {
const id = req.params.id;
const products = await api.getProductsByCollectionId(id);
const html = await utils.getPage(
engine,
inputFolderPath,
"collection",
products,
globalObject
);
res.send(html);
});
app.get("/blogs/:id", async (req, res) => {
const blogCatetegoryId = req.params.id;
const articles = await api.getArticlesByBlogId(blogCatetegoryId);
const html = await utils.getPage(
engine,
inputFolderPath,
"blog",
articles,
globalObject
);
res.send(html);
});
app.get("/blogs/:cateId/:articleId", async (req, res) => {
const articleId = req.params.articleId;
const cateId = req.params.cateId;
const article = await api.getArticleById(articleId, cateId);
const html = await utils.getPage(
engine,
inputFolderPath,
"article",
article,
globalObject
);
res.send(html);
});
app.listen(port, () => {
console.log(`test liquid server is listening on port ${port}`);
});