diff --git a/.env b/.env index 15f6c136..7866b18b 100644 --- a/.env +++ b/.env @@ -2,8 +2,8 @@ db_name=pgtest db_pass=postgres db_user=postgres db_type=postgres -db_host=database -# db_host=127.0.0.1 +# db_host=database +db_host=localhost db_port=5432 token_password=thisIsTheJwtPassword database_url=postgres://postgres:postgres@database:5432/tests \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 88d1de1c..12ec101f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,40 +1,30 @@ -# Use the official Golang image to create a build artifact. -# This is based on Debian and sets the GOPATH to /go. -# https://hub.docker.com/_/golang -FROM golang:1.14 as builder - -# Create and change to the app directory. -WORKDIR /app - -# Retrieve application dependencies using go modules. -# Allows container builds to reuse downloaded dependencies. -COPY go.* ./ -COPY go.mod ./ -COPY go.sum ./ -COPY .env ./ -COPY database.env ./ +FROM golang:1.14-alpine + +RUN apk add --no-cache git + +# ENV GO111MODULE=on + +# Set the Current Working Directory inside the container +WORKDIR /app/GoGinApi + +# We want to populate the module cache based on the go.{mod,sum} files. +COPY go.mod . +COPY go.sum . +COPY .env . +COPY database.env . RUN go mod download -# Copy local code to the container image. -COPY . ./ +COPY . . RUN CGO_ENABLED=0 go test -v -# Build the binary. -# -mod=readonly ensures immutable go.mod and go.sum in container builds. -RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server - -# Use the official Alpine image for a lean production container. -# https://hub.docker.com/_/alpine -# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds -FROM alpine:3 -RUN apk add --no-cache ca-certificates +# Build the Go app +RUN go build -o ./out/GoGinApi . -# Copy the binary to the production image from the builder stage. -COPY --from=builder /app/server /server +# This container exposes port 8080 to the outside world EXPOSE 8082 -# Run the web service on container startup. -CMD ["/server"] \ No newline at end of file +# Run the binary program produced by `go install` +CMD ["./out/GoGinApi"] \ No newline at end of file diff --git a/DockerfileOld b/DockerfileOld index 12ec101f..b742a801 100644 --- a/DockerfileOld +++ b/DockerfileOld @@ -1,30 +1,41 @@ -FROM golang:1.14-alpine -RUN apk add --no-cache git +# Use the official Golang image to create a build artifact. +# This is based on Debian and sets the GOPATH to /go. +# https://hub.docker.com/_/golang +FROM golang:1.14 as builder -# ENV GO111MODULE=on +# Create and change to the app directory. +WORKDIR /app -# Set the Current Working Directory inside the container -WORKDIR /app/GoGinApi - -# We want to populate the module cache based on the go.{mod,sum} files. -COPY go.mod . -COPY go.sum . -COPY .env . -COPY database.env . +# Retrieve application dependencies using go modules. +# Allows container builds to reuse downloaded dependencies. +COPY go.* ./ +COPY go.mod ./ +COPY go.sum ./ +COPY .env ./ +COPY database.env ./ RUN go mod download -COPY . . +# Copy local code to the container image. +COPY . ./ RUN CGO_ENABLED=0 go test -v -# Build the Go app -RUN go build -o ./out/GoGinApi . +# Build the binary. +# -mod=readonly ensures immutable go.mod and go.sum in container builds. +RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server + +# Use the official Alpine image for a lean production container. +# https://hub.docker.com/_/alpine +# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds +FROM alpine:3 +RUN apk add --no-cache ca-certificates +# Copy the binary to the production image from the builder stage. +COPY --from=builder /app/server /server -# This container exposes port 8080 to the outside world EXPOSE 8082 -# Run the binary program produced by `go install` -CMD ["./out/GoGinApi"] \ No newline at end of file +# Run the web service on container startup. +CMD ["/server"] \ No newline at end of file diff --git a/config/db/init.sql b/config/db/init.sql new file mode 100644 index 00000000..a811e470 --- /dev/null +++ b/config/db/init.sql @@ -0,0 +1,29 @@ +drop table people; +drop table videos; + +create table people +( + id bigserial not null + constraint people_pkey + primary key, + first_name varchar(32), + last_name varchar(32), + age integer, + email varchar(256) +); + +create table videos +( + id bigserial not null + constraint videos_pkey + primary key, + title varchar(100), + description varchar(200), + url varchar(256) + constraint videos_url_key + unique, + person_id bigint, + created_at timestamp with time zone default CURRENT_TIMESTAMP, + updated_at timestamp with time zone default CURRENT_TIMESTAMP +); + diff --git a/go.sum b/go.sum index ab5d82b9..023e91be 100644 --- a/go.sum +++ b/go.sum @@ -26,6 +26,8 @@ github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/mock v1.4.3 h1:GV+pQPG/EUUbkh47niozDcADz6go/dUwhVzdUQHIVRw= +github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= @@ -103,12 +105,15 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtD golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 h1:OjiUf46hAmXblsZdnoSXsEUSKU8r1UEzcL5RVZ4gO9Y= golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190425150028-36563e24a262 h1:qsl9y/CJx34tuA7QCPNp86JNJe4spst6Ff8MjvPUdPg= +golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= @@ -138,3 +143,5 @@ gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= +rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=