-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from Azure-Samples/npatilsen/goSample
getting_started in Golang
- Loading branch information
Showing
15 changed files
with
338 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,12 @@ | |
*.pem | ||
*.crt | ||
*.env | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
*.out | ||
launchSettings.json | ||
|
||
# User-specific files | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/eclipse/paho.golang v0.12.0 h1:EXQFJbJklDnUqW6lyAknMWRhM2NgpHxwrrL8riUmp3Q= | ||
github.com/eclipse/paho.golang v0.12.0/go.mod h1:TSDCUivu9JnoR9Hl+H7sQMcHkejWH2/xKK1NJGtLbIE= | ||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= | ||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= | ||
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
go 1.21.3 | ||
|
||
use ( | ||
./mqttclients/go | ||
./scenarios/getting_started/go | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= | ||
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4= | ||
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package ConnectionSettings | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"fmt" | ||
"log" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/joho/godotenv" | ||
) | ||
|
||
type MqttConnectionSettings struct { | ||
Hostname string | ||
TcpPort int | ||
UseTls bool | ||
CleanSession bool | ||
CaFile string | ||
CertFile string | ||
KeyFile string | ||
KeyFilePassword string | ||
KeepAlive uint16 | ||
ClientId string | ||
Username string | ||
Password string | ||
} | ||
|
||
var mqttSettingNames = [12]string{ | ||
"MQTT_HOST_NAME", | ||
"MQTT_TCP_PORT", | ||
"MQTT_USE_TLS", | ||
"MQTT_CLEAN_SESSION", | ||
"MQTT_KEEP_ALIVE_IN_SECONDS", | ||
"MQTT_CLIENT_ID", | ||
"MQTT_USERNAME", | ||
"MQTT_PASSWORD", | ||
"MQTT_CA_FILE", | ||
"MQTT_CERT_FILE", | ||
"MQTT_KEY_FILE", | ||
"MQTT_KEY_FILE_PASSWORD", | ||
} | ||
|
||
var defaults = map[string]string{ | ||
"MQTT_TCP_PORT": "8883", | ||
"MQTT_USE_TLS": "true", | ||
"MQTT_CLEAN_SESSION": "true", | ||
"MQTT_KEEP_ALIVE_IN_SECONDS": "30", | ||
} | ||
|
||
func parseIntValue(value string) int { | ||
parsed, err := strconv.Atoi(value) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return parsed | ||
} | ||
|
||
func parseBoolValue(value string) bool { | ||
parsed, err := strconv.ParseBool(value) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return parsed | ||
} | ||
|
||
func GetTlsConnection(cs MqttConnectionSettings) *tls.Conn { | ||
|
||
cfg := &tls.Config{} | ||
|
||
if cs.CertFile != "" && cs.KeyFile != "" { | ||
if cs.KeyFilePassword != "" { | ||
log.Fatal("Password protected key files are not supported at this time.") | ||
} | ||
|
||
cert, err := tls.LoadX509KeyPair(cs.CertFile, cs.KeyFile) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
cfg.Certificates = []tls.Certificate{cert} | ||
} | ||
|
||
if cs.CaFile != "" { | ||
ca, err := os.ReadFile(cs.CaFile) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
caCertPool := x509.NewCertPool() | ||
caCertPool.AppendCertsFromPEM(ca) | ||
cfg.RootCAs = caCertPool | ||
} | ||
|
||
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", cs.Hostname, cs.TcpPort), cfg) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return conn | ||
} | ||
|
||
func LoadConnectionSettings(path string) MqttConnectionSettings { | ||
godotenv.Load(path) | ||
cs := MqttConnectionSettings{} | ||
envVars := make(map[string]string) | ||
|
||
// Check to see which env vars are set | ||
for i := 0; i < len(mqttSettingNames); i++ { | ||
name := mqttSettingNames[i] | ||
value := os.Getenv(name) | ||
// If var is not set, check if it has a default value | ||
if value == "" && defaults[name] != "" { | ||
value = defaults[name] | ||
} | ||
|
||
envVars[name] = value | ||
} | ||
|
||
// Based on which vars are set, construct MqttConnectionSettings | ||
cs.Hostname = envVars["MQTT_HOST_NAME"] | ||
cs.TcpPort = parseIntValue(envVars["MQTT_TCP_PORT"]) | ||
cs.UseTls = parseBoolValue(envVars["MQTT_USE_TLS"]) | ||
cs.CleanSession = parseBoolValue(envVars["MQTT_CLEAN_SESSION"]) | ||
cs.KeepAlive = uint16(parseIntValue(envVars["MQTT_KEEP_ALIVE_IN_SECONDS"])) | ||
cs.ClientId = envVars["MQTT_CLIENT_ID"] | ||
cs.Username = envVars["MQTT_USERNAME"] | ||
cs.Password = envVars["MQTT_PASSWORD"] | ||
cs.CaFile = envVars["MQTT_CA_FILE"] | ||
cs.CertFile = envVars["MQTT_CERT_FILE"] | ||
cs.KeyFile = envVars["MQTT_KEY_FILE"] | ||
cs.KeyFilePassword = envVars["MQTT_KEY_FILE_PASSWORD"] | ||
|
||
return cs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Using Go Samples | ||
|
||
## Loading Environment Variables | ||
Samples import the code from `ConnectionSettings.go`, which includes the struct `MqttConnectionSettings` and the function `LoadConnectionSettings(path string)` that takes a parameter of type string that specifies the location of a `.env` file. Running samples requires passing this path as a command line argument: | ||
|
||
```bash | ||
go run ./program.go <path-to-env-file> | ||
``` | ||
|
||
## Relevant Libraries | ||
- MQTT V5 [Paho Client for Go](https://github.com/eclipse/paho.golang) | ||
- [Godotenv](https://github.com/joho/godotenv) to load environment variables |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module mqttapplicationsamples/ConnectionSettings | ||
|
||
go 1.21.3 | ||
|
||
require github.com/joho/godotenv v1.5.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= | ||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.