Skip to content

Commit

Permalink
Doc: Deploy from source code.
Browse files Browse the repository at this point in the history
  • Loading branch information
llaoj committed Jun 9, 2024
1 parent c84e3e7 commit ac233c4
Show file tree
Hide file tree
Showing 13 changed files with 350 additions and 648 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ dist/
frontend/.env
output/
bin/
.vscode
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,5 @@ Safari on iOS

## More

- [How to deploy](/docs/how-to-deploy.md)
- [Deployment GCopy in OpenWrt with source code](./docs/deploy-in-openwrt.md)
- [Deploy by docker](/docs/deploy-by-docker.md)
- [Deploy from source code](./docs/deploy-from-source.md)
4 changes: 2 additions & 2 deletions deploy/nginx-example.conf
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
upstream upstream_gcopy_frontend {
server 172.10.10.10:3375 weight=1 max_fails=3 fail_timeout=30s;
server 127.0.0.1:3375 weight=1 max_fails=3 fail_timeout=30s;
}

upstream upstream_gcopy {
server 172.10.10.10:3376 weight=1 max_fails=3 fail_timeout=30s;
server 127.0.0.1:3376 weight=1 max_fails=3 fail_timeout=30s;
}

server {
Expand Down
69 changes: 69 additions & 0 deletions docs/deploy-by-docker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Deploy by docker
We have prepared the container image for you. Deploying directly using Docker is the most convenient option.

## Standalone
By leveraging the container orchestration capabilities of Docker Compose, we can deploy the frontend and backend services of gcopy.

#### Create`docker-compose.yml`

Create directory and download `deploy/docker-compose.yml` to the directory, then modify the parameter `<var-name>` of `docker-compose.yml`.

```bash
# The directory location can be customized.
mkdir -p /opt/gcopy
wget -O /opt/gcopy/docker-compose.yml https://raw.githubusercontent.com/llaoj/gcopy/main/deploy/docker-compose.yml
```

Refer the usage of the gcopy:

```bash
$ /gcopy --help
Usage of gcopy:
-app-key string
Secret used to encrypt and decrypt data, recommend using random strings over 8 characters.
-debug
Enable debug mode
-listen string
The server will listen this ip and port, format: [ip]:port (default ":3376")
-max-content-length int
The max synchronized content length, unit: MiB. (default 10)
-smtp-host string
Represents the host of the SMTP server.
-smtp-password string
The password to use to authenticate to the SMTP server.
-smtp-port int
Represents the port of the SMTP server. (default 587)
-smtp-sender string
The Sender of the email, if the field is not given, the username will be used.
-smtp-ssl bool
Whether an SSL connection is used. It should be false in most cases since the authentication mechanism should use the STARTTLS extension instead.
-smtp-username string
The username to use to authenticate to the SMTP server.
-tls
Enable TLS
-tls-cert-file string
The certificate for the server, required if tls enable.
-tls-key-file string
The private key for the server, required if tls enable.
-version
Print version
```
### Create frontend configuration file
Download frontend configuration file to the frontend directory without any modifications needed.
```bash
mkdir -p /opt/gcopy/frontend
wget -O /opt/gcopy/frontend/.env.production https://raw.githubusercontent.com/llaoj/gcopy/main/frontend/.env.sample
```
### Start the containers
```sh
docker-compose up -d
```
## Behind a proxy
HTTPS is required. I recommend deploying it behind a proxy, eg. Nginx, Kong.
You can refer to the Nginx configuration file: `deploy/nginx-example.conf`.
101 changes: 101 additions & 0 deletions docs/deploy-from-source.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Deploy from source code
If you want to start from the source code, deploy a development environment, or contribute code to gcopy, this document is suitable for you.

## Prerequisites
The following software needs to be installed on the server in advance:

- git
- golang 1.20+
- Node.js 18.17+

## Clone the source code
We clone the code to `/opt/gcopy/`. This directory location is not mandatory, you can customize it according to your needs.

```bash
cd /opt
git clone https://github.com/llaoj/gcopy.git
cd gcopy
```

## Backend
The backend primarily responsible for temporary storage of clipboard data and authentication. It does not store user data long-term, instead, it temporarily stores the latest clipboard data in memory, which expires after a period of time.

### Direct run
You can quickly start the local server using the `go run` command. The backend service is configured via command-line arguments. You need to manually replace `<var-name>`. The `-app-key` parameter in the configuration is a custom encryption key used for data encryption, recommended to be at least 8 characters long. Parameters starting with `-smtp-*` are all related to email services because gcopy relies on email login. Common email service providers offer SMTP services, such as Gmail, QQ Mail etc.

```bash
go run cmd/gcopy.go \
-app-key=<app-key> \
-smtp-host=<smtp-host> \
-smtp-port=<smtp-port> \
-smtp-username=<smtp-username> \
-smtp-password=<smtp-password> \
-smtp-ssl \
-debug
```

### Build and run
You can also build before running. This way, you don't need to build before each run.

```shell
make ./bin/gcopy
chmod +x ./bin/gcopy
/opt/gcopy/bin/gcopy \
-app-key=<app-key> \
-smtp-host=<smtp-host> \
-smtp-port=<smtp-port> \
-smtp-username=<smtp-username> \
-smtp-password=<smtp-password> \
-smtp-ssl
```

## Frontend
The frontend primarily handles user interaction and is implemented based on the browser. It relies on the backend service to temporarily store user clipboard data, thereby achieving the goal of cross-device sharing.

### 配置文件
The frontend service is configured using a configuration file, mainly declaring information such as the backend service's address. Different environment configurations are stored in files with different `.env` file extensions.

```bash
cd /opt/gcopy/frontend
# development environment
cp .env.sample .env.local
# production environment
cp .env.sample .env.production
```

Modify the configuration file to change the backend service's address `SERVER_URL`. Since we are deploying locally, change the host to `localhost`.

```ini
- SERVER_URL=http://gcopy:3376
+ SERVER_URL=http://localhost:3376
```

### Run
The configuration files for the development environment and the production environment are different, as are the startup commands.

#### Development environment
Use the following command to start the development mode. It supports hot code reloading, error reporting, etc., and it listens on port 3375 by default.

```bash
cd /opt/gcopy/frontend
npm ci
npm run dev
```

Due to browser restrictions on using HTTPS, we'll use `--experimental-https` to enable HTTPS for the web server, using a self-signed certificate.
Now, you can access gcopy using `https://<hostip-or-localhost>:3375`, and you don't need to add a proxy in front of it.

#### Production environment
Unlike development mode, the production mode is a mini web server that only includes essential files. Before starting it, you need to compile it with `npm run build`.

```bash
cd /opt/gcopy/frontend
npm ci
npm run build
# Copy the .next/static folders or be handled by a CDN instead
cp -r .next/static .next/standalone/.next/
NODE_ENV=production PORT=3375 node .next/standalone/server.js
```

In production mode, we recommend deploying behind a reverse proxy such as Nginx or Kong. This way, you can easily manage certificates and configure proxies. You'll need to prepare a domain name and its corresponding certificate for this setup.
We'll use Nginx as an example, referring to `deploy/nginx-example.conf`.
Loading

0 comments on commit ac233c4

Please sign in to comment.