Skip to content
This repository has been archived by the owner on Oct 2, 2022. It is now read-only.

Commit

Permalink
Code reorg and deps
Browse files Browse the repository at this point in the history
  • Loading branch information
s32x committed Feb 17, 2021
1 parent 59122b3 commit 4e65255
Show file tree
Hide file tree
Showing 55 changed files with 2,936 additions and 266 deletions.
18 changes: 5 additions & 13 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,23 @@ jobs:
name: Build
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.15
uses: actions/setup-go@v2
- name: Set up Go
uses: actions/setup-go@v2.1.3
with:
go-version: ^1.15
go-version: ^1 # Also see: https://github.com/actions/go-versions/blob/main/versions-manifest.json
id: go

- name: Check out the repository
uses: actions/checkout@v2
uses: actions/checkout@v2.3.4

- name: Test all packages
run: go test -v ./...

- name: Build the apps server binary
run: go build -v -o server .

- name: Push to Docker Hub
uses: docker/build-push-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
repository: s32x/ovrstat
tag_with_ref: true

- name: Deploy to Heroku
uses: akhileshns/heroku-deploy@v3.7.8
uses: akhileshns/heroku-deploy@v3.11.10
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: ${{secrets.HEROKU_APP_NAME}}
Expand Down
11 changes: 6 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ COPY . /src
WORKDIR /src

# Dependencies
RUN apt-get update -y && \
apt-get upgrade -y
RUN apt-get update -y
RUN apt-get upgrade -y

# Vendor, Test and Build the Binary
RUN GO111MODULE=on go mod vendor
RUN go mod vendor
RUN go test ./...
RUN CGO_ENABLED=0 go build -o ./bin/server
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o ./bin/server

# =============================== FINAL IMAGE ===============================
FROM alpine:latest

# Dependencies
RUN apk update && apk add --no-cache ca-certificates
RUN apk update
RUN apk add --no-cache ca-certificates

# Static files and Binary
COPY --from=builder /src/static /static
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
The BSD 3-clause License
========================

Copyright (c) 2020, s32x. All rights reserved.
Copyright (c) 2021, s32x. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ovrstat isn’t endorsed by Blizzard and doesn’t reflect the views or opinions
The BSD 3-clause License
========================

Copyright (c) 2020, s32x. All rights reserved.
Copyright (c) 2021, s32x. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module s32x.com/ovrstat
go 1.15

require (
github.com/PuerkitoBio/goquery v1.6.0
github.com/PuerkitoBio/goquery v1.6.1
github.com/jinzhu/inflection v1.0.0
github.com/labstack/echo/v4 v4.1.17
github.com/labstack/echo/v4 v4.2.0
github.com/pkg/errors v0.9.1
)
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/PuerkitoBio/goquery v1.6.0 h1:j7taAbelrdcsOlGeMenZxc2AWXD5fieT1/znArdnx94=
github.com/PuerkitoBio/goquery v1.6.0/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/PuerkitoBio/goquery v1.6.1 h1:FgjbQZKl5HTmcn4sKBgvx8vv63nhyhIpv7lJpFGCWpk=
github.com/PuerkitoBio/goquery v1.6.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/andybalholm/cascadia v1.1.0 h1:BuuO6sSfQNFRu1LppgbD25Hr2vLYW25JvxHs5zzsLTo=
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
Expand All @@ -8,8 +8,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/labstack/echo/v4 v4.1.17 h1:PQIBaRplyRy3OjwILGkPg89JRtH2x5bssi59G2EL3fo=
github.com/labstack/echo/v4 v4.1.17/go.mod h1:Tn2yRQL/UclUalpb5rPdXDevbkJ+lp/2svdyFBg6CHQ=
github.com/labstack/echo/v4 v4.2.0 h1:jkCSsjXmBmapVXF6U4BrSz/cgofWM0CU3Q74wQvXkIc=
github.com/labstack/echo/v4 v4.2.0/go.mod h1:AA49e0DZ8kk5jTOOCKNuPR6oTnBS0dYiM4FW1e6jwpg=
github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0=
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
Expand Down Expand Up @@ -52,6 +52,8 @@ golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 h1:Hir2P/De0WpUhtrKGGjvSb2YxUgyZ7EFOSLIcSSpiwE=
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
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=
Expand Down
64 changes: 3 additions & 61 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,14 @@ package main

import (
"log"
"net/http"
"os"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/pkg/errors"
"s32x.com/ovrstat/ovrstat"
"s32x.com/ovrstat/service"
)

var port = getenv("PORT", "8080")

func main() {
// Create a new echo Echo and bind all middleware
e := echo.New()
e.HideBanner = true

// Bind middleware
e.Pre(middleware.RemoveTrailingSlashWithConfig(
middleware.TrailingSlashConfig{
RedirectCode: http.StatusPermanentRedirect,
}))
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Pre(middleware.Secure())
e.Use(middleware.Gzip())
e.Use(middleware.CORS())

// Serve the static web content on the base echo instance
e.Static("*", "./static")

// Handle stats API requests
e.GET("/stats/:platform/:tag", stats)
e.GET("/healthcheck", func(c echo.Context) error {
return c.NoContent(http.StatusOK)
})

// Listen on the specified port
e.Logger.Fatal(e.Start(":" + port))
}

// stats handles retrieving and serving Overwatch stats in JSON
func stats(c echo.Context) error {
// Perform a full player stats lookup
stats, err := ovrstat.Stats(c.Param("platform"), c.Param("tag"))
if err != nil {
if err == ovrstat.ErrPlayerNotFound {
return newErr(http.StatusNotFound, "Player not found")
}
return newErr(http.StatusInternalServerError,
errors.Wrap(err, "Failed to retrieve player stats"))
}
return c.JSON(http.StatusOK, stats)
}

// newErr creates and returns a new echo HTTPError with the passed status code
// and optional message. Message expected to be of type string or error
func newErr(code int, message ...interface{}) error {
if len(message) > 0 {
switch v := message[0].(type) {
case error:
return echo.NewHTTPError(code, v.Error())
case string:
return echo.NewHTTPError(code, v)
}
}
return echo.NewHTTPError(code, "An error has occurred")
// Start a new service
service.Start(getenv("PORT", "8080")) // The port the server will run on
}

// getenv attempts to retrieve and return a variable from the environment. If it
Expand Down
2 changes: 1 addition & 1 deletion ovrstat/player_stats.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ovrstat
package ovrstat /* import "s32x.com/ovrstat/ovrstat" */

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion ovrstat/pluralize.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ovrstat
package ovrstat /* import "s32x.com/ovrstat/ovrstat" */

import (
"sort"
Expand Down
2 changes: 1 addition & 1 deletion ovrstat/ranks.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ovrstat
package ovrstat /* import "s32x.com/ovrstat/ovrstat" */

// rankMap contains all mappings for Overwatch competitive ranks
var rankMap = map[string]int{
Expand Down
17 changes: 17 additions & 0 deletions service/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package service /* import "s32x.com/ovrstat/service" */

import "github.com/labstack/echo/v4"

// newErr creates and returns a new echo HTTPError with the passed status code
// and optional message. Message expected to be of type string or error
func newErr(code int, message ...interface{}) error {
if len(message) > 0 {
switch v := message[0].(type) {
case error:
return echo.NewHTTPError(code, v.Error())
case string:
return echo.NewHTTPError(code, v)
}
}
return echo.NewHTTPError(code, "An error has occurred")
}
38 changes: 38 additions & 0 deletions service/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package service /* import "s32x.com/ovrstat/service" */

import (
"net/http"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)

// Start starts serving the service on the passed port
func Start(port string) {
// Create a new echo Echo and bind all middleware
e := echo.New()
e.HideBanner = true

// Bind middleware
e.Pre(middleware.RemoveTrailingSlashWithConfig(
middleware.TrailingSlashConfig{
RedirectCode: http.StatusPermanentRedirect,
}))
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Pre(middleware.Secure())
e.Use(middleware.Gzip())
e.Use(middleware.CORS())

// Serve the static web content on the base echo instance
e.Static("*", "./static")

// Handle stats API requests
e.GET("/stats/:platform/:tag", stats)
e.GET("/healthcheck", func(c echo.Context) error {
return c.NoContent(http.StatusOK)
})

// Listen on the specified port
e.Logger.Fatal(e.Start(":" + port))
}
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions service/stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package service /* import "s32x.com/ovrstat/service" */

import (
"net/http"

"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"s32x.com/ovrstat/ovrstat"
)

// stats handles retrieving and serving Overwatch stats in JSON
func stats(c echo.Context) error {
// Perform a full player stats lookup
stats, err := ovrstat.Stats(c.Param("platform"), c.Param("tag"))
if err != nil {
if err == ovrstat.ErrPlayerNotFound {
return newErr(http.StatusNotFound, "Player not found")
}
return newErr(http.StatusInternalServerError,
errors.Wrap(err, "Failed to retrieve player stats"))
}
return c.JSON(http.StatusOK, stats)
}
27 changes: 27 additions & 0 deletions vendor/github.com/PuerkitoBio/goquery/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vendor/github.com/PuerkitoBio/goquery/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/github.com/PuerkitoBio/goquery/manipulation.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/github.com/labstack/echo/v4/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions vendor/github.com/labstack/echo/v4/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4e65255

Please sign in to comment.