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

feat: dedis #38

Merged
merged 22 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use nix
65 changes: 62 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,78 @@ jobs:
uses: actions/checkout@v3

- name: Download dependencies
run: sudo apt update && sudo apt install -y build-essential libpng-dev
run: |
sudo apt update && sudo apt install -y build-essential libpng-dev protobuf-compiler
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]

- name: Go Generate
run: go generate -tags tools -x ./...

- name: Build
run: go build -v -o api-linux-amd64 .
env:
CGO_ENABLED: 1

lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: Check out code into the Go module directory
uses: actions/checkout@v3

- name: Download dependencies
run: |
sudo apt update && sudo apt install -y build-essential libpng-dev protobuf-compiler
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]

- name: Go Generate
run: go generate -tags tools -x ./...

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.49.0
skip-pkg-cache: true
skip-build-cache: true
args: --timeout 5m

- name: Build
run: go build -v -o api-linux-amd64 .
test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: Check out code into the Go module directory
uses: actions/checkout@v3

- name: Download dependencies
run: |
sudo apt update && sudo apt install -y build-essential libpng-dev protobuf-compiler
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]

- name: Go Generate
run: go generate -tags tools -x ./...

- name: Start stack
run: docker-compose -f docker-compose-dev.yml up -d

- name: Test
run: go test -v -coverprofile=coverage.txt -covermode=atomic -coverpkg=./... ./...
env:
CGO_ENABLED: 1
REPO_PASETO.PUBLIC_KEY: 408c5155a389aeabf1c1b0da73ff5a3079b6aa6628e4c661b1e1ce412181cc8a
REPO_PASETO.PRIVATE_KEY: a5f7409588f6b72d443db0d432f37f1214a5ec88cb55a70e24b90194ed549465408c5155a389aeabf1c1b0da73ff5a3079b6aa6628e4c661b1e1ce412181cc8a

- name: Codecov
uses: codecov/codecov-action@v1
5 changes: 4 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ jobs:
fetch-depth: 0

- name: Download dependencies
run: sudo apt update && sudo apt install -y build-essential libpng-dev
run: |
sudo apt update && sudo apt install -y build-essential libpng-dev protobuf-compiler
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v3
Expand Down
36 changes: 31 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,58 @@ linters-settings:
ignorePackageGlobs:
- github.com/satisfactorymodding/smr-api/*

govet:
check-shadowing: true
enable-all: true
disable:
- shadow

gocritic:
disabled-checks:
- ifElseChain

gci:
custom-order: true
sections:
- standard
- default
- prefix(github.com/satisfactorymodding/smr-api)
- blank
- dot

run:
skip-files:
- ./generated/generated.go
- ./generated/models_gen.go
skip-dirs:
- ./docs/

issues:
exclude:
- should pass the context parameter

linters:
disable-all: true
enable:
- deadcode
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- structcheck
- typecheck
- unused
- varcheck
- bidichk
- contextcheck
- durationcheck
- errorlint
- goconst
- goimports
- revive
- ifshort
- misspell
- prealloc
- whitespace
- wrapcheck
- wrapcheck
- gci
- gocritic
- gofumpt
- nonamedreturns
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
FROM golang:1.18-alpine AS builder
FROM golang:1.19-alpine3.18 AS builder

RUN apk add --no-cache git build-base libpng-dev
RUN apk add --no-cache git build-base libpng-dev protoc
RUN go install google.golang.org/protobuf/cmd/[email protected]
RUN go install google.golang.org/grpc/cmd/[email protected]

WORKDIR $GOPATH/src/github.com/satisfactorymodding/smr-api/

Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SMR API [![build](https://github.com/satisfactorymodding/smr-api/actions/workflows/build.yml/badge.svg)](https://github.com/satisfactorymodding/smr-api/actions/workflows/build.yml)
# SMR API ![GitHub Workflow Status](https://img.shields.io/github/workflow/status/satisfactorymodding/smr-api/build) ![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/satisfactorymodding/smr-api) [![codecov](https://codecov.io/gh/satisfactorymodding/smr-api/branch/master/graph/badge.svg?token=LFNKYWS0N2)](https://codecov.io/gh/satisfactorymodding/smr-api) ![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/satisfactorymodding/smr-api)

The Satisfactory Mod Repository backend API

Expand Down Expand Up @@ -30,7 +30,7 @@ To run the API, you will need to have a working Postgres, Redis and Storage. The
start via:

```bash
docker-compose -f docker-compose.dev.yml up -d
docker-compose -f docker-compose-dev.yml up -d
```

It is suggested you create a configuration file at `config.json` (but you can also use environment variables).
Expand All @@ -49,6 +49,14 @@ Main configuration options:

The config format can be seen in `config/config.go` (each dot means a new level of nesting).

After startup requires the following minio commands to be executed:

```shell
mc alias set local http://localhost:9000 minio minio123
mc admin user svcacct add local minio --access-key REPLACE_ME_KEY --secret-key REPLACE_ME_SECRET
mc anonymous set public local/smr
```

## Contributing

Before contributing, please run the [linter](https://golangci-lint.run/) to ensure the code is clean and well-formed:
Expand Down
74 changes: 47 additions & 27 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,6 @@ import (
"syscall"
"time"

"github.com/satisfactorymodding/smr-api/auth"
"github.com/satisfactorymodding/smr-api/config"
"github.com/satisfactorymodding/smr-api/dataloader"
"github.com/satisfactorymodding/smr-api/db"
"github.com/satisfactorymodding/smr-api/db/postgres"

"github.com/pkg/errors"

// Load REST docs
_ "github.com/satisfactorymodding/smr-api/docs"
"github.com/satisfactorymodding/smr-api/generated"
"github.com/satisfactorymodding/smr-api/gql"
"github.com/satisfactorymodding/smr-api/migrations"
"github.com/satisfactorymodding/smr-api/nodes"
"github.com/satisfactorymodding/smr-api/oauth"
"github.com/satisfactorymodding/smr-api/redis"
"github.com/satisfactorymodding/smr-api/redis/jobs"

// Load redis consumers
_ "github.com/satisfactorymodding/smr-api/redis/jobs/consumers"
"github.com/satisfactorymodding/smr-api/storage"
"github.com/satisfactorymodding/smr-api/util"
"github.com/satisfactorymodding/smr-api/validation"

"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/lru"
Expand All @@ -42,6 +18,7 @@ import (
"github.com/labstack/echo-contrib/pprof"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
echoSwagger "github.com/swaggo/echo-swagger"
Expand All @@ -54,6 +31,27 @@ import (
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
"go.opentelemetry.io/otel/trace"
"gopkg.in/go-playground/validator.v9"

"github.com/satisfactorymodding/smr-api/auth"
"github.com/satisfactorymodding/smr-api/config"
"github.com/satisfactorymodding/smr-api/dataloader"
"github.com/satisfactorymodding/smr-api/db"
"github.com/satisfactorymodding/smr-api/db/postgres"
"github.com/satisfactorymodding/smr-api/generated"
"github.com/satisfactorymodding/smr-api/gql"
"github.com/satisfactorymodding/smr-api/migrations"
"github.com/satisfactorymodding/smr-api/nodes"
"github.com/satisfactorymodding/smr-api/oauth"
"github.com/satisfactorymodding/smr-api/redis"
"github.com/satisfactorymodding/smr-api/redis/jobs"
"github.com/satisfactorymodding/smr-api/storage"
"github.com/satisfactorymodding/smr-api/util"
"github.com/satisfactorymodding/smr-api/validation"

// Load REST docs
_ "github.com/satisfactorymodding/smr-api/docs"
// Load redis consumers
_ "github.com/satisfactorymodding/smr-api/redis/jobs/consumers"
)

type CustomValidator struct {
Expand All @@ -64,8 +62,8 @@ func (cv *CustomValidator) Validate(i interface{}) error {
return errors.Wrap(cv.validator.Struct(i), "validation error")
}

func Serve() {
ctx := config.InitializeConfig()
func Initialize(baseCtx context.Context) context.Context {
ctx := config.InitializeConfig(baseCtx)

if os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT") != "" {
cleanup := installExportPipeline(ctx)
Expand All @@ -81,9 +79,18 @@ func Serve() {
auth.InitializeAuth()
jobs.InitializeJobs(ctx)
validation.InitializeVirusTotal()
util.PrintFeatureFlags()

return ctx
}

func Migrate(ctx context.Context) {
migrations.RunMigrations(ctx)
}

var e *echo.Echo

func Setup(ctx context.Context) {
if viper.GetBool("profiler") {
go func() {
debugServer := echo.New()
Expand All @@ -101,7 +108,7 @@ func Serve() {

dataValidator := validator.New()

e := echo.New()
e = echo.New()
e.HideBanner = true
e.Validator = &CustomValidator{validator: dataValidator}

Expand Down Expand Up @@ -271,7 +278,9 @@ func Serve() {
<-signals
_ = e.Close()
}()
}

func Serve() {
address := fmt.Sprintf(":%d", viper.GetInt("port"))
log.Info().Str("address", address).Msg("starting server")

Expand Down Expand Up @@ -311,3 +320,14 @@ func newResource() *resource.Resource {
)
return r
}

func Start() {
ctx := Initialize(context.Background())
Migrate(ctx)
Setup(ctx)
Serve()
}

func Stop() error {
return errors.Wrap(e.Close(), "failed to stop http server")
}
6 changes: 4 additions & 2 deletions auth/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ var (
}
)

var idToGroupMapping = make(map[string]*Group)
var roleToGroupMapping = make(map[*Role][]*Group)
var (
idToGroupMapping = make(map[string]*Group)
roleToGroupMapping = make(map[*Role][]*Group)
)

func initializePermissions() {
groups := []*Group{GroupAdmin, GroupModerator, GroupSMLDev, GroupBootstrapDev, GroupCompatibilityOfficer}
Expand Down
4 changes: 2 additions & 2 deletions cmd/api/serve.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

import smr "github.com/satisfactorymodding/smr-api"
import "github.com/satisfactorymodding/smr-api"

// @title Satisfactory Mod Repo API
// @version 1
Expand All @@ -12,5 +12,5 @@ import smr "github.com/satisfactorymodding/smr-api"
// @host api.ficsit.app
// @BasePath /v1
func main() {
smr.Serve()
smr.Start()
}
1 change: 0 additions & 1 deletion cmd/paseto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

func main() {
publicKey, privateKey, err := ed25519.GenerateKey(nil)

if err != nil {
panic(err)
}
Expand Down
1 change: 0 additions & 1 deletion cmd/validate-zip/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func main() {

validation.InitializeValidator()
_, err := validation.ExtractModInfo(context.Background(), f, true, true, "N/A")

if err != nil {
panic(err)
}
Expand Down
7 changes: 6 additions & 1 deletion config.sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"key": "REPLACE_ME_KEY",
"secret": "REPLACE_ME_SECRET",
"endpoint": "http://localhost:9000",
"base_url": "http://localhost:9000"
"base_url": "http://localhost:9000",
"keypath": "%s/%s/%s"
},

"oauth": {
Expand All @@ -52,5 +53,9 @@

"frontend": {
"url": "http://localhost:4200"
},

"feature_flags": {
"allow_multi_target_upload": false
}
}
Loading
Loading