Skip to content

Commit

Permalink
modified devops
Browse files Browse the repository at this point in the history
  • Loading branch information
brian030128 committed May 24, 2024
1 parent 8229233 commit 1411fd1
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 18 deletions.
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:1.22-alpine3.19 as build

WORKDIR /usr/src/app

# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
COPY go.mod go.sum ./
RUN go mod download && go mod verify

COPY . .

RUN go build -o /app /usr/src/app/cmd/main.go


FROM alpine:3.19
COPY --from=build /app /app
EXPOSE 8080
CMD ["/app"]
14 changes: 9 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
SUB_DIRS = protobuf
SUB_DIRS = protobuf migrations
PACKAGES ?= $(shell go list ./...)

all: $(SUB_DIRS)

$(SUB_DIRS):
make -C $@

migrations:
make -C migrations

test:
-mkdir build
go test $(PACKAGES) -v -cover -failfast
Expand All @@ -19,7 +16,14 @@ test_docker:
docker run --name monify-test-postgres -p 5432:5432 -e POSTGRES_PASSWORD=password -d postgres
go test $(PACKAGES) -v -cover -failfast -tags docker

docker_push: docker_build
docker push registry.nccupass.com/monify

docker_build:
docker build -t registry.nccupass.com/monify .

clean:
-rm -rf build
.PHONY: $(SUB_DIRS)


.PHONY: $(SUB_DIRS)
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func main() {
if err != nil {
panic(err)
}
println("Server is running on port 8080")
err = s.Start(lis)
if err != nil {
log.Fatal(err)
Expand Down
6 changes: 5 additions & 1 deletion internal/infra/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ type Config struct {
}

func NewConfigFromSecrets(secrets map[string]string) Config {
postgresUri := secrets["POSTGRES_URI"]
if postgresUri == "" {
panic("Missing POSTGRES_URI")
}
return Config{
PostgresURI: secrets["POSTGRES_URI"],
PostgresURI: postgresUri,
}
}
3 changes: 3 additions & 0 deletions internal/infra/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ func SetupConnection(uri string) *sql.DB {
if err != nil {
panic(err)
}
if err = db.Ping(); err != nil {
panic(err)
}
return db
}
1 change: 1 addition & 0 deletions internal/utils/infisical.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func GetSecrets(token string, environment string) (Secrets, error) {
}

func LoadSecrets(env string) (map[string]string, error) {
println("Loading secrets with env: ", env)
id, ok := os.LookupEnv("CLIENT_ID")
if !ok {
panic("cannot load client id for infisical")
Expand Down
2 changes: 1 addition & 1 deletion migrations/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# List of SQL source files
SRC = $(wildcard *.sql)
SRC = $(wildcard *.sql) run.go

# File to track the last migration run
TIMESTAMP = .last_migration
Expand Down
31 changes: 20 additions & 11 deletions migrations/run.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main

import (
"database/sql"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/cockroachdb"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/database/cockroachdb"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/joho/godotenv"
"log"
"monify/internal/utils"
)

Expand All @@ -16,19 +18,26 @@ func main() {
if err != nil {
panic(secrets)
}
m, err := migrate.New(
"file://.",
secrets["POSTGRES_URI"])
if err != nil {

panic(err)
db, err := sql.Open("pgx", secrets["POSTGRES_URI"])
if err != nil {
log.Fatal(err)
}
err = m.Up()
driver, err := cockroachdb.WithInstance(db, &cockroachdb.Config{})
if err != nil {
log.Fatal(err)
}
m, err := migrate.NewWithDatabaseInstance(
"file://.",
"postgres", driver)
if err != nil {
log.Fatal(err)
}

if err = m.Up(); err != nil {
if err != migrate.ErrNoChange {
panic(err)
} else {
println("No Change")
log.Fatal(err)
}
}

}

0 comments on commit 1411fd1

Please sign in to comment.