Skip to content

Commit

Permalink
indexer test
Browse files Browse the repository at this point in the history
  • Loading branch information
pacoccino committed Jan 19, 2022
1 parent fd86895 commit d1242c1
Show file tree
Hide file tree
Showing 9 changed files with 965 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .env.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ PRISMA_HIDE_UPDATE_MESSAGE=true
# then invoke your api-side webhook function you will use this secret to sign and the verify.
# Important: Please change this default to a strong password or other secret
WEBHOOK_SECRET=THIS_IS_NOT_SECRET_PLEASE_CHANGE


#######

FILESYSTEM_FOLDER=./fs
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ yarn-error.log
web/public/mockServiceWorker.js
web/types/graphql.d.ts
api/types/graphql.d.ts

fs
3 changes: 2 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"dependencies": {
"@redwoodjs/api": "0.41.0",
"@redwoodjs/graphql-server": "0.41.0"
"@redwoodjs/graphql-server": "0.41.0",
"node-static": "^0.7.11"
}
}
13 changes: 13 additions & 0 deletions api/src/lib/files/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import fs from 'fs'

export async function listDir(path: string): Promise<Array<string>> {
return new Promise((resolve, reject) => {
fs.readdir(path, (err, files) => {
if(err) {
reject(err)
} else {
resolve(files)
}
})
})
}
15 changes: 15 additions & 0 deletions api/src/lib/static/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {Server} from 'node-static';

export function staticServer(path: string) {
const file = new Server(path);
const port = 8080;

require('http').createServer(function (request, response) {
request.addListener('end', function () {
file.serve(request, response);
}).resume();
}).listen(port);

console.log(`Static server started, path ${path}, port ${port}`)
}

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@
},
"prisma": {
"seed": "yarn rw exec seed"
},
"scripts": {
"indexer": "yarn rw exec FileIndexer/test",
"static": "yarn rw exec FileServer/test"
}
}
20 changes: 20 additions & 0 deletions scripts/FileIndexer/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Prisma } from '@prisma/client'

import { listDir } from "api/src/lib/files";
import { db } from 'api/src/lib/db'

export default async () => {

const path = process.env["FILESYSTEM_FOLDER"]
const files = await listDir(path)
console.log("importing files", files)
Promise.all(
//
// Change to match your data model and seeding needs
//
files.map(async (path: Prisma.ImageCreateInput['path']) => {
const record = await db.image.create({ data: { path } })
console.log(record)
})
)
}
6 changes: 6 additions & 0 deletions scripts/FileServer/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { staticServer } from "api/src/lib/static";

export default async () => {
const path = process.env["FILESYSTEM_FOLDER"]
staticServer(path)
}
Loading

0 comments on commit d1242c1

Please sign in to comment.