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

Feature/basic card deck operations #27

Merged
merged 14 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 15 additions & 0 deletions .github/workflows/carddeck_service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Carddeck Service

on:
pull_request:
branches: [ main ]
paths:
- 'backend/services/carddeck/**'

jobs:
build-carddeck:
uses: ./.github/workflows/build_service.yml
with:
image-name: kioku_carddeck
path: ./backend/services/carddeck
context: ./backend
15 changes: 15 additions & 0 deletions .github/workflows/collaboration_service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Collaboration Service

on:
pull_request:
branches: [ main ]
paths:
- 'backend/services/collaboration/**'

jobs:
build-collaboration:
uses: ./.github/workflows/build_service.yml
with:
image-name: kioku_collaboration
path: ./backend/services/collaboration
context: ./backend
15 changes: 0 additions & 15 deletions .github/workflows/register_service.yml

This file was deleted.

20 changes: 14 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ jobs:
path: ./frontend
context: ./frontend

build-register:
build-carddeck:
uses: ./.github/workflows/build_service.yml
with:
image-name: kioku_register
image-name: kioku_carddeck
image-tag: prod
path: ./backend/services/register
path: ./backend/services/carddeck
context: ./backend

build-login:
build-user:
uses: ./.github/workflows/build_service.yml
with:
image-name: kioku_login
image-name: kioku_user
image-tag: prod
path: ./backend/services/login
path: ./backend/services/user
context: ./backend

build-frontend-proxy:
Expand All @@ -34,4 +34,12 @@ jobs:
image-name: kioku_frontend_proxy
image-tag: prod
path: ./backend/services/frontend
context: ./backend

build-collaboration:
uses: ./.github/workflows/build_service.yml
with:
image-name: kioku_collaboration
image-tag: prod
path: ./backend/services/collaboration
context: ./backend
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
name: Login Service
name: User Service

on:
pull_request:
branches: [ main ]
paths:
- 'backend/services/login/**'
- 'backend/services/user/**'

jobs:
build-login:
build-user:
uses: ./.github/workflows/build_service.yml
with:
image-name: kioku_login
path: ./backend/services/login
image-name: kioku_user
path: ./backend/services/user
context: ./backend
7 changes: 6 additions & 1 deletion backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ POSTGRES_PORT=5432

MICRO_REGISTRY=mdns
SERVER_AUTH_USERNAME=
SERVER_AUTH_PASSWORD=
SERVER_AUTH_PASSWORD=

PGADMIN_DEFAULT_EMAIL=
PGADMIN_DEFAULT_PASSWORD=

JWT_PRIVATE_KEY=
13 changes: 6 additions & 7 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ require (
github.com/go-micro/plugins/v4/registry/kubernetes v1.1.1
github.com/go-micro/plugins/v4/server/grpc v1.2.0
github.com/gofiber/fiber/v2 v2.44.0
github.com/gofiber/jwt/v3 v3.3.9
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/joho/godotenv v1.5.1
go-micro.dev/v4 v4.10.2
google.golang.org/grpc v1.54.0
google.golang.org/grpc v1.55.0
gorm.io/driver/postgres v1.5.0
gorm.io/gorm v1.25.0
)

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/gofiber/jwt/v3 v3.3.9 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang-jwt/jwt/v5 v5.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.3.1 // indirect
Expand Down Expand Up @@ -51,7 +50,7 @@ require (
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-acme/lego/v4 v4.10.2 // indirect
github.com/go-acme/lego/v4 v4.11.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.4.1 // indirect
github.com/go-git/go-git/v5 v5.6.1 // indirect
Expand Down Expand Up @@ -79,8 +78,8 @@ require (
golang.org/x/crypto v0.8.0
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.8.0 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
Expand Down
16 changes: 8 additions & 8 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/gliderlabs/ssh v0.3.5 h1:OcaySEmAQJgyYcArR+gGGTHCyE7nvhEMTlYY+Dp8CpY=
github.com/gliderlabs/ssh v0.3.5/go.mod h1:8XB4KraRrX39qHhT6yxPsHedjA08I/uBVwj4xC+/+z4=
github.com/go-acme/lego/v4 v4.10.2 h1:5eW3qmda5v/LP21v1Hj70edKY1jeFZQwO617tdkwp6Q=
github.com/go-acme/lego/v4 v4.10.2/go.mod h1:EMbf0Jmqwv94nJ5WL9qWnSXIBZnvsS9gNypansHGc6U=
github.com/go-acme/lego/v4 v4.11.0 h1:oIPoU7zBJoTfoVrbqk62+/2NsGCSgCVK1JtZSZZ28SU=
github.com/go-acme/lego/v4 v4.11.0/go.mod h1:dENL0J3/WughN2NLy0T35otK5k1EWCmXTwCw0+X5ZaE=
github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=
github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E=
github.com/go-git/go-billy/v5 v5.3.1/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0=
Expand Down Expand Up @@ -68,8 +68,6 @@ github.com/gofiber/jwt/v3 v3.3.9 h1:vPNg8FNsF+rBUi9wvaqj0q4H2BRqz6rTyQmvIM6ko4Y=
github.com/gofiber/jwt/v3 v3.3.9/go.mod h1:ZijRDaj14kALpaMm2F+sNaZuwqCQS6CG1QoDCHN1h/o=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang-jwt/jwt/v5 v5.0.0 h1:1n1XNM9hk7O9mnQoNBGolZvzebBQ7p93ULHRc28XJUE=
github.com/golang-jwt/jwt/v5 v5.0.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
Expand Down Expand Up @@ -230,8 +228,9 @@ golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -255,8 +254,9 @@ golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down Expand Up @@ -290,8 +290,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 h1:KpwkzHKEF7B9Zxg18WzOa7djJ+Ha5DzthMyZYQfEn2A=
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1/go.mod h1:nKE/iIaLqn2bQwXBg8f1g2Ylh6r5MN5CmZvuzZCgsCU=
google.golang.org/grpc v1.54.0 h1:EhTqbhiYeixwWQtAEZAxmV9MGqcjEU2mFx52xCzNyag=
google.golang.org/grpc v1.54.0/go.mod h1:PUSEXI6iWghWaB6lXM4knEgpJNu2qUcKfDtNci3EC2g=
google.golang.org/grpc v1.55.0 h1:3Oj82/tFSCeUrRTg/5E/7d/W5A1tj6Ky1ABAuZuv5ag=
google.golang.org/grpc v1.55.0/go.mod h1:iYEXKGkEBhg1PjZQvoYEVPTDkHo1/bjTnfwTeGONTY8=
google.golang.org/grpc/examples v0.0.0-20211102180624-670c133e568e h1:m7aQHHqd0q89mRwhwS9Bx2rjyl/hsFAeta+uGrHsQaU=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
Expand Down
7 changes: 7 additions & 0 deletions backend/pkg/helper/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package helper

import "errors"

var (
ErrNotAuthorized = errors.New("user not authorized")
)
12 changes: 11 additions & 1 deletion backend/pkg/helper/jwtHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import (
"os"
"time"

"github.com/golang-jwt/jwt/v5"
memeToasty marked this conversation as resolved.
Show resolved Hide resolved
"github.com/gofiber/fiber/v2"
"github.com/golang-jwt/jwt/v4"
"go-micro.dev/v4/logger"
)

func getJWTPrivateKey() (*ecdsa.PrivateKey, error) {

pem, _ := pem2.Decode([]byte(os.Getenv("JWT_PRIVATE_KEY")))
if pem == nil {
panic("JWT Private Key not given")
}
priv, err := x509.ParseECPrivateKey(pem.Bytes)
if err != nil {
logger.Info(err)
Expand Down Expand Up @@ -76,3 +80,9 @@ func CreateJWTTokenString(exp time.Time, id interface{}, email interface{}, name
}
return tokenString, nil
}

func GetUserIDFromContext(c *fiber.Ctx) string {
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
return claims["sub"].(string)
}
60 changes: 60 additions & 0 deletions backend/pkg/helper/publicID.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package helper

import (
"errors"
"fmt"
"math/rand"
"time"

"gorm.io/gorm"
)

const (
charset = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789"
)

var (
random = rand.New(rand.NewSource(time.Now().UnixNano()))
)

var ErrRetryCountExceeded = errors.New("exceeded retry count")

type PublicID struct {
prefix rune
id string
}

func GenerateID(prefix rune) PublicID {
randomPublicID := make([]byte, 8)
for i := range randomPublicID {
randomPublicID[i] = charset[random.Intn(len(charset))]
}
return PublicID{prefix: prefix, id: string(randomPublicID)}
}

func (i PublicID) GetStringRepresentation() string {
return fmt.Sprintf("%c-%s", i.prefix, i.id)
}

func FindFreeID[T, C any](
db *gorm.DB,
retries int,
with func() (C, *T),
) (res C, err error) {
var currentTry int
var t T
for {
currentTry++
if currentTry > retries {
err = ErrRetryCountExceeded
return
}
candidate, val := with()
if err = db.Where(val).First(&t).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return candidate, nil
}
return
}
}
}
22 changes: 19 additions & 3 deletions backend/pkg/model/card.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
package model

import (
"github.com/kioku-project/kioku/pkg/helper"
"gorm.io/gorm"
)

type Card struct {
ID uint
DeckID uint
Deck Deck
ID string `gorm:"primaryKey"`
DeckID string `gorm:"not null"`
Deck Deck `gorm:"foreignKey:DeckID"`
Frontside string `gorm:"not null"`
Backside string `gorm:"not null"`
}

func (c *Card) BeforeCreate(db *gorm.DB) (err error) {
newID, err := helper.FindFreeID(db, 10, func() (helper.PublicID, *Card) {
id := helper.GenerateID('C')
return id, &Card{ID: id.GetStringRepresentation()}
})
c.ID = newID.GetStringRepresentation()
return
}
23 changes: 18 additions & 5 deletions backend/pkg/model/deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@ package model

import (
"time"

"github.com/kioku-project/kioku/pkg/helper"
"gorm.io/gorm"
)

type Deck struct {
ID uint
Name string
CreatedAt time.Time
GroupID uint
Group Group
ID string `gorm:"primaryKey"`
Name string `gorm:"not null"`
CreatedAt time.Time `gorm:"not null"`
GroupID string `gorm:"not null"`
Group Group `gorm:"foreignKey:GroupID"`
Cards []Card `gorm:"foreignKey:DeckID"`
}

func (d *Deck) BeforeCreate(db *gorm.DB) (err error) {
newID, err := helper.FindFreeID(db, 10, func() (helper.PublicID, *Deck) {
id := helper.GenerateID('D')
return id, &Deck{ID: id.GetStringRepresentation()}
})
d.ID = newID.GetStringRepresentation()
return
}
Loading