Skip to content
This repository was archived by the owner on May 11, 2022. It is now read-only.

Commit 1d3d439

Browse files
committed
fix bug with Dockerfile and service version
1 parent 19a4c19 commit 1d3d439

File tree

4 files changed

+13
-181
lines changed

4 files changed

+13
-181
lines changed

env/Dockerfile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ ENV GOOS linux
1010
ENV GOARCH amd64
1111
ENV CGO_ENABLED 0
1212

13-
RUN echo "os: ${GOOS}", "arch: ${GOARCH}", "cgo: ${CGO_ENABLED}" \
14-
&& go build -o /go/bin/service -ldflags '-s -w -X main.version=dev -X main.commit=dev -X main.date=dev' .
13+
RUN echo "os: ${GOOS}" "arch: ${GOARCH}" "cgo: ${CGO_ENABLED}" \
14+
&& export _commit="-X main.commit=$(git rev-parse --short HEAD)" \
15+
&& export _date="-X main.date=$(date -u +%FT%X%Z)" \
16+
&& export _version="-X main.version=$(git describe --tags | cut -d - -f 1)" \
17+
&& go build -o /go/bin/service \
18+
-ldflags "-s -w ${_commit} ${_date} ${_version}" .
1519

1620
# ~~~
1721

env/cmd.mk

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
LDFLAGS ?= -ldflags '-s -w -X main.version=dev -X main.commit=$(shell git rev-parse --short HEAD)'
2-
BUILD_FILES ?= main.go
1+
_commit = -X main.commit=$(shell git rev-parse --short HEAD)
2+
_date = -X main.date=$(shell date -u +%FT%X%Z)
3+
_version = -X main.version=dev
4+
5+
LDFLAGS = -ldflags '-s -w $(_commit) $(_date) $(_version)'
6+
BUILD_FILES = main.go
37

48

59
.PHONY: cmd-help

env/test/integration_test.go

Lines changed: 0 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -1,177 +1,3 @@
11
// +build integration
22

3-
//go:generate echo $PWD/$GOPACKAGE/$GOFILE
4-
//go:generate mockgen -package main -destination $PWD/mock_storage_test.go github.com/kamilsk/form-api/service Storage
53
package test
6-
7-
import (
8-
"io/ioutil"
9-
"net/http"
10-
"net/http/httptest"
11-
"net/url"
12-
"path"
13-
"testing"
14-
15-
"github.com/golang/mock/gomock"
16-
"github.com/kamilsk/form-api/pkg/config"
17-
"github.com/kamilsk/form-api/pkg/domain"
18-
"github.com/kamilsk/form-api/pkg/errors"
19-
"github.com/kamilsk/form-api/pkg/server/router/chi"
20-
"github.com/kamilsk/form-api/pkg/service"
21-
"github.com/kamilsk/form-api/pkg/transfer/encoding"
22-
"github.com/stretchr/testify/assert"
23-
)
24-
25-
const (
26-
HOST = "http://form-api.dev/"
27-
APIv1 = "api/v1"
28-
FAKE = domain.ID("41ca5e09-3ce2-0094-b108-3ecc257c6fa4")
29-
ZERO = domain.ID("00000000-0000-4000-8000-000000000000")
30-
UUID = domain.ID("41ca5e09-3ce2-4094-b108-3ecc257c6fa4")
31-
)
32-
33-
func TestAPI_GetV1(t *testing.T) {
34-
ctrl := gomock.NewController(t)
35-
defer ctrl.Finish()
36-
37-
var (
38-
storage = NewMockStorage(ctrl)
39-
)
40-
41-
handler := chi.NewRouter(config.ServerConfig{BaseURL: HOST, TemplateDir: "static/templates"}, service.New(storage))
42-
srv := httptest.NewServer(handler)
43-
defer srv.Close()
44-
45-
{
46-
var (
47-
schema = domain.Schema{
48-
Title: "Email subscription",
49-
Action: "https://kamil.samigullin.info/",
50-
Method: "post",
51-
EncodingType: "application/x-www-form-urlencoded",
52-
Inputs: []domain.Input{
53-
{
54-
Name: "email",
55-
Type: domain.EmailType,
56-
Title: "Email",
57-
MaxLength: 64,
58-
Required: true,
59-
},
60-
},
61-
}
62-
)
63-
64-
tests := []struct {
65-
name string
66-
request *http.Request
67-
golden string
68-
}{
69-
{"get schema as HTML", func() *http.Request {
70-
req, err := http.NewRequest(http.MethodGet, join(srv.URL, APIv1, UUID.String()), nil)
71-
if err != nil {
72-
panic(err)
73-
}
74-
req.Header.Set("Accept", encoding.HTML)
75-
return req
76-
}(), "./transfer/encoding/fixtures/email_subscription.html.golden"},
77-
{"get schema as JSON", func() *http.Request {
78-
req, err := http.NewRequest(http.MethodGet, join(srv.URL, APIv1, UUID.String()), nil)
79-
if err != nil {
80-
panic(err)
81-
}
82-
req.Header.Set("Accept", encoding.JSON)
83-
return req
84-
}(), "./transfer/encoding/fixtures/email_subscription.json.golden"},
85-
{"get schema as XML", func() *http.Request {
86-
req, err := http.NewRequest(http.MethodGet, join(srv.URL, APIv1, UUID.String()), nil)
87-
if err != nil {
88-
panic(err)
89-
}
90-
req.Header.Set("Accept", encoding.XML)
91-
return req
92-
}(), "./transfer/encoding/fixtures/email_subscription.xml.golden"},
93-
{"get schema as text", func() *http.Request {
94-
req, err := http.NewRequest(http.MethodGet, join(srv.URL, APIv1, UUID.String()), nil)
95-
if err != nil {
96-
panic(err)
97-
}
98-
req.Header.Set("Accept", encoding.TEXT)
99-
return req
100-
}(), "./transfer/encoding/fixtures/email_subscription.yaml.golden"},
101-
}
102-
storage.EXPECT().Schema(UUID).Times(len(tests)).Return(schema, nil)
103-
104-
for _, test := range tests {
105-
tc := test
106-
t.Run(test.name, func(t *testing.T) {
107-
resp, err := http.DefaultClient.Do(tc.request)
108-
assert.NoError(t, err)
109-
assert.Equal(t, http.StatusOK, resp.StatusCode)
110-
111-
expected, err := ioutil.ReadFile(tc.golden)
112-
assert.NoError(t, err)
113-
obtained, err := ioutil.ReadAll(resp.Body)
114-
assert.NoError(t, err)
115-
assert.NoError(t, resp.Body.Close())
116-
117-
// this is because server contains domain logic
118-
assert.NotEqual(t, string(expected), string(obtained))
119-
})
120-
}
121-
}
122-
123-
{
124-
tests := []struct {
125-
name string
126-
request *http.Request
127-
code int
128-
}{
129-
{http.StatusText(http.StatusBadRequest), func() *http.Request {
130-
req, err := http.NewRequest(http.MethodGet, join(srv.URL, APIv1, FAKE.String()), nil)
131-
if err != nil {
132-
panic(err)
133-
}
134-
return req
135-
}(), http.StatusBadRequest},
136-
{http.StatusText(http.StatusNotAcceptable), func() *http.Request {
137-
req, err := http.NewRequest(http.MethodGet, join(srv.URL, APIv1, UUID.String()), nil)
138-
if err != nil {
139-
panic(err)
140-
}
141-
req.Header.Set("Accept", "application/toml")
142-
return req
143-
}(), http.StatusNotAcceptable},
144-
{http.StatusText(http.StatusNotFound), func() *http.Request {
145-
req, err := http.NewRequest(http.MethodGet, join(srv.URL, APIv1, ZERO.String()), nil)
146-
if err != nil {
147-
panic(err)
148-
}
149-
return req
150-
}(), http.StatusNotFound},
151-
}
152-
storage.EXPECT().Schema(ZERO).Times(1).Return(domain.Schema{}, errors.NotFound("", nil, ""))
153-
154-
for _, test := range tests {
155-
tc := test
156-
t.Run(test.name, func(t *testing.T) {
157-
resp, err := http.DefaultClient.Do(tc.request)
158-
assert.NoError(t, err)
159-
assert.Equal(t, tc.code, resp.StatusCode)
160-
})
161-
}
162-
}
163-
}
164-
165-
func TestAPI_PostV1(t *testing.T) {
166-
ctrl := gomock.NewController(t)
167-
defer ctrl.Finish()
168-
}
169-
170-
func join(base string, paths ...string) string {
171-
u, err := url.Parse(base)
172-
if err != nil {
173-
panic(err)
174-
}
175-
u.Path = path.Join(append([]string{u.Path}, paths...)...)
176-
return u.String()
177-
}

main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import (
55
"io"
66
"os"
77
"runtime"
8-
"time"
98

109
"github.com/kamilsk/form-api/cmd"
1110
"github.com/kamilsk/form-api/pkg/errors"
1211
"github.com/spf13/cobra"
1312

14-
_ "github.com/grpc-ecosystem/go-grpc-middleware"
1513
_ "github.com/mailru/easyjson"
1614
_ "golang.org/x/sync/errgroup"
1715
)
@@ -23,7 +21,7 @@ const (
2321

2422
var (
2523
commit = "none"
26-
date = time.Now().Format(time.UnixDate)
24+
date = "unknown"
2725
version = "dev"
2826
)
2927

0 commit comments

Comments
 (0)