Skip to content

Commit

Permalink
Merge pull request #68 from ZeStream/feat/dockerfile
Browse files Browse the repository at this point in the history
Added Dockerfile and compose to generate build and run services
  • Loading branch information
abskrj authored Sep 17, 2023
2 parents 4f95245 + 02f75b1 commit fa527cc
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 15 deletions.
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM golang:latest AS builder

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .

RUN go build -o main

FROM alpine:latest AS run-http
WORKDIR /app
COPY --from=builder /app/main ./
CMD ["./main"]

FROM alpine:latest AS run-consumer
WORKDIR /app
COPY --from=builder /app/main ./
CMD ["./main", "--consumer"]
3 changes: 3 additions & 0 deletions configs/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ func InitCloud() {
if EnvVar[AWS_ACCESS_KEY_ID] != "" {
cloudSession.AWSSession = getAWSSession()
log.Println("Initialised AWS")
return
}

if EnvVar[AZURE_ACCESS_KEY] != "" {
cloudSession.AzureSession = getAzureSession()
log.Println("Initialised Azure")
return
}

if EnvVar[GCP_PROJECT_ID] != "" {
cloudSession.GCPSession = getGCPSession()
log.Println("Initialised GCP")
return
}
}

Expand Down
2 changes: 1 addition & 1 deletion configs/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func LoadEnv() {
err := dotEnv.Load()

if err != nil {
log.Fatalln(err)
log.Println(err)
}

for key, val := range configVars {
Expand Down
7 changes: 6 additions & 1 deletion configs/rabbitmq.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package configs

import (
"context"
"crypto/tls"
"log"
"zestream-server/constants"

Expand All @@ -15,8 +16,12 @@ var rabbitCtx context.Context
var rabbitCtxCancel context.CancelFunc

func InitRabbitMQ() (*rmq.Connection, *rmq.Channel, *rmq.Queue, *context.Context, context.CancelFunc) {
config := &tls.Config{
InsecureSkipVerify: true,
}

var err error
rabbitConn, err = rmq.Dial(EnvVar[RABBITMQ_URI])
rabbitConn, err = rmq.DialTLS(EnvVar[RABBITMQ_URI], config)
failOnError(err)

rabbitCh, err = rabbitConn.Channel()
Expand Down
25 changes: 25 additions & 0 deletions consumer.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM golang:latest AS builder

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .

ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux

RUN go build -o main

FROM ubuntu:latest AS zestream-consumer

WORKDIR /app

COPY --from=builder /app/main ./

RUN apt update
RUN apt install -y ffmpeg

CMD ["./main", "--consumer"]
40 changes: 40 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
version: '3'
services:
zestream-http:
build:
context: ./
dockerfile: http.Dockerfile
environment:
- PORT=${PORT}
- RABBITMQ_URI=${RABBITMQ_URI}
- GIN_MODE=${GIN_MODE}
- GOOGLE_APPLICATION_CREDENTIALS=${GOOGLE_APPLICATION_CREDENTIALS}
- AZURE_ACCOUNT_NAME=${AZURE_ACCOUNT_NAME}
- AZURE_ENDPOINT=${AZURE_ENDPOINT}
- AZURE_ACCESS_KEY=${AZURE_ACCESS_KEY}
- AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
- AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
- AWS_SESSION_TOKEN=${AWS_SESSION_TOKEN}
ports:
- "8080:${PORT}"
container_name: go-app-http-container

zestream-consumer:
build:
context: ./
dockerfile: consumer.Dockerfile
container_name: go-app-consumer-container
environment:
- RABBITMQ_URI=${RABBITMQ_URI}
- GOOGLE_APPLICATION_CREDENTIALS=${GOOGLE_APPLICATION_CREDENTIALS}
- AZURE_ACCOUNT_NAME=${AZURE_ACCOUNT_NAME}
- AZURE_ENDPOINT=${AZURE_ENDPOINT}
- AZURE_ACCESS_KEY=${AZURE_ACCESS_KEY}
- AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
- AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
- AWS_SESSION_TOKEN=${AWS_SESSION_TOKEN}
volumes:
- consumer-data:/app

volumes:
consumer-data:
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ require (
github.com/u2takey/go-utils v0.3.1 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.103.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand All @@ -208,6 +210,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 h1:nt+Q6cXKz4MosCSpnbMtqiQ8Oz0pxTef2B4Vca2lvfk=
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783/go.mod h1:h4gKUeWbJ4rQPri7E0u6Gs4e9Ri2zaLxzw5DI5XGrYg=
Expand All @@ -234,6 +238,8 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand All @@ -245,6 +251,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
23 changes: 23 additions & 0 deletions http.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM golang:latest AS builder

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .

ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux

RUN go build -o main


FROM ubuntu:latest AS zestream-http

WORKDIR /app

COPY --from=builder /app/main ./

CMD ["./main"]
19 changes: 11 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"log"
"os"
"zestream-server/configs"
Expand All @@ -10,18 +11,20 @@ import (
)

func main() {
configs.LoadEnv()

r := routes.Init()
isConsumer := flag.Bool("consumer", false, "run service as consumer")
flag.Parse()

port := os.Getenv(constants.PORT)

// initialize RabbitMQ
configs.LoadEnv()
configs.InitCloud()
conn, ch, q, _, cancel := configs.InitRabbitMQ()

configs.InitCloud()
if *isConsumer {
service.VideoProcessConsumer(ch, q)
return
}

go service.VideoProcessConsumer(ch, q)
r := routes.Init()
port := os.Getenv(constants.PORT)

err := r.Run(":" + port)
failOnError(err)
Expand Down
3 changes: 2 additions & 1 deletion routes/routes.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package routes

import (
"log"
"zestream-server/controllers"

"github.com/gin-gonic/gin"
)

// Init function will perform all route operations
func Init() *gin.Engine {

log.Println("Running ZeStream as HTTP Server")
r := gin.Default()

r.Use(func(c *gin.Context) {
Expand Down
1 change: 1 addition & 0 deletions service/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

func VideoProcessConsumer(ch *rmq.Channel, q *rmq.Queue) {
log.Println("Running ZeStream as Consumer")
var forever chan struct{}

maxProcesses, err := strconv.Atoi(configs.EnvVar[configs.MAX_CONCURRENT_PROCESSING])
Expand Down

0 comments on commit fa527cc

Please sign in to comment.