-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.Support gateway;2.Add annotation;3.Simplify syntax and struct
- Loading branch information
1 parent
7614853
commit 7ea8581
Showing
19 changed files
with
307 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright (c) 2019. ENNOO - All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package bow | ||
|
||
import ( | ||
"fmt" | ||
"github.com/ennoo/rivet/trans/request" | ||
"github.com/ennoo/rivet/trans/response" | ||
"github.com/ennoo/rivet/utils/log" | ||
"github.com/gin-gonic/gin" | ||
"go.uber.org/zap" | ||
"sync" | ||
) | ||
|
||
var ( | ||
instance *Bow | ||
once sync.Once | ||
serviceCount = 0 | ||
routeServices = make(map[string]*RouteService) | ||
) | ||
|
||
// GetBowInstance 获取路由管理对象 Bow 单例 | ||
func GetBowInstance() *Bow { | ||
once.Do(func() { | ||
instance = &Bow{AllWay: make(map[string]*RouteService)} | ||
}) | ||
return instance | ||
} | ||
|
||
// Bow 路由入口对象 | ||
type Bow struct { | ||
AllWay map[string]*RouteService | ||
} | ||
|
||
// RouteService 路由对象 | ||
type RouteService struct { | ||
Name string | ||
InURI string | ||
OutRemote string | ||
OutURI string | ||
} | ||
|
||
// Add 新增路由服务数组 | ||
func (s *Bow) Add(routeServiceArr ...*RouteService) { | ||
for index := range routeServiceArr { | ||
routeService := routeServiceArr[index] | ||
routeServices[routeService.Name] = routeService | ||
GetBowInstance().register(routeService) | ||
serviceCount++ | ||
} | ||
} | ||
|
||
// AddService 新增路由服务 | ||
func (s *Bow) AddService(serviceName, inURI, outRemote, outURI string) { | ||
routeServices[serviceName] = &RouteService{ | ||
Name: serviceName, | ||
InURI: inURI, | ||
OutRemote: outRemote, | ||
OutURI: outURI, | ||
} | ||
GetBowInstance().register(&RouteService{ | ||
Name: serviceName, | ||
InURI: inURI, | ||
OutRemote: outRemote, | ||
OutURI: outURI, | ||
}) | ||
serviceCount++ | ||
} | ||
|
||
// Register 注册新的路由方式 | ||
func (s *Bow) register(routeService *RouteService) { | ||
instance.AllWay[routeService.Name] = routeService | ||
} | ||
|
||
// RunBow 开启路由 | ||
func RunBow(context *gin.Context, serviceName string) { | ||
routeService, ok := instance.AllWay[serviceName] | ||
if !ok { | ||
err := fmt.Errorf("routeService not fount") | ||
fmt.Println("not found ", serviceName) | ||
log.Shunt.Error(err.Error(), zap.String("serviceName", serviceName)) | ||
} else { | ||
request.SyncPoolGetRequest().Call(context, context.Request.Method, routeService.OutRemote, routeService.OutURI) | ||
} | ||
} | ||
|
||
// RunBowCallback 开启路由并处理降级 | ||
func RunBowCallback(context *gin.Context, serviceName string, f func() *response.Result) { | ||
routeService, ok := instance.AllWay[serviceName] | ||
if !ok { | ||
err := fmt.Errorf("service not fount") | ||
fmt.Println("not found ", serviceName) | ||
log.Shunt.Error(err.Error(), zap.String("serviceName", serviceName)) | ||
} else { | ||
request.SyncPoolGetRequest().Callback(context, context.Request.Method, routeService.OutRemote, routeService.OutURI, f) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2019. ENNOO - All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package bow | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// Route 网关服务路由 | ||
func Route(engine *gin.Engine) { | ||
// 仓库相关路由设置 | ||
vRepo := engine.Group("/") | ||
for x := range routeServices { | ||
bowService := routeServices[x] | ||
vRepo.Any(bowService.InURI, func(context *gin.Context) { | ||
RunBow(context, bowService.Name) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2019. ENNOO - All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"github.com/ennoo/rivet/rivet" | ||
"github.com/ennoo/rivet/utils/env" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
rivet.Initialize(true, false, true, false) | ||
rivet.Bow().AddService("test1", "hello1", "http://localhost:8081", "rivet/shunt") | ||
rivet.Bow().AddService("test2", "hello2", "https://localhost:8092", "rivet/shunt") | ||
rivet.ListenAndServe(&rivet.ListenServe{ | ||
Engine: rivet.SetupRouter(), | ||
DefaultPort: "8084", | ||
}, strings.Join([]string{env.GetEnv(env.GOPath), "/src/github.com/ennoo/rivet/examples/tls/rootCA.crt"}, "")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2019. ENNOO - All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"github.com/ennoo/rivet/bow" | ||
"github.com/ennoo/rivet/rivet" | ||
"github.com/ennoo/rivet/utils/env" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
rivet.Initialize(true, false, true, false) | ||
rivet.Bow().Add( | ||
&bow.RouteService{ | ||
Name: "test1", | ||
InURI: "hello1", | ||
OutRemote: "http://localhost:8081", | ||
OutURI: "rivet/shunt", | ||
}, | ||
&bow.RouteService{ | ||
Name: "test2", | ||
InURI: "hello2", | ||
OutRemote: "https://localhost:8092", | ||
OutURI: "rivet/shunt", | ||
}, | ||
) | ||
rivet.ListenAndServe(&rivet.ListenServe{ | ||
Engine: rivet.SetupRouter(), | ||
DefaultPort: "8085", | ||
}, strings.Join([]string{env.GetEnv(env.GOPath), "/src/github.com/ennoo/rivet/examples/tls/rootCA.crt"}, "")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.