work in progress: message: interfaces, implementations

This commit is contained in:
Helmut Merz 2023-07-04 07:46:05 +02:00
parent b1cd65ea05
commit 8d8b64f8e7
5 changed files with 37 additions and 7 deletions

View file

@ -53,6 +53,17 @@ type Context interface {
// message, address // message, address
type MsgHead interface {
Action() string
}
type Message interface {
MsgHead
Head() MsgHead
Sender() Address
Payload() Payload
}
type Address interface { type Address interface {
fmt.Stringer fmt.Stringer
Url() string Url() string
@ -62,9 +73,9 @@ type Address interface {
SetInteraction(string, string) SetInteraction(string, string)
} }
type Message interface { type Payload interface {
Action() string fmt.Stringer
Sender() Address //Data() interface{}
} }
// action // action

View file

@ -3,6 +3,8 @@ package message
import ( import (
"fmt" "fmt"
"strings" "strings"
"git.sr.ht/~cco/go-scopes/lib"
) )
// Address implementation // Address implementation
@ -36,13 +38,13 @@ func (addr *address) SetInteraction(sid, iid string) {
addr.iid = iid addr.iid = iid
} }
func SimpleAddress(srv string) *address { func SimpleAddress(srv string) lib.Address {
return &address{ return &address{
srv: srv, srv: srv,
} }
} }
func ParseAddress(s string) *address { func ParseAddress(s string) lib.Address {
addr := address{} addr := address{}
p := strings.SplitN(s, "#", 2) p := strings.SplitN(s, "#", 2)
if len(p) > 1 { if len(p) > 1 {

View file

@ -8,6 +8,10 @@ import (
type strMessage string type strMessage string
func (msg strMessage) Head() lib.MsgHead {
return msg
}
func (msg strMessage) Action() string { func (msg strMessage) Action() string {
return string(msg) return string(msg)
} }
@ -16,7 +20,11 @@ func (msg strMessage) Sender() lib.Address {
return nil return nil
} }
func StrMessage(action string) strMessage { func (msg strMessage) Payload() lib.Payload {
return nil
}
func StrMessage(action string) lib.Message {
return strMessage(action) return strMessage(action)
} }

View file

@ -19,7 +19,7 @@ type ServerState struct {
} }
func Start(ctx lib.Context) { func Start(ctx lib.Context) {
gin.SetMode(gin.ReleaseMode) //gin.SetMode(gin.ReleaseMode)
lib.GetCfg[*Cfg](ctx).BaseCfg.WithDoneHandler(HandleDone) lib.GetCfg[*Cfg](ctx).BaseCfg.WithDoneHandler(HandleDone)
Serve(ctx) Serve(ctx)
lib.RunCtx(ctx, core.Listen) lib.RunCtx(ctx, core.Listen)
@ -33,6 +33,7 @@ func HandleDone(ctx lib.Context) bool {
func Serve(ctx lib.Context) { func Serve(ctx lib.Context) {
//r := gin.Default() //r := gin.Default()
r := gin.New() r := gin.New()
r.Use(gin.Recovery())
r.Use(Logger(ctx)) r.Use(Logger(ctx))
r.GET("/*action", func(c *gin.Context) { r.GET("/*action", func(c *gin.Context) {
c.String(http.StatusOK, "Hello World") c.String(http.StatusOK, "Hello World")

View file

@ -12,6 +12,7 @@ import (
func TestUnit(tb *tbase.T) { func TestUnit(tb *tbase.T) {
t := testing.SetUp(tb) t := testing.SetUp(tb)
t.Run("address", AddressTest) t.Run("address", AddressTest)
t.Run("strmsg", StrMessageTest)
} }
func AddressTest(t *testing.T) { func AddressTest(t *testing.T) {
@ -33,3 +34,10 @@ func AddressTest(t *testing.T) {
ad1.SetInteraction("1abc", "2cde") ad1.SetInteraction("1abc", "2cde")
t.AssertEqual(fmt.Sprint(ad1), "https://scopes.cy7.eu#worker/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")
}