Skip to content

Commit

Permalink
Send notification
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkLeong committed Feb 4, 2024
1 parent 2fdbdcf commit 3db4be0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions common/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package common

const Version = "0.4.4"
const SERVICENAME = "CasaOS-UserService"
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"context"
_ "embed"
"flag"
"fmt"
Expand All @@ -18,6 +19,7 @@ import (
util_http "github.com/IceWhaleTech/CasaOS-Common/utils/http"

Check failure on line 19 in main.go

View workflow job for this annotation

GitHub Actions / build

missing go.sum entry for module providing package github.com/IceWhaleTech/CasaOS-Common/utils/http (imported by github.com/IceWhaleTech/CasaOS-UserService); to add:
"github.com/IceWhaleTech/CasaOS-Common/utils/jwt"

Check failure on line 20 in main.go

View workflow job for this annotation

GitHub Actions / build

missing go.sum entry for module providing package github.com/IceWhaleTech/CasaOS-Common/utils/jwt (imported by github.com/IceWhaleTech/CasaOS-UserService); to add:
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"

Check failure on line 21 in main.go

View workflow job for this annotation

GitHub Actions / build

missing go.sum entry for module providing package github.com/IceWhaleTech/CasaOS-Common/utils/logger (imported by github.com/IceWhaleTech/CasaOS-UserService); to add:
"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"
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions route/v1/user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1

import (
"context"
"crypto/ecdsa"
"encoding/base64"
json2 "encoding/json"
Expand All @@ -21,6 +22,7 @@ import (
"github.com/IceWhaleTech/CasaOS-Common/utils/common_err"

Check failure on line 22 in route/v1/user.go

View workflow job for this annotation

GitHub Actions / build

missing go.sum entry for module providing package github.com/IceWhaleTech/CasaOS-Common/utils/common_err (imported by github.com/IceWhaleTech/CasaOS-UserService/route/v1); to add:
"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"
Expand Down Expand Up @@ -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))})
}

Expand Down
22 changes: 22 additions & 0 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -10,6 +12,7 @@ var MyService Repository
type Repository interface {
Gateway() external.ManagementService
User() UserService
MessageBus() *message_bus.ClientWithResponses
Event() EventService
}

Expand Down Expand Up @@ -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
}

0 comments on commit 3db4be0

Please sign in to comment.