add Dockerfile to build the go app
This commit is contained in:
42
Dockerfile
Normal file
42
Dockerfile
Normal file
@@ -0,0 +1,42 @@
|
||||
# Stage 1: Build the Go application
|
||||
FROM golang:1.23 AS builder
|
||||
|
||||
ENV CGO_ENABLED=0
|
||||
ENV GOOS=linux
|
||||
ENV GOARCH=amd64
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy the Go modules and dependencies
|
||||
COPY ./golang/go.mod ./golang/go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
# Copy the source code
|
||||
COPY ./golang/cmd ./cmd
|
||||
COPY ./golang/assets ./assets
|
||||
COPY ./golang/internal ./internal
|
||||
|
||||
# Build the application
|
||||
RUN go build -o main cmd/frontend/*
|
||||
|
||||
# Stage 2: Run the application
|
||||
FROM alpine:latest
|
||||
|
||||
ENV DB_HOST="10.99.0.3"
|
||||
ENV DB_PORT="5432"
|
||||
ENV DB_NAME="crowsnest"
|
||||
ENV DB_USER="crow"
|
||||
ENV DB_PASS=""
|
||||
|
||||
# Set the working directory
|
||||
WORKDIR /app
|
||||
|
||||
# Copy the built binary from the builder stage
|
||||
COPY --from=builder /app/main .
|
||||
COPY ./golang/assets ./assets
|
||||
|
||||
# Expose the desired port (e.g., 8080)
|
||||
EXPOSE 8080
|
||||
|
||||
# Command to run the application
|
||||
CMD ["/app/main"]
|
||||
BIN
golang/Index
BIN
golang/Index
Binary file not shown.
@@ -1,6 +1,6 @@
|
||||
serv:
|
||||
DB_DRIVER="postgres" DB_URL="user=crow password=4LlKpnQ2RZPzL13BSpkW4k dbname=crowsnest host=192.168.0.2 port=5432 sslmode=disable" go run cmd/frontend/*
|
||||
DB_USER=crow DB_PASS=4LlKpnQ2RZPzL13BSpkW4k DB_NAME=crowsnest DB_HOST=10.99.0.3 go run cmd/frontend/*
|
||||
crawl:
|
||||
DB_DRIVER="postgres" DB_URL="user=crow password=4LlKpnQ2RZPzL13BSpkW4k dbname=crowsnest host=192.168.0.2 port=5432 sslmode=disable" go run cmd/crawler/main.go
|
||||
DB_USER=crow DB_PASS=4LlKpnQ2RZPzL13BSpkW4k DB_NAME=crowsnest DB_HOST=192.168.0.2 go run cmd/crawler/main.go
|
||||
summ:
|
||||
DB_DRIVER="postgres" DB_URL="user=crow password=4LlKpnQ2RZPzL13BSpkW4k dbname=crowsnest host=192.168.0.2 port=5432 sslmode=disable" go run cmd/summarizer/main.go
|
||||
DB_USER=crow DB_PASS=4LlKpnQ2RZPzL13BSpkW4k DB_NAME=crowsnest DB_HOST=192.168.0.2 go run cmd/summarizer/main.go
|
||||
|
||||
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"crowsnest/internal/model/database"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -16,14 +17,35 @@ type App struct {
|
||||
|
||||
func main() {
|
||||
// collect environement variables
|
||||
databaseURL := os.Getenv("DB_URL")
|
||||
dbPass := os.Getenv("DB_PASS")
|
||||
if dbPass == "" {
|
||||
log.Fatal("empty DB_PASS")
|
||||
}
|
||||
dbHost := os.Getenv("DB_HOST")
|
||||
if dbHost == "" {
|
||||
log.Fatal("empty DB_HOST")
|
||||
}
|
||||
dbPort := os.Getenv("DB_PORT")
|
||||
if dbPort == "" {
|
||||
dbPort = "5432"
|
||||
}
|
||||
dbUser := os.Getenv("DB_USER")
|
||||
if dbUser == "" {
|
||||
dbUser = "postgres"
|
||||
}
|
||||
dbName := os.Getenv("DB_NAME")
|
||||
|
||||
// connect to database
|
||||
databaseURL := fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%s sslmode=disable",
|
||||
dbUser, dbPass, dbName, dbHost, dbPort)
|
||||
db, err := sql.Open("postgres", databaseURL)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
if err = db.Ping(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// define app
|
||||
app := &App{
|
||||
|
||||
Reference in New Issue
Block a user