Skip to content

Commit

Permalink
Merge pull request #25 from xushiwei/frame
Browse files Browse the repository at this point in the history
community framework
  • Loading branch information
xushiwei authored Jan 10, 2024
2 parents f35d6dd + c6be69a commit 2527532
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 242 deletions.
42 changes: 22 additions & 20 deletions cmd/gopcomm/community_yap.gox
Original file line number Diff line number Diff line change
@@ -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"
42 changes: 19 additions & 23 deletions cmd/gopcomm/gop_autogen.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
8 changes: 5 additions & 3 deletions cmd/gopcomm/yap/article_yap.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

</head>
<body>
<span>Article {{.id}}</span>
<h1>Title {{.title}}</h1>
<p>Content {{.content}}</p>
<span>Article {{.ID}}</span>
<h1>{{.Title}}</h1>
<pre>
{{.Body}}
</pre>
</body>
</html>
2 changes: 1 addition & 1 deletion cmd/gopcomm/yap/edit_yap.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<meta charset="utf-8"/>
</head>
<body>
Markdown Edit Page.
Edit {{.ID}}
</body>
</html>
5 changes: 5 additions & 0 deletions cmd/gopcomm/yap/home_yap.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@
</head>
<body>
Go+ Community <a href="/p/123">written in Go+</a>
<ul>
{{range .Items}}
<li>Title: <a href="/p/{{.ID}}">{{.Title}}</a></li>
{{end}}
</ul>
</body>
</html>
8 changes: 0 additions & 8 deletions cmd/gopcomm/yap/user_yap.html

This file was deleted.

31 changes: 0 additions & 31 deletions internal/config/config.gop

This file was deleted.

60 changes: 0 additions & 60 deletions internal/core/article.gop

This file was deleted.

89 changes: 63 additions & 26 deletions internal/core/community.gop
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading

0 comments on commit 2527532

Please sign in to comment.