Skip to content

Commit

Permalink
bug report endpoint and update to go 1.21 +deps
Browse files Browse the repository at this point in the history
Signed-off-by: Guillem Bonet <[email protected]>
  • Loading branch information
Guillembonet committed Oct 17, 2023
1 parent 00233de commit 9d7e4c6
Show file tree
Hide file tree
Showing 31 changed files with 1,443 additions and 1,989 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @n10ty @Guillembonet
45 changes: 45 additions & 0 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Build and test

on: push

env:
GITHUB_CI: true

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: "1.21"
- name: Build Binary
run: go run mage.go -v build
unit-test:
name: Run unit tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: "1.21"
- name: Run unit tests
run: go run mage.go -v test
e2e:
name: Run e2e scenarios
runs-on: ubuntu-latest
steps:
- shell: bash
env:
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }}
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
run: |
echo "$DOCKERHUB_PASSWORD" | docker login --username "$DOCKERHUB_USERNAME" --password-stdin || true
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: "1.21"
- name: Run e2e scenarios
run: go run mage.go -v e2e

44 changes: 0 additions & 44 deletions .gitlab-ci.yml

This file was deleted.

6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
FROM golang:1.17.6-alpine3.15 AS builder
FROM golang:1.21-alpine AS builder

# Install packages
RUN apk add --no-cache bash gcc musl-dev git curl
RUN apk add --no-cache bash gcc musl-dev linux-headers git curl

# Compile application
WORKDIR /src
ADD . .
RUN go run mage.go -v build

FROM alpine:3.15
FROM alpine:3.17

# Install application
COPY --from=builder /src/build/feedback /usr/bin/feedback
Expand Down
15 changes: 13 additions & 2 deletions ci/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package ci

import (
"fmt"
"os"
"time"

"github.com/cihub/seelog"
"github.com/magefile/mage/sh"
"github.com/mysteriumnetwork/feedback/di"
"github.com/mysteriumnetwork/feedback/params"
"github.com/rs/zerolog"
)

// E2E runs the e2e tests
Expand All @@ -22,7 +23,7 @@ func E2E() error {
// TODO: ping s3-mock and wiremock and await startup, it is very slow in the pipeline
time.Sleep(time.Second * 10)

logLevel := seelog.DebugStr
logLevel := zerolog.DebugLevel.String()
requestsPerSecond := 9999999999.0
logProxyBaseUrl := "http://someweb.com"
gparams := params.Generic{
Expand All @@ -48,6 +49,16 @@ func E2E() error {
EnvGithubRepository: &envGithubRepository,
}

//set mock env vars
err = os.Setenv("AWS_ACCESS_KEY_ID", "test_key_id")
if err != nil {
return err
}
err = os.Setenv("AWS_SECRET_ACCESS_KEY", "test_secret_key")
if err != nil {
return err
}

fmt.Println("starting server...")

container := &di.Container{}
Expand Down
11 changes: 6 additions & 5 deletions client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path"
"time"
)
Expand Down Expand Up @@ -71,7 +72,7 @@ func (f *FeedbackAPI) CreateIntercomIssue(request CreateIntercomIssueRequest) (r
}

func newCreateGithubIssueRequest(uri string, req CreateGithubIssueRequest) (multipartReq *http.Request, err error) {
fileContent, err := ioutil.ReadFile(req.Filepath)
fileContent, err := os.ReadFile(req.Filepath)
if err != nil {
return nil, fmt.Errorf("could not read input file: %w", err)
}
Expand Down Expand Up @@ -101,7 +102,7 @@ func newCreateGithubIssueRequest(uri string, req CreateGithubIssueRequest) (mult
}

func newCreateIntercomIssueRequest(uri string, req CreateIntercomIssueRequest) (multipartReq *http.Request, err error) {
fileContent, err := ioutil.ReadFile(req.Filepath)
fileContent, err := os.ReadFile(req.Filepath)
if err != nil {
return nil, fmt.Errorf("could not read input file: %w", err)
}
Expand Down Expand Up @@ -178,7 +179,7 @@ type GithubIssueCreated struct {

func parseCreateGithubIssueResult(httpRes *http.Response) (*CreateGithubIssueResult, error) {
res := &CreateGithubIssueResult{HTTPResponse: httpRes}
resJSON, err := ioutil.ReadAll(httpRes.Body)
resJSON, err := io.ReadAll(httpRes.Body)
if err != nil {
res.Success = false
res.Errors = singleErrorResponse("could not parse feedback response: " + err.Error())
Expand Down Expand Up @@ -207,7 +208,7 @@ func parseCreateGithubIssueResult(httpRes *http.Response) (*CreateGithubIssueRes

func parseCreateIntercomIssueResult(httpRes *http.Response) (*CreateIntercomIssueResult, error) {
res := &CreateIntercomIssueResult{HTTPResponse: httpRes}
resJSON, err := ioutil.ReadAll(httpRes.Body)
resJSON, err := io.ReadAll(httpRes.Body)
if err != nil {
res.Success = false
res.Errors = singleErrorResponse("could not parse feedback response: " + err.Error())
Expand Down
47 changes: 28 additions & 19 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
// The purpose of this documentation is to provide developers an insight of how to
// interact with Mysterium Feedback API
//
// schemes: https
// host: localhost
// basepath: /api/v1
// license: GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
// schemes: https
// host: localhost
// basepath: /api/v1
// license: GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
//
// consumes:
// - application/json
// consumes:
// - application/json
//
// produces:
// - application/json
// produces:
// - application/json
//
// version: 0.0.1
// version: 0.0.1
//
// swagger:meta
package main
Expand All @@ -24,18 +24,29 @@ import (
"flag"
"os"

log "github.com/cihub/seelog"
"github.com/mysteriumnetwork/feedback/constants"
"github.com/mysteriumnetwork/feedback/di"
"github.com/mysteriumnetwork/feedback/infra"
"github.com/mysteriumnetwork/feedback/params"
mlog "github.com/mysteriumnetwork/logger"
"github.com/rs/zerolog/log"
)

// @title Feedback
// @version 1.0
// @description This is a service dedicated to collecting feedback from Mysterium Network users
// @termsOfService https://docs.mysterium.network/en/latest/about/terms-and-conditions/

// @contact.name API Support
// @contact.url https://github.com/mysteriumnetwork/feedback/issues

// @BasePath /api
func main() {
os.Exit(app())
}

func app() (retValue int) {
logger := mlog.BootstrapDefaultLogger()

gparams := params.Generic{}
gparams.Init()

Expand All @@ -48,35 +59,33 @@ func app() (retValue int) {
constants.EnvIntercomAccessToken,
)
if err != nil {
_ = log.Critical(err)
log.Fatal().Err(err).Msg("Failed to initialize environment")
return -1
}

eparams := params.Environment{}
eparams.Init()

flag.Parse()
infra.ConfigureLogger(*gparams.LogLevelFlag)
infra.BootstrapLogger(infra.CurrentLogOptions)
mlog.SetLevel(logger, *gparams.LogLevelFlag)

log.Info("Starting feedback service")
log.Info().Msg("Starting feedback service")
defer func() {
log.Info("Stopping feedback service")
log.Flush()
log.Info().Msg("Stopping feedback service")
}()

container := &di.Container{}
defer container.Cleanup()

server, err := container.ConstructServer(gparams, eparams)
if err != nil {
_ = log.Critical("Error constructing API server: ", err)
log.Fatal().Err(err).Msg("Error constructing API server")
return -1
}

err = server.Serve()
if err != nil {
_ = log.Critical("Error running API server: ", err)
log.Fatal().Err(err).Msg("Error running API server")
return -1
}
return 0
Expand Down
5 changes: 5 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package constants

const (
EuCentral1RegionID = "eu-central-1"
)
6 changes: 2 additions & 4 deletions di/di.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package di
import (
"sync"

log "github.com/cihub/seelog"
"github.com/mysteriumnetwork/feedback/docs"
"github.com/mysteriumnetwork/feedback/feedback"
"github.com/mysteriumnetwork/feedback/infra"
"github.com/mysteriumnetwork/feedback/params"
"github.com/mysteriumnetwork/feedback/server"
"github.com/rs/zerolog/log"
)

// Container represents our dependency container
Expand All @@ -27,7 +26,7 @@ func (c *Container) ConstructServer(gparams params.Generic, eparams params.Envir
Bucket: *eparams.EnvAWSBucket,
})
if err != nil {
_ = log.Critical("Failed to initialize storage: ", err)
log.Fatal().Err(err).Msg("Failed to initialize storage")
return nil, err
}

Expand All @@ -48,7 +47,6 @@ func (c *Container) ConstructServer(gparams params.Generic, eparams params.Envir
srvr := server.New(
feedback.NewEndpoint(githubReporter, intercomReporter, storage, rateLimiter),
infra.NewPingEndpoint(),
docs.NewEndpoint(),
)

return srvr, nil
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
ports:
- '8090:8080'
s3-mock:
image: adobe/s3mock:2.4.6
image: adobe/s3mock:3.1.0
ports:
- 9090:9090
environment:
Expand Down
31 changes: 19 additions & 12 deletions docs/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,31 @@ package docs

import (
"github.com/gin-gonic/gin"
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)

// Endpoint API documentation endpoint
type Endpoint struct {
// Swagger object
type Swagger struct{}

var docsHandler = ginSwagger.WrapHandler(swaggerfiles.Handler)

// NewSwaggerHandler creates a new handler for swagger docs
func NewSwagger() *Swagger {
return &Swagger{}
}

// NewEndpoint creates new Endpoint
func NewEndpoint() *Endpoint {
return &Endpoint{}
// Index redirects root route to swagger docs
func (s *Swagger) Index(context *gin.Context) {
context.Redirect(301, "/swagger/index.html")
}

// SwaggerJSON return API schema
func (e *Endpoint) SwaggerJSON(c *gin.Context) {
c.Writer.Write(MustAsset(SwaggerJSONFilepath))
c.Status(200)
// Docs use ginSwagger middleware to serve the API docs
func (s *Swagger) Docs(context *gin.Context) {
docsHandler(context)
}

// RegisterRoutes registers API documentation routes
func (e *Endpoint) RegisterRoutes(r gin.IRoutes) {
r.GET("/swagger.json", e.SwaggerJSON)
func (s *Swagger) AttachHandlers(g *gin.Engine) {
g.GET("/", s.Index)
g.GET("/swagger/*any", s.Docs)
}
Loading

0 comments on commit 9d7e4c6

Please sign in to comment.