Skip to content

Commit

Permalink
feat: Add language field to blog posts and update dependencies
Browse files Browse the repository at this point in the history
- Add a new field "language" of type Language to the BlogPost object type in internal/web/blog/schema.graphql
- Make necessary changes in internal/web/blog/service/posts.go, including importing packages, updating functions, and updating the Post struct to include the Language field
- Update the .golangci.lint.yml file to disable certain linters and comment out the gosmopolitan linter
- Add the Language field to the Post struct in internal/web/blog/model/posts.go, excluding it from BSON but including it in the JSON response
- Add a package description comment for Auth in library/auth/auth.go
- Update the base image in the Dockerfile to golang:1.21.3-bullseye
- Modify functions in internal/web/blog/controller/blog.go, including adding a new Language function, fixing a typo, and adjusting the expiration time for the token in the BlogLogin function
- Change the generation of user.ActiveToken in internal/web/blog/service/users.go from UUID1 to UUID7
- Update go.sum and go.mod files with package versions changes and additions
  • Loading branch information
Laisky committed Oct 13, 2023
1 parent e7a8901 commit fafcf95
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .golangci.lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ linters:
- goprintffuncname
- gosec
- gosimple
- gosmopolitan
# - gosmopolitan
- govet
- grouper
# - ifshort # deprecated
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21.1-bullseye AS gobuild
FROM golang:1.21.3-bullseye AS gobuild

# install dependencies
RUN apt-get update \
Expand Down
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ require (
cloud.google.com/go/firestore v1.13.0
github.com/99designs/gqlgen v0.17.38
github.com/Laisky/errors/v2 v2.0.1
github.com/Laisky/gin-middlewares/v5 v5.0.2
github.com/Laisky/gin-middlewares/v5 v5.0.3-0.20231007055404-1e0c1ed0f4f5
github.com/Laisky/go-config/v2 v2.0.0
github.com/Laisky/go-utils/v4 v4.5.2
github.com/Laisky/zap v1.25.1
github.com/Laisky/go-utils/v4 v4.5.3-0.20231013025446-3e010e9407e4
github.com/Laisky/zap v1.25.2
github.com/gin-gonic/gin v1.9.1
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/gomarkdown/markdown v0.0.0-20230922112808-5421fefb8386
Expand All @@ -28,6 +28,8 @@ require (
cloud.google.com/go/compute v1.23.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/longrunning v0.5.1 // indirect
github.com/GoWebProd/gip v0.0.0-20230623090727-b60d41d5d320 // indirect
github.com/GoWebProd/uuid7 v0.0.0-20230623091058-5f5954faed6a // indirect
github.com/Laisky/errors v0.9.2-0.20221107084216-4b771a197718 // indirect
github.com/Laisky/go-chaining v0.0.0-20180507092046-43dcdc5a21be // indirect
github.com/Laisky/go-gin-prometheus v1.0.0 // indirect
Expand Down Expand Up @@ -102,7 +104,7 @@ require (
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/automaxprocs v1.5.3 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
Expand Down
20 changes: 12 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ github.com/99designs/gqlgen v0.17.38 h1:3r7G7i8UAdY0iYreNiBAA55auVsrowO0+ZhMl5g4
github.com/99designs/gqlgen v0.17.38/go.mod h1:2v+dKtpI8mIzYeW9dYN8mO69tMmjszW2xKLNcWR/5wQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/GoWebProd/gip v0.0.0-20230623090727-b60d41d5d320 h1:lddR7TsA0fUX8Kh+oc01z4GwmCoBveT79zhNLK43xLk=
github.com/GoWebProd/gip v0.0.0-20230623090727-b60d41d5d320/go.mod h1:eTC6ev1JFq+zoOOS5WKuHdBwtihV/9/Ouv3fZ3ufS0A=
github.com/GoWebProd/uuid7 v0.0.0-20230623091058-5f5954faed6a h1:mThF075zgPGTHRfPcRtaHSIKMDAG6Xd82HNazP5yP4I=
github.com/GoWebProd/uuid7 v0.0.0-20230623091058-5f5954faed6a/go.mod h1:ss8O0hJvHSgtrR6mG2N1LW4gYWwkSX3sVLiS9wA6m/8=
github.com/Laisky/errors v0.9.2-0.20221107084216-4b771a197718 h1:mDhSSwTXJe2HopFLjSLdBtl2NMEvmJlVxuGL1NHG83M=
github.com/Laisky/errors v0.9.2-0.20221107084216-4b771a197718/go.mod h1:4qFjpg18okeh2zj2ZWPmLAAycAXSYm2Q5TbRkjnlUpI=
github.com/Laisky/errors/v2 v2.0.1 h1:yqCBrRzaP012AMB+7fVlXrP34OWRHrSO/hZ38CFdH84=
github.com/Laisky/errors/v2 v2.0.1/go.mod h1:mTn1LHSmKm4CYug0rpYO7rz13dp/DKrtzlSELSrxvT0=
github.com/Laisky/gin-middlewares/v5 v5.0.2 h1:Yijw7QD/CTmMSdTpxDssVBjFAPdlK7v0mFCQJoAgaD4=
github.com/Laisky/gin-middlewares/v5 v5.0.2/go.mod h1:h9wo4FwYIkyjJjx7AMrnXZYNQdfPd1iFJfrrPRpNyDs=
github.com/Laisky/gin-middlewares/v5 v5.0.3-0.20231007055404-1e0c1ed0f4f5 h1:e5Q45chO1+CWU2MOKiyntFdz0IPgoZXE59/aVN9fQQ0=
github.com/Laisky/gin-middlewares/v5 v5.0.3-0.20231007055404-1e0c1ed0f4f5/go.mod h1:sOSA250Pi1VNSYt5yBio635heXnlX9m7JxEEye27wQ0=
github.com/Laisky/go-chaining v0.0.0-20180507092046-43dcdc5a21be h1:7Rxhm6IjOtDAyj8eScOFntevwzkWhx94zi48lxo4m4w=
github.com/Laisky/go-chaining v0.0.0-20180507092046-43dcdc5a21be/go.mod h1:1mdzaETo0kjvCQICPSePsoaatJN4l7JvEA1200lyevo=
github.com/Laisky/go-config/v2 v2.0.0 h1:CgiekTAN+L0HPcKdVbmtxDuYfexWacW21P0q680ZX2k=
Expand All @@ -64,12 +68,12 @@ github.com/Laisky/go-gin-prometheus v1.0.0 h1:eWb9jmUqNL2tz0bwt6vWjJHEpglSHJT6qt
github.com/Laisky/go-gin-prometheus v1.0.0/go.mod h1:3yC0gDd4f79ikEM1eGHLTWSRG0tluOV2mmx/liYLtIA=
github.com/Laisky/go-utils/v3 v3.0.1 h1:TeCk7KX+GFN8LYRhmTFjIj6OL51XVe8BRGy+npcwiig=
github.com/Laisky/go-utils/v3 v3.0.1/go.mod h1:x7hWlGcFgpgAOmv9qzIcfmJ/4o6gvQnsDAHIoyo42Yk=
github.com/Laisky/go-utils/v4 v4.5.2 h1:n0Z55VY3Q1j1vsikFk/LII9mh/Ax1xF4E8BPLF4v4lo=
github.com/Laisky/go-utils/v4 v4.5.2/go.mod h1:KDVXY8Wg4MxfEFP13X8Gr939D88XAT0fhmNvgHNdXUU=
github.com/Laisky/go-utils/v4 v4.5.3-0.20231013025446-3e010e9407e4 h1:Ika8/CTrTfi5BcVu9V24kgWOqLVGuT6mMUcl2Xnm73A=
github.com/Laisky/go-utils/v4 v4.5.3-0.20231013025446-3e010e9407e4/go.mod h1:Z0mnyQ2Rd4fNqrFN/ATGbhnE3BnmkclMinBCn5QcSV4=
github.com/Laisky/graphql v1.0.6 h1:NEULGxfxo+wbsW2OmqBXOMNUGgqo8uFjWNabwuNK10g=
github.com/Laisky/graphql v1.0.6/go.mod h1:zaKVqXmMQTnTkFJ2AA53oyBWMzlGCnzr3aodKTrtOxI=
github.com/Laisky/zap v1.25.1 h1:55ng3Am2GA7I5yTkDpPoonP6iTnYcEWrEmdB5iGtkbM=
github.com/Laisky/zap v1.25.1/go.mod h1:2Yd8KsMXqCw0GmGQMyplX5NnJa5Wk4pLKItW9eGk/b0=
github.com/Laisky/zap v1.25.2 h1:GFWM4mGpICpiVgsVGsVrZS+0ocko6DrCYASxdAUgkl4=
github.com/Laisky/zap v1.25.2/go.mod h1:6lEGuHnCEmRLqbyMHhU1AGUah1dV4lrhPIM8c05Uo2w=
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/agnivade/levenshtein v1.1.1 h1:QY8M92nrzkmr798gCo3kmMyqXFzdQVpxLlGPRBij0P8=
Expand Down Expand Up @@ -454,8 +458,8 @@ go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8=
go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
Expand Down
9 changes: 8 additions & 1 deletion internal/web/blog/controller/blog.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ func (r *PostResolver) Type(ctx context.Context, obj *model.Post) (models.BlogPo

return "", errors.Errorf("unknown blog post type: `%+v`", obj.Type)
}
func (r *PostResolver) Language(ctx context.Context, obj *model.Post) (models.Language, error) {
if gutils.Contains(models.AllLanguage, models.Language(obj.Language)) {
return models.Language(obj.Language), nil
}

return "", errors.Errorf("unknown language: %q", obj.Language)
}

func (r *PostSeriesResolver) Posts(ctx context.Context, obj *model.PostSeries) (posts []*model.Post, err error) {
se, err := r.svc.LoadPostSeries(ctx, obj.ID, "")
Expand Down Expand Up @@ -330,7 +337,7 @@ func (r *MutationResolver) BlogLogin(ctx context.Context,

uc := &jwt.UserClaims{
RegisteredClaims: jwtLib.RegisteredClaims{
ID: gutils.UUID1(),
ID: gutils.UUID7(),
Subject: user.ID.Hex(),
Issuer: "laisky-sso",
IssuedAt: &jwtLib.NumericDate{
Expand Down
2 changes: 2 additions & 0 deletions internal/web/blog/model/posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type Post struct {
Hidden bool `bson:"hidden" json:"hidden"`
// I18N internationalization of the post
I18N PostI18N `bson:"i18n" json:"i18n"`
// Language language of the post content or markdown
Language string `bson:"-" json:"language"`
}

// PostI18N blog post internationalization
Expand Down
1 change: 1 addition & 0 deletions internal/web/blog/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type BlogPost {
markdown: String
tags: [String!]!
category: BlogCategory
language: Language!
}

type BlogUser {
Expand Down
35 changes: 25 additions & 10 deletions internal/web/blog/service/posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/Laisky/errors/v2"
gmw "github.com/Laisky/gin-middlewares/v5"
gconfig "github.com/Laisky/go-config/v2"
gutils "github.com/Laisky/go-utils/v4"
glog "github.com/Laisky/go-utils/v4/log"
Expand All @@ -24,7 +25,6 @@ import (
"github.com/Laisky/laisky-blog-graphql/library/auth"
mongoSDK "github.com/Laisky/laisky-blog-graphql/library/db/mongo"
"github.com/Laisky/laisky-blog-graphql/library/jwt"
"github.com/Laisky/laisky-blog-graphql/library/log"
)

// Blog blog service
Expand Down Expand Up @@ -79,7 +79,7 @@ func (s *Blog) LoadPostSeries(ctx context.Context, id primitive.ObjectID, key st
if err != nil {
return nil, errors.Wrap(err, "find series")
}
defer cur.Close(ctx)
defer cur.Close(ctx) //nolint:errcheck

if err = cur.All(ctx, &se); err != nil {
return nil, errors.Wrap(err, "load series")
Expand Down Expand Up @@ -199,7 +199,7 @@ func (s *Blog) filterPosts(ctx context.Context, cfg *dto.PostCfg, iter *mongo.Cu
passwordFilter,
hiddenFilter,
getContentLengthFilter(cfg.Length),
getI18NFilter(cfg.Language),
s.getI18NFilter(ctx, cfg.Language),
defaultTypeFilter,
} {
if !f(post) {
Expand Down Expand Up @@ -227,18 +227,33 @@ func defaultTypeFilter(docu *model.Post) bool {
return true
}

// i18NFilter
func getI18NFilter(language models.Language) func(*model.Post) bool {
// getI18NFilter replace content with specified language
func (s *Blog) getI18NFilter(ctx context.Context, language models.Language) func(*model.Post) bool {
return func(p *model.Post) bool {
logger := gmw.GetLogger(ctx).
With(zap.String("language", language.String()), zap.String("post", p.ID.String()))
switch language {
case models.LanguageEnUs:
log.Logger.Debug("getI18NFilter", zap.String("language", language.String()))
if p.I18N.EnUs.PostContent != "" {
p.Content = p.I18N.EnUs.PostMarkdown
if p.I18N.EnUs.PostMarkdown != "" {
p.Language = models.LanguageEnUs.String()
p.Markdown = p.I18N.EnUs.PostMarkdown

if p.I18N.EnUs.PostContent == "" {
p.I18N.EnUs.PostContent = ParseMarkdown2HTML([]byte(p.Markdown))
p.Content = p.I18N.EnUs.PostContent

// update post i18n content
if _, err := s.dao.GetPostsCol().UpdateByID(ctx, p.ID, bson.M{
"$set": bson.M{
"i18n.en_us.post_content": p.I18N.EnUs.PostContent,
},
}); err != nil {
logger.Error("try to update post got error", zap.Error(err))
}
}
}
default:
log.Logger.Debug("getI18NFilter", zap.String("language", language.String()))
return true
p.Language = models.LanguageZhCn.String()
}

return true
Expand Down
2 changes: 1 addition & 1 deletion internal/web/blog/service/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (s *Blog) UserRegister(ctx context.Context,
return nil, errors.Wrapf(err, "generate password hash for %q", account)
}
user.Password = pwd
user.ActiveToken = gutils.UUID1()
user.ActiveToken = gutils.UUID7()

// insert new user
if _, err = col.InsertOne(ctx, user); err != nil {
Expand Down
97 changes: 97 additions & 0 deletions internal/web/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions library/auth/auth.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// package Auth provides JWT
package auth

import (
Expand Down

0 comments on commit fafcf95

Please sign in to comment.