Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: adding docker file, script and little documentation for self hosting #3

Merged
merged 3 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ dist
# IDEs and editors
.github
.vscode
.vs
.vs
localFileStore
28 changes: 20 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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" ]
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"]
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
13 changes: 8 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
Expand All @@ -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();

Expand All @@ -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;

Expand All @@ -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}`));
Loading