From c6be69acd6680551b9d4397fd9a04f68f4f93996 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Wed, 10 Jan 2024 14:00:17 +0800 Subject: [PATCH] community framework --- cmd/gopcomm/community_yap.gox | 42 +++--- cmd/gopcomm/gop_autogen.go | 42 +++--- cmd/gopcomm/yap/article_yap.html | 8 +- cmd/gopcomm/yap/edit_yap.html | 2 +- cmd/gopcomm/yap/home_yap.html | 5 + cmd/gopcomm/yap/user_yap.html | 8 -- internal/config/config.gop | 31 ----- internal/core/article.gop | 60 --------- internal/core/community.gop | 89 +++++++++---- internal/core/gop_autogen.go | 124 +++++++++--------- markdown/gop_autogen.go | 8 ++ .../core/models.gop => markdown/render.gop | 10 +- 12 files changed, 187 insertions(+), 242 deletions(-) delete mode 100644 cmd/gopcomm/yap/user_yap.html delete mode 100644 internal/config/config.gop delete mode 100644 internal/core/article.gop create mode 100644 markdown/gop_autogen.go rename internal/core/models.gop => markdown/render.gop (80%) diff --git a/cmd/gopcomm/community_yap.gox b/cmd/gopcomm/community_yap.gox index 718ec5bf..6fd9a05f 100644 --- a/cmd/gopcomm/community_yap.gox +++ b/cmd/gopcomm/community_yap.gox @@ -1,33 +1,35 @@ -import "github.com/goplus/community/internal/core" +import ( + "github.com/goplus/community/internal/core" + "github.com/goplus/community/markdown" +) -// TODO: Config Init -config := &core.Config{ - core.ArticleConfig{}, -} -community := core.New(config) - -get "/article/:id", ctx => { - param := ctx.param("id") - println "Visiting article " + param - - h := community.GetArticleHandler() - info, _ := h.GetArticle(param) +var ( + community *core.Community +) +get "/p/:id", ctx => { + id := ctx.param("id") + article, _ := community.article(id) + html, _ := markdown.render(article.Content) ctx.yap "article", { - "id": info.Id, - "title": info.Title, - "content": info.Content, + "ID": id, + "Title": article.Title, + "Body": string(html), } } get "/", ctx => { + articles, _, _ := community.listArticle(core.MarkBegin, 20) ctx.yap "home", { + "Items": articles, } } -get "/user/:id", ctx => { - ctx.yap "user", { - "id": ctx.param("id"), +get "/edit", ctx => { + ctx.yap "edit", { + "ID": ctx.param("id"), } } -println "Community server running on :8080" +config := &core.Config{} +community, _ = core.New(config) + run ":8080" diff --git a/cmd/gopcomm/gop_autogen.go b/cmd/gopcomm/gop_autogen.go index fac19bd3..0f71f416 100644 --- a/cmd/gopcomm/gop_autogen.go +++ b/cmd/gopcomm/gop_autogen.go @@ -1,49 +1,45 @@ package main import ( - "fmt" "github.com/goplus/yap" "github.com/goplus/community/internal/core" + "github.com/goplus/community/markdown" ) type community struct { yap.App + community *core.Community } -// TODO: Config Init -// -//line cmd/gopcomm/community_yap.gox:4 +//line cmd/gopcomm/community_yap.gox:10 func (this *community) MainEntry() { -//line cmd/gopcomm/community_yap.gox:4:1 - config := &core.Config{core.ArticleConfig{}} -//line cmd/gopcomm/community_yap.gox:7:1 - community := core.New(config) -//line cmd/gopcomm/community_yap.gox:9:1 - this.Get("/article/:id", func(ctx *yap.Context) { //line cmd/gopcomm/community_yap.gox:10:1 - param := ctx.Param("id") + this.Get("/p/:id", func(ctx *yap.Context) { //line cmd/gopcomm/community_yap.gox:11:1 - fmt.Println("Visiting article " + param) + id := ctx.Param("id") +//line cmd/gopcomm/community_yap.gox:12:1 + article, _ := this.community.Article(id) //line cmd/gopcomm/community_yap.gox:13:1 - h := community.GetArticleHandler() + html, _ := markdown.Render(article.Content) //line cmd/gopcomm/community_yap.gox:14:1 - info, _ := h.GetArticle(param) -//line cmd/gopcomm/community_yap.gox:16:1 - ctx.Yap__1("article", map[string]string{"id": info.Id, "title": info.Title, "content": info.Content}) + ctx.Yap__1("article", map[string]string{"ID": id, "Title": article.Title, "Body": string(html)}) }) -//line cmd/gopcomm/community_yap.gox:22:1 +//line cmd/gopcomm/community_yap.gox:20:1 this.Get("/", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:23:1 - ctx.Yap__1("home", map[string]interface { - }{}) +//line cmd/gopcomm/community_yap.gox:21:1 + articles, _, _ := this.community.ListArticle(core.MarkBegin, 20) +//line cmd/gopcomm/community_yap.gox:22:1 + ctx.Yap__1("home", map[string][]*core.ArticleEntry{"Items": articles}) }) //line cmd/gopcomm/community_yap.gox:26:1 - this.Get("/user/:id", func(ctx *yap.Context) { + this.Get("/edit", func(ctx *yap.Context) { //line cmd/gopcomm/community_yap.gox:27:1 - ctx.Yap__1("user", map[string]string{"id": ctx.Param("id")}) + ctx.Yap__1("edit", map[string]string{"ID": ctx.Param("id")}) }) //line cmd/gopcomm/community_yap.gox:32:1 - fmt.Println("Community server running on :8080") + config := &core.Config{} //line cmd/gopcomm/community_yap.gox:33:1 + this.community, _ = core.New(config) +//line cmd/gopcomm/community_yap.gox:35:1 this.Run__1(":8080") } func main() { diff --git a/cmd/gopcomm/yap/article_yap.html b/cmd/gopcomm/yap/article_yap.html index 4226c074..b02a1b1c 100644 --- a/cmd/gopcomm/yap/article_yap.html +++ b/cmd/gopcomm/yap/article_yap.html @@ -4,8 +4,10 @@ -Article {{.id}} -

Title {{.title}}

-

Content {{.content}}

+Article {{.ID}} +

{{.Title}}

+
+{{.Body}}
+
diff --git a/cmd/gopcomm/yap/edit_yap.html b/cmd/gopcomm/yap/edit_yap.html index 967cc445..690dc729 100644 --- a/cmd/gopcomm/yap/edit_yap.html +++ b/cmd/gopcomm/yap/edit_yap.html @@ -3,6 +3,6 @@ -Markdown Edit Page. +Edit {{.ID}} diff --git a/cmd/gopcomm/yap/home_yap.html b/cmd/gopcomm/yap/home_yap.html index a18de26b..aae884b9 100644 --- a/cmd/gopcomm/yap/home_yap.html +++ b/cmd/gopcomm/yap/home_yap.html @@ -4,5 +4,10 @@ Go+ Community written in Go+ + diff --git a/cmd/gopcomm/yap/user_yap.html b/cmd/gopcomm/yap/user_yap.html deleted file mode 100644 index f2bbca11..00000000 --- a/cmd/gopcomm/yap/user_yap.html +++ /dev/null @@ -1,8 +0,0 @@ - - - - - -User {{.id}}. - - diff --git a/internal/config/config.gop b/internal/config/config.gop deleted file mode 100644 index 53fea58d..00000000 --- a/internal/config/config.gop +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package config - -//Config contains all application configurations in goplus community -type Config struct { - App - Database -} - -//App contains application configurations -type App struct { -} - -// Database contains database configurations -type Database struct { -} diff --git a/internal/core/article.gop b/internal/core/article.gop deleted file mode 100644 index 3ca2925f..00000000 --- a/internal/core/article.gop +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package core - -// Check interface implementation. -var _ articleHandler = (*articleHandlerImpl)(nil) - -// Article Config -// -// ArticleConfig is the config for article. -type ArticleConfig struct { -} - -// articleHandler -// -// articleHandler is the interface for article handler, -// which defines the methods that must be implemented by the article handler. -// We need to implement this interface in the community/internal/core package. -type articleHandler interface { - GetArticle(id string) (Article, error) -} - -// ArticleHandler -// -// ArticleHandler is the handler for article. -type articleHandlerImpl struct { - config *ArticleConfig -} - -// NewArticleHandler creates a new article handler. -func newArticleHandler(conf *ArticleConfig) *articleHandlerImpl { - return &articleHandlerImpl{ - config: conf, - } -} - -// GetArticle gets article by id. -func (h *articleHandlerImpl) GetArticle(id string) (Article, error) { - // TODO: get data from db. - - return Article{ - Id: id, - Title: "Goplus Community Article Content: " + string(id), - Content: "Goplus Community Article Content: " + string(id), - }, nil -} diff --git a/internal/core/community.gop b/internal/core/community.gop index 78a849db..3f55f895 100644 --- a/internal/core/community.gop +++ b/internal/core/community.gop @@ -16,41 +16,78 @@ package core -// Check interface implementation. -var _ communityHandler = (*communityHandlerImpl)(nil) +import ( + "io" + "os" + "time" +) + +var ( + ErrNotExist = os.ErrNotExist +) -// Config -// -// Top level config for community handlers. type Config struct { - ArticleConfig } -// communityHandler -// -// communityHandler is the interface for community handler, -// which defines the methods that must be implemented by the community handler. -// It returns the handler for each service. -type communityHandler interface { - GetArticleHandler() articleHandler +type ArticleEntry struct { + ID string + Title string + Ctime time.Time + Mtime time.Time +} + +type Article struct { + ArticleEntry + Content []byte // in markdown } -// CommunityHandler -// -// CommunityHandler is the top handler for community. -// It contains all the handlers for each service. -type communityHandlerImpl struct { - articleHandler +type Community struct { } -// New creates a new community handler. -func New(conf *Config) *communityHandlerImpl { - return &communityHandlerImpl{ - articleHandler: newArticleHandler(&conf.ArticleConfig), +func New(conf *Config) (*Community, error) { + return &Community{}, nil +} + +const contentSummary = ` +Content +==== + +Text body +` + +// Article returns an article. +func (p *Community) Article(id string) (article *Article, err error) { + if id == "123" { + article = &Article{ + ArticleEntry{ + ID: id, + Title: "Title", + }, + []byte(contentSummary), + } + return } + return nil, ErrNotExist +} + +// PutArticle adds new article (ID == "") or edits an existing article (ID != ""). +func (p *Community) PutArticle(article *Article) (id string, err error) { + return } -// GetArticleHandler gets article handler. -func (p *communityHandlerImpl) GetArticleHandler() articleHandler { - return p.articleHandler +const ( + MarkBegin = "" + MarkEnd = "eof" +) + +// ListArticle lists articles from an position. +func (p *Community) ListArticle(from string, limit int) (items []*ArticleEntry, next string, err error) { + if from == MarkBegin { + item := &ArticleEntry{ + ID: "123", + Title: "Title", + } + return []*ArticleEntry{item}, MarkEnd, nil + } + return nil, MarkEnd, io.EOF } diff --git a/internal/core/gop_autogen.go b/internal/core/gop_autogen.go index 1ce13635..79571a6e 100644 --- a/internal/core/gop_autogen.go +++ b/internal/core/gop_autogen.go @@ -1,79 +1,75 @@ package core -// Article Config -// -// ArticleConfig is the config for article. -type ArticleConfig struct { -} -// articleHandler -// -// articleHandler is the interface for article handler, -// which defines the methods that must be implemented by the article handler. -// We need to implement this interface in the community/internal/core package. -type articleHandler interface { - GetArticle(id string) (Article, error) -} -// ArticleHandler -// -// ArticleHandler is the handler for article. -type articleHandlerImpl struct { - config *ArticleConfig -} -// Config -// -// Top level config for community handlers. + +import ( + "os" + "io" + "time" +) + type Config struct { - ArticleConfig -} -// communityHandler -// -// communityHandler is the interface for community handler, -// which defines the methods that must be implemented by the community handler. -// It returns the handler for each service. -type communityHandler interface { - GetArticleHandler() articleHandler } -// CommunityHandler -// -// CommunityHandler is the top handler for community. -// It contains all the handlers for each service. -type communityHandlerImpl struct { - articleHandler +type ArticleEntry struct { + ID string + Title string + Ctime time.Time + Mtime time.Time } -//Article Article struct type Article struct { - Id string `json:"id"` - Title string `json:"title"` - Content string `json:"content"` + ArticleEntry + Content []byte } -// GetArticle gets article by id. -// -//line internal/core/article.gop:52:1 -func (h *articleHandlerImpl) GetArticle(id string) (Article, error) { -//line internal/core/article.gop:55:1 - return Article{Id: id, Title: "Goplus Community Article Content: " + string(id), Content: "Goplus Community Article Content: " + string(id)}, nil +type Community struct { } -// NewArticleHandler creates a new article handler. + +const contentSummary = ` +Content +==== + +Text body +` +const ( + MarkBegin = "" + MarkEnd = "eof" +) + +var ErrNotExist = os.ErrNotExist +// Article returns an article. // -//line internal/core/article.gop:45:1 -func newArticleHandler(conf *ArticleConfig) *articleHandlerImpl { -//line internal/core/article.gop:46:1 - return &articleHandlerImpl{config: conf} +//line internal/core/community.gop:59:1 +func (p *Community) Article(id string) (article *Article, err error) { +//line internal/core/community.gop:60:1 + if id == "123" { +//line internal/core/community.gop:61:1 + article = &Article{ArticleEntry{ID: id, Title: "Title"}, []byte(contentSummary)} +//line internal/core/community.gop:68:1 + return + } +//line internal/core/community.gop:70:1 + return nil, ErrNotExist } -// GetArticleHandler gets article handler. +// PutArticle adds new article (ID == "") or edits an existing article (ID != ""). // -//line internal/core/community.gop:54:1 -func (p *communityHandlerImpl) GetArticleHandler() articleHandler { -//line internal/core/community.gop:55:1 - return p.articleHandler +//line internal/core/community.gop:74:1 +func (p *Community) PutArticle(article *Article) (id string, err error) { +//line internal/core/community.gop:75:1 + return } -// New creates a new community handler. +// ListArticle lists articles from an position. // +//line internal/core/community.gop:84:1 +func (p *Community) ListArticle(from string, limit int) (items []*ArticleEntry, next string, err error) { +//line internal/core/community.gop:85:1 + if from == MarkBegin { +//line internal/core/community.gop:86:1 + item := &ArticleEntry{ID: "123", Title: "Title"} +//line internal/core/community.gop:90:1 + return []*ArticleEntry{item}, MarkEnd, nil + } +//line internal/core/community.gop:92:1 + return nil, MarkEnd, io.EOF +} //line internal/core/community.gop:47:1 -func New(conf *Config) *communityHandlerImpl { +func New(conf *Config) (*Community, error) { //line internal/core/community.gop:48:1 - return &communityHandlerImpl{articleHandler: newArticleHandler(&conf.ArticleConfig)} + return &Community{}, nil } -// Check interface implementation. -var _ articleHandler = (*articleHandlerImpl)(nil) -// Check interface implementation. -var _ communityHandler = (*communityHandlerImpl)(nil) diff --git a/markdown/gop_autogen.go b/markdown/gop_autogen.go new file mode 100644 index 00000000..64d26e15 --- /dev/null +++ b/markdown/gop_autogen.go @@ -0,0 +1,8 @@ +package markdown +// Render renders a markdown text into html. +// +//line markdown/render.gop:20:1 +func Render(md []byte) (html []byte, err error) { +//line markdown/render.gop:21:1 + return md, nil +} diff --git a/internal/core/models.gop b/markdown/render.gop similarity index 80% rename from internal/core/models.gop rename to markdown/render.gop index 9c5f5a72..8da2aaa8 100644 --- a/internal/core/models.gop +++ b/markdown/render.gop @@ -14,11 +14,9 @@ * limitations under the License. */ -package core +package markdown -//Article Article struct -type Article struct { - Id string `json:"id"` - Title string `json:"title"` - Content string `json:"content"` +// Render renders a markdown text into html. +func Render(md []byte) (html []byte, err error) { + return md, nil }