Browse Source

lint fix on laptop and move micropub GET

master
Rich Brown 2 months ago
parent
commit
f3a1349f3f
4 changed files with 133 additions and 106 deletions
  1. +8
    -9
      .eslintrc.js
  2. +7
    -0
      .prettierrc
  3. +97
    -78
      src/index.js
  4. +21
    -19
      src/micropub/micropubRouter.js

+ 8
- 9
.eslintrc.js View File

@@ -1,21 +1,20 @@
module.exports = {
env: {
es6: true,
node: true,
node: true
},
extends: [
'airbnb-base',
],
extends: ["airbnb-base"],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
Atomics: "readonly",
SharedArrayBuffer: "readonly"
},
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
sourceType: "module"
},
rules: {
"no-console": 0,
"max-len": ["error", { "code": 120 }]
},
"max-len": ["error", { code: 120 }],
quotes: ["error", "double"]
}
};

+ 7
- 0
.prettierrc View File

@@ -0,0 +1,7 @@
{
"trailingComma": "all",
"tabWidth": 2,
"semi": true,
"singleQuote": false,
"arrowParens": "always"
}

+ 97
- 78
src/index.js View File

@@ -1,63 +1,66 @@
/* eslint-disable max-len */
import path from 'path';
import express from 'express';
import './env';
import engine from 'consolidate';
import cors from 'cors';
import session from 'express-session';
import CMS from 'connect-mongodb-session';
import helmet from 'helmet';
import path from "path";
import express from "express";
import "./env";
import engine from "consolidate";
import cors from "cors";
import session from "express-session";
import CMS from "connect-mongodb-session";
import helmet from "helmet";

import postTemp from './apps/weather';
import { isMicropubTokenAuthed } from './helpers/tokenAuth';
import postTemp from "./apps/weather";
import { isMicropubTokenAuthed } from "./helpers/tokenAuth";
/*
* import all routers
*/
import uploadRouter from './uploader/uploadRouter';
import { notifyRouter } from './notifiers/notifyRouter';
import { colorRouter } from './colors/colorRouter';
import authRouter from './auth/authRouter';
import createRouter from './create/createRouter';
import episodeRouter from './episode/episodeRouter';
import sayRouter from './say/sayRouter';
import danielRouter from './daniel/danielRouter';
import radioRouter from './radio/radioRouter';
import micropubRouter from './micropub/micropubRouter';
* import all routers
*/
import uploadRouter from "./uploader/uploadRouter";
import { notifyRouter } from "./notifiers/notifyRouter";
import { colorRouter } from "./colors/colorRouter";
import authRouter from "./auth/authRouter";
import createRouter from "./create/createRouter";
import episodeRouter from "./episode/episodeRouter";
import sayRouter from "./say/sayRouter";
import danielRouter from "./daniel/danielRouter";
import radioRouter from "./radio/radioRouter";
import micropubRouter from "./micropub/micropubRouter";

/* app setup */
const { PORT } = process.env;
const app = express();

const MongoDBStore = CMS(session);
const store = new MongoDBStore({
// eslint-disable-next-line
uri: `mongodb+srv://colors_admin:${process.env.MONGO_PASSWORD}@cluster0-4udef.mongodb.net/test?retryWrites=true&w=majority`,
databaseName: 'notifierDb',
collection: 'sessions',
}, (error) => {
// eslint-disable-next-line no-console
if (error) console.log('MongoDBStore connection error: ', error);
});
const store = new MongoDBStore(
{
// eslint-disable-next-line
uri: `mongodb+srv://colors_admin:${process.env.MONGO_PASSWORD}@cluster0-4udef.mongodb.net/test?retryWrites=true&w=majority`,
databaseName: "notifierDb",
collection: "sessions",
},
(error) => {
// eslint-disable-next-line no-console
if (error) console.log("MongoDBStore connection error: ", error);
},
);

// Catch errors
store.on('error', (error) => {
store.on("error", (error) => {
// eslint-disable-next-line no-console
console.log('store error:', error);
console.log("store error:", error);
});
app.set('views', `${__dirname}/views`);
app.engine('njk', engine.nunjucks);
app.set('view engine', 'njk');
app.use('/public', express.static(path.join(__dirname, '../public')));
app.set("views", `${__dirname}/views`);
app.engine("njk", engine.nunjucks);
app.set("view engine", "njk");
app.use("/public", express.static(path.join(__dirname, "../public")));
app.use(helmet());

const corsArrays = [
'https://post.porknachos.com',
'http://localhost:8087',
'http://localhost:3000',
'http://localhost:8080',
'https://dashboard.porknachos.com',
'https://www.rich-text.net',
'https://www.pineandvine.com',
"https://post.porknachos.com",
"http://localhost:8087",
"http://localhost:3000",
"http://localhost:8080",
"https://dashboard.porknachos.com",
"https://www.rich-text.net",
"https://www.pineandvine.com",
];
const corsOptions = {
origin: corsArrays,
@@ -66,23 +69,26 @@ const corsOptions = {
app.use(cors(corsOptions));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.set('trust proxy', 1); // trust first proxy
const routesArray = ['/create', '/auth', '/micropub']; // TODO: aha!
app.use(routesArray, session({
secret: process.env.MONGO_PASSWORD, // dumb
name: 'sessionId',
store,
resave: false,
saveUninitialized: false,
cookie: { secure: false, maxAge: 1000 * 60 * 60 * 3, sameSite: false },
// TODO: i think i can't secure cookie bc Apache proxies to http://localhost:8086
}));
app.set("trust proxy", 1); // trust first proxy
const routesArray = ["/create", "/auth", "/micropub"]; // TODO: aha!
app.use(
routesArray,
session({
secret: process.env.MONGO_PASSWORD, // dumb
name: "sessionId",
store,
resave: false,
saveUninitialized: false,
cookie: { secure: false, maxAge: 1000 * 60 * 60 * 3, sameSite: false },
// TODO: i think i can't secure cookie bc Apache proxies to http://localhost:8086
}),
);

/*
* setup middleware
*/
* setup middleware
*/
app.use((req, res, next) => {
console.log('%s %s %s', req.method, req.url, req.path); //eslint-disable-line
console.log("%s %s %s", req.method, req.url, req.path); //eslint-disable-line
// if (req.session && req.session.cookie) {
// console.log(`cookie ${JSON.stringify(req.session.cookie, null, 4)}`); //eslint-disable-line
// } else {
@@ -91,34 +97,47 @@ app.use((req, res, next) => {
next();
});

/**
* FIXME: this is a kludge for this route to avoid token auth below
*/
app.get("/micropub", (req, res) => {
const { query } = req;
if (query.q && query.q === "config") {
// TODO: (maybe) add 'syndicate-to' objects here. see spec 3.7.1
return res.json({
"media-endpoint": "https://www.porknachos.com/node/micropub/media",
});
}
return res.json({ message: "this is the media endpoint" });
});

/*
* use all routers
*/
app.use('/upload/', uploadRouter);
app.use('/notify/', notifyRouter);
app.use('/color/', colorRouter);
app.use('/auth/', authRouter); // indieauth
app.use('/micropub/', isMicropubTokenAuthed, micropubRouter);
app.use('/create/', isMicropubTokenAuthed, createRouter); // TODO: create micro.blogs
app.use('/episode/', episodeRouter); // GET returns a webpage, POST to / creates a random podcast, POST to /existingMP3 makes one from a chosen mp3
app.use('/say/', sayRouter);
app.use('/daniel/', danielRouter); // POST a string (and code) to /daniel. puts MP3 in /podcasts/phrases
app.use('/radio/', radioRouter);
* use all routers
*/
app.use("/upload/", uploadRouter);
app.use("/notify/", notifyRouter);
app.use("/color/", colorRouter);
app.use("/auth/", authRouter); // indieauth
app.use("/micropub/", isMicropubTokenAuthed, micropubRouter);
app.use("/create/", isMicropubTokenAuthed, createRouter); // TODO: create micro.blogs
app.use("/episode/", episodeRouter); // GET returns a webpage, POST to / creates a random podcast, POST to /existingMP3 makes one from a chosen mp3
app.use("/say/", sayRouter);
app.use("/daniel/", danielRouter); // POST a string (and code) to /daniel. puts MP3 in /podcasts/phrases
app.use("/radio/", radioRouter);
/*
* root router
*/
app.get('/', (req, res) => {
res.json({ message: 'This is the root route for node-aggregate.' });
* root router
*/
app.get("/", (req, res) => {
res.json({ message: "This is the root route for node-aggregate." });
});

/*
* serve entire app
*/
* serve entire app
*/
app.listen(PORT, () => console.log(`node-agrregate app listening on ${PORT}.`)); //eslint-disable-line

/*
* run any apps without http routes
*/
* run any apps without http routes
*/
postTemp(); // this is the whitman-weather account on colorshare.club
setInterval(postTemp, 60000 * 60); // then, every hour

+ 21
- 19
src/micropub/micropubRouter.js View File

@@ -1,32 +1,34 @@
import express from 'express';
import express from "express";

import { uploadImage } from '../uploader/uploadRouter';
import { uploadImage } from "../uploader/uploadRouter";
// import { isMicropubTokenAuthed } from '../helpers/tokenAuth';

const micropubRouter = express.Router();

micropubRouter.get('/', (req, res) => {
const { query } = req;
console.log(query);
if (query.q && query.q === 'config') {
// TODO: (maybe) add 'syndicate-to' objects here. see spec 3.7.1
return res.json({
'media-endpoint': 'https://www.porknachos.com/node/micropub/media',
});
}
return res.json({ message: 'this is the media endpoint' });
});
// FIXME: moved to index, to avoid isMpTokenAuth check

// micropubRouter.get('/', (req, res) => {
// const { query } = req;
// console.log(query);
// if (query.q && query.q === 'config') {
// // TODO: (maybe) add 'syndicate-to' objects here. see spec 3.7.1
// return res.json({
// 'media-endpoint': 'https://www.porknachos.com/node/micropub/media',
// });
// }
// return res.json({ message: 'this is the media endpoint' });
// });

micropubRouter.post('/media', (req, res) => {
micropubRouter.post("/media", (req, res) => {
uploadImage(req, res, async (error) => {
const fqdnFilename = req.file.path.replace(
'/var/www/fileserver/public/blog/',
'https://www.porknachos.com/files/blog/',
"/var/www/fileserver/public/blog/",
"https://www.porknachos.com/files/blog/",
);
// FIXME::W3C spec says filename *should* not be guessable
// see: https://www.w3.org/TR/micropub/#response-3
// FIXME::W3C spec says filename *should* not be guessable
// see: https://www.w3.org/TR/micropub/#response-3
if (error) return res.sendStatus(500).json({ error });
res.setHeader('Location', fqdnFilename);
res.setHeader("Location", fqdnFilename);
return res.status(201).json({ url: fqdnFilename });
});
});


Loading…
Cancel
Save