Skip to content

Commit

Permalink
Add examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
8treenet committed May 8, 2023
1 parent 02c6041 commit 73fa26c
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 26 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# Freedom DDD Framework

[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/8treenet/freedom/blob/master/LICENSE) [![Go Report Card](https://goreportcard.com/badge/github.com/8treenet/freedom)](https://goreportcard.com/report/github.com/8treenet/freedom)[![GoDoc](https://godoc.org/github.com/8treenet/freedom?status.svg)](https://godoc.org/github.com/8treenet/freedom)
[![GitHub release](https://img.shields.io/github/v/release/8treenet/freedom.svg)](https://github.com/8treenet/freedom/releases)
<img align="right" width="200px" src="https://raw.githubusercontent.com/8treenet/blog/master/img/freedom.png">
###### Freedom是一个基于六边形架构的框架,可以支撑充血的领域模型范式。

###### Freedom 是一个基于六边形架构的框架,可以支撑充血的领域模型范式。

## Overview

- 集成 Iris
- HTTP/H2C Server & Client
- 集成普罗米修斯
Expand All @@ -18,20 +21,25 @@
- 一级缓存 & 二级缓存 & 防击穿

## 安装

```sh
$ go install github.com/8treenet/freedom/freedom@latest
$ freedom version
```

## 脚手架创建项目

```sh
$ freedom new-project [project-name]
$ cd [project-name]
$ go mod tidy
$ go run server/main.go
```

## 脚手架生成增删查改和持久化对象

####

```sh
# freedom new-po -h 查看更多
$ cd [project-name]
Expand All @@ -46,8 +54,9 @@ $ freedom new-po --json ./domain/po/schema.json
## Example

#### [基础教程](https://github.com/8treenet/freedom/blob/master/example/base)
#### [http2监听和依赖倒置](https://github.com/8treenet/freedom/blob/master/example/http2)
#### [事务组件&自定义组件&Kafka&领域事件组件](https://github.com/8treenet/freedom/blob/master/example/infra-example)

#### [一个完整的电商demo,包含CQS、聚合、实体、领域事件、资源库、基础设施](https://github.com/8treenet/freedom/blob/master/example/fshop)
#### [http2 监听和依赖倒置](https://github.com/8treenet/freedom/blob/master/example/http2)

#### [事务组件&自定义组件&Kafka&领域事件组件](https://github.com/8treenet/freedom/blob/master/example/infra-example)

#### [一个完整的电商 demo,包含 CQS、聚合、实体、领域事件、资源库、基础设施](https://github.com/8treenet/freedom/blob/master/example/fshop)
12 changes: 11 additions & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Freedom DDD Framework

[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/8treenet/freedom/blob/master/LICENSE) [![Go Report Card](https://goreportcard.com/badge/github.com/8treenet/freedom)](https://goreportcard.com/report/github.com/8treenet/freedom)[![GoDoc](https://godoc.org/github.com/8treenet/freedom?status.svg)](https://godoc.org/github.com/8treenet/freedom)
<img align="right" width="200px" src="https://raw.githubusercontent.com/8treenet/blog/master/img/freedom.png">

###### Freedom is a framework based on a hexagonal architecture that supports the congestion domain model paradigm.

## Overview

- Integrated Iris v12
- Integrated Prometheus
- Link Tracing
Expand All @@ -18,19 +21,23 @@
- Primary Cache & Secondary Cache & Prevent Breakdown

## Install

```sh
$ go install github.com/8treenet/freedom/freedom@latest
$ freedom version
```

## Create Project

```sh
$ freedom new-project [project-name]
$ cd [project-name]
$ go mod tidy
$ go run server/main.go
```

## Build Persistent Objects(PO)

```sh
# Configurable address and output directory, using 'freedom new-po -h' to see more
$ cd [project-name]
Expand All @@ -45,8 +52,11 @@ $ freedom new-po --json ./domain/po/schema.json
## Example

#### [Basic Tutorial](https://github.com/8treenet/freedom/blob/master/example/base)

#### [Http2 Listening And Dependency Inversion](https://github.com/8treenet/freedom/blob/master/example/http2)

#### [Transaction Components And Custom Components](https://github.com/8treenet/freedom/blob/master/example/infra-example)

#### [Message Events And Domain Events](https://github.com/8treenet/freedom/blob/master/example/event-example)
#### [Electronic Demo(Contains CQS、Aggregation、entity、Domain Events、Repository、Infrastructure)](https://github.com/8treenet/freedom/blob/master/example/fshop)

#### [Electronic Demo(Contains CQS、Aggregation、entity、Domain Events、Repository、Infrastructure)](https://github.com/8treenet/freedom/blob/master/example/fshop)
56 changes: 45 additions & 11 deletions example/base/adapter/controller/default.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package controller

import (
"fmt"
"io"
"os"

Expand Down Expand Up @@ -38,13 +37,15 @@ type Default struct {

// Get handles the GET: / route.
func (c *Default) Get() freedom.Result {
//curl http://127.0.0.1:8000
c.Worker.Logger().Info("I'm Controller")
remote := c.Sev.RemoteInfo()
return &infra.JSONResponse{Object: remote}
}

// GetHello handles the GET: /hello route.
func (c *Default) GetHello() string {
//curl http://127.0.0.1:8000/hello
field := freedom.LogFields{
"framework": "freedom",
"like": "DDD",
Expand All @@ -64,11 +65,13 @@ func (c *Default) GetHello() string {

// PutHello handles the PUT: /hello route.
func (c *Default) PutHello() freedom.Result {
//curl -X PUT http://127.0.0.1:8000/hello
return &infra.JSONResponse{Object: "putHello"}
}

// PostHello handles the POST: /hello route.
func (c *Default) PostHello() freedom.Result {
//curl -X POST -d '{"userName":"freedom","userPassword":"freedom"}' http://127.0.0.1:8000/hello
var postJSONData struct {
UserName string `json:"userName" validate:"required"`
UserPassword string `json:"userPassword" validate:"required"`
Expand All @@ -77,26 +80,55 @@ func (c *Default) PostHello() freedom.Result {
return &infra.JSONResponse{Error: err}
}

return &infra.JSONResponse{Object: "postHello"}
return &infra.JSONResponse{Object: postJSONData}
}

// PutHello handles the DELETE: /hello route.
func (c *Default) DeleteHello() freedom.Result {
//curl -X DELETE http://127.0.0.1:8000/hello
return &infra.JSONResponse{Object: "deleteHello"}
}

/* Can use more than one, the factory will make sure
that the correct http methods are being registered for each route
for this controller, uncomment these if you want:
func (c *Default) ConnectHello() {}
func (c *Default) HeadHello() {}
func (c *Default) PatchHello() {}
func (c *Default) OptionsHello() {}
func (c *Default) TraceHello() {}
*/

// BeforeActivation .
func (c *Default) BeforeActivation(b freedom.BeforeActivation) {
b.Handle("ANY", "/custom", "CustomHello")
//b.Handle("GET", "/custom", "CustomHello")
//b.Handle("PUT", "/custom", "CustomHello")
//b.Handle("POST", "/custom", "CustomHello")
b.Handle("GET", "/customPath/{id:int64}/{uid:int}/{username:string}", "CustomPath")
b.Handle("ANY", "/custom", "Custom")
//b.Handle("GET", "/custom", "Custom")
//b.Handle("PUT", "/custom", "Custom")
//b.Handle("POST", "/custom", "Custom")
//b.Handle("DELETE", "/custom", "Custom")
}

// CustomPath handles the GET: /customPath/{id:int64}/{uid:int}/{username:string} route.
func (c *Default) CustomPath(id int64, uid int, username string) freedom.Result {
//curl http://127.0.0.1:8000/customPath/1/2/freedom
return &infra.JSONResponse{Object: map[string]interface{}{"id": id, "uid": uid, "username": username}}
}

// CustomHello handles the POST: /hello route.
func (c *Default) CustomHello() freedom.Result {
// Custom handles the ANY: /custom route.
func (c *Default) Custom() freedom.Result {
//curl http://127.0.0.1:8000/custom
//curl -X PUT http://127.0.0.1:8000/custom
//curl -X DELETE http://127.0.0.1:8000/custom
//curl -X POST http://127.0.0.1:8000/custom
method := c.Worker.IrisContext().Request().Method
c.Worker.Logger().Info("CustomHello", freedom.LogFields{"method": method})
return &infra.JSONResponse{Object: method + "CustomHello"}
return &infra.JSONResponse{Object: method + "/Custom"}
}

// GetUserBy handles the GET: /user/{username:string} route.
func (c *Default) GetUserBy(username string) freedom.Result {
//curl 'http://127.0.0.1:8000/user/freedom?token=ftoken123&id=1&ip=192&ip=168&ip=1&ip=1'
var query struct {
Token string `url:"token" validate:"required"`
ID int64 `url:"id" validate:"required"`
Expand All @@ -120,6 +152,7 @@ func (c *Default) GetUserBy(username string) freedom.Result {

// GetAgeByUserBy handles the GET: /age/{age:int}/user/{user:string} route.
func (c *Default) GetAgeByUserBy(age int, user string) freedom.Result {
//curl http://127.0.0.1:8000/age/20/user/freedom
var result struct {
User string
Age int
Expand All @@ -132,6 +165,7 @@ func (c *Default) GetAgeByUserBy(age int, user string) freedom.Result {

// PostForm handles the Post: /form route.
func (c *Default) PostForm() freedom.Result {
//curl -X POST --data "userName=freedom&[email protected]&myData=data1&myData=data2" http://127.0.0.1:8000/form
var visitor struct {
UserName string `form:"userName" validate:"required"`
Mail string `form:"mail" validate:"required"`
Expand All @@ -147,6 +181,7 @@ func (c *Default) PostForm() freedom.Result {

// PostFile handles the Post: /file route.
func (c *Default) PostFile() freedom.Result {
//curl -X POST -F "file=@example/base/adapter/controller/default.go" http://127.0.0.1:8000/file
file, info, err := c.Worker.IrisContext().FormFile("file")
if err != nil {
return &infra.JSONResponse{Error: err}
Expand All @@ -159,8 +194,7 @@ func (c *Default) PostFile() freedom.Result {
return &infra.JSONResponse{Error: err}
}
defer out.Close()
fmt.Println("file:", os.TempDir()+fname)
_, err = io.Copy(out, file)

return &infra.JSONResponse{Error: err}
return &infra.JSONResponse{Error: err, Object: os.TempDir() + fname}
}
2 changes: 1 addition & 1 deletion freedom/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

const (
versionNum = "v1.9.2"
versionNum = "v1.9.3"
)

var (
Expand Down
Loading

0 comments on commit 73fa26c

Please sign in to comment.