-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from unusualcodeorg/micro
add micro service arch
- Loading branch information
Showing
10 changed files
with
253 additions
and
2 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,19 @@ | ||
package micro | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type NatsContext struct { | ||
context.Context | ||
Client *NatsClient | ||
Subject string | ||
} | ||
|
||
func NewNatsContext(client *NatsClient, baseSub string) *NatsContext { | ||
return &NatsContext{ | ||
Context: context.Background(), | ||
Client: client, | ||
Subject: baseSub, | ||
} | ||
} |
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,24 @@ | ||
package micro | ||
|
||
import ( | ||
"github.com/unusualcodeorg/goserve/arch/network" | ||
) | ||
|
||
type baseController struct { | ||
network.BaseController | ||
natsCtx *NatsContext | ||
} | ||
|
||
func NewBaseController(basePath string, authProvider network.AuthenticationProvider, authorizeProvider network.AuthorizationProvider) BaseController { | ||
return &baseController{ | ||
BaseController: network.NewBaseController(basePath, authProvider, authorizeProvider), | ||
} | ||
} | ||
|
||
func (c *baseController) SetNatsContext(ctx *NatsContext) { | ||
c.natsCtx = ctx | ||
} | ||
|
||
func (c *baseController) NatsContext() *NatsContext { | ||
return c.natsCtx | ||
} |
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,34 @@ | ||
package micro | ||
|
||
import ( | ||
"github.com/nats-io/nats.go/micro" | ||
"github.com/unusualcodeorg/goserve/arch/network" | ||
) | ||
|
||
type NatsGroup = micro.Group | ||
type NatsHandlerFunc = micro.HandlerFunc | ||
type NatsRequest = micro.Request | ||
|
||
type Config struct { | ||
NatsUrl string | ||
NatsServiceName string | ||
NatsServiceVersion string | ||
} | ||
|
||
type BaseController interface { | ||
network.BaseController | ||
SetNatsContext(ctx *NatsContext) | ||
NatsContext() *NatsContext | ||
} | ||
|
||
type Controller interface { | ||
BaseController | ||
MountNats(group NatsGroup) | ||
} | ||
|
||
type Router interface { | ||
network.BaseRouter | ||
NatsClient() *NatsClient | ||
Disconnect() | ||
LoadControllers(controllers []Controller) | ||
} |
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,19 @@ | ||
package micro | ||
|
||
type Message[T any] struct { | ||
Data *T `json:"data,omitempty"` | ||
Error *string `json:"error,omitempty"` | ||
} | ||
|
||
func NewMessage[T any](data *T, err error) *Message[T] { | ||
var e *string | ||
if err != nil { | ||
er := err.Error() | ||
e = &er | ||
} | ||
|
||
return &Message[T]{ | ||
Data: data, | ||
Error: e, | ||
} | ||
} |
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,40 @@ | ||
package micro | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/nats-io/nats.go" | ||
"github.com/nats-io/nats.go/micro" | ||
) | ||
|
||
type NatsClient struct { | ||
Conn *nats.Conn | ||
Service micro.Service | ||
Timeout time.Duration | ||
} | ||
|
||
func NewNatsClient(config Config) *NatsClient { | ||
fmt.Println("connecting to nats..") | ||
|
||
nc, err := nats.Connect(config.NatsUrl) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
srv, err := micro.AddService(nc, micro.Config{ | ||
Name: config.NatsServiceName, | ||
Version: config.NatsServiceVersion, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println("connected to nats..") | ||
|
||
return &NatsClient{ | ||
Conn: nc, | ||
Service: srv, | ||
Timeout: nats.DefaultTimeout, | ||
} | ||
} |
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,37 @@ | ||
package micro | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
func Respond[T any](req NatsRequest, data *T, err error) { | ||
req.RespondJSON(NewMessage(data, err)) | ||
} | ||
|
||
func Request[T any, V any](ctx *NatsContext, sub string, send *T, receive *V) (*V, error) { | ||
sendMsg := NewMessage(send, nil) | ||
sendPayload, err := json.Marshal(sendMsg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
subject := fmt.Sprintf(`%s.%s`, ctx.Subject, sub) | ||
msg, err := ctx.Client.Conn.Request(subject, sendPayload, ctx.Client.Timeout) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var receiveMsg Message[V] | ||
err = json.Unmarshal(msg.Data, &receiveMsg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if receiveMsg.Error != nil { | ||
err = errors.New(*receiveMsg.Error) | ||
} | ||
|
||
return receiveMsg.Data, err | ||
} |
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,65 @@ | ||
package micro | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/go-playground/validator/v10" | ||
"github.com/unusualcodeorg/goserve/arch/network" | ||
) | ||
|
||
type router struct { | ||
netRouter network.Router | ||
natsClient *NatsClient | ||
} | ||
|
||
func NewRouter(mode string, config Config) Router { | ||
natsClient := NewNatsClient(config) | ||
return &router{ | ||
netRouter: network.NewRouter(mode), | ||
natsClient: natsClient, | ||
} | ||
} | ||
|
||
func (r *router) GetEngine() *gin.Engine { | ||
return r.netRouter.GetEngine() | ||
} | ||
|
||
func (r *router) NatsClient() *NatsClient { | ||
return r.natsClient | ||
} | ||
|
||
func (r *router) Disconnect() { | ||
r.natsClient.Conn.Close() | ||
} | ||
|
||
func (r *router) LoadRootMiddlewares(middlewares []network.RootMiddleware) { | ||
r.netRouter.LoadRootMiddlewares(middlewares) | ||
} | ||
|
||
func (r *router) LoadControllers(controllers []Controller) { | ||
nc := make([]network.Controller, len(controllers)) | ||
for i, c := range controllers { | ||
nc[i] = c.(network.Controller) | ||
} | ||
r.netRouter.LoadControllers(nc) | ||
|
||
for _, c := range controllers { | ||
baseSub := fmt.Sprintf(`%s.%s`, r.natsClient.Service.Info().Name, strings.ReplaceAll(c.Path(), "/", "")) | ||
|
||
ctx := NewNatsContext(r.natsClient, baseSub) | ||
c.SetNatsContext(ctx) | ||
|
||
ng := r.natsClient.Service.AddGroup(baseSub) | ||
c.MountNats(ng) | ||
} | ||
} | ||
|
||
func (r *router) Start(ip string, port uint16) { | ||
r.netRouter.Start(ip, port) | ||
} | ||
|
||
func (r *router) RegisterValidationParsers(tagNameFunc validator.TagNameFunc) { | ||
r.netRouter.RegisterValidationParsers(tagNameFunc) | ||
} |
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