Skip to content

Deploying a Beego App

Beego is a popular open-source web framework for Go, designed for building scalable, high-performance web applications and APIs. It features a powerful MVC architecture, built-in ORM, and a rich ecosystem, making it ideal for rapid development of robust Go services.

This guide explains how to deploy a Beego application to Klutch.sh, both with and without a Dockerfile. It also covers installation and provides sample code to get started.

Prerequisites

  • Go 1.20+
  • Git and GitHub account
  • Klutch.sh account

Getting Started: Install Beego

  1. Create a new directory for your app and initialize a Go module:
    Terminal window
    mkdir my-beego-app
    cd my-beego-app
    go mod init my-beego-app
  2. Install Beego:
    Terminal window
    go get github.com/beego/beego/v2@latest
    go get github.com/beego/bee/v2@latest
  3. Create a basic Beego app (main.go):
    package main
    import (
    "github.com/beego/beego/v2/server/web"
    "os"
    )
    func main() {
    web.Get("/", func(ctx *web.Context) {
    ctx.WriteString("Hello from Beego on Klutch.sh!")
    })
    port := os.Getenv("PORT")
    if port == "" {
    port = "8080"
    }
    web.Run("0.0.0.0:" + port)
    }
  4. Test locally:
    Terminal window
    go run main.go
    Visit http://localhost:8080 to see your app running.

Deploying Without a Dockerfile

  1. Push your Beego app to a GitHub repository.
  2. Log in to Klutch.sh.
  3. Create a new project and give it a name.
  4. Create a new app:
    • Select your Beego GitHub repository and branch
    • Set the port to route traffic (usually 8080 for Beego)
    • Choose region, compute, number of instances, and add any environment variables
  5. Add a start command in your app settings:
    Terminal window
    go run main.go
  6. Click “Create” to deploy. Klutch.sh will build and deploy your app automatically.

Deploying With a Dockerfile

  1. Add a Dockerfile to your project root. Example:
    # Use official Golang image for build
    FROM golang:1.20-alpine AS builder
    WORKDIR /app
    COPY . .
    RUN go build -o beego-app main.go
    # Use minimal image for running
    FROM alpine:latest
    WORKDIR /app
    COPY --from=builder /app/beego-app .
    # Expose port (match your Beego app)
    EXPOSE 8080
    # Start the app
    CMD ["./beego-app"]
  2. Push your code (with Dockerfile) to GitHub.
  3. In Klutch.sh, follow the same steps to create a project and app, but select the Dockerfile option when prompted.
  4. Set the service details and environment variables as needed.
  5. Click “Create” to deploy. Klutch.sh will build your Docker image and deploy your app.

Note: Your Beego app should always listen on the PORT environment variable as shown above.


Resources


Deploying to Klutch.sh is simple and flexible. Choose the method that best fits your workflow and project requirements.