From 3bf6ef3da9d83382f7b4804e5fe342e463a06a0b Mon Sep 17 00:00:00 2001 From: shoofle Date: Tue, 24 Sep 2024 22:49:37 -0400 Subject: [PATCH] switch to using nginx reverse proxy --- client/src/apiTools.jsx | 2 +- db/package.json | 1 + db/the_big_db.db/dbs/default/data-shm | Bin 32768 -> 32768 bytes db/the_big_db.db/dbs/default/stats.json | 2 +- db/the_big_db.db/metastore/data-shm | Bin 32768 -> 32768 bytes readme.md | 90 ++---------------------- server/api.js | 56 +++++++++++++++ server/dbHelper.js | 2 +- server/server.js | 53 +------------- the-forest.nginx.conf | 12 ++++ 10 files changed, 80 insertions(+), 138 deletions(-) create mode 100644 server/api.js create mode 100644 the-forest.nginx.conf diff --git a/client/src/apiTools.jsx b/client/src/apiTools.jsx index 980b409e..af9ad6f0 100644 --- a/client/src/apiTools.jsx +++ b/client/src/apiTools.jsx @@ -2,7 +2,7 @@ import Graph from 'graphology'; // This is wrapper functtions to do requests to the api, from the frontend. -export const apiUrl = "http://127.0.0.1:3001" +export const apiUrl = `${window.location.origin}/api`; const defaults = { headers: {'Content-Type': 'application/json'} diff --git a/db/package.json b/db/package.json index 2c9a8d7b..3030f34e 100644 --- a/db/package.json +++ b/db/package.json @@ -4,6 +4,7 @@ "description": "database tools for the forest", "main": "db.js", "scripts": { + "start": "sqld -d the_big_db.db --http-listen-addr=127.0.0.1:8000", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { diff --git a/db/the_big_db.db/dbs/default/data-shm b/db/the_big_db.db/dbs/default/data-shm index 4b2bc325225a5c3b9b08736f7a2a10f5c454f8f2..617845ee7f15fbd8b3e9c3e0d5b6012d29093609 100644 GIT binary patch delta 479 zcmb7<$4WzC5Ju0v8YCd-M$nytz4va6J$7T)*n98YXm0FXuQi~E@8Hg*_zLbt+=@#P z1pOmM#GS(&m~Up7qA0SW$QIAXFdQa-*FU*yAIVGaA`wJ5 z`$D|RNAEcXi$4Jb5ajdnWdq>FBP=%tT-0|N{)#4sa_GR8O)OftnZ zGt4r_JZ2VH#DapAC6-xXl{MDcV3RGj*w K<_w#VQq(7LwrL&! delta 165 zcmZo@U}|V!s+V}A%K!t63=9IQKn@cS@9_EI@%iwSj(MDc59ZYLANrB^u3>%0xeQX( u!^{Sm`yUBF#hDmXCpOOB{Dq0jVH5KWMn=ZXADR9#P5$HRvH26z9!mfUuRA{g diff --git a/db/the_big_db.db/dbs/default/stats.json b/db/the_big_db.db/dbs/default/stats.json index b7b47342..568bea35 100644 --- a/db/the_big_db.db/dbs/default/stats.json +++ b/db/the_big_db.db/dbs/default/stats.json @@ -1 +1 @@ -{"id":"7de13931-444c-4dd2-a19a-5cd1e70b7f36","rows_written":149,"rows_read":12929,"storage_bytes_used":20480,"write_requests_delegated":0,"current_frame_no":151,"top_query_threshold":15,"top_queries":[{"rows_written":0,"rows_read":15,"query":"select id, html from pages"},{"rows_written":0,"rows_read":15,"query":"select id, title from pages"},{"rows_written":0,"rows_read":16,"query":"select id, html from pages"},{"rows_written":0,"rows_read":16,"query":"select id, title from pages"},{"rows_written":0,"rows_read":17,"query":"select id, html from pages"},{"rows_written":0,"rows_read":17,"query":"select id, title from pages"},{"rows_written":0,"rows_read":18,"query":"select id, html from pages"},{"rows_written":0,"rows_read":18,"query":"select id, title from pages"},{"rows_written":0,"rows_read":19,"query":"select id, html from pages"},{"rows_written":0,"rows_read":19,"query":"select id, title from pages"}],"slowest_query_threshold":0,"slowest_queries":[{"elapsed_ms":0,"query":"select title from pages","rows_written":0,"rows_read":1},{"elapsed_ms":0,"query":"select title from pages","rows_written":0,"rows_read":2},{"elapsed_ms":1,"query":"\nalter table pages \nadd column html text\n","rows_written":1,"rows_read":4},{"elapsed_ms":1,"query":"\ncreate table if not exists renders (\n id integer primary key,\n title varchar(255),\n description text\n)\n","rows_written":2,"rows_read":1},{"elapsed_ms":1,"query":"insert into pages (title, description) values (?, ?) returning id","rows_written":1,"rows_read":1},{"elapsed_ms":1,"query":"replace into pages (id, title, description) values (?, ?, ?)","rows_written":1,"rows_read":1},{"elapsed_ms":1,"query":"replace into pages (id, title, description, html) values (?, ?, ?, ?)","rows_written":1,"rows_read":1},{"elapsed_ms":1,"query":"select * from pages where id=@val","rows_written":0,"rows_read":1},{"elapsed_ms":1,"query":"select count(*) as num_pages from pages","rows_written":0,"rows_read":1},{"elapsed_ms":7,"query":"replace into pages (id, title, description) values (?, ?, ?)","rows_written":1,"rows_read":1}],"embedded_replica_frames_replicated":0,"query_count":1934,"query_latency":161187} \ No newline at end of file +{"id":"7de13931-444c-4dd2-a19a-5cd1e70b7f36","rows_written":149,"rows_read":13997,"storage_bytes_used":20480,"write_requests_delegated":0,"current_frame_no":151,"top_query_threshold":0,"top_queries":[{"rows_written":0,"rows_read":16,"query":"select id, html from pages"},{"rows_written":0,"rows_read":16,"query":"select id, title from pages"},{"rows_written":0,"rows_read":17,"query":"select id, html from pages"},{"rows_written":0,"rows_read":17,"query":"select id, title from pages"},{"rows_written":0,"rows_read":18,"query":"select id, html from pages"},{"rows_written":0,"rows_read":18,"query":"select id, title from pages"},{"rows_written":0,"rows_read":19,"query":"select id, html from pages"},{"rows_written":0,"rows_read":19,"query":"select id, title from pages"},{"rows_written":0,"rows_read":19,"query":"select id, html from pages"},{"rows_written":0,"rows_read":19,"query":"select id, title from pages"}],"slowest_query_threshold":0,"slowest_queries":[{"elapsed_ms":0,"query":"select title from pages","rows_written":0,"rows_read":1},{"elapsed_ms":0,"query":"select title from pages","rows_written":0,"rows_read":2},{"elapsed_ms":1,"query":"\nalter table pages \nadd column html text\n","rows_written":1,"rows_read":4},{"elapsed_ms":1,"query":"\ncreate table if not exists renders (\n id integer primary key,\n title varchar(255),\n description text\n)\n","rows_written":2,"rows_read":1},{"elapsed_ms":1,"query":"insert into pages (title, description) values (?, ?) returning id","rows_written":1,"rows_read":1},{"elapsed_ms":1,"query":"replace into pages (id, title, description) values (?, ?, ?)","rows_written":1,"rows_read":1},{"elapsed_ms":1,"query":"replace into pages (id, title, description, html) values (?, ?, ?, ?)","rows_written":1,"rows_read":1},{"elapsed_ms":1,"query":"select * from pages where id=@val","rows_written":0,"rows_read":1},{"elapsed_ms":1,"query":"select count(*) as num_pages from pages","rows_written":0,"rows_read":1},{"elapsed_ms":7,"query":"replace into pages (id, title, description) values (?, ?, ?)","rows_written":1,"rows_read":1}],"embedded_replica_frames_replicated":0,"query_count":1994,"query_latency":165074} \ No newline at end of file diff --git a/db/the_big_db.db/metastore/data-shm b/db/the_big_db.db/metastore/data-shm index dab33ccc23d4332072f2b132db77dc386d303c8f..9a229a3399ad7d40b888d56a6e0810ea1478b8c4 100644 GIT binary patch delta 40 icmZo@U}|V!nxMj?a`Ahd1-k@$WXk1CH15WP1@! delta 40 icmZo@U}|V!nxMinsdjgT1-k_Mae?X0Xxxnn3+e$OcMjJ8 diff --git a/readme.md b/readme.md index a263d1f9..6a082d8e 100644 --- a/readme.md +++ b/readme.md @@ -8,87 +8,9 @@ I'm leaving all the create-react-app stsuff at the bottom. # build log -`npx create-react-app the-forest` -`brew tap libsql/sqld` -`brew install sqld-beta` -to run cast `sqld -d /tmp/data.db --http-listen-addr=127.0.0.1:8000` and alsso `npm start` -`node initialize_db.js` `node populate_db.js` -`npm install react-router-dom` -`npm install @tanstack/react-query` i think - - - - - - - -# Getting Started with Create React App - -This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). - -## Available Scripts - -In the project directory, you can run: - -### `npm start` - -Runs the app in the development mode.\ -Open [http://localhost:3000](http://localhost:3000) to view it in your browser. - -The page will reload when you make changes.\ -You may also see any lint errors in the console. - -### `npm test` - -Launches the test runner in the interactive watch mode.\ -See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. - -### `npm run build` - -Builds the app for production to the `build` folder.\ -It correctly bundles React in production mode and optimizes the build for the best performance. - -The build is minified and the filenames include the hashes.\ -Your app is ready to be deployed! - -See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. - -### `npm run eject` - -**Note: this is a one-way operation. Once you `eject`, you can't go back!** - -If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. - -Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own. - -You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it. - -## Learn More - -You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). - -To learn React, check out the [React documentation](https://reactjs.org/). - -### Code Splitting - -This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) - -### Analyzing the Bundle Size - -This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) - -### Making a Progressive Web App - -This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) - -### Advanced Configuration - -This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) - -### Deployment - -This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) - -### `npm run build` fails to minify - -This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) +- `brew tap libsql/sqld` +- `brew install sqld-beta` +- to run cast `npm start` in each directory +- `node initialize_db.js` `node populate_db.js` +- `npm install` should install all dependencies +- also you need nginx. `brew install nginx` and then `ln -s the-forest.nginx.conf /usr/local/etc/nginx/servers/the-forest.nginx.conf` or wherever it needs to go \ No newline at end of file diff --git a/server/api.js b/server/api.js new file mode 100644 index 00000000..5cef24e7 --- /dev/null +++ b/server/api.js @@ -0,0 +1,56 @@ +const express = require('express'); +const showdown = require('showdown'); +const { query } = require('./dbHelper.js'); +const { graphFromList } = require('./graphStuff.js'); + +const app = express.Router(); +const converter = new showdown.Converter(); + +app.get('/pages', (req, res) => { + query('select id, title from pages', []) + .then((r) => r.rows) + .then((row) => row.map(([id, title]) => {return {"id": id, "title": title};})) + .then((pages) => res.status(200).json(pages)) + .catch((error) => res.status(500).json({"error": error})); +}); + +app.post('/page/new', (req, res) => { + query('insert into pages (title, description) values (?, ?) returning id', + ["new page", "this page is new!"]) + .then((r) => r.rows[0]) + .then((row) => res.status(200).json({id: row[0]})) + .catch((error) => res.status(500).json({"error": error})) +}); + +app.get('/page/:id', (req, res) => { + query('select * from pages where id=?', [req.params.id]) + .then((r) => { + if (r.rows.length == 0) res.status(404).json({"error": "page not found in db"}); + else return r.rows[0]; + }).then((row) => res.status(200).json(row)) + .catch((error) => res.status(500).json({"error": error})); +}); + +app.post('/page/:id', (req, res) => { + const html = converter.makeHtml(req.body.description); + query('replace into pages (id, title, description, html) values (?, ?, ?, ?)', + [req.params.id, req.body.title, req.body.description, html]) + .then(() => res.status(200).json({})) + .catch((error) => res.status(500).json({"error": error})); +}); + +app.delete('/page/:id', (req, res) => { + query('delete from pages where id = ?', [req.params.id]) + .then(() => res.status(200).json({id: req.params.id})) + .catch((error) => res.status(500).json({"error": error})) +}) + +app.get('/graph', (req, res) => { + query('select id, html from pages', []) + .then((r) => r.rows) + .then((stuff) => graphFromList(stuff)) + .then((graph) => res.json(graph)) + .catch((error) => res.status(500).json({"error": error})); +}); + +module.exports = app; \ No newline at end of file diff --git a/server/dbHelper.js b/server/dbHelper.js index 39f3dcad..e26a657d 100644 --- a/server/dbHelper.js +++ b/server/dbHelper.js @@ -1,6 +1,6 @@ // helper function(s?) to make a query to the database -const db_url = 'http://localhost:8000' +const db_url = 'http://127.0.0.1:8000' async function query(q, params) { return fetch(db_url, { diff --git a/server/server.js b/server/server.js index 414128c1..83dad1b3 100644 --- a/server/server.js +++ b/server/server.js @@ -1,7 +1,5 @@ const express = require('express'); -const showdown = require('showdown'); -const { query } = require('./dbHelper.js'); -const { graphFromList } = require('./graphStuff.js'); +const apiRoutes = require('./api.js'); const port = process.env.PORT || 3001; // Use the port provided by the host or default to 3000 @@ -14,55 +12,8 @@ app.use(function (req, res, next) { next(); }); -const converter = new showdown.Converter(); +app.use("/api", apiRoutes); app.listen(port, () => { console.log(`Server listening on port ${port}`); -}); - -app.get('/pages', (req, res) => { - query('select id, title from pages', []) - .then((r) => r.rows) - .then((row) => row.map(([id, title]) => {return {"id": id, "title": title};})) - .then((pages) => res.status(200).json(pages)) - .catch((error) => res.status(500).json({"error": error})); -}); - -app.post('/page/new', (req, res) => { - query('insert into pages (title, description) values (?, ?) returning id', - ["new page", "this page is new!"]) - .then((r) => r.rows[0]) - .then((row) => res.status(200).json({id: row[0]})) - .catch((error) => res.status(500).json({"error": error})) -}); - -app.get('/page/:id', (req, res) => { - query('select * from pages where id=?', [req.params.id]) - .then((r) => { - if (r.rows.length == 0) res.status(404).json({"error": "page not found in db"}); - else return r.rows[0]; - }).then((row) => res.status(200).json(row)) - .catch((error) => res.status(500).json({"error": error})); -}); - -app.post('/page/:id', (req, res) => { - const html = converter.makeHtml(req.body.description); - query('replace into pages (id, title, description, html) values (?, ?, ?, ?)', - [req.params.id, req.body.title, req.body.description, html]) - .then(() => res.status(200).json({})) - .catch((error) => res.status(500).json({"error": error})); -}); - -app.delete('/page/:id', (req, res) => { - query('delete from pages where id = ?', [req.params.id]) - .then(() => res.status(200).json({id: req.params.id})) - .catch((error) => res.status(500).json({"error": error})) -}) - -app.get('/graph', (req, res) => { - query('select id, html from pages', []) - .then((r) => r.rows) - .then((stuff) => graphFromList(stuff)) - .then((graph) => res.json(graph)) - .catch((error) => res.status(500).json({"error": error})); }); \ No newline at end of file diff --git a/the-forest.nginx.conf b/the-forest.nginx.conf new file mode 100644 index 00000000..5fddc5ca --- /dev/null +++ b/the-forest.nginx.conf @@ -0,0 +1,12 @@ +# another virtual host using mix of IP-, name-, and port-based configuration + +server { + listen 8080; + + location /api/ { + proxy_pass http://localhost:3001/api/; + } + location / { + proxy_pass http://localhost:3000/; + } +} \ No newline at end of file