Skip to content

Commit

Permalink
Merge branch 'develop' into orpheus/fix-code-typos
Browse files Browse the repository at this point in the history
  • Loading branch information
orpheuslummis committed Feb 2, 2022
2 parents 823bb47 + 9d822c3 commit 75121bd
Show file tree
Hide file tree
Showing 116 changed files with 4,879 additions and 571 deletions.
9 changes: 6 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
# Specify the execution environment. You can specify an image from Dockerhub or use one of our Convenience Images from CircleCI's Developer Hub.
# See: https://circleci.com/docs/2.0/configuration-reference/#docker-machine-macos-windows-executor
docker:
- image: circleci/golang:1.15.8
- image: circleci/golang:1.17.5
# Add steps to the job
# See: https://circleci.com/docs/2.0/configuration-reference/#steps
steps:
Expand All @@ -20,16 +20,19 @@ jobs:
- go-mod-v4-{{ checksum "go.sum" }}
- run:
name: Install Dependencies
command: make deps:circle-ci
command: make deps
- save_cache:
key: go-mod-v4-{{ checksum "go.sum" }}
paths:
- "/go/pkg/mod"
- run:
name: Run tests
command: |
DEFRA_BADGER_MEMORY=true
DEFRA_BADGER_FILE=true
DEFRA_MAP=true
mkdir -p /tmp/test-reports
gotestsum --junitfile /tmp/test-reports/unit-tests.xml
gotestsum --junitfile /tmp/test-reports/unit-tests.xml -- ./... -race
- store_test_results:
path: /tmp/test-reports

Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/code-test-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: '1.17'
go-version: '1.17.5'

- name: Generate coverage report
run: go test ./... -race -coverprofile=coverage.out -covermode=atomic
- name: Generate full test coverage report using go-acc
run: make test:coverage-full

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
files: ./coverage.out
flags: unittests
files: ./coverage-full.txt
flags: defra-tests
name: codecov-umbrella
verbose: true
# path_to_write_report: ./coverage/codecov_report.txt
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ cli/defradb/defradb
cli/defradb/defradb.exe
build/defradb*
cover.out
coverage-full.txt
coverage-quick.txt
bench/*.log
bench/*.svg
8 changes: 4 additions & 4 deletions .golangci.sourceinc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ linters-settings:
# list of functions to exclude from checking, where each entry is a single function to exclude.
# see https://github.com/kisielk/errcheck#excluding-functions for details
exclude-functions:
- io/ioutil.ReadFile
- io.Copy(*bytes.Buffer)
- io.Copy(os.Stdout)
# - io/ioutil.ReadFile
# - io.Copy(*bytes.Buffer)
# - io.Copy(os.Stdout)

errorlint:
# Check whether fmt.Errorf uses the %w verb for formatting errors. See the readme for caveats
Expand Down Expand Up @@ -789,7 +789,7 @@ linters:
- typecheck
- unused
- varcheck
# - errcheck
- errcheck
# - wrapcheck
# - errorlint
# - gci
Expand Down
48 changes: 35 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,57 @@ multi-build:
start: build
./build/defradb start

.PHONY: deps\:circle-ci
deps\:circle-ci:
go mod download
.PHONY: dump
dump: build
./build/defradb client dump

.PHONY: deps\:golangci-lint
deps\:golangci-lint:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ${GOPATH}/bin v1.43.0

.PHONY: deps\:go-acc
deps\:go-acc:
go install github.com/ory/go-acc@latest

.PHONY: deps
deps: deps\:circle-ci
deps: deps\:golangci-lint deps\:go-acc
go mod download

.PHONY: tidy
tidy:
go mod tidy

.PHONY: clean
clean:
go clean cli/defradb/main.go
rm -f build/defradb

.PHONY: tidy
tidy:
go mod tidy
.PHONY: clean\:test
clean\:test:
go clean -testcache

.PHONY: test
test:
go test ./...
go test ./... -race

.PHONY: test\:clean
test\:clean: clean\:test test

.PHONY: test\:bench
test\:bench:
go test -bench
go test ./... -race -bench=.

# This also takes integration tests into account.
.PHONY: test\:coverage-full
test\:coverage-full: deps\:go-acc
go-acc ./... --output=coverage-full.txt --covermode=atomic
go tool cover -func coverage-full.txt | grep total | awk '{print $$3}'

.PHONY: test\:coverage
test\:coverage:
go test ./... -coverprofile cover.out
go tool cover -func cover.out | grep total | awk '{print $$3}'
# This only covers how much of the package is tested by itself (unit test).
.PHONY: test\:coverage-quick
test\:coverage-quick:
go test ./... -race -coverprofile=coverage-quick.txt -covermode=atomic
go tool cover -func coverage-quick.txt | grep total | awk '{print $$3}'

.PHONY: validate\:codecov
validate\:codecov:
Expand Down
127 changes: 107 additions & 20 deletions api/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"context"
"encoding/json"
"io/ioutil"
"log"
"net/http"

"github.com/multiformats/go-multihash"
Expand All @@ -39,7 +40,10 @@ func NewServer(db client.DB) *Server {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Welcome to the DefraDB HTTP API. Use /graphql to send queries to the database"))
_, err := w.Write([]byte("Welcome to the DefraDB HTTP API. Use /graphql to send queries to the database"))
if err != nil {
log.Printf("DefraDB HTTP API Welcome message writing failed: %v", err)
}
})

r.Get("/ping", s.ping)
Expand All @@ -52,51 +56,88 @@ func NewServer(db client.DB) *Server {
}

func (s *Server) Listen(addr string) {
http.ListenAndServe(addr, s.router)
if err := http.ListenAndServe(addr, s.router); err != nil {
log.Fatalln("Error: HTTP Listening and Serving Failed: ", err)
}
}

func (s *Server) ping(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong"))
_, err := w.Write([]byte("pong"))
if err != nil {
log.Printf("Writing pong with HTTP failed: %v", err)
}
}

func (s *Server) dump(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
s.db.PrintDump(ctx)
w.Write([]byte("ok"))

_, err := w.Write([]byte("ok"))
if err != nil {
log.Printf("Writing ok with HTTP failed: %v", err)
}
}

func (s *Server) execGQL(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
query := r.URL.Query().Get("query")
result := s.db.ExecQuery(ctx, query)
json.NewEncoder(w).Encode(result)

err := json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
}

func (s *Server) loadSchema(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
var result client.QueryResult
sdl, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()

defer func() {
err = r.Body.Close()
if err != nil {
log.Print(err) // Should this be `log.Fatal(err)` ??
}
}()

if err != nil {
result.Errors = []interface{}{err.Error()}
json.NewEncoder(w).Encode(result)

err = json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}

w.WriteHeader(http.StatusBadRequest)
return
}

err = s.db.AddSchema(ctx, string(sdl))
if err != nil {
result.Errors = []interface{}{err.Error()}
json.NewEncoder(w).Encode(result)

err = json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}

w.WriteHeader(http.StatusBadRequest)
return
}

result.Data = map[string]string{
"result": "success",
}
json.NewEncoder(w).Encode(result)

err = json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
}

func (s *Server) getBlock(w http.ResponseWriter, r *http.Request) {
Expand All @@ -115,7 +156,13 @@ func (s *Server) getBlock(w http.ResponseWriter, r *http.Request) {
if err != nil {
result.Errors = []interface{}{err.Error()}
result.Data = err.Error()
json.NewEncoder(w).Encode(result)

err = json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}

w.WriteHeader(http.StatusBadRequest)
return
}
Expand All @@ -125,7 +172,13 @@ func (s *Server) getBlock(w http.ResponseWriter, r *http.Request) {
block, err := s.db.GetBlock(ctx, c)
if err != nil {
result.Errors = []interface{}{err.Error()}
json.NewEncoder(w).Encode(result)

err = json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}

w.WriteHeader(http.StatusBadRequest)
return
}
Expand All @@ -134,14 +187,26 @@ func (s *Server) getBlock(w http.ResponseWriter, r *http.Request) {
if err != nil {
result.Errors = []interface{}{err.Error()}
result.Data = err.Error()
json.NewEncoder(w).Encode(result)

err = json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}

w.WriteHeader(http.StatusBadRequest)
return
}
buf, err := nd.MarshalJSON()
if err != nil {
result.Errors = []interface{}{err.Error()}
json.NewEncoder(w).Encode(result)

err = json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}

w.WriteHeader(http.StatusBadRequest)
return
}
Expand All @@ -151,26 +216,42 @@ func (s *Server) getBlock(w http.ResponseWriter, r *http.Request) {
delta, err := reg.DeltaDecode(nd)
if err != nil {
result.Errors = []interface{}{err.Error()}
json.NewEncoder(w).Encode(result)

err = json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}

w.WriteHeader(http.StatusBadRequest)
return
}

data, err := delta.Marshal()
if err != nil {
result.Errors = []interface{}{err.Error()}
json.NewEncoder(w).Encode(result)

err = json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}

w.WriteHeader(http.StatusBadRequest)
return
}

// var val interface{}
// err = cbor.Unmarshal(delta.Value().([]byte), &val)
// if err != nil {
// result.Errors = []interface{}{err.Error()}
// json.NewEncoder(w).Encode(result)
// w.WriteHeader(http.StatusBadRequest)
// return
// result.Errors = []interface{}{err.Error()}
// err = json.NewEncoder(w).Encode(result)
// if err != nil {
// http.Error(w, err.Error(), 500)
// return
// }
// w.WriteHeader(http.StatusBadRequest)
// return
// }
result.Data = map[string]interface{}{
"block": string(buf),
Expand All @@ -184,7 +265,13 @@ func (s *Server) getBlock(w http.ResponseWriter, r *http.Request) {
if err != nil {
result.Errors = []interface{}{err.Error()}
result.Data = nil
json.NewEncoder(w).Encode(result)

err := json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}

w.WriteHeader(http.StatusBadRequest)
return
}
Expand Down
Loading

0 comments on commit 75121bd

Please sign in to comment.