auto formatting
This commit is contained in:
		
							parent
							
								
									803f53af7a
								
							
						
					
					
						commit
						f7918b45f1
					
				
					 8 changed files with 104 additions and 90 deletions
				
			
		|  | @ -13,7 +13,7 @@ | |||
|     "start": "nodemon src/main.mjs", | ||||
|     "prod": "NODE_ENV=production node src/main.mjs", | ||||
|     "test": "echo \"Error: no test specified\" && exit 1", | ||||
|     "format": "npx prettier . --write", | ||||
|     "format": "npx prettier src --write --tab-width 4", | ||||
|     "lint": "npx eslint src" | ||||
|   }, | ||||
|   "dependencies": { | ||||
|  |  | |||
|  | @ -1,7 +1,7 @@ | |||
| import express from 'express'; | ||||
| import express from "express"; | ||||
| 
 | ||||
| import utils from './utils.mjs'; | ||||
| import db from './db.mjs'; | ||||
| import utils from "./utils.mjs"; | ||||
| import db from "./db.mjs"; | ||||
| 
 | ||||
| // server
 | ||||
| 
 | ||||
|  | @ -13,7 +13,7 @@ router.use(express.json()); | |||
| 
 | ||||
| // POST
 | ||||
| 
 | ||||
| router.post('/:site/*', utils.post_limiter, (req, res) => { | ||||
| router.post("/:site/*", utils.post_limiter, (req, res) => { | ||||
|     const site_url = req.params.site; | ||||
|     const path = req.params[0]; | ||||
| 
 | ||||
|  | @ -46,9 +46,8 @@ router.post('/:site/*', utils.post_limiter, (req, res) => { | |||
|     } | ||||
| }); | ||||
| 
 | ||||
| 
 | ||||
| // GET
 | ||||
| router.get('/:site/*', utils.get_limiter, (req, res) => { | ||||
| router.get("/:site/*", utils.get_limiter, (req, res) => { | ||||
|     const site = req.params.site; | ||||
|     const path = req.params[0]; | ||||
|     const comments = db.pageComments(site, path); | ||||
|  |  | |||
|  | @ -1,4 +1,4 @@ | |||
| import config from 'config'; | ||||
| import config from "config"; | ||||
| 
 | ||||
| const configuration = config.util.toObject(); | ||||
| 
 | ||||
|  | @ -8,5 +8,5 @@ export default { | |||
|     config: configuration, | ||||
|     getSite: (site_url) => { | ||||
|         return configuration.sites[site_url]; | ||||
|     } | ||||
|     }, | ||||
| }; | ||||
|  |  | |||
|  | @ -1,8 +1,8 @@ | |||
| import Database from 'better-sqlite3'; | ||||
| import { migrate } from '@blackglory/better-sqlite3-migrations'; | ||||
| import Database from "better-sqlite3"; | ||||
| import { migrate } from "@blackglory/better-sqlite3-migrations"; | ||||
| 
 | ||||
| import utils from './utils.mjs'; | ||||
| import config from './config.mjs'; | ||||
| import utils from "./utils.mjs"; | ||||
| import config from "./config.mjs"; | ||||
| 
 | ||||
| // class
 | ||||
| 
 | ||||
|  | @ -31,7 +31,8 @@ export default db; | |||
| 
 | ||||
| function migrations() { | ||||
|     return [ | ||||
|     { version: 1, | ||||
|         { | ||||
|             version: 1, | ||||
|             up: ` | ||||
| CREATE TABLE comment ( | ||||
|   id integer not null, | ||||
|  | @ -47,8 +48,8 @@ CREATE TABLE comment ( | |||
|       `,
 | ||||
|             down: ` | ||||
| DROP TABLE comment; | ||||
|       ` | ||||
|     } | ||||
|       `,
 | ||||
|         }, | ||||
|     ]; | ||||
| } | ||||
| 
 | ||||
|  | @ -56,7 +57,7 @@ DROP TABLE comment; | |||
| 
 | ||||
| function createDB() { | ||||
|     const db = new Database(utils.db_path); | ||||
|     db.pragma('journal_mode = WAL'); | ||||
|     db.pragma("journal_mode = WAL"); | ||||
|     migrate(db, migrations(), 1); | ||||
|     return db; | ||||
| } | ||||
|  | @ -65,15 +66,19 @@ function createDB() { | |||
| 
 | ||||
| function getSiteInfo(db, site_url) { | ||||
|     const site = config.getSite(site_url); | ||||
|     if (!site || !site.info) { throw "Unknown site" } | ||||
|     if (!site || !site.info) { | ||||
|         throw "Unknown site"; | ||||
|     } | ||||
|     return site; | ||||
| } | ||||
| 
 | ||||
| function insertPageComment(db, site_url, path, comment) { | ||||
|     const site = config.getSite(site_url); | ||||
|     if (!site || !site.info.id) { throw "Unknown site" } | ||||
|     if (!site || !site.info.id) { | ||||
|         throw "Unknown site"; | ||||
|     } | ||||
| 
 | ||||
|     let object = {...comment, site_id: site.info.id, path: path }; | ||||
|     let object = { ...comment, site_id: site.info.id, path: path }; | ||||
| 
 | ||||
|     const stmt = db.prepare(` | ||||
|         INSERT INTO comment(id, site, path, user, user_website, message, reply_to) | ||||
|  | @ -102,7 +107,9 @@ function insertPageComment(db, site_url, path, comment) { | |||
| 
 | ||||
| function getPageComments(db, site_url, path) { | ||||
|     const site = config.getSite(site_url); | ||||
|     if (!site || !site.info.id) { return []; } | ||||
|     if (!site || !site.info.id) { | ||||
|         return []; | ||||
|     } | ||||
|     const stmt = db.prepare(` | ||||
|         SELECT | ||||
|           id, | ||||
|  | @ -120,7 +127,9 @@ function getPageComments(db, site_url, path) { | |||
| 
 | ||||
| function getSiteComments(db, site_url) { | ||||
|     const site = config.getSite(site_url); | ||||
|     if (!site || !site.info.id) { return []; } | ||||
|     if (!site || !site.info.id) { | ||||
|         return []; | ||||
|     } | ||||
|     const stmt = db.prepare(` | ||||
|         SELECT | ||||
|           id, | ||||
|  |  | |||
|  | @ -1,8 +1,8 @@ | |||
| import express from 'express'; | ||||
| import { Feed } from 'feed'; | ||||
| import express from "express"; | ||||
| import { Feed } from "feed"; | ||||
| 
 | ||||
| import utils from './utils.mjs'; | ||||
| import db from './db.mjs'; | ||||
| import utils from "./utils.mjs"; | ||||
| import db from "./db.mjs"; | ||||
| 
 | ||||
| // Feed
 | ||||
| 
 | ||||
|  | @ -14,16 +14,16 @@ router.use(express.json()); | |||
| 
 | ||||
| const domain = utils.domain; | ||||
| 
 | ||||
| router.get('/:site', utils.get_limiter, (req, res) => { | ||||
| router.get("/:site", utils.get_limiter, (req, res) => { | ||||
|     const site = req.params.site; | ||||
| 
 | ||||
|     var feed = new Feed({ | ||||
|         title: 'UCS', | ||||
|         generator: 'UCS', | ||||
|         description: 'תגובות עבור האתר ' + site, | ||||
|         id: domain + '/feed/' + site, | ||||
|         title: "UCS", | ||||
|         generator: "UCS", | ||||
|         description: "תגובות עבור האתר " + site, | ||||
|         id: domain + "/feed/" + site, | ||||
|         link: domain, | ||||
|         language: 'he' | ||||
|         language: "he", | ||||
|     }); | ||||
| 
 | ||||
|     const comments = db.siteComments(site); | ||||
|  | @ -34,27 +34,27 @@ router.get('/:site', utils.get_limiter, (req, res) => { | |||
|             description: comment.message, | ||||
|             id: `${comment.site}/${comment.path}#comment-${comment.id}`, | ||||
|             link: `${comment.site}/${comment.path}#comment-${comment.id}`, | ||||
|            date: new Date(comment.published) | ||||
|             date: new Date(comment.published), | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     var xml = feed.atom1(); | ||||
| 
 | ||||
|     res.set('Content-Type', 'text/xml'); | ||||
|     res.set("Content-Type", "text/xml"); | ||||
|     res.send(xml); | ||||
| }); | ||||
| 
 | ||||
| router.get('/:site/*', utils.get_limiter, (req, res) => { | ||||
| router.get("/:site/*", utils.get_limiter, (req, res) => { | ||||
|     const site = req.params.site; | ||||
|     const path = req.params[0]; | ||||
| 
 | ||||
|     var feed = new Feed({ | ||||
|         title: 'UCS', | ||||
|         generator: 'UCS', | ||||
|         description: 'תגובות עבור הדף ' + site + '/' + path, | ||||
|         id: domain + '/feed/' + site, | ||||
|         title: "UCS", | ||||
|         generator: "UCS", | ||||
|         description: "תגובות עבור הדף " + site + "/" + path, | ||||
|         id: domain + "/feed/" + site, | ||||
|         link: domain, | ||||
|         language: 'he' | ||||
|         language: "he", | ||||
|     }); | ||||
| 
 | ||||
|     const comments = db.pageComments(site, path); | ||||
|  | @ -65,12 +65,12 @@ router.get('/:site/*', utils.get_limiter, (req, res) => { | |||
|             description: comment.message, | ||||
|             id: `${site}/${path}#comment-${comment.id}`, | ||||
|             link: `${site}/${path}#comment-${comment.id}`, | ||||
|            date: new Date(comment.published) | ||||
|             date: new Date(comment.published), | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     var xml = feed.atom1(); | ||||
| 
 | ||||
|     res.set('Content-Type', 'text/xml'); | ||||
|     res.set("Content-Type", "text/xml"); | ||||
|     res.send(xml); | ||||
| }); | ||||
|  |  | |||
|  | @ -1,20 +1,24 @@ | |||
| import express from 'express'; | ||||
| import path from 'node:path'; | ||||
| import { fileURLToPath } from 'node:url'; | ||||
| import express from "express"; | ||||
| import path from "node:path"; | ||||
| import { fileURLToPath } from "node:url"; | ||||
| 
 | ||||
| import { app } from './server.mjs'; | ||||
| import api from './api.mjs'; | ||||
| import feed from './feed.mjs'; | ||||
| import utils from './utils.mjs'; | ||||
| import { app } from "./server.mjs"; | ||||
| import api from "./api.mjs"; | ||||
| import feed from "./feed.mjs"; | ||||
| import utils from "./utils.mjs"; | ||||
| 
 | ||||
| // server
 | ||||
| 
 | ||||
| app.use(utils.get_limiter); | ||||
| 
 | ||||
| app.use(express.static(path.join(path.dirname(fileURLToPath(import.meta.url)), "../public"))); | ||||
| app.use( | ||||
|     express.static( | ||||
|         path.join(path.dirname(fileURLToPath(import.meta.url)), "../public"), | ||||
|     ), | ||||
| ); | ||||
| 
 | ||||
| app.use('/api', api); | ||||
| app.use('/atom', feed); | ||||
| app.use("/api", api); | ||||
| app.use("/atom", feed); | ||||
| 
 | ||||
| // Listen
 | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,10 +1,10 @@ | |||
| import express from 'express'; | ||||
| import compression from 'compression'; | ||||
| import helmet from 'helmet'; | ||||
| import morgan from 'morgan'; | ||||
| import cors from 'cors'; | ||||
| import express from "express"; | ||||
| import compression from "compression"; | ||||
| import helmet from "helmet"; | ||||
| import morgan from "morgan"; | ||||
| import cors from "cors"; | ||||
| 
 | ||||
| import utils from './utils.mjs'; | ||||
| import utils from "./utils.mjs"; | ||||
| 
 | ||||
| // server
 | ||||
| 
 | ||||
|  | @ -15,17 +15,17 @@ app.use(compression()); | |||
| app.use( | ||||
|     helmet.contentSecurityPolicy({ | ||||
|         directives: { | ||||
|             "script-src": ["'self'"] | ||||
|             "script-src": ["'self'"], | ||||
|         }, | ||||
|     }) | ||||
|     }), | ||||
| ); | ||||
| app.use(morgan('combined')); | ||||
| app.use(morgan("combined")); | ||||
| 
 | ||||
| const corsOptions = { | ||||
|     origin: utils.cors, | ||||
|     optionsSuccessStatus: 200 | ||||
|     optionsSuccessStatus: 200, | ||||
| }; | ||||
| 
 | ||||
| app.use(cors(corsOptions)); | ||||
| 
 | ||||
| app.set('trust proxy', '127.0.0.1'); | ||||
| app.set("trust proxy", "127.0.0.1"); | ||||
|  |  | |||
|  | @ -1,6 +1,6 @@ | |||
| import RateLimit from 'express-rate-limit'; | ||||
| import RateLimit from "express-rate-limit"; | ||||
| 
 | ||||
| import config from './config.mjs'; | ||||
| import config from "./config.mjs"; | ||||
| 
 | ||||
| // Constants
 | ||||
| 
 | ||||
|  | @ -10,10 +10,12 @@ const port = process.env.PORT || config.config.port || 8080; | |||
| 
 | ||||
| const db_path = process.env.DB || config.config.db_path || "ucs.db"; | ||||
| 
 | ||||
| const cors = (function() { | ||||
| const cors = (function () { | ||||
|     let origins = new Set(); | ||||
|     for (const site in config.config.sites) { | ||||
|         config.config.sites[site].cors.forEach(origin => {origins.add(origin) }); | ||||
|         config.config.sites[site].cors.forEach((origin) => { | ||||
|             origins.add(origin); | ||||
|         }); | ||||
|     } | ||||
|     return Array.from(origins); | ||||
| })(); | ||||
|  | @ -31,11 +33,11 @@ function escapeHtml(unsafe) { | |||
| 
 | ||||
| // limiters
 | ||||
| 
 | ||||
| const limiter = limit => RateLimit({ | ||||
| const limiter = (limit) => | ||||
|     RateLimit({ | ||||
|         windowMs: 1 * 60 * 1000, | ||||
|         max: limit, | ||||
| }); | ||||
| 
 | ||||
|     }); | ||||
| 
 | ||||
| export default { | ||||
|     domain, | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue