Skip to content

Commit

Permalink
feat: update go-usip demo2 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
zsq1234 authored Oct 8, 2024
1 parent 1cfa0b7 commit 072030f
Show file tree
Hide file tree
Showing 18 changed files with 833 additions and 40 deletions.
10 changes: 7 additions & 3 deletions go-usip/demo2/configs/config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
host: http://localhost:8080

database:
driver: postgresql
dsn: user=postgres password=postgres dbname=demo host=127.0.0.1 port=5432 sslmode=disable TimeZone=Asia/Shanghai
Expand All @@ -6,8 +8,10 @@ redis:
addr: 127.0.0.1:6379

universer:
host: http://127.0.0.1:8000
host: http://localhost:8000

univer:
docHost: http://127.0.0.1:3010/doc
sheetHost: http://127.0.0.1:3010
docHost: http://localhost:3010/doc
sheetHost: http://localhost:3010

font: web/public/font/SourceHanSansCN-VF.ttf
10 changes: 8 additions & 2 deletions go-usip/demo2/datamodels/file_collaborator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ const (
RoleReader Role = "reader"
)

var RoleLever = map[Role]int{
RoleOwner: 3,
RoleEditor: 2,
RoleReader: 1,
}

type FileCollaborator struct {
ID int64 `json:"id" gorm:"primary_key"`
UserId string `json:"user_id"`
FileId uint `json:"file_id"`
UserId string `json:"user_id" gorm:"uniqueIndex:uqe_file_id_user_id,piroity:2"`
FileId uint `json:"file_id" gorm:"uniqueIndex:uqe_file_id_user_id,piroity:1;index"`
Role Role `json:"role"`
}
4 changes: 2 additions & 2 deletions go-usip/demo2/datamodels/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
// of the example, we will use this datamodel
// as the only one User model in our application.
type User struct {
gorm.Model
gorm.Model `json:"-" form:"-"`
UserId string `json:"user_id" form:"user_id" gorm:"unique"`
Nickname string `json:"nickname" form:"nickname"`
Username string `json:"username" form:"username" gorm:"unique"`
Username string `json:"-" form:"-" gorm:"unique"`
HashedPassword []byte `json:"-" form:"-"`
}

Expand Down
8 changes: 5 additions & 3 deletions go-usip/demo2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21.3

require (
github.com/go-resty/resty/v2 v2.15.2
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/google/uuid v1.6.0
github.com/kataras/iris/v12 v12.2.0
github.com/spf13/viper v1.18.2
Expand All @@ -21,7 +22,7 @@ require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 // indirect
github.com/fatih/structs v1.1.0 // indirect
Expand Down Expand Up @@ -84,10 +85,11 @@ require (
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/image v0.20.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/time v0.6.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/protobuf v1.33.0 // indirect
Expand Down
16 changes: 10 additions & 6 deletions go-usip/demo2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuP
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927/go.mod h1:h/aW8ynjgkuj+NQRlZcDbAbM1ORAbXjXX77sX7T289U=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -50,6 +50,8 @@ github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
github.com/gobwas/ws v1.1.0 h1:7RFti/xnNkMJnrK7D1yQ/iCIB5OrrY/54/H930kIbHA=
github.com/gobwas/ws v1.1.0/go.mod h1:nzvNcVha5eUziGrbxFCo6qFIojQHjJV5cLYIbezhfL0=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
Expand Down Expand Up @@ -220,6 +222,8 @@ golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/image v0.20.0 h1:7cVCUjQwfL18gyBJOmYvptfSHS8Fb3YUDtfLIZ7Nbpw=
golang.org/x/image v0.20.0/go.mod h1:0a88To4CYVBAHp5FXJm8o7QbUl37Vd85ply1vyD8auM=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/net v0.0.0-20190327091125-710a502c58a2/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand All @@ -229,8 +233,8 @@ golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
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-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -246,8 +250,8 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
3 changes: 2 additions & 1 deletion go-usip/demo2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ func main() {
fileRepo := repositories.NewFileRepository(db)
fileCollaRepo := repositories.NewFileCollaboratorRepository(db)

userService := services.NewUserService(userRepo)
avatarService := services.NewAvatarService()
userService := services.NewUserService(userRepo, avatarService)
universerService := services.NewUniverseService()
fileService := services.NewFileService(fileRepo, fileCollaRepo, universerService)

Expand Down
25 changes: 25 additions & 0 deletions go-usip/demo2/repositories/file_collaborator_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import (
"log"

"gorm.io/gorm"
"gorm.io/gorm/clause"
)

type FileCollaboratorRepository interface {
Get(fileId uint, userId string) (datamodels.FileCollaborator, bool)
GetByUserId(userId string) ([]datamodels.FileCollaborator, bool)
GetByFileId(fileId uint) ([]datamodels.FileCollaborator, bool)

Create(fileCollaborator datamodels.FileCollaborator) (datamodels.FileCollaborator, error)
InsertOrUpdate(fileCollaborators []datamodels.FileCollaborator) error

BatchDelete(userId string, fileIds []uint) error
}

func NewFileCollaboratorRepository(db *gorm.DB) FileCollaboratorRepository {
Expand All @@ -26,6 +31,15 @@ type fileCollaboratorRepository struct {
db *gorm.DB
}

func (r *fileCollaboratorRepository) Get(fileId uint, userId string) (datamodels.FileCollaborator, bool) {
var fileCollaborator datamodels.FileCollaborator
if err := r.db.Where("file_id = ? AND user_id = ?", fileId, userId).First(&fileCollaborator).Error; err != nil {
log.Printf("Error while getting collaborator: %v", err)
return fileCollaborator, false
}
return fileCollaborator, true
}

func (r *fileCollaboratorRepository) GetByUserId(userId string) ([]datamodels.FileCollaborator, bool) {
var fileCollaborators []datamodels.FileCollaborator
if err := r.db.Where("user_id = ?", userId).Find(&fileCollaborators).Error; err != nil {
Expand All @@ -47,3 +61,14 @@ func (r *fileCollaboratorRepository) GetByFileId(fileId uint) ([]datamodels.File
func (r *fileCollaboratorRepository) Create(fileCollaborator datamodels.FileCollaborator) (datamodels.FileCollaborator, error) {
return fileCollaborator, r.db.Create(&fileCollaborator).Error
}

func (r *fileCollaboratorRepository) InsertOrUpdate(fileCollaborators []datamodels.FileCollaborator) error {
return r.db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "file_id"}, {Name: "user_id"}},
DoUpdates: clause.AssignmentColumns([]string{"role"}),
}).Create(&fileCollaborators).Error
}

func (r *fileCollaboratorRepository) BatchDelete(userId string, fileIds []uint) error {
return r.db.Where("user_id = ? AND file_id IN ?", userId, fileIds).Delete(&datamodels.FileCollaborator{}).Error
}
6 changes: 6 additions & 0 deletions go-usip/demo2/repositories/file_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type FileRepository interface {
BatchGet(ids []uint) (files []datamodels.File, found bool)

Create(file datamodels.File) (datamodels.File, error)

BatchDelete(ids []uint) error
}

func NewFileRepository(db *gorm.DB) FileRepository {
Expand Down Expand Up @@ -56,3 +58,7 @@ func (r *fileRepository) BatchGet(ids []uint) (files []datamodels.File, found bo
func (r *fileRepository) Create(file datamodels.File) (datamodels.File, error) {
return file, r.db.Create(&file).Error
}

func (r *fileRepository) BatchDelete(ids []uint) error {
return r.db.Where("id IN ?", ids).Delete(&datamodels.File{}).Error
}
10 changes: 10 additions & 0 deletions go-usip/demo2/repositories/user_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type UserRepository interface {
Get(userId string) (user datamodels.User, found bool)
BatchGet(userIds []string) (users []datamodels.User, found bool)
GetByUsername(username string) (user datamodels.User, found bool)
GetByPage(nextId, size uint) ([]datamodels.User, bool)

InsertOrUpdate(user datamodels.User) (updatedUser datamodels.User, err error)
Delete(userId string) (deleted bool)
Expand Down Expand Up @@ -59,6 +60,15 @@ func (r *userRepository) GetByUsername(username string) (user datamodels.User, f
return user, true
}

func (r *userRepository) GetByPage(nextId, size uint) ([]datamodels.User, bool) {
users := []datamodels.User{}
if err := r.db.Where("id > ?", nextId).Order("id").Limit(int(size)).Find(&users).Error; err != nil {
log.Printf("Error while getting users by page: %v", err)
return users, false
}
return users, true
}

func (r *userRepository) InsertOrUpdate(user datamodels.User) (datamodels.User, error) {
if user.ID > 0 {
return user, r.db.Save(&user).Error
Expand Down
98 changes: 98 additions & 0 deletions go-usip/demo2/services/avatar_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package services

import (
"image"
"image/color"
"image/draw"
"log"
"os"

"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"github.com/spf13/viper"
)

const (
fontSize = 400
imageW = 640
imageH = 480
)

type AvatarService interface {
GenerateAvatar(word string) (image.Image, error)
}

type avatarService struct {
font *truetype.Font
}

func NewAvatarService() AvatarService {
// Read the font data.
fontBytes, err := os.ReadFile(viper.GetString("font"))
if err != nil {
panic(err)
}

font, err := freetype.ParseFont(fontBytes)
if err != nil {
panic(err)
}

return &avatarService{
font: font,
}
}

func randomColor(k int16) color.Color {
loc := uint8(k & 0x03)
switch loc {
case 0:
return color.RGBA{
R: uint8(k & 0xff),
G: 0,
B: 0,
A: 255,
}
case 1:
return color.RGBA{
R: 0,
G: uint8(k & 0xff),
B: 0,
A: 255,
}
default:
return color.RGBA{
R: 0,
G: 0,
B: uint8(k & 0xff),
A: 255,
}
}
}

func (s *avatarService) GenerateAvatar(word string) (image.Image, error) {
wordB := []byte(word)
rand := int16(wordB[0])
if len(wordB) > 1 {
rand += int16(wordB[1])
}
m := image.NewRGBA(image.Rect(0, 0, imageW, imageH))
draw.Draw(m, m.Bounds(), &image.Uniform{randomColor(rand)}, image.Point{}, draw.Src)

c := freetype.NewContext()
c.SetDPI(72)
c.SetFont(s.font)
c.SetFontSize(fontSize)
c.SetClip(m.Bounds())
c.SetDst(m)
c.SetSrc(image.White)

pt := freetype.Pt(int(c.PointToFixed((imageW-fontSize)/2)>>6)+10, int(c.PointToFixed(fontSize)>>6)-20)
_, err := c.DrawString(word, pt)
if err != nil {
log.Printf("GenerateAvatar error: %v", err)
return nil, err
}

return m, nil
}
Loading

0 comments on commit 072030f

Please sign in to comment.