This commit is contained in:
Zeev Diukman 2024-08-22 06:15:17 +03:00
commit 9a868bb5f8
3 changed files with 84 additions and 0 deletions

26
Dockerfile Normal file
View file

@ -0,0 +1,26 @@
FROM golang:alpine as builder
WORKDIR /go/src/app
# Get Reflex for live reload in dev
ENV GO111MODULE=on
RUN go get github.com/cespare/reflex
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN go build -o ./run .
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
#Copy executable from builder
COPY --from=builder /go/src/app/run .
EXPOSE 8080
CMD ["./run"]

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module github.com/zeevdiukman/zprox
go 1.23.0

55
main.go Normal file
View file

@ -0,0 +1,55 @@
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func init() {
fmt.Println("ZPROX started")
}
func handler1(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("HELLO ZPROX"))
}
func main() {
router := http.NewServeMux()
router.Handle("/", http.HandlerFunc(handler1))
srv := &http.Server{
Addr: ":80",
Handler: router,
}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Failed to initialize server: %v\n", err)
}
}()
log.Printf("Listening on port %v\n", srv.Addr)
// Wait for kill signal of channel
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
// This blocks until a signal is passed into the quit channel
<-quit
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Shutdown server
log.Println("Shutting down server...")
if err := srv.Shutdown(ctx); err != nil {
log.Fatalf("Server forced to shutdown: %v\n", err)
}
}