瀏覽代碼

add dockerfile, more elegant exit

Aneurin Barker Snook 1 年之前
父節點
當前提交
d0dfed5b24
共有 3 個文件被更改,包括 25 次插入4 次删除
  1. 21 0
      Dockerfile
  2. 1 1
      go.mod
  3. 3 3
      main.go

+ 21 - 0
Dockerfile

@@ -0,0 +1,21 @@
+# Build image
+FROM golang:1.21.3 AS build
+
+WORKDIR /build
+
+# Configure known hosts
+RUN mkdir -p /root/.ssh && ssh-keyscan github.com >> /root/.ssh/known_hosts
+
+# Copy source
+COPY go.mod go.sum main.go ./
+
+# Install dependencies and build
+RUN go install
+RUN go build -o short-url-service
+
+# Distribution image
+FROM debian:12
+
+# Copy binary and set as command
+COPY --from=build /build/short-url-service /usr/local/bin/short-url-service
+CMD ["short-url-service"]

+ 1 - 1
go.mod

@@ -1,6 +1,6 @@
 module github.com/recipeer/short-url-service
 
-go 1.21.4
+go 1.21.3
 
 require (
 	github.com/fsnotify/fsnotify v1.6.0 // indirect

+ 3 - 3
main.go

@@ -80,7 +80,8 @@ func main() {
 
 	redirects, err := ReadRedirects(config)
 	if err != nil {
-		panic(err)
+		fmt.Println(err)
+		os.Exit(1)
 	}
 
 	errc := ListenHTTP(config, redirects)
@@ -91,9 +92,8 @@ func main() {
 	select {
 	case err := <-errc:
 		fmt.Println(err)
-		return
+		os.Exit(1)
 	case <-sigc:
 		fmt.Println("Stopped")
-		return
 	}
 }