diff --git a/README.md b/README.md index a44fb00..ed823a5 100644 --- a/README.md +++ b/README.md @@ -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. ## NATS Micro PlugisServiceIntf Integration 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) # Files diff --git a/pkg/systeminfo/systeminfo.go b/pkg/systeminfo/systeminfo.go new file mode 100644 index 0000000..e843977 --- /dev/null +++ b/pkg/systeminfo/systeminfo.go @@ -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 +} diff --git a/plugis.go b/plugis.go index b8248d7..4df1b14 100644 --- a/plugis.go +++ b/plugis.go @@ -13,7 +13,7 @@ import ( "github.com/telemac/goutils/net" "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/synadia-io/orbit.go/natsext" ) @@ -182,7 +182,7 @@ func isRunningInDockerContainer() bool { } // 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 meta ServiceMetadata meta.Prefix = prefix @@ -201,8 +201,5 @@ func NewServiceMetadata(prefix string, startedAt time.Time) (Metadata, error) { return nil, err } - // Convert struct to map using mapstructure - var result Metadata - err = mapstructure.Decode(meta, &result) - return result, err + return &meta, nil }