add NewServiceMetadata
This commit is contained in:
parent
848d7406a0
commit
4704a0a27f
3 changed files with 50 additions and 9 deletions
|
|
@ -1,13 +1,13 @@
|
||||||
# plugis3-service
|
# plugisservice
|
||||||
|
|
||||||
plugis3-service is a package to write plugis3 services in Go.
|
plugisservice is a package to write plugis3 services in Go.
|
||||||
|
|
||||||
A Plugis3 service is a NATS micro service with some specific properties that must implement the `PlugisServiceIntf` interface.
|
A Plugis3 service is a NATS micro service with some specific properties that must implement the `PlugisServiceIntf` interface.
|
||||||
|
|
||||||
## NATS Micro PlugisServiceIntf Integration
|
## NATS Micro PlugisServiceIntf Integration
|
||||||
|
|
||||||
For NATS services, see:
|
For NATS services, see:
|
||||||
- [Building a PlugisServiceIntf](https://docs.nats.io/using-nats/nex/getting-started/building-service)
|
- [Building a Service](https://docs.nats.io/using-nats/nex/getting-started/building-service)
|
||||||
- [NATS micro on Github](https://github.com/nats-io/nats.go/tree/main/micro)
|
- [NATS micro on Github](https://github.com/nats-io/nats.go/tree/main/micro)
|
||||||
|
|
||||||
# Files
|
# Files
|
||||||
|
|
|
||||||
44
pkg/systeminfo/systeminfo.go
Normal file
44
pkg/systeminfo/systeminfo.go
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
package systeminfo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/telemac/goutils/net"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SystemInfo struct {
|
||||||
|
Platform string `json:"platform"` // ex: linux/amd64
|
||||||
|
Docker bool `json:"docker"` // true if runnint in docker container
|
||||||
|
Hostname string `json:"hostname"`
|
||||||
|
MacAddress string `json:"mac_address"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSystemInfo() (*SystemInfo, error) {
|
||||||
|
var systemInfo SystemInfo
|
||||||
|
err := systemInfo.Fill()
|
||||||
|
return &systemInfo, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (si *SystemInfo) Fill() error {
|
||||||
|
var err error
|
||||||
|
si.Platform = runtime.GOOS + "/" + runtime.GOARCH
|
||||||
|
si.Docker = isRunningInDockerContainer()
|
||||||
|
si.Hostname, err = os.Hostname()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to get hostname: %v", err)
|
||||||
|
}
|
||||||
|
si.MacAddress, err = net.GetMACAddress()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to get MAC address: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// isRunningInDockerContainer
|
||||||
|
func isRunningInDockerContainer() bool {
|
||||||
|
if _, err := os.Stat("/.dockerenv"); err == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
@ -13,7 +13,7 @@ import (
|
||||||
"github.com/telemac/goutils/net"
|
"github.com/telemac/goutils/net"
|
||||||
"github.com/telemac/goutils/task"
|
"github.com/telemac/goutils/task"
|
||||||
|
|
||||||
"github.com/go-viper/mapstructure/v2"
|
//"github.com/go-viper/mapstructure/v2"
|
||||||
"github.com/nats-io/nats.go"
|
"github.com/nats-io/nats.go"
|
||||||
"github.com/synadia-io/orbit.go/natsext"
|
"github.com/synadia-io/orbit.go/natsext"
|
||||||
)
|
)
|
||||||
|
|
@ -182,7 +182,7 @@ func isRunningInDockerContainer() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewServiceMetadata creates and fills a ServiceMetadata structure
|
// NewServiceMetadata creates and fills a ServiceMetadata structure
|
||||||
func NewServiceMetadata(prefix string, startedAt time.Time) (Metadata, error) {
|
func NewServiceMetadata(prefix string, startedAt time.Time) (*ServiceMetadata, error) {
|
||||||
var err error
|
var err error
|
||||||
var meta ServiceMetadata
|
var meta ServiceMetadata
|
||||||
meta.Prefix = prefix
|
meta.Prefix = prefix
|
||||||
|
|
@ -201,8 +201,5 @@ func NewServiceMetadata(prefix string, startedAt time.Time) (Metadata, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert struct to map using mapstructure
|
return &meta, nil
|
||||||
var result Metadata
|
|
||||||
err = mapstructure.Decode(meta, &result)
|
|
||||||
return result, err
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue