-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Collect statistics and serve over API (#13)
* Add back linter on push * Add bbolt to project * Make it work with the new structure * Add new helper for ip * Add middleware for collecting stats * Add function to collect download asked * doc: update documentation * Add skeleton for API * Review structure a little bit * Add a proto for CORS custom middleware * Add visit statistic * Add unique switch stat * Add details per switch * Add ip for unkown switch * Trigger stats for download game only on existing game * Add stats about downloads * Begin to add test for StatsMiddleware * Add tests for StatsMiddleware * doc: Add more info * Add new github Action for testing * Add tests for API * Move bytes functions to utils * Add more tests * Fix ginkgo workflow * Another fix for ginkgo * Install dependencies in CI * Fix ginkgo workflow * Fix randomize tests * New badges * Another fix for GithubAction * doc: Cleaner readme * doc: More spaces
- Loading branch information
Showing
26 changed files
with
957 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: test | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 0 | ||
- | ||
id: vars | ||
run: | | ||
echo ::set-output name=go_version::$(cat go.mod | head -3 | tail -1 | cut -d ' ' -f 2) | ||
echo "Using Go version ${{ steps.vars.outputs.go_version }}" | ||
- name: Set up Go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: ${{ steps.vars.outputs.go_version }} | ||
- run: go mod tidy && git diff --exit-code go.mod go.sum | ||
- name: Install ginkgo | ||
run: | | ||
go get github.com/onsi/ginkgo/v2/ginkgo | ||
go get github.com/onsi/gomega/... | ||
- name: Print out Ginkgo version | ||
run: ginkgo version | ||
- run: ginkgo -r --randomize-all --randomize-suites --race --trace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
name: golangci-lint | ||
on: | ||
# push: | ||
push: | ||
pull_request: | ||
permissions: | ||
contents: read | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,6 @@ | |
# TinShop specific | ||
config.yaml | ||
titles.US.en.json | ||
/dist | ||
/dist | ||
stats.db | ||
coverprofile.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/DblK/tinshop/repository" | ||
) | ||
|
||
type endpoint struct { | ||
} | ||
|
||
// New returns a new api | ||
func New() repository.API { | ||
return &endpoint{} | ||
} | ||
|
||
func (e *endpoint) Stats(w http.ResponseWriter, stats repository.StatsSummary) { | ||
jsonResponse, jsonError := json.Marshal(stats) | ||
|
||
if jsonError != nil { | ||
log.Println("[API] Unable to encode JSON") | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write(jsonResponse) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package api_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestApi(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Api Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package api_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/DblK/tinshop/api" | ||
"github.com/DblK/tinshop/repository" | ||
) | ||
|
||
var _ = Describe("Api", func() { | ||
var ( | ||
myAPI repository.API | ||
writer *httptest.ResponseRecorder | ||
) | ||
BeforeEach(func() { | ||
myAPI = api.New() | ||
}) | ||
Describe("Stats", func() { | ||
It("Test with empty stats", func() { | ||
emptyStats := &repository.StatsSummary{} | ||
writer = httptest.NewRecorder() | ||
|
||
myAPI.Stats(writer, *emptyStats) | ||
Expect(writer.Code).To(Equal(http.StatusOK)) | ||
Expect(writer.Body.String()).To(Equal("{}")) | ||
}) | ||
It("Test with some stats", func() { | ||
emptyStats := &repository.StatsSummary{ | ||
Visit: 42, | ||
} | ||
writer = httptest.NewRecorder() | ||
|
||
myAPI.Stats(writer, *emptyStats) | ||
Expect(writer.Code).To(Equal(http.StatusOK)) | ||
Expect(writer.Body.String()).To(Equal("{\"visit\":42}")) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.