-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
42 lines (36 loc) · 945 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"context"
"net/http"
"github.com/gin-gonic/gin"
"github.com/luyunbo/protoc-gen-gin/example/api/product/app/ecode"
v1 "github.com/luyunbo/protoc-gen-gin/example/api/product/app/v1"
)
type service struct {
}
func (s service) CreateArticle(ctx context.Context, article *v1.Article) (*v1.Article, error) {
if article.AuthorId < 1 {
return nil, ecode.Errorf(http.StatusBadRequest, 400, "author id must > 0")
}
return article, nil
}
func (s service) GetArticles(ctx context.Context, req *v1.GetArticlesReq) (*v1.GetArticlesResp, error) {
if req.AuthorId < 0 {
return nil, ecode.Errorf(http.StatusBadRequest, 400, "author id must >= 0")
}
return &v1.GetArticlesResp{
Total: 1,
Articles: []*v1.Article{
{
Title: "test article: " + req.Title,
Content: "test",
AuthorId: 1,
},
},
}, nil
}
func main() {
e := gin.Default()
v1.RegisterBlogServiceHTTPServer(e, &service{})
e.Run()
}