diff --git a/.dockerignore b/.dockerignore index 3c151c2..d1fa66a 100644 --- a/.dockerignore +++ b/.dockerignore @@ -7,4 +7,5 @@ dist # IDEs and editors .github .vscode -.vs \ No newline at end of file +.vs +localFileStore \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index b6e6a05..e73a1c2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,18 +1,30 @@ -################### -# BUILD FOR LOCAL DEVELOPMENT -################### +FROM node:21-alpine3.18 -FROM node:18-alpine +# get git +RUN apk add --no-cache git bash -WORKDIR app +WORKDIR /mydraft +# setup the server COPY . . RUN npm i +RUN echo -e "\nVITE_SERVER_URL=/api" >> ./.env RUN npm run build RUN cp ./.env ./dist/ -EXPOSE 8001 +# setup react app +RUN cd ./dist/ && git clone https://github.com/mydraft-cc/ui.git -# Start the server using the production build -CMD [ "node", "dist/index.js" ] \ No newline at end of file +RUN cd ./dist/ui && echo "VITE_SERVER_URL=/api" > ./.env +RUN cd ./dist/ui && rm -rf ./package-lock.json +RUN cd ./dist/ui && npm install +RUN cd ./dist/ui && npm run build + +RUN mkdir ./localFileStore +RUN chmod -R a+rw ./localFileStore + +USER node +EXPOSE 8001/tcp + +CMD ["node", "./dist/index.js"] \ No newline at end of file diff --git a/README.md b/README.md index a1a9694..5ff1864 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,10 @@ # Server for mydraft Stores the documents in Google Cloud storage and provides a websocket interface for live collaboration over yjs. + +### Self hosting + +``` +docker build -t mydraft/app . +docker run --name mydraft -d -p 8001:8001 -v ${PWD}/localFileStore:/mydraft/localFileStore mydraft/app +``` \ No newline at end of file diff --git a/index.ts b/index.ts index b3d2013..08fe64b 100644 --- a/index.ts +++ b/index.ts @@ -9,6 +9,7 @@ import express from 'express'; import expressWebsockets from 'express-ws'; import ShortUniqueId from 'short-unique-id'; import fileStore from './fileStore'; +import path from 'node:path'; const uid = new ShortUniqueId({ length: 20 }); const serverPort = parseInt(process.env.SERVER_PORT || '8080'); @@ -56,17 +57,17 @@ const { app } = expressWebsockets(express()); app.use(cors()); app.use(express.json()); -app.get('/health', (_, response) => { +app.get('/api/health', (_, response) => { response.send({ status: 'Healthy' }); }); -app.ws('/collaboration', (websocket, request) => { +app.ws('/api/collaboration', (websocket, request) => { server.handleConnection(websocket, request, {}); }); -app.get('/:tokenRead', async (request, response) => { +app.get('/api/:tokenRead', async (request, response) => { const file = storageBucket.file(request.params.tokenRead); const [exists] = await file.exists(); @@ -80,7 +81,7 @@ app.get('/:tokenRead', async (request, response) => { file.createReadStream().pipe(response); }); -app.post('/', async (request, response) => { +app.post('/api/', async (request, response) => { const tokenWrite = uid.rnd(); const tokenRead = uid.rnd(); @@ -97,7 +98,7 @@ app.post('/', async (request, response) => { return response.status(201).json({ readToken: tokenRead, writeToken: tokenWrite }); }); -app.put('/:tokenRead/:tokenWrite', async (request, response) => { +app.put('/api/:tokenRead/:tokenWrite', async (request, response) => { const tokenWrite = request.params.tokenWrite; const tokenRead = request.params.tokenRead; @@ -121,4 +122,6 @@ app.put('/:tokenRead/:tokenWrite', async (request, response) => { return response.status(201).json({ readToken: tokenRead, writeToken: tokenWrite }); }); +app.use("/*", express.static(path.join(__dirname, './ui/dist'))) + app.listen(serverPort, () => console.log(`Listening on http://127.0.0.1:${serverPort}`)); \ No newline at end of file