Skip to content

Commit

Permalink
feat: allow public event by unix socket (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
CorrectRoadH authored Feb 23, 2024
1 parent 4422fd4 commit 7e9e1f6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
42 changes: 40 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ var (

//go:embed build/sysroot/etc/casaos/message-bus.conf.sample
_confSample string

unixSocketPath = "/tmp/message-bus.sock"
)

func main() {
Expand Down Expand Up @@ -116,6 +118,14 @@ func main() {
panic(err)
}

// remove unix socket file. don't need check whether it exists or not
os.Remove(unixSocketPath)
// socket listener
socketListener, err := net.Listen("unix", unixSocketPath)
if err != nil {
panic(err)
}

// register at gateway
u, err := url.Parse(swagger.Servers[0].URL)
if err != nil {
Expand Down Expand Up @@ -164,8 +174,36 @@ func main() {
ReadHeaderTimeout: 5 * time.Second,
}

err = server.Serve(listener)
logger.Info("MessageBus service is stopped", zap.Error(err))
socketServer := &http.Server{
Handler: mux,
ReadHeaderTimeout: 5 * time.Second,
}

httpServerErrChan := make(chan error, 1)
socketServerErrChan := make(chan error, 1)

go func() {
err := server.Serve(listener)
httpServerErrChan <- err
}()

go func() {
err := socketServer.Serve(socketListener)
socketServerErrChan <- err
}()

select {
case err := <-httpServerErrChan:
if err != nil {
logger.Info("MessageBus service is stopped", zap.Error(err))
panic(err)
}
case err := <-socketServerErrChan:
if err != nil {
logger.Info("MessageBus socket service is stopped", zap.Error(err))
panic(err)
}
}
}

func writeAddressFile(runtimePath string, filename string, address string) (string, error) {
Expand Down
5 changes: 5 additions & 0 deletions route/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func NewAPIRouter(swagger *openapi3.T, services *service.Services) (http.Handler

e.Use(echo_middleware.JWTWithConfig(echo_middleware.JWTConfig{
Skipper: func(c echo.Context) bool {
// skip when source is unix socket
if c.Request().Host == "unix" {
return true
}

if c.RealIP() == "::1" || c.RealIP() == "127.0.0.1" {
return true
}
Expand Down

0 comments on commit 7e9e1f6

Please sign in to comment.