work in progress: message: interfaces, implementations
This commit is contained in:
parent
b1cd65ea05
commit
8d8b64f8e7
5 changed files with 37 additions and 7 deletions
17
lib/lib.go
17
lib/lib.go
|
@ -53,6 +53,17 @@ type Context interface {
|
|||
|
||||
// message, address
|
||||
|
||||
type MsgHead interface {
|
||||
Action() string
|
||||
}
|
||||
|
||||
type Message interface {
|
||||
MsgHead
|
||||
Head() MsgHead
|
||||
Sender() Address
|
||||
Payload() Payload
|
||||
}
|
||||
|
||||
type Address interface {
|
||||
fmt.Stringer
|
||||
Url() string
|
||||
|
@ -62,9 +73,9 @@ type Address interface {
|
|||
SetInteraction(string, string)
|
||||
}
|
||||
|
||||
type Message interface {
|
||||
Action() string
|
||||
Sender() Address
|
||||
type Payload interface {
|
||||
fmt.Stringer
|
||||
//Data() interface{}
|
||||
}
|
||||
|
||||
// action
|
||||
|
|
|
@ -3,6 +3,8 @@ package message
|
|||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
)
|
||||
|
||||
// Address implementation
|
||||
|
@ -36,13 +38,13 @@ func (addr *address) SetInteraction(sid, iid string) {
|
|||
addr.iid = iid
|
||||
}
|
||||
|
||||
func SimpleAddress(srv string) *address {
|
||||
func SimpleAddress(srv string) lib.Address {
|
||||
return &address{
|
||||
srv: srv,
|
||||
}
|
||||
}
|
||||
|
||||
func ParseAddress(s string) *address {
|
||||
func ParseAddress(s string) lib.Address {
|
||||
addr := address{}
|
||||
p := strings.SplitN(s, "#", 2)
|
||||
if len(p) > 1 {
|
||||
|
|
|
@ -8,6 +8,10 @@ import (
|
|||
|
||||
type strMessage string
|
||||
|
||||
func (msg strMessage) Head() lib.MsgHead {
|
||||
return msg
|
||||
}
|
||||
|
||||
func (msg strMessage) Action() string {
|
||||
return string(msg)
|
||||
}
|
||||
|
@ -16,7 +20,11 @@ func (msg strMessage) Sender() lib.Address {
|
|||
return nil
|
||||
}
|
||||
|
||||
func StrMessage(action string) strMessage {
|
||||
func (msg strMessage) Payload() lib.Payload {
|
||||
return nil
|
||||
}
|
||||
|
||||
func StrMessage(action string) lib.Message {
|
||||
return strMessage(action)
|
||||
}
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ type ServerState struct {
|
|||
}
|
||||
|
||||
func Start(ctx lib.Context) {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
//gin.SetMode(gin.ReleaseMode)
|
||||
lib.GetCfg[*Cfg](ctx).BaseCfg.WithDoneHandler(HandleDone)
|
||||
Serve(ctx)
|
||||
lib.RunCtx(ctx, core.Listen)
|
||||
|
@ -33,6 +33,7 @@ func HandleDone(ctx lib.Context) bool {
|
|||
func Serve(ctx lib.Context) {
|
||||
//r := gin.Default()
|
||||
r := gin.New()
|
||||
r.Use(gin.Recovery())
|
||||
r.Use(Logger(ctx))
|
||||
r.GET("/*action", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "Hello World")
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
func TestUnit(tb *tbase.T) {
|
||||
t := testing.SetUp(tb)
|
||||
t.Run("address", AddressTest)
|
||||
t.Run("strmsg", StrMessageTest)
|
||||
}
|
||||
|
||||
func AddressTest(t *testing.T) {
|
||||
|
@ -33,3 +34,10 @@ func AddressTest(t *testing.T) {
|
|||
ad1.SetInteraction("1abc", "2cde")
|
||||
t.AssertEqual(fmt.Sprint(ad1), "https://scopes.cy7.eu#worker/1abc/2cde")
|
||||
}
|
||||
|
||||
func StrMessageTest(t *testing.T) {
|
||||
t.AssertEqual(message.Quit.Action(), "quit")
|
||||
var msg lib.Message = message.StrMessage("doit")
|
||||
t.AssertEqual(msg.Action(), "doit")
|
||||
t.AssertEqual(msg.Head().Action(), "doit")
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue