44b7d804bc3b84782e1a2809ecff53aa991ce95c
[vchess.git] / routes / variant.js
1 let router = require("express").Router();
2 const createError = require('http-errors');
3 const sqlite3 = require('sqlite3');
4 const DbPath = __dirname.replace("/routes", "/db/vchess.sqlite");
5 const db = new sqlite3.Database(DbPath);
6 const selectLanguage = require(__dirname.replace("/routes", "/utils/language.js"));
7
8 router.get("/:variant([a-zA-Z0-9]+)", (req,res,next) => {
9 const vname = req.params["variant"];
10 db.serialize(function() {
11 db.all("SELECT * FROM Variants WHERE name='" + vname + "'", (err,variant) => {
12 if (!!err)
13 return next(err);
14 if (!variant || variant.length==0)
15 return next(createError(404));
16 res.render('variant', {
17 title: vname + ' Variant',
18 variant: vname,
19 lang: selectLanguage(req, res),
20 });
21 });
22 });
23 });
24
25 // Load a rules page (AJAX)
26 router.get("/rules/:variant([a-zA-Z0-9]+)", (req,res) => {
27 if (!req.xhr)
28 return res.json({errmsg: "Unauthorized access"});
29 const lang = selectLanguage(req, res);
30 res.render("rules/" + req.params["variant"] + "/" + lang);
31 });
32
33 module.exports = router;