Skip to content

Commit

Permalink
feat: add GET /tournaments + reorganized server folder
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Apr 25, 2019
1 parent a4d798e commit b0ffecc
Show file tree
Hide file tree
Showing 13 changed files with 795 additions and 458 deletions.
632 changes: 402 additions & 230 deletions entity/entity.pb.go

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions entity/entity.proto
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ message Tournament {
repeated Coupon coupons = 201 [(gogoproto.moretags) = "gorm:\"PRELOAD:false\""];
}

message Tournaments {
repeated Tournament items = 1;
}

message TournamentTeam {
Metadata metadata = 1 [(gogoproto.nullable) = false, (gogoproto.embed) = true];

Expand Down
166 changes: 166 additions & 0 deletions server/api.dev.generate-fake-data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package server

import (
"context"
"math/rand"

"github.com/moul/gofakeit"
"go.uber.org/zap"

"pathwar.pw/entity"
)

func (s *svc) GenerateFakeData(ctx context.Context, _ *Void) (*Void, error) {
hypervisors := []*entity.Hypervisor{}
for i := 0; i < 3; i++ {
hypervisor := &entity.Hypervisor{
Name: gofakeit.HipsterWord(),
Address: gofakeit.IPv4Address(),
Status: entity.Hypervisor_Active,
}
hypervisors = append(hypervisors, hypervisor)
}

levels := []*entity.Level{}
for i := 0; i < 5; i++ {
level := &entity.Level{
Name: gofakeit.HipsterWord(),
Description: gofakeit.HipsterSentence(10),
Author: gofakeit.Name(),
Locale: "fr_FR",
IsDraft: false,
Versions: []*entity.LevelVersion{},
}
for i := 0; i < 2; i++ {
version := &entity.LevelVersion{
Driver: entity.LevelVersion_Docker,
Version: gofakeit.IPv4Address(),
Changelog: gofakeit.HipsterSentence(5),
IsDraft: false,
IsLatest: i == 0,
SourceURL: gofakeit.URL(),
Flavors: []*entity.LevelFlavor{},
}
for j := 0; j < 2; j++ {
flavor := &entity.LevelFlavor{
Instances: []*entity.LevelInstance{},
}
for k := 0; k < 2; k++ {
instance := &entity.LevelInstance{
HypervisorID: hypervisors[rand.Int()%len(hypervisors)].ID,
Status: entity.LevelInstance_Active,
}
flavor.Instances = append(flavor.Instances, instance)
}
version.Flavors = append(version.Flavors, flavor)
}
level.Versions = append(level.Versions, version)
}
levels = append(levels, level)
}

teams := []*entity.Team{}
for i := 0; i < 5; i++ {
team := &entity.Team{
Name: gofakeit.HipsterWord(),
GravatarURL: gofakeit.URL(),
Locale: "fr_FR",
}
teams = append(teams, team)
}

users := []*entity.User{}
for i := 0; i < 10; i++ {
user := &entity.User{
Username: gofakeit.Name(),
GravatarURL: gofakeit.URL(),
WebsiteURL: gofakeit.URL(),
Locale: "fr_FR",
IsStaff: false,
Memberships: []*entity.TeamMember{},
}
users = append(users, user)
}

tournaments := []*entity.Tournament{}
for i := 0; i < 3; i++ {
tournament := &entity.Tournament{
Name: gofakeit.HipsterWord(),
Status: entity.Tournament_Started,
Visibility: entity.Tournament_Public,
IsDefault: false,
}
tournaments = append(tournaments, tournament)
}
tournaments[0].IsDefault = true

zap.L().Debug("Generating hypervisors")
for _, entity := range hypervisors {
if err := s.db.Set("gorm:association_autoupdate", true).Create(entity).Error; err != nil {
return nil, err
}
}
zap.L().Debug("Generating users")
for _, entity := range users {
if err := s.db.Set("gorm:association_autoupdate", true).Create(entity).Error; err != nil {
return nil, err
}
}
zap.L().Debug("Generating levels")
for _, entity := range levels {
if err := s.db.Set("gorm:association_autoupdate", true).Create(entity).Error; err != nil {
return nil, err
}
}
zap.L().Debug("Generating tournaments")
for _, entity := range tournaments {
if err := s.db.Set("gorm:association_autoupdate", true).Create(entity).Error; err != nil {
return nil, err
}
}
zap.L().Debug("Generating teams")
for _, entity := range teams {
if err := s.db.Set("gorm:association_autoupdate", true).Create(entity).Error; err != nil {
return nil, err
}
}

coupons := []*entity.Coupon{}
for i := 0; i < 3; i++ {
coupon := &entity.Coupon{
Hash: gofakeit.UUID(),
MaxValidationCount: int32(rand.Int() % 5),
Value: int32(rand.Int() % 10),
TournamentID: tournaments[rand.Int()%len(tournaments)].ID,
}
coupons = append(coupons, coupon)
}

memberships := []*entity.TeamMember{}
for _, user := range users {
for i := 0; i < 2; i++ {
memberships = append(
memberships,
&entity.TeamMember{
TeamID: teams[rand.Int()%len(teams)].ID,
UserID: user.ID,
Role: entity.TeamMember_Member, // or Owner
},
)
}
}

zap.L().Debug("Generating memberships")
for _, entity := range memberships {
if err := s.db.Set("gorm:association_autoupdate", true).Create(entity).Error; err != nil {
return nil, err
}
}
zap.L().Debug("Generating coupons")
for _, entity := range coupons {
if err := s.db.Set("gorm:association_autoupdate", true).Create(entity).Error; err != nil {
return nil, err
}
}
return &Void{}, nil
}
12 changes: 12 additions & 0 deletions server/api.dev.sql-dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package server

import (
"context"

"pathwar.pw/entity"
"pathwar.pw/sql"
)

func (s *svc) Dump(ctx context.Context, _ *Void) (*entity.Dump, error) {
return sql.DoDump(s.db)
}
23 changes: 23 additions & 0 deletions server/api.pub.authenticate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package server

import (
"context"

jwt "github.com/dgrijalva/jwt-go"
)

func (s *svc) Authenticate(ctx context.Context, input *AuthenticateInput) (*AuthenticateOutput, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
// FIXME: use mapstructure
"username": input.Username,
// FIXME: if needed encrypt sensitive data
})
tokenString, err := token.SignedString(s.jwtKey)
if err != nil {
return nil, err
}
// FIXME: set "cookie"
return &AuthenticateOutput{
Token: tokenString,
}, nil
}
7 changes: 7 additions & 0 deletions server/api.pub.ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package server

import "context"

func (s *svc) Ping(_ context.Context, _ *Void) (*Void, error) {
return &Void{}, nil
}
16 changes: 16 additions & 0 deletions server/api.pub.tournaments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package server

import (
"context"

"pathwar.pw/entity"
)

func (s *svc) Tournaments(ctx context.Context, _ *Void) (*entity.Tournaments, error) {
var tournaments entity.Tournaments
if err := s.db.Find(&tournaments.Items).Error; err != nil {
return nil, err
}

return &tournaments, nil
}
17 changes: 17 additions & 0 deletions server/api.pub.user-session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package server

import (
"context"

"github.com/pkg/errors"

"pathwar.pw/entity"
)

func (s *svc) UserSession(ctx context.Context, _ *Void) (*entity.UserSession, error) {
sess, err := userSessionFromContext(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to get context session")
}
return &sess, nil
}
Loading

0 comments on commit b0ffecc

Please sign in to comment.