Learn how to deploy a Go web application to Valyent
curl -L https://cli.valyent.cloud | sh
valyent auth login
mkdir go-webapp && cd go-webapp go mod init go-webapp
touch main.go
main.go
package main import ( "fmt" "log" "net/http" "os" ) func main() { mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { http.NotFound(w, r) return } w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(http.StatusOK) fmt.Fprint(w, "Welcome to the Go Web App!") }) port := os.Getenv("PORT") if port == "" { port = "8080" } addr := ":" + port log.Printf("Server starting on port %s", port) if err := http.ListenAndServe(addr, mux); err != nil { log.Fatalf("Server failed to start: %v", err) } }
valyent.toml
valyent init
touch Dockerfile
# Stage 1: Builder FROM golang:1.21 AS builder WORKDIR /app # Copy module files COPY go.mod ./ # Copy the source code COPY . . # Build the application RUN CGO_ENABLED=0 GOOS=linux go build -o webapp . # Stage 2: Runtime FROM alpine:latest WORKDIR /app # Copy the binary from builder COPY --from=builder /app/webapp . # Expose the application port EXPOSE 8080 # Run the binary ENTRYPOINT ["/app/webapp"]
valyent gateways create
valyent deploy
valyent open