go-web-practice is a learning project. Implement a simple web application by using Golang's native library.
This project is learned and written by the video "Go Web Programming Quick Start [Golang/Go Language] (Complete)" by UP's main software technician on B station.
Based on the video, some modifications have been made:
This project has been tested and deployed, and the Linux Host Deployment Guide is below.
Project structure:
go-web-practice
.
├── LICENSE
├── README.md
├── build
│ └── package
│ ├── dockerfile.build_run
│ └── dockerfile.run
├── cmd
│ └── main.go
├── configs
│ └── config.json
├── deployments
│ └── docker-compose.yaml
├── go.mod
├── internal
│ ├── config
│ │ ├── config.go
│ │ └── config_test.go
│ ├── controller
│ │ ├── controller.go
│ │ ├── index.go
│ │ ├── look.go
│ │ └── welcome.go
│ ├── log
│ │ └── log.go
│ ├── middleware
│ │ ├── cross.go
│ │ ├── log.go
│ │ └── timeout.go
│ └── templates
│ └── template.go
└── web
├── img
│ ├── favicon.ico
│ ├── golang-down.png
│ ├── golang-right.png
│ └── golang.png
├── plugins
│ ├── bootstrap
│ │ ├── css
│ │ │ └── bootstrap.min.css
│ │ └── js
│ │ └── bootstrap.min.js
│ └── jquery
│ └── js
│ └── jquery.min.js
└── templates
├── index.tmpl
├── look.tmpl
├── test.tmpl
└── welcome.tmpl
Notice
The following are the current problems of the project
Or something to pay attention to when doing university homework.
- The project was developed using Goland, and the migration of other IDEs is unknown.
- Only a little comment is written in main.go, and the comments of other files are updated in the future.
- There are very few routing interfaces in the current project, so you can add them yourself if you need them.
- No database.
- Before running with docker and docker-compose, you need to compile the project yourself or place the executable file in the project directory.
First you have to make sure you have installed Golang's environment. If you modify the project, you need to recompile and get the executable file before it can be deployed and run. **
Golang's cross-compilation provides great convenience, which allows us to compile executable files that can run in Linux on Windows.
You can use the following .bat script to complete the executable file compilation in Windows and Linux environments in Windows.
# 请以项目目录为工作目录执行该脚本
# windows 可执行文件编译
go build -o go-web-practice.exe cmdmain.go
# linxu 可执行文件编译
set CGO_ENABLED = 0
set GOOS = linux
set GOARCH = amd64
go build -o go-web-practice .cmdmain.go
pause If you are developing on MacOS or Linux, you can build it with make .
makeThis project uses a JSON file to configure.
Check the configuration semantics of config/conifg.json as shown in the following table and modify them according to your needs.
| Configuration fields | Semantics |
|---|---|
| static | The path to the static file |
| template | The path to the go template |
| address | The address of the service startup (default is empty, and public network deployment can be implemented) |
| port | The port for service startup |
| handleTimeoutSecond | request processing timeout time (this configuration is not used in the project and is reserved) |
| trace | trace logger The path to output the log (not used in the project, reserved) |
| info | info logger path to output log |
| Warning | warning logger path to output log |
| error | error logger The path to output log |
The following directory structure must be ensured when running (taking Linux running as an example):
.
├── configs
│ └── config.json
├── go-web-practice
└── web
└── ...Run the executable file directly and see the following prompts that the run is successful:

You can test it with curl localhost:8000/welcome , which will return the html of the welcome interface.
Native access (port configuration is not modified): http://localhost:8000.
If you are deploying on a server and want to access it through the public network, remember to set up a firewall to open the corresponding port. If it is a cloud server, you must also open the security group.
The master branch has updated the dockerfile and used docker to test the compilation and operation of go-web-practice.
The following is the Docker compilation/deployment run guide for go-web-practice.
What is Docker, how to install and how to use it, will not be explained in detail here.
For details, please refer to the official Docker document: https://docs.docker.com/
Execute the commands in the following order in the project directory to complete the compilation at the time of image building and run the service when the container starts.
$ docker build -t go-web-practice -f ./build/package/dockerfile.build_run . docker run -d -p 8000:8000 go-web-practiceAfter local compilation and generation of executable files, build the running image, which can ensure that the image can run normally even if the image size is small.
The image size of the image compiled and run using Docker above is close to 1GB, while the runnable image built in this section is only about 100MB.
Note: After the image named go-web-practice has been built above, the old image must be deleted before executing the instructions in this section.
First, make sure that there is a compiled executable file in the project directory.
There is a prepared dockerfile in the /build/package directory of the project to generate the image file. Use the following instructions to build the docker image.
$ docker build -t go-web-practice -f ./build/package/dockerfile.run . Enter the following command when running the container in the background:
$ docker run -d -p 8000:8000 go-web-practicedocker-composeAfter building the image, it is also deployed directly by using the docker-compose directive:
$ docker-compose -f deployments/docker-compose.yaml upNote that if the port configuration is modified, you need to modify the docker run command and the port mapping field in the docker-compose.yaml configuration file.
If you have any questions, please contact me by email: [email protected]