mirror of
https://github.com/KaySar12/NextZen-UserService.git
synced 2025-06-26 21:50:57 +07:00
Send notification
This commit is contained in:
parent
2fdbdcf1ec
commit
3db4be01c4
@ -1,3 +1,4 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
const Version = "0.4.4"
|
const Version = "0.4.4"
|
||||||
|
const SERVICENAME = "CasaOS-UserService"
|
||||||
|
2
go.mod
2
go.mod
@ -3,7 +3,7 @@ module github.com/IceWhaleTech/CasaOS-UserService
|
|||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
require (
|
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/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
|
||||||
github.com/deepmap/oapi-codegen v1.12.4
|
github.com/deepmap/oapi-codegen v1.12.4
|
||||||
github.com/getkin/kin-openapi v0.117.0
|
github.com/getkin/kin-openapi v0.117.0
|
||||||
|
19
main.go
19
main.go
@ -3,6 +3,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -18,6 +19,7 @@ import (
|
|||||||
util_http "github.com/IceWhaleTech/CasaOS-Common/utils/http"
|
util_http "github.com/IceWhaleTech/CasaOS-Common/utils/http"
|
||||||
"github.com/IceWhaleTech/CasaOS-Common/utils/jwt"
|
"github.com/IceWhaleTech/CasaOS-Common/utils/jwt"
|
||||||
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
|
"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/common"
|
||||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/config"
|
"github.com/IceWhaleTech/CasaOS-UserService/pkg/config"
|
||||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/sqlite"
|
"github.com/IceWhaleTech/CasaOS-UserService/pkg/sqlite"
|
||||||
@ -154,6 +156,23 @@ func main() {
|
|||||||
go route.EventListen()
|
go route.EventListen()
|
||||||
logger.Info("User service is listening...", zap.Any("address", listener.Addr().String()), zap.String("filepath", addressFilePath))
|
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{
|
s := &http.Server{
|
||||||
Handler: mux,
|
Handler: mux,
|
||||||
ReadHeaderTimeout: 5 * time.Second, // fix G112: Potential slowloris attack (see https://github.com/securego/gosec)
|
ReadHeaderTimeout: 5 * time.Second, // fix G112: Potential slowloris attack (see https://github.com/securego/gosec)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package v1
|
package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
json2 "encoding/json"
|
json2 "encoding/json"
|
||||||
@ -21,6 +22,7 @@ import (
|
|||||||
"github.com/IceWhaleTech/CasaOS-Common/utils/common_err"
|
"github.com/IceWhaleTech/CasaOS-Common/utils/common_err"
|
||||||
"github.com/IceWhaleTech/CasaOS-Common/utils/jwt"
|
"github.com/IceWhaleTech/CasaOS-Common/utils/jwt"
|
||||||
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
|
"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"
|
||||||
"github.com/IceWhaleTech/CasaOS-UserService/model/system_model"
|
"github.com/IceWhaleTech/CasaOS-UserService/model/system_model"
|
||||||
"github.com/IceWhaleTech/CasaOS-UserService/pkg/config"
|
"github.com/IceWhaleTech/CasaOS-UserService/pkg/config"
|
||||||
@ -508,6 +510,20 @@ func PostUserCustomConf(c *gin.Context) {
|
|||||||
return
|
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))})
|
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: json2.RawMessage(string(data))})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,8 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/IceWhaleTech/CasaOS-Common/external"
|
"github.com/IceWhaleTech/CasaOS-Common/external"
|
||||||
|
"github.com/IceWhaleTech/CasaOS-UserService/codegen/message_bus"
|
||||||
|
"github.com/IceWhaleTech/CasaOS-UserService/pkg/config"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -10,6 +12,7 @@ var MyService Repository
|
|||||||
type Repository interface {
|
type Repository interface {
|
||||||
Gateway() external.ManagementService
|
Gateway() external.ManagementService
|
||||||
User() UserService
|
User() UserService
|
||||||
|
MessageBus() *message_bus.ClientWithResponses
|
||||||
Event() EventService
|
Event() EventService
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,3 +46,22 @@ func (c *store) Gateway() external.ManagementService {
|
|||||||
func (c *store) User() UserService {
|
func (c *store) User() UserService {
|
||||||
return c.user
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user