add deploy files

This commit is contained in:
kenneth 2023-12-25 11:41:23 +08:00
parent 887c5bc859
commit 5cc3de8e26
3 changed files with 67 additions and 3 deletions

16
Dockerfile Normal file
View File

@ -0,0 +1,16 @@
# Build Stage
FROM golang:1.21.5 AS builder
ENV GO111MODULE=on \
GOPROXY=https://goproxy.cn,direct
WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main main.go
# Run Stage
FROM zc1185230223/alpine:3.18
WORKDIR /app
COPY --from=builder /app/main .
EXPOSE 9090
CMD ["/app/main", "-debug=false"]

42
docker-compose.yaml Normal file
View File

@ -0,0 +1,42 @@
version: '3.4'
services:
url-short-service:
build:
context: .
dockerfile: Dockerfile
container_name: url-short
restart: always
environment:
SERVER_PORT: "9090"
REDIS_ADDR: "redis:6379"
REDIS_PASSWORD: "hPxyR4"
REDIS_DB: "2"
networks:
- url-short-network
ports:
- "9090:9090"
depends_on:
- redis
redis:
image: 'redis:7.2.3'
container_name: 'redis'
restart: always
networks:
- url-short-network
volumes:
- redis-db:/data:rw
ports:
- 6379:6379
command:
--requirepass "hPxyR4" #这一行是设置密码
privileged: true #使用该参数container内的root拥有真正的root权限
networks:
url-short-network:
driver: bridge
volumes:
redis-db: {}

10
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"context"
"flag"
"log"
"net/http"
"os"
@ -16,10 +17,15 @@ import (
)
func main() {
err := godotenv.Load()
if err != nil {
var local bool
flag.BoolVar(&local, "debug", true, "server running in debug?")
flag.Parse()
if local {
if err := godotenv.Load(); err != nil {
log.Fatalf("failed to load env: %v", err)
}
}
router := mux.NewRouter()
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {