WIP: pre dep injection

This commit is contained in:
Daniel Ponte 2022-12-20 11:34:25 -05:00
parent 2de97a6936
commit a468f0629b
6 changed files with 52 additions and 26 deletions

1
go.mod
View file

@ -21,6 +21,7 @@ require (
github.com/labstack/gommon v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect

2
go.sum
View file

@ -36,6 +36,8 @@ github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2y
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

View file

@ -0,0 +1,15 @@
package component
import (
"dynatron.me/x/blasphem/pkg/blas"
)
type (
Setup func(blas.Core) (Instance, error)
Instance interface {
Shutdown()
}
Key string
)

View file

@ -0,0 +1,18 @@
package reg
import (
"fmt"
"dynatron.me/x/blasphem/pkg/component"
)
var Registry = make(map[component.Key]component.Setup)
func Register(key component.Key, c component.Setup) {
_, already := Registry[key]
if already {
panic(fmt.Sprintf("component %s already exists", key))
}
Registry[key] = c
}

View file

@ -1,7 +1,7 @@
package wsapi
import (
"encoding/json"
"io"
"dynatron.me/x/blasphem/pkg/auth"
"dynatron.me/x/blasphem/pkg/blas"
@ -32,8 +32,7 @@ type (
}
phaseHandler interface {
handleMsg(msg interface{}) error
msgSchema() interface{}
handleMsg(io.Reader) error
}
cmdHandler struct {
@ -59,13 +58,13 @@ func (ws *wsSession) Handle() error {
}
for {
msg := ws.h.msgSchema()
err := ws.ReadJSON(msg)
_, rdr, err := ws.NextReader()
if err != nil {
log.Error().Err(err).Str("remote", ws.ec.Request().RemoteAddr).Msg("websocket read fail")
return err
}
err = ws.h.handleMsg(msg)
err = ws.h.handleMsg(rdr)
if err != nil {
return err
}
@ -77,23 +76,6 @@ type cmdMsg struct {
type MsgType string
func (cm *cmdMsg) UnmarshalJSON(b []byte) error {
var t typeType struct {
Type MsgType `json:"type"`
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
}
func (ws *cmdHandler) msgSchema() interface{} {
return &cmdMsg{}
}
func (ws *cmdHandler) handleMsg(msg interface{}) error {
func (ws *cmdHandler) handleMsg(r io.Reader) error {
return nil
}

View file

@ -1,6 +1,9 @@
package wsapi
import (
"encoding/json"
"io"
"dynatron.me/x/blasphem/pkg/auth"
"github.com/rs/zerolog/log"
@ -36,8 +39,13 @@ func (ap *authPhase) finishAuth(rt *auth.RefreshToken) {
ap.h = &cmdHandler{ap.wsSession}
}
func (ap *authPhase) handleMsg(msg interface{}) error {
authMsg := msg.(*authMsg)
func (ap *authPhase) handleMsg(r io.Reader) error {
var authMsg authMsg
err := json.NewDecoder(r).Decode(&authMsg)
if err != nil {
return err
}
refreshToken := ap.b.ValidateAccessToken(authMsg.AccessToken)
if refreshToken != nil {
ap.finishAuth(refreshToken)