├── 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 |