-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.md
executable file
·162 lines (126 loc) · 5.17 KB
/
README.md
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
### dandelion 多用户商城
-- 我仔细看了看蒲公英,它并不是一个娇气的花
目前主要功能:
* 1、后台管理接口
* 2、活动数据接口
* 3、组织信息管理
* 4、配制管理
* 5、内容管理与发布
* 6、商品管理
* 7、日志管理
* 8、订单管理
* 9、用户管理
安装:
```sh
go get -u github.com/nbvghost/dandelion
```
## 例示代码:
### 启动程序:
```go
package main
import (
"flag"
"github.com/nbvghost/stone/internal/enter/interceptor"
"github.com/nbvghost/stone/internal/mappingEntity"
"github.com/nbvghost/tool/encryption"
"log"
"net/http"
"strings"
"time"
clientv3 "go.etcd.io/etcd/client/v3"
"github.com/nbvghost/stone/internal/enter/api"
"github.com/nbvghost/stone/internal/enter/view"
"github.com/nbvghost/stone/internal/service"
"github.com/gorilla/mux"
"github.com/nbvghost/dandelion/config"
"github.com/nbvghost/dandelion/constrain"
"github.com/nbvghost/dandelion/constrain/key"
"github.com/nbvghost/dandelion/library/contexext"
"github.com/nbvghost/dandelion/library/environments"
"github.com/nbvghost/dandelion/library/mapping"
"github.com/nbvghost/dandelion/library/result"
"github.com/nbvghost/dandelion/server/etcd"
"github.com/nbvghost/dandelion/server/httpext"
"github.com/nbvghost/dandelion/server/redis"
"github.com/nbvghost/dandelion/server/route"
"github.com/nbvghost/dandelion/server/serviceobject"
)
func init() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
func main() {
flag.Parse()
serverConfig := config.MicroServerConfig{
MicroServer: key.MicroServer{Name: "stone", ServerType: key.ServerTypeHttp},
IP: environments.IP(),
Port: environments.Port(),
}
etcdConfig := clientv3.Config{
Endpoints: environments.EtcdEndpoints(),
DialTimeout: 30 * time.Second,
Username: environments.EtcdUsername(),
//Password: environments.EtcdPassword(),
}
etcdService := etcd.NewServer(etcdConfig)
err := service.Init(etcdService, "stone")
if err != nil {
log.Fatalln(err)
}
redisOption, err := etcdService.ObtainRedis()
if err != nil {
log.Fatalln(err)
}
redisClient := redis.NewClient(*redisOption, etcdService)
if environments.Release() {
//service.StartTask(redisClient)
}
//patch.StartPatch()
var serverDesc *serviceobject.ServerDesc
serverDesc, err = etcdService.Register(serviceobject.NewServerDesc(serverConfig.MicroServer, serverConfig.Port, serverConfig.IP))
if err != nil {
panic(err)
}
//mappings := mappingEntity.New(&mappingInstance.OrganizationMapping{}, &mappingInstance.AdminMapping{})
mappings := mapping.New()
mappings.AddMapping(&mappingEntity.AdminMapping{})
engine := mux.NewRouter()
engine.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/", http.FileServer(http.Dir("view/assets"))))
subRouter := engine.PathPrefix("/").Subrouter()
r := route.New(subRouter, mappings)
r.RegisterInterceptors("/api/", []string{"/api/account/login", "/api/image/captcha", "/api/account/register"}, &interceptor.AdminInterceptor{})
api.Register(r.RegisterRoute)
view.Register(r)
httpServer := httpext.NewHttpServer(engine, subRouter, r,
httpext.WithServerDesc(serverDesc.MicroServer.Name, serverDesc.IP, serverDesc.Port),
httpext.WithRedisOption(redisClient), httpext.WithCustomizeViewRenderOption(&CustomizeViewRender{}))
httpServer.Use(&httpMiddleware{})
httpServer.Use(httpext.DefaultHttpMiddleware)
httpServer.Listen()
}
type CustomizeViewRender struct{}
func (m *CustomizeViewRender) Render(context constrain.IContext, request *http.Request, writer http.ResponseWriter, viewData constrain.IViewResult) error {
http.FileServer(http.Dir("view")).ServeHTTP(writer, request)
return nil
}
type httpMiddleware struct{}
func (m *httpMiddleware) CreateContext(redisClient constrain.IRedis, router constrain.IRoute, w http.ResponseWriter, r *http.Request) constrain.IContext {
return httpext.DefaultHttpMiddleware.CreateContext(redisClient, router, w, r)
}
func (m *httpMiddleware) Handle(ctx constrain.IContext, router constrain.IRoute, customizeViewRender constrain.IViewRender, w http.ResponseWriter, r *http.Request) (bool, error) {
contextValue := contexext.FromContext(ctx)
contextValue.Response.Header().Set("Access-Control-Allow-Origin", strings.TrimRight(r.Referer(), "/"))
contextValue.Response.Header().Set("Access-Control-Allow-Headers", "Content-Type,Page-No,Page-Size,Order-Field,Order-Method") //todo
contextValue.Response.Header().Set("Access-Control-Allow-Methods", "DELETE,POST,OPTIONS,GET,PUT") //todo
contextValue.Response.Header().Set("Access-Control-Allow-Credentials", "true") //todo
if contextValue.IsApi && contextValue.Request.Method == http.MethodOptions {
returnResult := &result.EmptyResult{}
returnResult.Apply(ctx)
return false, nil
}
return true, nil
}
```
### 技术支持(微信联系)
<img src="https://raw.githubusercontent.com/nbvghost/dandelion/master/add-me.jpg" alt="微信联系" width="256" />
### 使用IDEA开发
<a href="https://www.jetbrains.com/?from=dandelion"><img src="https://raw.githubusercontent.com/nbvghost/dandelion/master/icon-goland.png" alt="使用IDEA开发" width="128" height="128" align="bottom" /></a>