From 14bef7168a0316e1cbed8c7812663e4135dfa973 Mon Sep 17 00:00:00 2001 From: Timothy Farrell Date: Wed, 13 Jun 2018 04:22:02 -0500 Subject: [PATCH] Need a backend to integrate with b2. --- packages/gallery/package.json | 16 +++-- .../scripts/b2/bucket_setup_examples.sh | 49 ++++++++++++++ packages/gallery/src/server.js | 65 +++++++++++++++++++ packages/gallery/webpack.config.js | 28 +++++++- 4 files changed, 151 insertions(+), 7 deletions(-) create mode 100755 packages/gallery/scripts/b2/bucket_setup_examples.sh create mode 100644 packages/gallery/src/server.js diff --git a/packages/gallery/package.json b/packages/gallery/package.json index be34ceb..b2de9ca 100644 --- a/packages/gallery/package.json +++ b/packages/gallery/package.json @@ -6,15 +6,16 @@ "author": "Timothy Farrell (https://github.com/explorigin)", "license": "Apache-2.0", "scripts": { + "build": "webpack", "dev": "webpack-dev-server" }, "dependencies": { - "css-loader": "^0.28.7", + "body-parser": "~1.18.3", "date-fns": "~1.29.0", "domvm": "~3.2.1", "exif-parser": "~0.1.9", - "extract-text-webpack-plugin": "^3.0.2", "frptools": "3.1.1", + "express": "~4.16.3", "linear-partitioning": "0.3.2", "pica": "~2.0.8", "pouchdb-adapter-http": "~6.4.1", @@ -33,8 +34,13 @@ "styletron-utils": "^2.5.4" }, "devDependencies": { - "html-webpack-plugin": "^2.30.1", - "webpack": "~3.8.1", - "webpack-dev-server": "~2.9.2" + "css-loader": "~0.28.11", + "extract-text-webpack-plugin": "~4.0.0-beta.0", + "file-loader": "~1.1.11", + "html-webpack-plugin": "~3.2.0", + "url-loader": "~1.0.1", + "webpack": "~4.10.2", + "webpack-cli": "~2.1.5", + "webpack-dev-server": "~3.1.4" } } diff --git a/packages/gallery/scripts/b2/bucket_setup_examples.sh b/packages/gallery/scripts/b2/bucket_setup_examples.sh new file mode 100755 index 0000000..fa6b83f --- /dev/null +++ b/packages/gallery/scripts/b2/bucket_setup_examples.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +# Fill these in +ACCOUNT_ID="" +APPLICATION_KEY="" +BUCKET_ID="" + +# These need to be populated by get account token +API_HOST="https://api001.backblazeb2.com" +ACCOUNT_AUTHORIZATION_TOKEN="" + +# Get account token +# curl "https://api.backblazeb2.com/b2api/v1/b2_authorize_account" -u "$ACCOUNT_ID:$APPLICATION_KEY" + + +# Read CORS rules on bucket +# curl \ +# -H "Authorization: $ACCOUNT_AUTHORIZATION_TOKEN" \ +# -d "{\"accountId\": \"$ACCOUNT_ID\", \"bucketTypes\": [\"allPrivate\",\"allPublic\"]}" \ +# "$API_HOST/b2api/v1/b2_list_buckets" + + +# Set CORS rules on bucket +# read -d "" UPDATE_BUCKET_JSON << EOF +# { +# "accountId": "$ACCOUNT_ID", +# "bucketId": "$BUCKET_ID", +# "corsRules": [ +# { +# "allowedHeaders": ["*"], +# "allowedOperations": [ +# "b2_download_file_by_id", +# "b2_download_file_by_name", +# "b2_upload_file" +# ], +# "allowedOrigins": [ +# "*" +# ], +# "corsRuleName": "downloadFromAnyOrigin", +# "exposeHeaders": null, +# "maxAgeSeconds": 3600 +# } +# ] +# } +# EOF +# curl \ +# -H "Authorization: $ACCOUNT_AUTHORIZATION_TOKEN" \ +# -d "$UPDATE_BUCKET_JSON" \ +# "$API_HOST/b2api/v1/b2_update_bucket" diff --git a/packages/gallery/src/server.js b/packages/gallery/src/server.js new file mode 100644 index 0000000..d754e9c --- /dev/null +++ b/packages/gallery/src/server.js @@ -0,0 +1,65 @@ +'use strict'; + +const express = require('express'); +const request = require('request'); +const bodyParser = require('body-parser'); + +// Constants +const B2_BASE_URL = 'https://api.backblazeb2.com/b2api/v1/'; + +// App +const app = express(); +app.use(bodyParser.text()); + +app.use( + express.static('.', { + dotfiles: 'ignore', + etag: false, + index: ['src/index.html'] + }) +); + +app.get('/api/v1/authorize_account', (req, res) => { + const auth = req.get('Authorization'); + + request( + { + url: B2_BASE_URL + 'b2_authorize_account', + headers: { Authorization: auth } + }, + function(error, response, body) { + res.set(response.headers); + res.send(body); + } + ); +}); + +app.post('/api/v1/get_upload_url', (req, res) => { + request.post( + { + url: req.get('apiUrl') + '/b2api/v1/b2_get_upload_url', + headers: { Authorization: req.get('Authorization') }, + body: req.body + }, + function(error, response, body) { + res.send(body); + } + ); +}); + +app.post('/api/v1/remove_file', (req, res) => { + request.post( + { + url: req.get('apiUrl') + '/b2api/v1/b2_delete_file_version', + headers: { Authorization: req.get('Authorization') }, + body: req.body + }, + function(error, response, body) { + res.send(body); + } + ); +}); + +module.exports = { + app: app +}; diff --git a/packages/gallery/webpack.config.js b/packages/gallery/webpack.config.js index 36cd3e8..d7d75c4 100644 --- a/packages/gallery/webpack.config.js +++ b/packages/gallery/webpack.config.js @@ -2,9 +2,14 @@ const path = require('path'); const webpack = require('webpack'); const ExtractTextPlugin = require('extract-text-webpack-plugin'); const HtmlWebpackPlugin = require('html-webpack-plugin'); +const server = require('./src/server.js'); + +const API_PORT = 8888; +const API_HOST = '127.0.0.1'; module.exports = { context: path.resolve(__dirname, './src'), + mode: 'development', entry: { app: './app.js', sw: './sw.js' @@ -15,9 +20,19 @@ module.exports = { publicPath: '/' }, devServer: { - contentBase: path.resolve(__dirname, './src'), + before: () => { + server.app.listen(API_PORT, API_HOST); + console.log(`Running api host on ${API_HOST}:${API_PORT}`); + }, + contentBase: path.join(__dirname, 'dist'), + host: '0.0.0.0', + https: true, headers: { - 'Service-Worker-Allowed': '/' + 'Service-Worker-Allowed': '/', + 'Access-Control-Allow-Origin': '*' + }, + proxy: { + '/api': `http://${API_HOST}:${API_PORT}` } }, module: { @@ -28,6 +43,15 @@ module.exports = { fallback: 'style-loader', use: 'css-loader' }) + }, + // fonts + { + test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, + loader: 'url-loader?limit=10000&minetype=application/font-woff' + }, + { + test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, + loader: 'file-loader' } ] },