diff --git a/common/version.go b/common/version.go index feb4161..b7ad0ad 100644 --- a/common/version.go +++ b/common/version.go @@ -1,3 +1,4 @@ package common const Version = "0.4.4" +const SERVICENAME = "CasaOS-UserService" diff --git a/go.mod b/go.mod index bfe4c38..a2829b2 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/IceWhaleTech/CasaOS-UserService go 1.20 require ( - github.com/IceWhaleTech/CasaOS-Common v0.4.8-alpha3 + github.com/IceWhaleTech/CasaOS-Common v0.4.8-alpha12 github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf github.com/deepmap/oapi-codegen v1.12.4 github.com/getkin/kin-openapi v0.117.0 diff --git a/main.go b/main.go index 67a9041..5d886bf 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( + "context" _ "embed" "flag" "fmt" @@ -18,6 +19,7 @@ import ( util_http "github.com/IceWhaleTech/CasaOS-Common/utils/http" "github.com/IceWhaleTech/CasaOS-Common/utils/jwt" "github.com/IceWhaleTech/CasaOS-Common/utils/logger" + "github.com/IceWhaleTech/CasaOS-UserService/codegen/message_bus" "github.com/IceWhaleTech/CasaOS-UserService/common" "github.com/IceWhaleTech/CasaOS-UserService/pkg/config" "github.com/IceWhaleTech/CasaOS-UserService/pkg/sqlite" @@ -154,6 +156,23 @@ func main() { go route.EventListen() logger.Info("User service is listening...", zap.Any("address", listener.Addr().String()), zap.String("filepath", addressFilePath)) + var events []message_bus.EventType + events = append(events, message_bus.EventType{Name: "zimaos:user:save_config", SourceID: common.SERVICENAME, PropertyTypeList: []message_bus.PropertyType{}}) + // register at message bus + for i := 0; i < 10; i++ { + response, err := service.MyService.MessageBus().RegisterEventTypesWithResponse(context.Background(), events) + if err != nil { + logger.Error("error when trying to register one or more event types - some event type will not be discoverable", zap.Error(err)) + } + if response != nil && response.StatusCode() != http.StatusOK { + logger.Error("error when trying to register one or more event types - some event type will not be discoverable", zap.String("status", response.Status()), zap.String("body", string(response.Body))) + } + if response.StatusCode() == http.StatusOK { + break + } + time.Sleep(time.Second) + } + s := &http.Server{ Handler: mux, ReadHeaderTimeout: 5 * time.Second, // fix G112: Potential slowloris attack (see https://github.com/securego/gosec) diff --git a/route/v1/user.go b/route/v1/user.go index c772c95..87e0077 100644 --- a/route/v1/user.go +++ b/route/v1/user.go @@ -1,6 +1,7 @@ package v1 import ( + "context" "crypto/ecdsa" "encoding/base64" json2 "encoding/json" @@ -21,6 +22,7 @@ import ( "github.com/IceWhaleTech/CasaOS-Common/utils/common_err" "github.com/IceWhaleTech/CasaOS-Common/utils/jwt" "github.com/IceWhaleTech/CasaOS-Common/utils/logger" + "github.com/IceWhaleTech/CasaOS-UserService/common" "github.com/IceWhaleTech/CasaOS-UserService/model" "github.com/IceWhaleTech/CasaOS-UserService/model/system_model" "github.com/IceWhaleTech/CasaOS-UserService/pkg/config" @@ -508,6 +510,20 @@ func PostUserCustomConf(c *gin.Context) { return } + if name == "system" { + dataMap := make(map[string]string, 1) + dataMap["system"] = string(data) + response, err := service.MyService.MessageBus().PublishEventWithResponse(context.Background(), common.SERVICENAME, "zimaos:user:save_config", dataMap) + if err != nil { + logger.Error("failed to publish event to message bus", zap.Error(err), zap.Any("event", string(data))) + return + } + if response.StatusCode() != http.StatusOK { + logger.Error("failed to publish event to message bus", zap.String("status", response.Status()), zap.Any("response", response)) + } + + } + c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: json2.RawMessage(string(data))}) } diff --git a/service/service.go b/service/service.go index 8d29cab..8235635 100644 --- a/service/service.go +++ b/service/service.go @@ -2,6 +2,8 @@ package service import ( "github.com/IceWhaleTech/CasaOS-Common/external" + "github.com/IceWhaleTech/CasaOS-UserService/codegen/message_bus" + "github.com/IceWhaleTech/CasaOS-UserService/pkg/config" "gorm.io/gorm" ) @@ -10,6 +12,7 @@ var MyService Repository type Repository interface { Gateway() external.ManagementService User() UserService + MessageBus() *message_bus.ClientWithResponses Event() EventService } @@ -43,3 +46,22 @@ func (c *store) Gateway() external.ManagementService { func (c *store) User() UserService { return c.user } +func (c *store) MessageBus() *message_bus.ClientWithResponses { + client, _ := message_bus.NewClientWithResponses("", func(c *message_bus.Client) error { + // error will never be returned, as we always want to return a client, even with wrong address, + // in order to avoid panic. + // + // If we don't avoid panic, message bus becomes a hard dependency, which is not what we want. + + messageBusAddress, err := external.GetMessageBusAddress(config.CommonInfo.RuntimePath) + if err != nil { + c.Server = "message bus address not found" + return nil + } + + c.Server = messageBusAddress + return nil + }) + + return client +}