├── http ├── middleware │ └── README.md ├── routes.go └── controller │ └── demo_handler.go ├── go.mod ├── cmd └── goweblayout │ └── main.go ├── model └── demo.go ├── .gitignore ├── demo ├── logic.go ├── repository.go ├── logic │ ├── demo_logic.go │ └── demo_logic_test.go └── repository │ ├── demo_file.go │ └── demo_file_test.go ├── README.md └── LICENSE /http/middleware/README.md: -------------------------------------------------------------------------------- 1 | 实现 http 中间件,不同框架可能不太一样。 -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/polaris1119/go-web-layout 2 | 3 | go 1.12 4 | -------------------------------------------------------------------------------- /cmd/goweblayout/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | myhttp "github.com/polaris1119/go-web-layout/http" 5 | "log" 6 | "net/http" 7 | ) 8 | 9 | func main() { 10 | myhttp.Route() 11 | 12 | log.Fatal(http.ListenAndServe(":6061", nil)) 13 | } 14 | -------------------------------------------------------------------------------- /model/demo.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | import "strconv" 4 | 5 | type Demo struct { 6 | ID int `json:"id"` 7 | Name string `json:"name"` 8 | } 9 | 10 | func (d *Demo) String() string { 11 | return "id:" + strconv.Itoa(d.ID) + ";name:" + d.Name 12 | } 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, build with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | .idea 15 | -------------------------------------------------------------------------------- /demo/logic.go: -------------------------------------------------------------------------------- 1 | package demo 2 | 3 | import ( 4 | "context" 5 | "github.com/polaris1119/go-web-layout/model" 6 | ) 7 | 8 | type Logic interface { 9 | Fetch(ctx context.Context, id int) (*model.Demo, error) 10 | Create(ctx context.Context, data string) error 11 | } 12 | -------------------------------------------------------------------------------- /http/routes.go: -------------------------------------------------------------------------------- 1 | package http 2 | 3 | import ( 4 | "github.com/polaris1119/go-web-layout/demo/logic" 5 | "github.com/polaris1119/go-web-layout/demo/repository" 6 | "github.com/polaris1119/go-web-layout/http/controller" 7 | ) 8 | 9 | // 存放路由 10 | func Route() { 11 | file := "demo.json" 12 | controller.NewDemoHttpHandler(logic.NewDemoLogic(repository.NewFileRepository(file))) 13 | } 14 | -------------------------------------------------------------------------------- /demo/repository.go: -------------------------------------------------------------------------------- 1 | package demo 2 | 3 | import ( 4 | "context" 5 | "github.com/polaris1119/go-web-layout/model" 6 | ) 7 | 8 | type Repository interface { 9 | Create(ctx context.Context, demo *model.Demo) error 10 | FindById(ctx context.Context, id int) (*model.Demo, error) 11 | Update(ctx context.Context, demo *model.Demo) error 12 | Delete(ctx context.Context, id int) error 13 | } 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-web-layout 2 | 基于整洁架构结合其他语言成熟结构整理的 Go 语言 Web 目录结构 3 | 4 | ## 参考文章 5 | 6 | - [【译】The Clean Architecture](https://studygolang.com/articles/20075) 7 | - [使用 Golang 构建整洁架构](https://studygolang.com/articles/12530) 8 | - [在 Golang 中尝试简洁架构](https://studygolang.com/articles/12909) 9 | - [Applying The Clean Architecture to Go applications](https://manuel.kiessling.net/2012/09/28/applying-the-clean-architecture-to-go-applications/) 10 | - [Go 语言的简洁架构](https://studygolang.com/articles/20076) -------------------------------------------------------------------------------- /demo/logic/demo_logic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | "encoding/json" 6 | "github.com/polaris1119/go-web-layout/demo" 7 | "github.com/polaris1119/go-web-layout/model" 8 | ) 9 | 10 | type demoLogic struct { 11 | repository demo.Repository 12 | } 13 | 14 | func NewDemoLogic(repository demo.Repository) *demoLogic { 15 | return &demoLogic{repository: repository} 16 | } 17 | 18 | func (dl *demoLogic) Fetch(ctx context.Context, id int) (*model.Demo, error) { 19 | return dl.repository.FindById(ctx, id) 20 | } 21 | 22 | func (dl *demoLogic) Create(ctx context.Context, data string) error { 23 | d := &model.Demo{} 24 | err := json.Unmarshal([]byte(data), d) 25 | if err != nil { 26 | return err 27 | } 28 | 29 | return dl.repository.Create(ctx, d) 30 | } 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 徐新华 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /demo/repository/demo_file.go: -------------------------------------------------------------------------------- 1 | package repository 2 | 3 | import ( 4 | "context" 5 | "encoding/json" 6 | "io/ioutil" 7 | "os" 8 | 9 | "github.com/polaris1119/go-web-layout/model" 10 | ) 11 | 12 | type fileRepository struct { 13 | filename string 14 | } 15 | 16 | func NewFileRepository(filename string) *fileRepository { 17 | return &fileRepository{ 18 | filename: filename, 19 | } 20 | } 21 | 22 | func (f *fileRepository) Create(ctx context.Context, demo *model.Demo) error { 23 | b, err := json.Marshal(demo) 24 | if err != nil { 25 | return err 26 | } 27 | 28 | return ioutil.WriteFile(f.filename, b, 0664) 29 | } 30 | 31 | func (f *fileRepository) FindById(ctx context.Context, id int) (*model.Demo, error) { 32 | file, err := os.Open(f.filename) 33 | if err != nil { 34 | return nil, err 35 | } 36 | defer file.Close() 37 | 38 | d := &model.Demo{} 39 | 40 | decoder := json.NewDecoder(file) 41 | err = decoder.Decode(d) 42 | 43 | return d, err 44 | } 45 | 46 | func (f *fileRepository) Update(ctx context.Context, demo *model.Demo) error { 47 | return nil 48 | } 49 | 50 | func (f *fileRepository) Delete(ctx context.Context, id int) error { 51 | return nil 52 | } 53 | -------------------------------------------------------------------------------- /demo/logic/demo_logic_test.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | "reflect" 6 | "testing" 7 | 8 | "github.com/polaris1119/go-web-layout/demo" 9 | "github.com/polaris1119/go-web-layout/model" 10 | ) 11 | 12 | func Test_demoLogic_Fetch(t *testing.T) { 13 | type fields struct { 14 | repository demo.Repository 15 | } 16 | type args struct { 17 | ctx context.Context 18 | id int 19 | } 20 | tests := []struct { 21 | name string 22 | fields fields 23 | args args 24 | want *model.Demo 25 | wantErr bool 26 | }{ 27 | // TODO: Add test cases. 28 | } 29 | for _, tt := range tests { 30 | t.Run(tt.name, func(t *testing.T) { 31 | dl := &demoLogic{ 32 | repository: tt.fields.repository, 33 | } 34 | got, err := dl.Fetch(tt.args.ctx, tt.args.id) 35 | if (err != nil) != tt.wantErr { 36 | t.Errorf("demoLogic.Fetch() error = %v, wantErr %v", err, tt.wantErr) 37 | return 38 | } 39 | if !reflect.DeepEqual(got, tt.want) { 40 | t.Errorf("demoLogic.Fetch() = %v, want %v", got, tt.want) 41 | } 42 | }) 43 | } 44 | } 45 | 46 | func Test_demoLogic_Create(t *testing.T) { 47 | type fields struct { 48 | repository demo.Repository 49 | } 50 | type args struct { 51 | ctx context.Context 52 | data string 53 | } 54 | tests := []struct { 55 | name string 56 | fields fields 57 | args args 58 | wantErr bool 59 | }{ 60 | // TODO: Add test cases. 61 | } 62 | for _, tt := range tests { 63 | t.Run(tt.name, func(t *testing.T) { 64 | dl := &demoLogic{ 65 | repository: tt.fields.repository, 66 | } 67 | if err := dl.Create(tt.args.ctx, tt.args.data); (err != nil) != tt.wantErr { 68 | t.Errorf("demoLogic.Create() error = %v, wantErr %v", err, tt.wantErr) 69 | } 70 | }) 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /http/controller/demo_handler.go: -------------------------------------------------------------------------------- 1 | package controller 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "github.com/polaris1119/go-web-layout/demo" 7 | "html/template" 8 | "net/http" 9 | "strconv" 10 | ) 11 | 12 | type DemoHttpHandler struct { 13 | logic demo.Logic 14 | } 15 | 16 | func NewDemoHttpHandler(demoLogic demo.Logic) *DemoHttpHandler { 17 | handler := &DemoHttpHandler{ 18 | logic: demoLogic, 19 | } 20 | 21 | http.HandleFunc("/demo/detail", handler.Fetch) 22 | http.HandleFunc("/demo/create", handler.Create) 23 | http.HandleFunc("/", handler.Index) 24 | 25 | return handler 26 | } 27 | 28 | func (dh *DemoHttpHandler) Index(writer http.ResponseWriter, req *http.Request) { 29 | htmlTxt := ` 30 | 31 | 32 | 33 | 34 | Demo 35 | 36 | 37 |
38 | JSON 数据: 39 | 40 |
41 | 获取数据 42 | 43 | ` 44 | 45 | tpl, err := template.New("demo").Parse(htmlTxt) 46 | if err != nil { 47 | fmt.Fprintln(writer, "parse error:", err) 48 | return 49 | } 50 | 51 | err = tpl.Execute(writer, nil) 52 | if err != nil { 53 | fmt.Fprintln(writer, "execute error:", err) 54 | return 55 | } 56 | } 57 | 58 | func (dh *DemoHttpHandler) Fetch(writer http.ResponseWriter, req *http.Request) { 59 | id := mustInt(req.FormValue("id")) 60 | 61 | ctx := context.Background() 62 | 63 | demo, err := dh.logic.Fetch(ctx, id) 64 | if err != nil { 65 | fmt.Fprintln(writer, err) 66 | return 67 | } 68 | 69 | fmt.Fprintln(writer, demo) 70 | } 71 | 72 | func (dh *DemoHttpHandler) Create(writer http.ResponseWriter, req *http.Request) { 73 | data := req.FormValue("data") 74 | 75 | ctx := context.Background() 76 | 77 | err := dh.logic.Create(ctx, data) 78 | if err != nil { 79 | fmt.Fprintln(writer, err) 80 | return 81 | } 82 | 83 | fmt.Fprintln(writer, "创建成功") 84 | } 85 | 86 | func mustInt(s string, defVal ...int) int { 87 | i, err := strconv.Atoi(s) 88 | if err != nil { 89 | if len(defVal) > 0 { 90 | return defVal[0] 91 | } 92 | } 93 | 94 | return i 95 | } 96 | -------------------------------------------------------------------------------- /demo/repository/demo_file_test.go: -------------------------------------------------------------------------------- 1 | package repository 2 | 3 | import ( 4 | "context" 5 | "os" 6 | "reflect" 7 | "testing" 8 | 9 | "github.com/polaris1119/go-web-layout/model" 10 | ) 11 | 12 | func Test_fileRepository_Create(t *testing.T) { 13 | type fields struct { 14 | filename string 15 | } 16 | type args struct { 17 | ctx context.Context 18 | demo *model.Demo 19 | } 20 | tests := []struct { 21 | name string 22 | fields fields 23 | args args 24 | wantErr bool 25 | }{ 26 | { 27 | "first", 28 | fields{"tmp.json"}, 29 | args{context.Background(), &model.Demo{ID: 1, Name: "polaris"}}, 30 | false, 31 | }, 32 | } 33 | defer func() { 34 | os.Remove("tmp.json") 35 | }() 36 | for _, tt := range tests { 37 | t.Run(tt.name, func(t *testing.T) { 38 | f := &fileRepository{ 39 | filename: tt.fields.filename, 40 | } 41 | if err := f.Create(tt.args.ctx, tt.args.demo); (err != nil) != tt.wantErr { 42 | t.Errorf("fileRepository.Create() error = %v, wantErr %v", err, tt.wantErr) 43 | } 44 | }) 45 | } 46 | } 47 | 48 | func Test_fileRepository_FindById(t *testing.T) { 49 | type fields struct { 50 | filename string 51 | } 52 | type args struct { 53 | ctx context.Context 54 | id int 55 | } 56 | tests := []struct { 57 | name string 58 | fields fields 59 | args args 60 | want *model.Demo 61 | wantErr bool 62 | }{ 63 | // TODO: Add test cases. 64 | } 65 | for _, tt := range tests { 66 | t.Run(tt.name, func(t *testing.T) { 67 | f := &fileRepository{ 68 | filename: tt.fields.filename, 69 | } 70 | got, err := f.FindById(tt.args.ctx, tt.args.id) 71 | if (err != nil) != tt.wantErr { 72 | t.Errorf("fileRepository.FindById() error = %v, wantErr %v", err, tt.wantErr) 73 | return 74 | } 75 | if !reflect.DeepEqual(got, tt.want) { 76 | t.Errorf("fileRepository.FindById() = %v, want %v", got, tt.want) 77 | } 78 | }) 79 | } 80 | } 81 | 82 | func Test_fileRepository_Update(t *testing.T) { 83 | type fields struct { 84 | filename string 85 | } 86 | type args struct { 87 | ctx context.Context 88 | demo *model.Demo 89 | } 90 | tests := []struct { 91 | name string 92 | fields fields 93 | args args 94 | wantErr bool 95 | }{ 96 | // TODO: Add test cases. 97 | } 98 | for _, tt := range tests { 99 | t.Run(tt.name, func(t *testing.T) { 100 | f := &fileRepository{ 101 | filename: tt.fields.filename, 102 | } 103 | if err := f.Update(tt.args.ctx, tt.args.demo); (err != nil) != tt.wantErr { 104 | t.Errorf("fileRepository.Update() error = %v, wantErr %v", err, tt.wantErr) 105 | } 106 | }) 107 | } 108 | } 109 | 110 | func Test_fileRepository_Delete(t *testing.T) { 111 | type fields struct { 112 | filename string 113 | } 114 | type args struct { 115 | ctx context.Context 116 | id int 117 | } 118 | tests := []struct { 119 | name string 120 | fields fields 121 | args args 122 | wantErr bool 123 | }{ 124 | // TODO: Add test cases. 125 | } 126 | for _, tt := range tests { 127 | t.Run(tt.name, func(t *testing.T) { 128 | f := &fileRepository{ 129 | filename: tt.fields.filename, 130 | } 131 | if err := f.Delete(tt.args.ctx, tt.args.id); (err != nil) != tt.wantErr { 132 | t.Errorf("fileRepository.Delete() error = %v, wantErr %v", err, tt.wantErr) 133 | } 134 | }) 135 | } 136 | } 137 | --------------------------------------------------------------------------------