From f656b027a3c769545301ab30ba97772c63198481 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Fri, 11 Aug 2023 10:37:35 +0800 Subject: [PATCH 1/6] add visit user commment/media tweets support --- CHANGELOG.md | 32 ++++++++++ docker-compose.yaml | 2 +- go.mod | 4 +- go.sum | 8 +-- internal/conf/db.go | 2 + internal/conf/setting.go | 2 + internal/core/tweets.go | 2 + internal/dao/jinzhu/dbr/post.go | 4 ++ internal/dao/jinzhu/gorm.go | 4 ++ internal/dao/jinzhu/tweets.go | 33 +++++++++++ internal/servants/web/loose.go | 58 +++++++++---------- .../0009_create_view_post_filter.down.sql | 5 ++ .../mysql/0009_create_view_post_filter.up.sql | 30 ++++++++++ .../0008_create_view_post_filter.down.sql | 5 ++ .../0008_create_view_post_filter.up.sql | 30 ++++++++++ .../0009_create_view_post_filter.down.sql | 5 ++ .../0009_create_view_post_filter.up.sql | 30 ++++++++++ scripts/paopao-mysql.sql | 33 +++++++++++ scripts/paopao-postgres.sql | 33 +++++++++++ scripts/paopao-sqlite3.sql | 33 +++++++++++ 20 files changed, 319 insertions(+), 36 deletions(-) create mode 100644 scripts/migration/mysql/0009_create_view_post_filter.down.sql create mode 100644 scripts/migration/mysql/0009_create_view_post_filter.up.sql create mode 100644 scripts/migration/postgres/0008_create_view_post_filter.down.sql create mode 100644 scripts/migration/postgres/0008_create_view_post_filter.up.sql create mode 100644 scripts/migration/sqlite3/0009_create_view_post_filter.down.sql create mode 100644 scripts/migration/sqlite3/0009_create_view_post_filter.up.sql diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e917b31..65affa16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,38 @@ All notable changes to paopao-ce are documented in this file. - use compiler profile-guided optimization (PGO) to further optimize builds. [#327](https://github.com/rocboss/paopao-ce/pull/327) - frontend: re-add stars page embed to profile page. [#339](https://github.com/rocboss/paopao-ce/pull/339) - simple support for user posts filter by style(post/comment/media/star). [#345](https://github.com/rocboss/paopao-ce/pull/345) + mirgration database first(sql ddl file in `scripts/migration/**/*_create_view_post_filter.up.sql`): + ```sql + CREATE VIEW p_post_by_media AS + SELECT post.* + FROM + ( SELECT DISTINCT post_id FROM p_post_content WHERE ( TYPE = 3 OR TYPE = 4 OR TYPE = 7 OR TYPE = 8 ) AND is_del = 0 ) media + JOIN p_post post ON media.post_id = post.ID + WHERE + post.is_del = 0; + + CREATE VIEW p_post_by_comment AS + SELECT P.* + FROM + ( + SELECT + post_id + FROM + p_comment + WHERE + is_del = 0 UNION + SELECT + post_id + FROM + p_comment_reply reply + JOIN p_comment COMMENT ON reply.comment_id = COMMENT.ID + WHERE + reply.is_del = 0 AND COMMENT.is_del = 0 + ) + C JOIN p_post P ON C.post_id = P.ID + WHERE + P.is_del = 0; + ``` - add user highlight tweet support include custom tweet set to highlight and list in user/profile page. ### Changed diff --git a/docker-compose.yaml b/docker-compose.yaml index 45bb0d82..3aafafc9 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -102,7 +102,7 @@ services: - paopao-network backend: - image: bitbus/paopao-ce:0.3 + image: bitbus/paopao-ce:0.4 restart: always depends_on: - db diff --git a/go.mod b/go.mod index f73ef15c..cd9faef5 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,7 @@ require ( github.com/minio/minio-go/v7 v7.0.61 github.com/onsi/ginkgo/v2 v2.11.0 github.com/onsi/gomega v1.27.10 - github.com/pyroscope-io/client v0.7.1 + github.com/pyroscope-io/client v0.7.2-0.20230804044655-36760e422a95 github.com/redis/rueidis v1.0.14 github.com/sirupsen/logrus v1.9.3 github.com/smartwalle/alipay/v3 v3.2.15 @@ -102,7 +102,7 @@ require ( github.com/mozillazg/go-httpheader v0.2.1 // indirect github.com/pelletier/go-toml/v2 v2.0.8 // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/pyroscope-io/godeltaprof v0.1.0 // indirect + github.com/pyroscope-io/godeltaprof v0.1.2 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/rogpeppe/go-internal v1.9.0 // indirect github.com/rs/xid v1.5.0 // indirect diff --git a/go.sum b/go.sum index 3cb171d4..7e65b4f2 100644 --- a/go.sum +++ b/go.sum @@ -1105,10 +1105,10 @@ github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= -github.com/pyroscope-io/client v0.7.1 h1:yFRhj3vbgjBxehvxQmedmUWJQ4CAfCHhn+itPsuWsHw= -github.com/pyroscope-io/client v0.7.1/go.mod h1:4h21iOU4pUOq0prKyDlvYRL+SCKsBc5wKiEtV+rJGqU= -github.com/pyroscope-io/godeltaprof v0.1.0 h1:UBqtjt0yZi4jTxqZmLAs34XG6ycS3vUTlhEUSq4NHLE= -github.com/pyroscope-io/godeltaprof v0.1.0/go.mod h1:psMITXp90+8pFenXkKIpNhrfmI9saQnPbba27VIaiQE= +github.com/pyroscope-io/client v0.7.2-0.20230804044655-36760e422a95 h1:ZpE0l+tD3ykwYWQh/UVdIpxHmr9B6DTtOUBiGw3lB2E= +github.com/pyroscope-io/client v0.7.2-0.20230804044655-36760e422a95/go.mod h1:FEocnjn+Ngzxy6EtU9ZxXWRvQ0+pffkrBxHLnPpxwi8= +github.com/pyroscope-io/godeltaprof v0.1.2 h1:MdlEmYELd5w+lvIzmZvXGNMVzW2Qc9jDMuJaPOR75g4= +github.com/pyroscope-io/godeltaprof v0.1.2/go.mod h1:psMITXp90+8pFenXkKIpNhrfmI9saQnPbba27VIaiQE= github.com/redis/rueidis v1.0.14 h1:qdFZahk1F/2L+sZeOECx5E2N5J4Qc51b7ezSUpQXJfs= github.com/redis/rueidis v1.0.14/go.mod h1:8B+r5wdnjwK3lTFml5VtxjzGOQAC+5UmujoD12pDrEo= github.com/remyoudompheng/bigfft v0.0.0-20190728182440-6a916e37a237/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= diff --git a/internal/conf/db.go b/internal/conf/db.go index f04fc3fb..1fa2a0ea 100644 --- a/internal/conf/db.go +++ b/internal/conf/db.go @@ -29,6 +29,8 @@ const ( TableContactGroup = "contact_group" TableMessage = "message" TablePost = "post" + TablePostByComment = "post_by_comment" + TablePostByMedia = "post_by_media" TablePostAttachmentBill = "post_attachment_bill" TablePostCollection = "post_collection" TablePostContent = "post_content" diff --git a/internal/conf/setting.go b/internal/conf/setting.go index d7c7223b..8efff37a 100644 --- a/internal/conf/setting.go +++ b/internal/conf/setting.go @@ -309,6 +309,8 @@ func (s *databaseConf) TableNames() (res TableNameMap) { TableContactGroup, TableMessage, TablePost, + TablePostByComment, + TablePostByMedia, TablePostAttachmentBill, TablePostCollection, TablePostContent, diff --git a/internal/core/tweets.go b/internal/core/tweets.go index 9cb2e54a..b5aee860 100644 --- a/internal/core/tweets.go +++ b/internal/core/tweets.go @@ -24,6 +24,8 @@ type TweetService interface { GetPostContentsByIDs(ids []int64) ([]*ms.PostContent, error) GetPostContentByID(id int64) (*ms.PostContent, error) ListUserStarTweets(user *cs.VistUser, limit int, offset int) ([]*ms.PostStar, int64, error) + ListUserMediaTweets(user *cs.VistUser, limit int, offset int) ([]*ms.Post, int64, error) + ListUserCommentTweets(user *cs.VistUser, limit int, offset int) ([]*ms.Post, int64, error) } // TweetManageService 推文管理服务,包括创建/删除/更新推文 diff --git a/internal/dao/jinzhu/dbr/post.go b/internal/dao/jinzhu/dbr/post.go index be4582c6..f3d3e824 100644 --- a/internal/dao/jinzhu/dbr/post.go +++ b/internal/dao/jinzhu/dbr/post.go @@ -21,6 +21,10 @@ const ( PostVisitInvalid ) +type PostByMedia = Post + +type PostByComment = Post + type Post struct { *Model UserID int64 `json:"user_id"` diff --git a/internal/dao/jinzhu/gorm.go b/internal/dao/jinzhu/gorm.go index 8cd44639..c160c579 100644 --- a/internal/dao/jinzhu/gorm.go +++ b/internal/dao/jinzhu/gorm.go @@ -21,6 +21,8 @@ var ( _contactGroup_ string _message_ string _post_ string + _post_by_comment_ string + _post_by_media_ string _postAttachmentBill_ string _postCollection_ string _postContent_ string @@ -44,6 +46,8 @@ func initTableName() { _contactGroup_ = m[conf.TableContactGroup] _message_ = m[conf.TableMessage] _post_ = m[conf.TablePost] + _post_by_comment_ = m[conf.TablePostByComment] + _post_by_media_ = m[conf.TablePostByMedia] _postAttachmentBill_ = m[conf.TablePostAttachmentBill] _postCollection_ = m[conf.TablePostCollection] _postContent_ = m[conf.TablePostContent] diff --git a/internal/dao/jinzhu/tweets.go b/internal/dao/jinzhu/tweets.go index 0e517b16..29adb165 100644 --- a/internal/dao/jinzhu/tweets.go +++ b/internal/dao/jinzhu/tweets.go @@ -426,6 +426,39 @@ func (s *tweetSrv) ListUserStarTweets(user *cs.VistUser, limit int, offset int) return } +func (s *tweetSrv) getUserTweets(db *gorm.DB, user *cs.VistUser, limit int, offset int) (res []*ms.Post, total int64, err error) { + visibilities := []core.PostVisibleT{core.PostVisitPublic} + switch user.RelTyp { + case cs.RelationAdmin, cs.RelationSelf: + visibilities = append(visibilities, core.PostVisitPrivate, core.PostVisitFriend) + case cs.RelationFriend: + visibilities = append(visibilities, core.PostVisitFriend) + case cs.RelationGuest: + fallthrough + default: + // nothing + } + db = db.Where("user_id=? AND visibility IN ? AND is_del=0", user.UserId, visibilities).Order("latest_replied_on DESC") + if offset >= 0 && limit > 0 { + db = db.Offset(offset).Limit(limit) + } + if err = db.Find(&res).Error; err != nil { + return + } + err = db.Count(&total).Error + return +} + +func (s *tweetSrv) ListUserMediaTweets(user *cs.VistUser, limit int, offset int) ([]*ms.Post, int64, error) { + db := s.db.Table(_post_by_media_) + return s.getUserTweets(db, user, limit, offset) +} + +func (s *tweetSrv) ListUserCommentTweets(user *cs.VistUser, limit int, offset int) ([]*ms.Post, int64, error) { + db := s.db.Table(_post_by_comment_) + return s.getUserTweets(db, user, limit, offset) +} + func (s *tweetSrv) GetUserPostStarCount(userID int64) (int64, error) { star := &dbr.PostStar{ UserID: userID, diff --git a/internal/servants/web/loose.go b/internal/servants/web/loose.go index 9c2877e7..236964e8 100644 --- a/internal/servants/web/loose.go +++ b/internal/servants/web/loose.go @@ -66,14 +66,11 @@ func (s *looseSrv) GetUserTweets(req *web.GetUserTweetsReq) (res *web.GetUserTwe if xerr != nil { return nil, err } - switch req.Style { - case web.UserPostsStyleComment: - res, err = s.getUserCommentTweets(req, user) + case web.UserPostsStyleComment, web.UserPostsStyleMedia: + res, err = s.listUserTweets(req, user) case web.UserPostsStyleHighlight: res, err = s.getUserPostTweets(req, user, true) - case web.UserPostsStyleMedia: - res, err = s.getUserMediaTweets(req, user) case web.UserPostsStyleStar: res, err = s.getUserStarTweets(req, user) case web.UserPostsStylePost: @@ -84,30 +81,6 @@ func (s *looseSrv) GetUserTweets(req *web.GetUserTweetsReq) (res *web.GetUserTwe return } -func (s *looseSrv) getUserCommentTweets(req *web.GetUserTweetsReq, user *cs.VistUser) (*web.GetUserTweetsResp, mir.Error) { - // TODO: add implement logic - resp := base.PageRespFrom(nil, req.Page, req.PageSize, 0) - return (*web.GetUserTweetsResp)(resp), nil -} - -func (s *looseSrv) getUserHighlightTweets(req *web.GetUserTweetsReq, user *cs.VistUser) (*web.GetUserTweetsResp, mir.Error) { - // TODO: add implement logic - resp := base.PageRespFrom(nil, req.Page, req.PageSize, 0) - return (*web.GetUserTweetsResp)(resp), nil -} - -func (s *looseSrv) getUserMediaTweets(req *web.GetUserTweetsReq, user *cs.VistUser) (*web.GetUserTweetsResp, mir.Error) { - // TODO: add implement logic - resp := base.PageRespFrom(nil, req.Page, req.PageSize, 0) - return (*web.GetUserTweetsResp)(resp), nil -} - -func (s *looseSrv) getSelfMediaTweets(req *web.GetUserTweetsReq) (*web.GetUserTweetsResp, mir.Error) { - // TODO: add implement logic - resp := base.PageRespFrom(nil, req.Page, req.PageSize, 0) - return (*web.GetUserTweetsResp)(resp), nil -} - func (s *looseSrv) getUserStarTweets(req *web.GetUserTweetsReq, user *cs.VistUser) (*web.GetUserTweetsResp, mir.Error) { stars, totalRows, err := s.Ds.ListUserStarTweets(user, req.PageSize, (req.Page-1)*req.PageSize) if err != nil { @@ -129,6 +102,33 @@ func (s *looseSrv) getUserStarTweets(req *web.GetUserTweetsReq, user *cs.VistUse return (*web.GetUserTweetsResp)(resp), nil } +func (s *looseSrv) listUserTweets(req *web.GetUserTweetsReq, user *cs.VistUser) (*web.GetUserTweetsResp, mir.Error) { + var ( + tweets []*ms.Post + total int64 + err error + ) + if req.Style == web.UserPostsStyleComment { + tweets, total, err = s.Ds.ListUserCommentTweets(user, req.PageSize, (req.Page-1)*req.PageSize) + } else if req.Style == web.UserPostsStyleMedia { + tweets, total, err = s.Ds.ListUserMediaTweets(user, req.PageSize, (req.Page-1)*req.PageSize) + } else { + logrus.Errorf("s.listUserTweets unknow style: %s", req.Style) + return nil, web.ErrGetPostsFailed + } + if err != nil { + logrus.Errorf("s.listUserTweets err: %s", err) + return nil, web.ErrGetPostsFailed + } + postFormated, err := s.Ds.MergePosts(tweets) + if err != nil { + logrus.Errorf("s.listUserTweets err: %s", err) + return nil, web.ErrGetPostsFailed + } + resp := base.PageRespFrom(postFormated, req.Page, req.PageSize, total) + return (*web.GetUserTweetsResp)(resp), nil +} + func (s *looseSrv) getUserPostTweets(req *web.GetUserTweetsReq, user *cs.VistUser, isHighlight bool) (*web.GetUserTweetsResp, mir.Error) { visibilities := []core.PostVisibleT{core.PostVisitPublic} switch user.RelTyp { diff --git a/scripts/migration/mysql/0009_create_view_post_filter.down.sql b/scripts/migration/mysql/0009_create_view_post_filter.down.sql new file mode 100644 index 00000000..b1550d2e --- /dev/null +++ b/scripts/migration/mysql/0009_create_view_post_filter.down.sql @@ -0,0 +1,5 @@ +DROP VIEW IF EXISTS p_post_by_media; +DROP VIEW IF EXISTS p_post_by_comment; + + + diff --git a/scripts/migration/mysql/0009_create_view_post_filter.up.sql b/scripts/migration/mysql/0009_create_view_post_filter.up.sql new file mode 100644 index 00000000..af3e1c43 --- /dev/null +++ b/scripts/migration/mysql/0009_create_view_post_filter.up.sql @@ -0,0 +1,30 @@ +CREATE VIEW p_post_by_media AS +SELECT post.* +FROM + ( SELECT DISTINCT post_id FROM p_post_content WHERE ( TYPE = 3 OR TYPE = 4 OR TYPE = 7 OR TYPE = 8 ) AND is_del = 0 ) media + JOIN p_post post ON media.post_id = post.ID +WHERE + post.is_del = 0; + +CREATE VIEW p_post_by_comment AS +SELECT P.* +FROM + ( + SELECT + post_id + FROM + p_comment + WHERE + is_del = 0 UNION + SELECT + post_id + FROM + p_comment_reply reply + JOIN p_comment COMMENT ON reply.comment_id = COMMENT.ID + WHERE + reply.is_del = 0 + AND COMMENT.is_del = 0 + ) + C JOIN p_post P ON C.post_id = P.ID +WHERE + P.is_del = 0; diff --git a/scripts/migration/postgres/0008_create_view_post_filter.down.sql b/scripts/migration/postgres/0008_create_view_post_filter.down.sql new file mode 100644 index 00000000..b1550d2e --- /dev/null +++ b/scripts/migration/postgres/0008_create_view_post_filter.down.sql @@ -0,0 +1,5 @@ +DROP VIEW IF EXISTS p_post_by_media; +DROP VIEW IF EXISTS p_post_by_comment; + + + diff --git a/scripts/migration/postgres/0008_create_view_post_filter.up.sql b/scripts/migration/postgres/0008_create_view_post_filter.up.sql new file mode 100644 index 00000000..af3e1c43 --- /dev/null +++ b/scripts/migration/postgres/0008_create_view_post_filter.up.sql @@ -0,0 +1,30 @@ +CREATE VIEW p_post_by_media AS +SELECT post.* +FROM + ( SELECT DISTINCT post_id FROM p_post_content WHERE ( TYPE = 3 OR TYPE = 4 OR TYPE = 7 OR TYPE = 8 ) AND is_del = 0 ) media + JOIN p_post post ON media.post_id = post.ID +WHERE + post.is_del = 0; + +CREATE VIEW p_post_by_comment AS +SELECT P.* +FROM + ( + SELECT + post_id + FROM + p_comment + WHERE + is_del = 0 UNION + SELECT + post_id + FROM + p_comment_reply reply + JOIN p_comment COMMENT ON reply.comment_id = COMMENT.ID + WHERE + reply.is_del = 0 + AND COMMENT.is_del = 0 + ) + C JOIN p_post P ON C.post_id = P.ID +WHERE + P.is_del = 0; diff --git a/scripts/migration/sqlite3/0009_create_view_post_filter.down.sql b/scripts/migration/sqlite3/0009_create_view_post_filter.down.sql new file mode 100644 index 00000000..b1550d2e --- /dev/null +++ b/scripts/migration/sqlite3/0009_create_view_post_filter.down.sql @@ -0,0 +1,5 @@ +DROP VIEW IF EXISTS p_post_by_media; +DROP VIEW IF EXISTS p_post_by_comment; + + + diff --git a/scripts/migration/sqlite3/0009_create_view_post_filter.up.sql b/scripts/migration/sqlite3/0009_create_view_post_filter.up.sql new file mode 100644 index 00000000..af3e1c43 --- /dev/null +++ b/scripts/migration/sqlite3/0009_create_view_post_filter.up.sql @@ -0,0 +1,30 @@ +CREATE VIEW p_post_by_media AS +SELECT post.* +FROM + ( SELECT DISTINCT post_id FROM p_post_content WHERE ( TYPE = 3 OR TYPE = 4 OR TYPE = 7 OR TYPE = 8 ) AND is_del = 0 ) media + JOIN p_post post ON media.post_id = post.ID +WHERE + post.is_del = 0; + +CREATE VIEW p_post_by_comment AS +SELECT P.* +FROM + ( + SELECT + post_id + FROM + p_comment + WHERE + is_del = 0 UNION + SELECT + post_id + FROM + p_comment_reply reply + JOIN p_comment COMMENT ON reply.comment_id = COMMENT.ID + WHERE + reply.is_del = 0 + AND COMMENT.is_del = 0 + ) + C JOIN p_post P ON C.post_id = P.ID +WHERE + P.is_del = 0; diff --git a/scripts/paopao-mysql.sql b/scripts/paopao-mysql.sql index 419cc53e..90a36add 100644 --- a/scripts/paopao-mysql.sql +++ b/scripts/paopao-mysql.sql @@ -395,4 +395,37 @@ CREATE TABLE `p_wallet_statement` ( KEY `idx_wallet_statement_user_id` (`user_id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=10010 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='钱包流水'; +DROP VIEW IF EXISTS p_post_by_media; +CREATE VIEW p_post_by_media AS +SELECT post.* +FROM + ( SELECT DISTINCT post_id FROM p_post_content WHERE ( TYPE = 3 OR TYPE = 4 OR TYPE = 7 OR TYPE = 8 ) AND is_del = 0 ) media + JOIN p_post post ON media.post_id = post.ID +WHERE + post.is_del = 0; + +DROP VIEW IF EXISTS p_post_by_comment; +CREATE VIEW p_post_by_comment AS +SELECT P.* +FROM + ( + SELECT + post_id + FROM + p_comment + WHERE + is_del = 0 UNION + SELECT + post_id + FROM + p_comment_reply reply + JOIN p_comment COMMENT ON reply.comment_id = COMMENT.ID + WHERE + reply.is_del = 0 + AND COMMENT.is_del = 0 + ) + C JOIN p_post P ON C.post_id = P.ID +WHERE + P.is_del = 0; + SET FOREIGN_KEY_CHECKS = 1; diff --git a/scripts/paopao-postgres.sql b/scripts/paopao-postgres.sql index 9cc7a14f..4ea5b019 100644 --- a/scripts/paopao-postgres.sql +++ b/scripts/paopao-postgres.sql @@ -325,3 +325,36 @@ CREATE TABLE p_wallet_statement ( is_del SMALLINT NOT NULL DEFAULT 0 ); CREATE INDEX idx_wallet_statement_user_id ON p_wallet_statement USING btree (user_id); + +DROP VIEW IF EXISTS p_post_by_media; +CREATE VIEW p_post_by_media AS +SELECT post.* +FROM + ( SELECT DISTINCT post_id FROM p_post_content WHERE ( TYPE = 3 OR TYPE = 4 OR TYPE = 7 OR TYPE = 8 ) AND is_del = 0 ) media + JOIN p_post post ON media.post_id = post.ID +WHERE + post.is_del = 0; + +DROP VIEW IF EXISTS p_post_by_comment; +CREATE VIEW p_post_by_comment AS +SELECT P.* +FROM + ( + SELECT + post_id + FROM + p_comment + WHERE + is_del = 0 UNION + SELECT + post_id + FROM + p_comment_reply reply + JOIN p_comment COMMENT ON reply.comment_id = COMMENT.ID + WHERE + reply.is_del = 0 + AND COMMENT.is_del = 0 + ) + C JOIN p_post P ON C.post_id = P.ID +WHERE + P.is_del = 0; diff --git a/scripts/paopao-sqlite3.sql b/scripts/paopao-sqlite3.sql index 51c427de..2d8a6e32 100644 --- a/scripts/paopao-sqlite3.sql +++ b/scripts/paopao-sqlite3.sql @@ -356,6 +356,39 @@ CREATE TABLE "p_wallet_statement" ( PRIMARY KEY ("id") ); +DROP VIEW IF EXISTS p_post_by_media; +CREATE VIEW p_post_by_media AS +SELECT post.* +FROM + ( SELECT DISTINCT post_id FROM p_post_content WHERE ( TYPE = 3 OR TYPE = 4 OR TYPE = 7 OR TYPE = 8 ) AND is_del = 0 ) media + JOIN p_post post ON media.post_id = post.ID +WHERE + post.is_del = 0; + +DROP VIEW IF EXISTS p_post_by_comment; +CREATE VIEW p_post_by_comment AS +SELECT P.* +FROM + ( + SELECT + post_id + FROM + p_comment + WHERE + is_del = 0 UNION + SELECT + post_id + FROM + p_comment_reply reply + JOIN p_comment COMMENT ON reply.comment_id = COMMENT.ID + WHERE + reply.is_del = 0 + AND COMMENT.is_del = 0 + ) + C JOIN p_post P ON C.post_id = P.ID +WHERE + P.is_del = 0; + -- ---------------------------- -- Indexes structure for table p_attachment -- ---------------------------- From 8d6312d4a5aa2227bc59136b7a37e7b639d0a283 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Fri, 11 Aug 2023 11:03:42 +0800 Subject: [PATCH 2/6] fixed get user comment/media tweets total count error --- CHANGELOG.md | 31 ++----------------------------- internal/dao/jinzhu/topics.go | 4 +--- internal/dao/jinzhu/tweets.go | 11 ++++++----- 3 files changed, 9 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65affa16..0f28cc61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,35 +9,8 @@ All notable changes to paopao-ce are documented in this file. - simple support for user posts filter by style(post/comment/media/star). [#345](https://github.com/rocboss/paopao-ce/pull/345) mirgration database first(sql ddl file in `scripts/migration/**/*_create_view_post_filter.up.sql`): ```sql - CREATE VIEW p_post_by_media AS - SELECT post.* - FROM - ( SELECT DISTINCT post_id FROM p_post_content WHERE ( TYPE = 3 OR TYPE = 4 OR TYPE = 7 OR TYPE = 8 ) AND is_del = 0 ) media - JOIN p_post post ON media.post_id = post.ID - WHERE - post.is_del = 0; - - CREATE VIEW p_post_by_comment AS - SELECT P.* - FROM - ( - SELECT - post_id - FROM - p_comment - WHERE - is_del = 0 UNION - SELECT - post_id - FROM - p_comment_reply reply - JOIN p_comment COMMENT ON reply.comment_id = COMMENT.ID - WHERE - reply.is_del = 0 AND COMMENT.is_del = 0 - ) - C JOIN p_post P ON C.post_id = P.ID - WHERE - P.is_del = 0; + CREATE VIEW p_post_by_media AS SELECT post.*FROM (SELECT DISTINCT post_id FROM p_post_content WHERE (TYPE=3 OR TYPE=4 OR TYPE=7 OR TYPE=8) AND is_del=0) media JOIN p_post post ON media.post_id=post.ID WHERE post.is_del=0; + CREATE VIEW p_post_by_comment AS SELECT P.*FROM (SELECT post_id FROM p_comment WHERE is_del=0 UNION SELECT post_id FROM p_comment_reply reply JOIN p_comment COMMENT ON reply.comment_id=COMMENT.ID WHERE reply.is_del=0 AND COMMENT.is_del=0) C JOIN p_post P ON C.post_id=P.ID WHERE P.is_del=0; ``` - add user highlight tweet support include custom tweet set to highlight and list in user/profile page. diff --git a/internal/dao/jinzhu/topics.go b/internal/dao/jinzhu/topics.go index de317164..aedca3f3 100644 --- a/internal/dao/jinzhu/topics.go +++ b/internal/dao/jinzhu/topics.go @@ -143,7 +143,7 @@ func (s *topicSrv) GetFollowTags(userId int64, limit int, offset int) (cs.TagLis } // 置顶排序后处理 // TODO: 垃圾办法,最好是topic_user join tag 一次查询,但是gorm的join真他喵的别扭,F*K - res := make(cs.TagList, len(topicIds), len(topicIds)) + res := make(cs.TagList, len(topicIds)) for _, tag := range formtedTags { res[topicIdsMap[tag.ID]] = tag } @@ -217,10 +217,8 @@ func (s *topicSrv) tagsFormatA(userId int64, tags cs.TagList) (cs.TagList, error func (s *topicSrv) tagsFormatB(userTopicsMap map[int64]*topicInfo, tags cs.TagInfoList) (cs.TagList, error) { // 获取创建者User IDs userIds := []int64{} - tagIds := []int64{} for _, tag := range tags { userIds = append(userIds, tag.UserID) - tagIds = append(tagIds, tag.ID) } users, err := (&dbr.User{}).ListUserInfoById(s.db, userIds) if err != nil { diff --git a/internal/dao/jinzhu/tweets.go b/internal/dao/jinzhu/tweets.go index 29adb165..ec0b3fff 100644 --- a/internal/dao/jinzhu/tweets.go +++ b/internal/dao/jinzhu/tweets.go @@ -438,14 +438,15 @@ func (s *tweetSrv) getUserTweets(db *gorm.DB, user *cs.VistUser, limit int, offs default: // nothing } - db = db.Where("user_id=? AND visibility IN ? AND is_del=0", user.UserId, visibilities).Order("latest_replied_on DESC") + db = db.Where("user_id=? AND visibility IN ? AND is_del=0", user.UserId, visibilities) + err = db.Count(&total).Error + if err != nil { + return + } if offset >= 0 && limit > 0 { db = db.Offset(offset).Limit(limit) } - if err = db.Find(&res).Error; err != nil { - return - } - err = db.Count(&total).Error + err = db.Order("latest_replied_on DESC").Find(&res).Error return } From b4f4a226f766abd2a347b7cb22e718dee4f5d7c7 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Fri, 11 Aug 2023 12:31:44 +0800 Subject: [PATCH 3/6] fixed mysql sql ddl define error for post_content/comemnt_content/comment_reply table --- scripts/migration/mysql/0008_content_type_alter.up.sql | 6 +++--- scripts/paopao-mysql.sql | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/migration/mysql/0008_content_type_alter.up.sql b/scripts/migration/mysql/0008_content_type_alter.up.sql index 511c47ec..4dec8a0e 100644 --- a/scripts/migration/mysql/0008_content_type_alter.up.sql +++ b/scripts/migration/mysql/0008_content_type_alter.up.sql @@ -1,3 +1,3 @@ -ALTER TABLE `p_post_content` MODIFY COLUMN `content` varchar(65535) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '内容'; -ALTER TABLE `p_comment_content` MODIFY COLUMN `content` varchar(65535) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '内容'; -ALTER TABLE `p_comment_reply` MODIFY COLUMN `content` varchar(65535) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '内容'; +ALTER TABLE `p_post_content` MODIFY COLUMN `content` varchar(4000) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '内容'; +ALTER TABLE `p_comment_content` MODIFY COLUMN `content` varchar(4000) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '内容'; +ALTER TABLE `p_comment_reply` MODIFY COLUMN `content` varchar(4000) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '内容'; diff --git a/scripts/paopao-mysql.sql b/scripts/paopao-mysql.sql index 90a36add..76cec80a 100644 --- a/scripts/paopao-mysql.sql +++ b/scripts/paopao-mysql.sql @@ -70,7 +70,7 @@ CREATE TABLE `p_comment_content` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '内容ID', `comment_id` bigint unsigned NOT NULL DEFAULT '0' COMMENT '评论ID', `user_id` bigint unsigned NOT NULL DEFAULT '0' COMMENT '用户ID', - `content` varchar(65535) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '内容', + `content` varchar(4000) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '内容', `type` tinyint unsigned NOT NULL DEFAULT '2' COMMENT '类型,1标题,2文字段落,3图片地址,4视频地址,5语音地址,6链接地址', `sort` bigint unsigned NOT NULL DEFAULT '100' COMMENT '排序,越小越靠前', `created_on` bigint unsigned NOT NULL DEFAULT '0' COMMENT '创建时间', @@ -93,7 +93,7 @@ CREATE TABLE `p_comment_reply` ( `comment_id` bigint unsigned NOT NULL DEFAULT '0' COMMENT '评论ID', `user_id` bigint unsigned NOT NULL DEFAULT '0' COMMENT '用户ID', `at_user_id` bigint unsigned NOT NULL DEFAULT '0' COMMENT '@用户ID', - `content` varchar(65535) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '内容', + `content` varchar(4000) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '内容', `ip` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'IP地址', `ip_loc` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'IP城市地址', `thumbs_up_count` int unsigned NOT NULL DEFAULT '0' COMMENT '点赞数', @@ -224,7 +224,7 @@ CREATE TABLE `p_post_content` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '内容ID', `post_id` bigint unsigned NOT NULL DEFAULT '0' COMMENT 'POST ID', `user_id` bigint unsigned NOT NULL DEFAULT '0' COMMENT '用户ID', - `content` varchar(65535) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '内容', + `content` varchar(4000) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '内容', `type` tinyint unsigned NOT NULL DEFAULT '2' COMMENT '类型,1标题,2文字段落,3图片地址,4视频地址,5语音地址,6链接地址,7附件资源,8收费资源', `sort` int unsigned NOT NULL DEFAULT '100' COMMENT '排序,越小越靠前', `created_on` bigint unsigned NOT NULL DEFAULT '0' COMMENT '创建时间', From 1a410756793282a3f9cf83e6b75f9c25fc2cfafa Mon Sep 17 00:00:00 2001 From: Michael Li Date: Fri, 11 Aug 2023 13:50:00 +0800 Subject: [PATCH 4/6] fixed get user comment tweets return incorrect data error --- CHANGELOG.md | 2 +- internal/dao/jinzhu/tweets.go | 6 +++--- .../mysql/0009_create_view_post_filter.up.sql | 8 +++++--- .../postgres/0008_create_view_post_filter.up.sql | 10 ++++++---- .../sqlite3/0009_create_view_post_filter.up.sql | 8 +++++--- scripts/paopao-mysql.sql | 8 +++++--- scripts/paopao-postgres.sql | 8 +++++--- scripts/paopao-sqlite3.sql | 8 +++++--- 8 files changed, 35 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f28cc61..8eafb109 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ All notable changes to paopao-ce are documented in this file. mirgration database first(sql ddl file in `scripts/migration/**/*_create_view_post_filter.up.sql`): ```sql CREATE VIEW p_post_by_media AS SELECT post.*FROM (SELECT DISTINCT post_id FROM p_post_content WHERE (TYPE=3 OR TYPE=4 OR TYPE=7 OR TYPE=8) AND is_del=0) media JOIN p_post post ON media.post_id=post.ID WHERE post.is_del=0; - CREATE VIEW p_post_by_comment AS SELECT P.*FROM (SELECT post_id FROM p_comment WHERE is_del=0 UNION SELECT post_id FROM p_comment_reply reply JOIN p_comment COMMENT ON reply.comment_id=COMMENT.ID WHERE reply.is_del=0 AND COMMENT.is_del=0) C JOIN p_post P ON C.post_id=P.ID WHERE P.is_del=0; + CREATE VIEW p_post_by_comment AS SELECT P.*,C.user_id comment_user_id FROM (SELECT post_id,user_id FROM p_comment WHERE is_del=0 UNION SELECT post_id,reply.user_id user_id FROM p_comment_reply reply JOIN p_comment COMMENT ON reply.comment_id=COMMENT.ID WHERE reply.is_del=0 AND COMMENT.is_del=0) C JOIN p_post P ON C.post_id=P.ID WHERE P.is_del=0; ``` - add user highlight tweet support include custom tweet set to highlight and list in user/profile page. diff --git a/internal/dao/jinzhu/tweets.go b/internal/dao/jinzhu/tweets.go index ec0b3fff..d7bead87 100644 --- a/internal/dao/jinzhu/tweets.go +++ b/internal/dao/jinzhu/tweets.go @@ -438,7 +438,7 @@ func (s *tweetSrv) getUserTweets(db *gorm.DB, user *cs.VistUser, limit int, offs default: // nothing } - db = db.Where("user_id=? AND visibility IN ? AND is_del=0", user.UserId, visibilities) + db = db.Where("visibility IN ? AND is_del=0", visibilities) err = db.Count(&total).Error if err != nil { return @@ -451,12 +451,12 @@ func (s *tweetSrv) getUserTweets(db *gorm.DB, user *cs.VistUser, limit int, offs } func (s *tweetSrv) ListUserMediaTweets(user *cs.VistUser, limit int, offset int) ([]*ms.Post, int64, error) { - db := s.db.Table(_post_by_media_) + db := s.db.Table(_post_by_media_).Where("user_id=?", user.UserId) return s.getUserTweets(db, user, limit, offset) } func (s *tweetSrv) ListUserCommentTweets(user *cs.VistUser, limit int, offset int) ([]*ms.Post, int64, error) { - db := s.db.Table(_post_by_comment_) + db := s.db.Table(_post_by_comment_).Where("comment_user_id=?", user.UserId) return s.getUserTweets(db, user, limit, offset) } diff --git a/scripts/migration/mysql/0009_create_view_post_filter.up.sql b/scripts/migration/mysql/0009_create_view_post_filter.up.sql index af3e1c43..1ac93bf3 100644 --- a/scripts/migration/mysql/0009_create_view_post_filter.up.sql +++ b/scripts/migration/mysql/0009_create_view_post_filter.up.sql @@ -7,17 +7,19 @@ WHERE post.is_del = 0; CREATE VIEW p_post_by_comment AS -SELECT P.* +SELECT P.*, C.user_id comment_user_id FROM ( SELECT - post_id + post_id, + user_id FROM p_comment WHERE is_del = 0 UNION SELECT - post_id + post_id, + reply.user_id user_id FROM p_comment_reply reply JOIN p_comment COMMENT ON reply.comment_id = COMMENT.ID diff --git a/scripts/migration/postgres/0008_create_view_post_filter.up.sql b/scripts/migration/postgres/0008_create_view_post_filter.up.sql index af3e1c43..b0e62a31 100644 --- a/scripts/migration/postgres/0008_create_view_post_filter.up.sql +++ b/scripts/migration/postgres/0008_create_view_post_filter.up.sql @@ -7,17 +7,19 @@ WHERE post.is_del = 0; CREATE VIEW p_post_by_comment AS -SELECT P.* +SELECT P.*, C.user_id comment_user_id FROM ( SELECT - post_id + post_id, + user_id FROM p_comment WHERE is_del = 0 UNION SELECT - post_id + post_id, + reply.user_id user_id FROM p_comment_reply reply JOIN p_comment COMMENT ON reply.comment_id = COMMENT.ID @@ -27,4 +29,4 @@ FROM ) C JOIN p_post P ON C.post_id = P.ID WHERE - P.is_del = 0; + P.is_del = 0; \ No newline at end of file diff --git a/scripts/migration/sqlite3/0009_create_view_post_filter.up.sql b/scripts/migration/sqlite3/0009_create_view_post_filter.up.sql index af3e1c43..1ac93bf3 100644 --- a/scripts/migration/sqlite3/0009_create_view_post_filter.up.sql +++ b/scripts/migration/sqlite3/0009_create_view_post_filter.up.sql @@ -7,17 +7,19 @@ WHERE post.is_del = 0; CREATE VIEW p_post_by_comment AS -SELECT P.* +SELECT P.*, C.user_id comment_user_id FROM ( SELECT - post_id + post_id, + user_id FROM p_comment WHERE is_del = 0 UNION SELECT - post_id + post_id, + reply.user_id user_id FROM p_comment_reply reply JOIN p_comment COMMENT ON reply.comment_id = COMMENT.ID diff --git a/scripts/paopao-mysql.sql b/scripts/paopao-mysql.sql index 76cec80a..1e2401c7 100644 --- a/scripts/paopao-mysql.sql +++ b/scripts/paopao-mysql.sql @@ -406,17 +406,19 @@ WHERE DROP VIEW IF EXISTS p_post_by_comment; CREATE VIEW p_post_by_comment AS -SELECT P.* +SELECT P.*, C.user_id comment_user_id FROM ( SELECT - post_id + post_id, + user_id FROM p_comment WHERE is_del = 0 UNION SELECT - post_id + post_id, + reply.user_id user_id FROM p_comment_reply reply JOIN p_comment COMMENT ON reply.comment_id = COMMENT.ID diff --git a/scripts/paopao-postgres.sql b/scripts/paopao-postgres.sql index 4ea5b019..c21fc2ac 100644 --- a/scripts/paopao-postgres.sql +++ b/scripts/paopao-postgres.sql @@ -337,17 +337,19 @@ WHERE DROP VIEW IF EXISTS p_post_by_comment; CREATE VIEW p_post_by_comment AS -SELECT P.* +SELECT P.*, C.user_id comment_user_id FROM ( SELECT - post_id + post_id, + user_id FROM p_comment WHERE is_del = 0 UNION SELECT - post_id + post_id, + reply.user_id user_id FROM p_comment_reply reply JOIN p_comment COMMENT ON reply.comment_id = COMMENT.ID diff --git a/scripts/paopao-sqlite3.sql b/scripts/paopao-sqlite3.sql index 2d8a6e32..fc17748e 100644 --- a/scripts/paopao-sqlite3.sql +++ b/scripts/paopao-sqlite3.sql @@ -367,17 +367,19 @@ WHERE DROP VIEW IF EXISTS p_post_by_comment; CREATE VIEW p_post_by_comment AS -SELECT P.* +SELECT P.*, C.user_id comment_user_id FROM ( SELECT - post_id + post_id, + user_id FROM p_comment WHERE is_del = 0 UNION SELECT - post_id + post_id, + reply.user_id user_id FROM p_comment_reply reply JOIN p_comment COMMENT ON reply.comment_id = COMMENT.ID From b80be74fd4efe2ec057b02e96687e9b9323f0a76 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Sat, 12 Aug 2023 11:06:04 +0800 Subject: [PATCH 5/6] add cli subcommand to start paopao-ce serve - add cli subcommand to start paopao-ce serve or other task. - optimize embed web ui to paopao execute binary file logic. --- .gitignore | 5 +- CHANGELOG.md | 12 ++- Dockerfile | 11 +-- Makefile | 21 ++--- README.md | 8 +- build-release.sh | 4 +- cmd/migrate/migrate.go | 27 +++++++ cmd/root.go | 36 +++++++++ cmd/serve/serve.go | 94 ++++++++++++++++++++++ cmd/version.go | 25 ++++++ go.mod | 2 + go.sum | 6 ++ internal/servants/statick/statick.go | 4 +- internal/servants/statick/statick_embed.go | 4 +- main.go | 91 +-------------------- pkg/version/version.go | 3 + web/embed.go | 4 +- 17 files changed, 242 insertions(+), 115 deletions(-) create mode 100644 cmd/migrate/migrate.go create mode 100644 cmd/root.go create mode 100644 cmd/serve/serve.go create mode 100644 cmd/version.go diff --git a/.gitignore b/.gitignore index 96f9f38d..3980640a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,10 @@ __debug_bin !*.example /config.yaml *.log -paopao-ce* +/paopao-ce +/paopao +/.env +/.envrc /release /data /custom diff --git a/CHANGELOG.md b/CHANGELOG.md index 8eafb109..fce87311 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,10 +13,18 @@ All notable changes to paopao-ce are documented in this file. CREATE VIEW p_post_by_comment AS SELECT P.*,C.user_id comment_user_id FROM (SELECT post_id,user_id FROM p_comment WHERE is_del=0 UNION SELECT post_id,reply.user_id user_id FROM p_comment_reply reply JOIN p_comment COMMENT ON reply.comment_id=COMMENT.ID WHERE reply.is_del=0 AND COMMENT.is_del=0) C JOIN p_post P ON C.post_id=P.ID WHERE P.is_del=0; ``` - add user highlight tweet support include custom tweet set to highlight and list in user/profile page. +- add cli subcommand to start paopao-ce serve or other task. [#354](https://github.com/rocboss/paopao-ce/pull/354) ### Changed -- change man content width to 600px and optimize tweet/comment/replay text length. [#333](https://github.com/rocboss/paopao-ce/pull/333) - +- change man content width to 600px and optimize tweet/comment/replay text length. [#333](https://github.com/rocboss/paopao-ce/pull/333) +- optimize embed web ui to paopao execute binary file logic. [#354](https://github.com/rocboss/paopao-ce/pull/354) + ```sh + # embed web ui to execute file default + make build + # use slim model to disable embed web ui to exectute file + make build TAGS='slim embed' + ``` + ## 0.3.1 ### Fixed - fixed: video player assets cdn error. [&caff8c0](https://github.com/rocboss/paopao-ce/commit/caff8c052be6c8d59576011192f830fd98e17ab3 'commit caff8c0') diff --git a/Dockerfile b/Dockerfile index 48190375..91fcdd6d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,8 +23,8 @@ WORKDIR /paopao-ce COPY . . COPY --from=frontend /web/dist ./web/dist ENV GOPROXY=https://goproxy.cn -RUN [ $EMBED_UI != yes ] || make build TAGS='embed go_json' -RUN [ $EMBED_UI = yes ] || make build TAGS='go_json' +RUN [ $EMBED_UI != yes ] || make build TAGS='go_json' +RUN [ $EMBED_UI = yes ] || make build TAGS='slim embed go_json' FROM bitbus/paopao-ce-backend-runner:latest ARG API_HOST @@ -34,10 +34,11 @@ ARG USE_DIST=no ENV TZ=Asia/Shanghai WORKDIR /app/paopao-ce -COPY --from=backend /paopao-ce/release/paopao-ce . +COPY --from=backend /paopao-ce/release/paopao . COPY --from=backend /paopao-ce/config.yaml.sample config.yaml VOLUME ["/app/paopao-ce/custom"] EXPOSE 8008 -HEALTHCHECK --interval=5s --timeout=3s --retries=3 CMD ps -ef | grep paopao-ce || exit 1 -ENTRYPOINT ["/app/paopao-ce/paopao-ce"] +HEALTHCHECK --interval=5s --timeout=3s --retries=3 CMD ps -ef | grep paopao || exit 1 +ENTRYPOINT ["/app/paopao-ce/paopao"] +CMD ["serve"] diff --git a/Makefile b/Makefile index 8b1d1dae..1a6e807e 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ .PHONY: all build run test clean fmt pre-commit help -TARGET = paopao-ce +PROJECT = paopao-ce +TARGET = paopao ifeq ($(OS),Windows_NT) TARGET := $(TARGET).exe endif @@ -12,10 +13,10 @@ endif RELEASE_ROOT = release RELEASE_FILES = LICENSE README.md CHANGELOG.md config.yaml.sample docker-compose.yaml scripts docs -RELEASE_LINUX_AMD64 = $(RELEASE_ROOT)/linux-amd64/$(TARGET) -RELEASE_DARWIN_AMD64 = $(RELEASE_ROOT)/darwin-amd64/$(TARGET) -RELEASE_DARWIN_ARM64 = $(RELEASE_ROOT)/darwin-arm64/$(TARGET) -RELEASE_WINDOWS_AMD64 = $(RELEASE_ROOT)/windows-amd64/$(TARGET) +RELEASE_LINUX_AMD64 = $(RELEASE_ROOT)/linux-amd64/$(PROJECT) +RELEASE_DARWIN_AMD64 = $(RELEASE_ROOT)/darwin-amd64/$(PROJECT) +RELEASE_DARWIN_ARM64 = $(RELEASE_ROOT)/darwin-arm64/$(PROJECT) +RELEASE_WINDOWS_AMD64 = $(RELEASE_ROOT)/windows-amd64/$(PROJECT) BUILD_VERSION := $(shell git describe --tags --always | cut -f1 -f2 -d "-") BUILD_DATE := $(shell date +'%Y-%m-%d %H:%M:%S') @@ -35,7 +36,7 @@ build: @go build -pgo=auto -trimpath -tags '$(TAGS)' -ldflags '$(LDFLAGS)' -o $(RELEASE_ROOT)/$(TARGET) run: - @go run -pgo=auto -trimpath -gcflags "all=-N -l" -tags '$(TAGS)' -ldflags '$(LDFLAGS)' . + @go run -pgo=auto -trimpath -gcflags "all=-N -l" -tags '$(TAGS)' -ldflags '$(LDFLAGS)' . serve .PHONY: release release: linux-amd64 darwin-amd64 darwin-arm64 windows-x64 @@ -44,10 +45,10 @@ release: linux-amd64 darwin-amd64 darwin-arm64 windows-x64 @cp -rf $(RELEASE_FILES) $(RELEASE_DARWIN_AMD64) @cp -rf $(RELEASE_FILES) $(RELEASE_DARWIN_ARM64) @cp -rf $(RELEASE_FILES) $(RELEASE_WINDOWS_AMD64) - @cd $(RELEASE_LINUX_AMD64)/.. && rm -f *.zip && zip -r $(TARGET)-linux_amd64.zip $(TARGET) && cd - - @cd $(RELEASE_DARWIN_AMD64)/.. && rm -f *.zip && zip -r $(TARGET)-darwin_amd64.zip $(TARGET) && cd - - @cd $(RELEASE_DARWIN_ARM64)/.. && rm -f *.zip && zip -r $(TARGET)-darwin_arm64.zip $(TARGET) && cd - - @cd $(RELEASE_WINDOWS_AMD64)/.. && rm -f *.zip && zip -r $(TARGET)-windows_amd64.zip $(TARGET) && cd - + @cd $(RELEASE_LINUX_AMD64)/.. && rm -f *.zip && zip -r $(PROJECT)-linux_amd64.zip $(PROJECT) && cd - + @cd $(RELEASE_DARWIN_AMD64)/.. && rm -f *.zip && zip -r $(PROJECT)-darwin_amd64.zip $(PROJECT) && cd - + @cd $(RELEASE_DARWIN_ARM64)/.. && rm -f *.zip && zip -r $(PROJECT)-darwin_arm64.zip $(PROJECT) && cd - + @cd $(RELEASE_WINDOWS_AMD64)/.. && rm -f *.zip && zip -r $(PROJECT)-windows_amd64.zip $(PROJECT) && cd - .PHONY: linux-amd64 linux-amd64: diff --git a/README.md b/README.md index 7cf224b7..2908f224 100644 --- a/README.md +++ b/README.md @@ -97,11 +97,15 @@ PaoPao主要由以下优秀的开源项目/工具构建 ``` 编译api服务、内嵌web前端ui: ```sh - make build TAGS='embed' + make build + ``` + 也可以使用精简模式编译,不内嵌web前端ui: + ```sh + make build TAGS='slim embed' ``` 编译后在`release`目录可以找到对应可执行文件。 ```sh - release/paopao-ce + release/paopao ``` 4. 直接运行后端 diff --git a/build-release.sh b/build-release.sh index 9530c07f..b20909df 100755 --- a/build-release.sh +++ b/build-release.sh @@ -1,8 +1,8 @@ #!/bin/sh # eg.1 : sh build-image.sh -# eg.2, set tags: sh build-image.sh 'embed go_json' +# eg.2, set tags: sh build-image.sh 'go_json' -TAGS='embed go_json' +TAGS='go_json' if [ -n "$1" ]; then TAGS="$1" diff --git a/cmd/migrate/migrate.go b/cmd/migrate/migrate.go new file mode 100644 index 00000000..51f64381 --- /dev/null +++ b/cmd/migrate/migrate.go @@ -0,0 +1,27 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package migrate + +import ( + "fmt" + + "github.com/rocboss/paopao-ce/cmd" + "github.com/spf13/cobra" +) + +func init() { + migrateCmd := &cobra.Command{ + Use: "migrate", + Short: "migrate database data", + Long: "miegrate database data when paopao-ce upgrade", + Run: migrateRun, + } + cmd.Register(migrateCmd) +} + +func migrateRun(_cmd *cobra.Command, _args []string) { + // TODO: add some logic for migrate cmd feature + fmt.Println("sorry, this feature is not implemented yet.") +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 00000000..e0e59974 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,36 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package cmd + +import ( + "github.com/spf13/cobra" +) + +var ( + rootCmd = &cobra.Command{ + Use: "paopao", + Short: `an artistic "twitter like" community`, + Long: `an artistic "twitter like" community`, + } +) + +// Setup set root command name,short-describe, long-describe +// return &cobra.Command to custom other options +func Setup(use, short, long string) *cobra.Command { + rootCmd.Use = use + rootCmd.Short = short + rootCmd.Long = long + return rootCmd +} + +// Register add sub-command +func Register(cmd *cobra.Command) { + rootCmd.AddCommand(cmd) +} + +// Execute start application +func Execute() { + rootCmd.Execute() +} diff --git a/cmd/serve/serve.go b/cmd/serve/serve.go new file mode 100644 index 00000000..5ffd7070 --- /dev/null +++ b/cmd/serve/serve.go @@ -0,0 +1,94 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package serve + +import ( + "fmt" + "log" + "os" + "os/signal" + "syscall" + "time" + + "github.com/alimy/cfg" + "github.com/fatih/color" + "github.com/getsentry/sentry-go" + "github.com/rocboss/paopao-ce/cmd" + "github.com/rocboss/paopao-ce/internal" + "github.com/rocboss/paopao-ce/internal/conf" + "github.com/rocboss/paopao-ce/internal/service" + "github.com/rocboss/paopao-ce/pkg/debug" + "github.com/rocboss/paopao-ce/pkg/utils" + "github.com/rocboss/paopao-ce/pkg/version" + "github.com/sourcegraph/conc" + "github.com/spf13/cobra" + "go.uber.org/automaxprocs/maxprocs" +) + +var ( + noDefaultFeatures bool + features []string +) + +func init() { + serveCmd := &cobra.Command{ + Use: "serve", + Short: "start paopao-ce server", + Long: "start paopao-ce server", + Run: serveRun, + } + + serveCmd.Flags().BoolVar(&noDefaultFeatures, "no-default-features", false, "whether not use default features") + serveCmd.Flags().StringSliceVarP(&features, "features", "f", []string{}, "use special features") + + cmd.Register(serveCmd) +} + +func deferFn() { + if cfg.If("Sentry") { + // Flush buffered events before the program terminates. + sentry.Flush(2 * time.Second) + } +} + +func serveRun(_cmd *cobra.Command, _args []string) { + utils.PrintHelloBanner(version.VersionInfo()) + + // set maxprocs automatic + maxprocs.Set(maxprocs.Logger(log.Printf)) + + // initial configure + conf.Initial(features, noDefaultFeatures) + internal.Initial() + ss := service.MustInitService() + if len(ss) < 1 { + fmt.Fprintln(color.Output, "no service need start so just exit") + return + } + + // do defer function + defer deferFn() + + // start pyroscope if need + debug.StartPyroscope() + + // start services + wg := conc.NewWaitGroup() + fmt.Fprintf(color.Output, "\nstarting run service...\n\n") + service.Start(wg) + + // graceful stop services + wg.Go(func() { + quit := make(chan os.Signal, 1) + // kill (no param) default send syscall.SIGTERM + // kill -2 is syscall.SIGINT + // kill -9 is syscall.SIGKILL but can't be catch, so don't need add it + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) + <-quit + fmt.Fprintf(color.Output, "\nshutting down server...\n\n") + service.Stop() + }) + wg.Wait() +} diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 00000000..fc7da675 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,25 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package cmd + +import ( + "github.com/rocboss/paopao-ce/pkg/utils" + "github.com/rocboss/paopao-ce/pkg/version" + "github.com/spf13/cobra" +) + +func init() { + versionCmd := &cobra.Command{ + Use: "version", + Short: "show version information", + Long: "show version information", + Run: versionRun, + } + Register(versionCmd) +} + +func versionRun(_cmd *cobra.Command, _args []string) { + utils.PrintHelloBanner(version.VersionInfo()) +} diff --git a/go.mod b/go.mod index cd9faef5..39f6661f 100644 --- a/go.mod +++ b/go.mod @@ -33,6 +33,7 @@ require ( github.com/sirupsen/logrus v1.9.3 github.com/smartwalle/alipay/v3 v3.2.15 github.com/sourcegraph/conc v0.3.0 + github.com/spf13/cobra v1.7.0 github.com/spf13/viper v1.16.0 github.com/tencentyun/cos-go-sdk-v5 v0.7.42 github.com/yinheli/mahonia v0.0.0-20131226213531-0eef680515cc @@ -76,6 +77,7 @@ require ( github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/hcl v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.1.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 diff --git a/go.sum b/go.sum index 7e65b4f2..b0a16bcc 100644 --- a/go.sum +++ b/go.sum @@ -363,6 +363,7 @@ github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfc github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= @@ -740,6 +741,8 @@ github.com/imdario/mergo v0.3.10/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/intel/goresctrl v0.2.0/go.mod h1:+CZdzouYFn5EsxgqAQTEzMfwKwuc0fVdMrT9FCCAVRQ= github.com/j-keck/arping v0.0.0-20160618110441-2cf9dc699c56/go.mod h1:ymszkNOg6tORTn+6F6j+Jc8TOr5osrynvN6ivFWZ2GA= github.com/j-keck/arping v1.0.2/go.mod h1:aJbELhR92bSk7tp79AWM/ftfc90EfEi2bQJrbBFOsPw= @@ -1130,6 +1133,7 @@ github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/safchain/ethtool v0.0.0-20190326074333-42ed695e3de8/go.mod h1:Z0q5wiBQGYcxhMZ6gUqHn6pYNLypFAvaL3UvgZLR0U4= @@ -1185,6 +1189,8 @@ github.com/spf13/cobra v0.0.2-0.20171109065643-2da4a54c5cee/go.mod h1:1l0Ry5zgKv github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo= +github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= +github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk= github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= diff --git a/internal/servants/statick/statick.go b/internal/servants/statick/statick.go index ef323b0b..fdde1a7d 100644 --- a/internal/servants/statick/statick.go +++ b/internal/servants/statick/statick.go @@ -2,8 +2,8 @@ // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. -//go:build !embed -// +build !embed +//go:build slim && embed +// +build slim,embed package statick diff --git a/internal/servants/statick/statick_embed.go b/internal/servants/statick/statick_embed.go index 96c37f95..51047289 100644 --- a/internal/servants/statick/statick_embed.go +++ b/internal/servants/statick/statick_embed.go @@ -2,8 +2,8 @@ // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. -//go:build embed -// +build embed +//go:build !(slim && embed) +// +build !slim !embed package statick diff --git a/main.go b/main.go index c2514e19..a193d55f 100644 --- a/main.go +++ b/main.go @@ -5,94 +5,11 @@ package main import ( - "flag" - "fmt" - "os" - "os/signal" - "strings" - "syscall" - "time" - - "github.com/alimy/cfg" - "github.com/fatih/color" - "github.com/getsentry/sentry-go" - "github.com/rocboss/paopao-ce/internal" - "github.com/rocboss/paopao-ce/internal/conf" - "github.com/rocboss/paopao-ce/internal/service" - "github.com/rocboss/paopao-ce/pkg/debug" - "github.com/rocboss/paopao-ce/pkg/utils" - "github.com/rocboss/paopao-ce/pkg/version" - "github.com/sourcegraph/conc" - _ "go.uber.org/automaxprocs" -) - -var ( - noDefaultFeatures bool - features suites + "github.com/rocboss/paopao-ce/cmd" + _ "github.com/rocboss/paopao-ce/cmd/migrate" + _ "github.com/rocboss/paopao-ce/cmd/serve" ) -type suites []string - -func (s *suites) String() string { - return strings.Join(*s, ",") -} - -func (s *suites) Set(value string) error { - for _, item := range strings.Split(value, ",") { - *s = append(*s, strings.TrimSpace(item)) - } - return nil -} - -func init() { - flagParse() - - conf.Initial(features, noDefaultFeatures) - internal.Initial() -} - -func deferFn() { - if cfg.If("Sentry") { - // Flush buffered events before the program terminates. - sentry.Flush(2 * time.Second) - } -} - -func flagParse() { - flag.BoolVar(&noDefaultFeatures, "no-default-features", false, "whether not use default features") - flag.Var(&features, "features", "use special features") - flag.Parse() -} - func main() { - utils.PrintHelloBanner(version.VersionInfo()) - ss := service.MustInitService() - if len(ss) < 1 { - fmt.Fprintln(color.Output, "no service need start so just exit") - return - } - - // do defer function - defer deferFn() - - // start pyroscope if need - debug.StartPyroscope() - - // start services - wg := conc.NewWaitGroup() - fmt.Fprintf(color.Output, "\nstarting run service...\n\n") - service.Start(wg) - - // graceful stop services - wg.Go(func() { - quit := make(chan os.Signal, 1) - // kill (no param) default send syscall.SIGTERM - // kill -2 is syscall.SIGINT - // kill -9 is syscall.SIGKILL but can't be catch, so don't need add it - signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) - <-quit - fmt.Fprintf(color.Output, "\nshutting down server...\n\n") - service.Stop() - }) - wg.Wait() + cmd.Execute() } diff --git a/pkg/version/version.go b/pkg/version/version.go index b3683bef..ce30181c 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -21,6 +21,9 @@ func VersionInfo() string { } func ReadBuildInfo() *BuildInfo { + if version == "" { + version = "unknow" + } return &BuildInfo{ Version: version, Sum: commitID, diff --git a/web/embed.go b/web/embed.go index a16df24a..1544fd4e 100644 --- a/web/embed.go +++ b/web/embed.go @@ -2,8 +2,8 @@ // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. -//go:build embed -// +build embed +//go:build !(slim && embed) +// +build !slim !embed package web From ceb047ce37343449fe03d9a1e284fa1692c310db Mon Sep 17 00:00:00 2001 From: Michael Li Date: Sat, 12 Aug 2023 11:10:05 +0800 Subject: [PATCH 6/6] ci: add go1.21.x toolchain --- .github/workflows/go.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index a38b564a..66a46ea3 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -47,7 +47,7 @@ jobs: name: Test strategy: matrix: - go-version: [ 1.20.x ] + go-version: [ 1.20.x, 1.21.x ] platform: [ ubuntu-latest, macos-latest ] runs-on: ${{ matrix.platform }} steps: @@ -66,7 +66,7 @@ jobs: name: TestOnWindows strategy: matrix: - go-version: [ 1.20.x ] + go-version: [ 1.20.x, 1.21.x ] platform: [ windows-latest ] runs-on: ${{ matrix.platform }} steps: