This is a monolithic application that combines a Vue.js frontend with a Golang backend using the Echo framework. The frontend uses 3 templates but user can just check /api/* as well
vue-project/
├── public/
│ ├── index.html
│ ├── favicon.ico
│ └── robots.txt
├── src/
│ ├── App.vue
│ ├── main.js
│ ├── plugins/
│ │ └── ..
│ ├── views/
│ │ ├── Home.vue
│ │ ├── Health.vue
│ │ └── NotFound.vue
│ ├── router/
│ │ └── index.js
│ ├── store/
│ │ └── index.js
├── dist/ (generated after running `npm run build`)
├── package.json
├── vue.config.js
└── main.go
- Node.js and npm
- Vue CLI
- Golang
We are using Vue.js version 2.6.12 and other related dependencies to ensure compatibility with macOS Big Sur (version 11.7.10). This specific version of Vue.js and its ecosystem are known to work well with older versions of macOS and npm (such as version 6.13.4). By using these versions, we can avoid potential compatibility issues and ensure a smoother development experience on macOS Big Sur. But it's strognly advised to use the most recent packages by simply using @latest
Why this demo can make sense? Using Echo to serve static files for a Vue.js application in production is a common practice, especially when the backend is written in Golang. However, it is important to understand the context and the requirements of your application. So, here are the quick tips to get started:
If you don't have nvm installed, follow the instructions from the official nvm repository to install it.
Use nvm to install Node.js version 14:
nvm install 14Switch to Node.js version 14 using nvm:
nvm use 14If you don't have Vue CLI installed, install it globally:
npm install -g @vue/cli@4.5.15Navigate to the project's root directory and install the dependencies:
npm installInstall the required packages:
#npm install @mdi/font sass-loader@10 sass --saveBuild the Vue.js project to generate the dist directory:
npm run buildClear the npm cache: Run the following command to clear the npm cache:
npm cache clean --forceDelete node_modules and package-lock.json: Delete the node_modules directory and the package-lock.json file to ensure a fresh installation of dependencies:
rm -rf node_modules package-lock.jsonRun the following command to initialize a new Go module in your project directory:
go mod init vue-projectRun the following command to download and add the required packages to your go.mod file:
go get github.com/labstack/echo/v4
go get github.com/labstack/echo/v4/middlewareVerify the Go Module:
go mod tidyRun the Golang server to serve the frontend and provide API endpoints:
go run main.goOpen your browser and navigate to http://localhost:8080. You should see the Vue.js application served by the Golang server.
#!/bin/bash
# Navigate to the project directory
cd /path/to/your/project
# Install dependencies
npm install
# Build the Vue.js application
npm run build
# Ensure that the Go server uses the generated dist directory
# No need to copy files to a public directory since the Go server already serves from dist
#compile the Go app & run:
go build -o app
note the bash script required permisions are chmod +x deploy.sh
name: Deploy
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install Dependencies
run: npm install
- name: Build Vue.js Application
run: npm run build
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.16'
- name: Build Go Application
run: go build -o app
- name: Deploy to Server
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
run: |
mkdir -p ~/.ssh
echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan your-server.com >> ~/.ssh/known_hosts
scp -r dist/* user@your-server:/path/to/your/deployment/directory
scp app user@your-server:/path/to/your/deployment/directory
ssh user@your-server 'bash -s' << 'EOF'
cd /path/to/your/deployment/directory
screen -dmS go_app ./app
EOF
listen 80;
server_name yourdomain.com;
location / {
root /path/to/your/vuejs/dist;
try_files $uri $uri/ /index.html;
}
}
- Golang website app that uses templates via "html/template"
- Golang used for Scrap data using Go Colly
- Go RESTful service with GoWeb framework
- ..
- ..
- study & understand php vs golang
- ...
This project is licensed under the MIT License.