Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make gogetable #2

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
*.iml
.env
/jsight-server

6 changes: 3 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ linters-settings:
gocyclo:
min-complexity: 15
goimports:
local-prefixes: github.com/golangci/golangci-lint
local-prefixes: github.com/jsightapi/jsight-server
gomnd:
# List of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description.
checks:
Expand Down Expand Up @@ -98,7 +98,7 @@ linters:
- gocritic
- gocyclo
- gofmt
# - goimports
- goimports
- gomnd
- goprintffuncname
- gosec
Expand Down Expand Up @@ -156,4 +156,4 @@ run:
- test/testdata_etc
- internal/cache
- internal/renameio
- internal/robustio
- internal/robustio
28 changes: 4 additions & 24 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
FROM golang:1.17-alpine as builder

RUN apk add --no-cache git
ARG GITHUB_TOKEN
# Install jschema library dependency
ARG JSCHEMA_BRANCH
WORKDIR /go/src/j/schema
RUN git clone -b ${JSCHEMA_BRANCH} --depth 1 \
https://${GITHUB_TOKEN}@github.com/jsightapi/jsight-schema-go-library.git . \
&& git branch --show-current \
&& git show -s

# Install japi library dependency
ARG JAPI_BRANCH
WORKDIR /go/src/j/japi
RUN git clone -b ${JAPI_BRANCH} --depth 1 \
https://${GITHUB_TOKEN}@github.com/jsightapi/jsight-api-go-library.git . \
&& git branch --show-current \
&& git show -s

# build
WORKDIR /go/src/j/server
WORKDIR /go/src/github.com/jsightapi/jsight-server
COPY . .
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o jsight-server .

RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o /go/bin/jsight-server .

FROM alpine
FROM scratch
ARG CORS
ARG STATISTICS
ENV JSIGHT_SERVER_CORS=$CORS
ENV JSIGHT_SERVER_STATISTICS=$STATISTICS
COPY --from=builder /go/src/j/server/jsight-server .
COPY --from=builder /go/bin/jsight-server .
EXPOSE 8080
CMD [ "/jsight-server" ]
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ fmt:
lint:
golangci-lint run

.PHONY: test
test:
go test -cover ./...

.PHONY: build
build:
go build -o jsight-server .
4 changes: 3 additions & 1 deletion cors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import "net/http"
import (
"net/http"
)

func cors(w http.ResponseWriter) {
w.Header().Set("Access-Control-Allow-Origin", "*")
Expand Down
29 changes: 29 additions & 0 deletions cors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_cors(t *testing.T) {
t.Run("positive", func(t *testing.T) {
r := httptest.NewRecorder()

cors(r)

assert.Equal(t, http.StatusOK, r.Code)
assert.Len(t, r.Header(), 3)
assert.Equal(t, "*", r.Header().Get("Access-Control-Allow-Origin"))
assert.Equal(t, "POST, GET, OPTIONS, PUT, DELETE", r.Header().Get("Access-Control-Allow-Methods"))
assert.Equal(t, "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, X-Browser-UUID", r.Header().Get("Access-Control-Allow-Headers")) //nolint:lll
})

t.Run("negative", func(t *testing.T) {
assert.Panics(t, func() {
cors(nil)
})
})
}
34 changes: 34 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_getBoolEnv(t *testing.T) {
const env = "JSIGHT_GET_BOOL_ENV_TEST"

cc := map[string]bool{
"": false,
"invalid": false,
"false": false,
"true": true,
"True": true,
"TRUE": true,
"1": true,
"t": true,
}

for given, expected := range cc {
t.Run(given, func(t *testing.T) {
require.NoError(t, os.Setenv(env, given))

assert.Equal(t, expected, getBoolEnv(env))
})
}

require.NoError(t, os.Unsetenv(env))
}
3 changes: 2 additions & 1 deletion error_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package main

import (
"errors"
"j/japi/jerr"

"github.com/jsightapi/jsight-api-go-library/jerr"
)

type errorInfo struct {
Expand Down
50 changes: 50 additions & 0 deletions error_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"errors"
"testing"

"github.com/jsightapi/jsight-api-go-library/jerr"
"github.com/jsightapi/jsight-schema-go-library/fs"
"github.com/stretchr/testify/assert"
)

func Test_newErrorInfo(t *testing.T) {
t.Run("positive", func(t *testing.T) {
cc := map[string]struct {
given error
expected errorInfo
}{
"ordinal error": {
errors.New("fake error"),
errorInfo{
Status: "Error",
Message: "fake error",
},
},

"JAPI error": {
jerr.NewJAPIError("fake error", fs.NewFile("foo", []byte("123")), 2),
errorInfo{
Status: "Error",
Message: "fake error",
Line: 1,
Index: 2,
},
},
}

for n, c := range cc {
t.Run(n, func(t *testing.T) {
actual := newErrorInfo(c.given)
assert.Equal(t, c.expected, actual)
})
}
})

t.Run("negative", func(t *testing.T) {
assert.Panics(t, func() {
newErrorInfo(nil)
})
})
}
16 changes: 9 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
module j/server
module github.com/jsightapi/jsight-server

go 1.17

replace j/japi => ./../japi

replace j/schema => ./../schema

require j/japi v0.0.0-00010101000000-000000000000
require (
github.com/jsightapi/jsight-api-go-library v0.3.1-0.20220427155423-7c489a57b5aa
github.com/jsightapi/jsight-schema-go-library v0.0.0-20220426180928-6a830af498a5
github.com/stretchr/testify v1.7.0
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/lucasjones/reggen v0.0.0-20200904144131-37ba4fa293bb // indirect
j/schema v0.0.0-00010101000000-000000000000 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jsightapi/jsight-api-go-library v0.3.1-0.20220427155423-7c489a57b5aa h1:2ax1u08xXQS0ku4j5M7ilC9FDmGrYD4He25dTHMQ9lo=
github.com/jsightapi/jsight-api-go-library v0.3.1-0.20220427155423-7c489a57b5aa/go.mod h1:X152Brrp/EcXkz2HNkGTHwVrwkZLuA6T02nTvfZJNQw=
github.com/jsightapi/jsight-schema-go-library v0.0.0-20220426180928-6a830af498a5 h1:wILPGWoEgYRmA+Dbnq++iL0qbR26XEQ3UufaKapgprc=
github.com/jsightapi/jsight-schema-go-library v0.0.0-20220426180928-6a830af498a5/go.mod h1:DhVaDHosRwijwgTxEAw8u3fw1zN6U1W/2bak371mSv8=
github.com/lucasjones/reggen v0.0.0-20200904144131-37ba4fa293bb h1:w1g9wNDIE/pHSTmAaUhv4TZQuPBS6GV3mMz5hkgziIU=
github.com/lucasjones/reggen v0.0.0-20200904144131-37ba4fa293bb/go.mod h1:5ELEyG+X8f+meRWHuqUOewBOhvHkl7M76pdGEansxW4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -8,6 +12,9 @@ github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3 changes: 2 additions & 1 deletion http_response_200.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"fmt"
"j/japi/catalog"
"log"
"net/http"

"github.com/jsightapi/jsight-api-go-library/catalog"
)

func httpResponse200(w http.ResponseWriter, b []byte) {
Expand Down
45 changes: 45 additions & 0 deletions http_response_200_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/jsightapi/jsight-api-go-library/catalog"
"github.com/stretchr/testify/assert"
)

func Test_httpResponse200(t *testing.T) {
t.Run("positive", func(t *testing.T) {
t.Run("with content", func(t *testing.T) {
const content = "foobar"
r := httptest.NewRecorder()

httpResponse200(r, []byte(content))

assert.Equal(t, http.StatusOK, r.Code)
assert.Len(t, r.Header(), 2)
assert.Equal(t, "application/json", r.Header().Get("Content-Type"))
assert.Equal(t, catalog.JDocExchangeFileSchemaVersion, r.Header().Get("X-Jdoc-Exchange-File-Schema-Version"))
assert.Equal(t, content, r.Body.String())
})

t.Run("nil content", func(t *testing.T) {
r := httptest.NewRecorder()

httpResponse200(r, nil)

assert.Equal(t, http.StatusOK, r.Code)
assert.Len(t, r.Header(), 2)
assert.Equal(t, "application/json", r.Header().Get("Content-Type"))
assert.Equal(t, catalog.JDocExchangeFileSchemaVersion, r.Header().Get("X-Jdoc-Exchange-File-Schema-Version"))
assert.Equal(t, "", r.Body.String())
})
})

t.Run("negative", func(t *testing.T) {
assert.Panics(t, func() {
httpResponse200(nil, nil)
})
})
}
37 changes: 37 additions & 0 deletions http_response_409_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main //nolint:dupl

import (
"errors"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_httpResponse409(t *testing.T) {
t.Run("positive", func(t *testing.T) {
r := httptest.NewRecorder()

httpResponse409(r, errors.New("fake error"))

assert.Equal(t, http.StatusConflict, r.Code)
assert.Len(t, r.Header(), 1)
assert.Equal(t, "application/json", r.Header().Get("Content-Type"))
assert.Equal(t, `{"Status":"Error","Message":"fake error","Line":0,"Index":0}`, r.Body.String())
})

t.Run("negative", func(t *testing.T) {
t.Run("nil writer", func(t *testing.T) {
assert.Panics(t, func() {
httpResponse409(nil, nil)
})
})

t.Run("nil error", func(t *testing.T) {
assert.Panics(t, func() {
httpResponse409(httptest.NewRecorder(), nil)
})
})
})
}
37 changes: 37 additions & 0 deletions http_response_500_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main //nolint:dupl

import (
"errors"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_httpResponse500(t *testing.T) {
t.Run("positive", func(t *testing.T) {
r := httptest.NewRecorder()

httpResponse500(r, errors.New("fake error"))

assert.Equal(t, http.StatusInternalServerError, r.Code)
assert.Len(t, r.Header(), 1)
assert.Equal(t, "text/plain", r.Header().Get("Content-Type"))
assert.Equal(t, "fake error", r.Body.String())
})

t.Run("negative", func(t *testing.T) {
t.Run("nil writer", func(t *testing.T) {
assert.Panics(t, func() {
httpResponse500(nil, nil)
})
})

t.Run("nil error", func(t *testing.T) {
assert.Panics(t, func() {
httpResponse500(httptest.NewRecorder(), nil)
})
})
})
}
Loading