add new file go/l19/l1.go for service post
This commit is contained in:
parent
b36df6082f
commit
e1911b7fb6
7 changed files with 95 additions and 20 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,2 +1,3 @@
|
||||||
.vscode
|
.vscode
|
||||||
.env
|
.env
|
||||||
|
.idea
|
||||||
|
|
|
||||||
51
go/l17.go
51
go/l17.go
|
|
@ -1,16 +1,17 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"github.com/joho/godotenv"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"net/http"
|
|
||||||
"time"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||||
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OpenWeather struct {
|
type OpenWeather struct {
|
||||||
|
|
@ -44,8 +45,8 @@ func WeatherReq() (string, float64, string, float64, float64, string) {
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: 20 * time.Second,
|
Timeout: 20 * time.Second,
|
||||||
}
|
}
|
||||||
link := "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=metric&appid=" + api
|
lin := "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=metric&appid=" + api
|
||||||
url := strings.ReplaceAll(link, " ", "%20")
|
url := strings.ReplaceAll(lin, " ", "%20")
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("%v\n", err)
|
log.Fatalf("%v\n", err)
|
||||||
|
|
@ -83,17 +84,29 @@ func main() {
|
||||||
opts := mqtt.NewClientOptions()
|
opts := mqtt.NewClientOptions()
|
||||||
|
|
||||||
err := godotenv.Load()
|
err := godotenv.Load()
|
||||||
if err != nil { log.Fatalf("%v\n", err) }
|
if err != nil {
|
||||||
|
log.Fatalf("%v\n", err)
|
||||||
|
}
|
||||||
broker := os.Getenv("broker_mqtt")
|
broker := os.Getenv("broker_mqtt")
|
||||||
if broker == "" { log.Fatalf("add yout broker in .env file\n") }
|
if broker == "" {
|
||||||
|
log.Fatalf("add yout broker in .env file\n")
|
||||||
|
}
|
||||||
ClientID := os.Getenv("clientid_mqtt")
|
ClientID := os.Getenv("clientid_mqtt")
|
||||||
if ClientID == "" { log.Fatalf("add your client id in .env file") }
|
if ClientID == "" {
|
||||||
|
log.Fatalf("add your client id in .env file")
|
||||||
|
}
|
||||||
topic := os.Getenv("topic_mqtt")
|
topic := os.Getenv("topic_mqtt")
|
||||||
if topic == "" { log.Fatalf("add your topic in .env file") }
|
if topic == "" {
|
||||||
|
log.Fatalf("add your topic in .env file")
|
||||||
|
}
|
||||||
username := os.Getenv("login_mqtt")
|
username := os.Getenv("login_mqtt")
|
||||||
if username != "" { opts.SetUsername(username) }
|
if username != "" {
|
||||||
|
opts.SetUsername(username)
|
||||||
|
}
|
||||||
password := os.Getenv("password_mqtt")
|
password := os.Getenv("password_mqtt")
|
||||||
if password != "" { opts.SetPassword(password) }
|
if password != "" {
|
||||||
|
opts.SetPassword(password)
|
||||||
|
}
|
||||||
TimeHour := os.Getenv("TimeHourSendMQTT")
|
TimeHour := os.Getenv("TimeHourSendMQTT")
|
||||||
|
|
||||||
opts.AddBroker(broker)
|
opts.AddBroker(broker)
|
||||||
|
|
@ -101,14 +114,18 @@ func main() {
|
||||||
opts.SetConnectTimeout(5 * time.Second)
|
opts.SetConnectTimeout(5 * time.Second)
|
||||||
|
|
||||||
client := mqtt.NewClient(opts)
|
client := mqtt.NewClient(opts)
|
||||||
if token := client.Connect(); token.Wait() && token.Error() != nil { log.Fatalf("%v\n", token.Error()) }
|
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
||||||
|
log.Fatalf("%v\n", token.Error())
|
||||||
|
}
|
||||||
defer client.Disconnect(250)
|
defer client.Disconnect(250)
|
||||||
|
|
||||||
fmt.Println("Connect to a broker")
|
fmt.Println("Connect to a broker")
|
||||||
var ticker *time.Ticker
|
var ticker *time.Ticker
|
||||||
if TimeHour != "" {
|
if TimeHour != "" {
|
||||||
ReallyTimeHour, err := strconv.Atoi(TimeHour)
|
ReallyTimeHour, err := strconv.Atoi(TimeHour)
|
||||||
if err != nil { log.Fatalf("%s\n", err) }
|
if err != nil {
|
||||||
|
log.Fatalf("%s\n", err)
|
||||||
|
}
|
||||||
ticker = time.NewTicker(time.Duration(ReallyTimeHour) * time.Hour)
|
ticker = time.NewTicker(time.Duration(ReallyTimeHour) * time.Hour)
|
||||||
} else {
|
} else {
|
||||||
ticker = time.NewTicker(2 * time.Hour)
|
ticker = time.NewTicker(2 * time.Hour)
|
||||||
|
|
|
||||||
4
go/l19/.gitignore
vendored
Normal file
4
go/l19/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
.env
|
||||||
|
.*.*.swp
|
||||||
|
.idea
|
||||||
|
.vscode
|
||||||
52
go/l19/l1.go
Normal file
52
go/l19/l1.go
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"encoding/json"
|
||||||
|
)
|
||||||
|
|
||||||
|
type User struct{
|
||||||
|
Age int `json:"age"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var setChan = make(chan User)
|
||||||
|
|
||||||
|
func handle(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "not", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var data User
|
||||||
|
err := json.NewDecoder(r.Body).Decode(&data)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w,"no", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
|
setChan <- data
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte("Data received"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func WatchChanell() {
|
||||||
|
for u := range setChan {
|
||||||
|
JsonData, err := json.Marshal(u)
|
||||||
|
if err != nil { continue }
|
||||||
|
fmt.Println(string(JsonData))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
go WatchChanell()
|
||||||
|
http.HandleFunc("/user", handle)
|
||||||
|
fmt.Println(":7777")
|
||||||
|
err := http.ListenAndServe(":7777", nil)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
go/l19/output/l1
Executable file
BIN
go/l19/output/l1
Executable file
Binary file not shown.
1
go/output/go_build_my_app
Executable file
1
go/output/go_build_my_app
Executable file
|
|
@ -0,0 +1 @@
|
||||||
|
ELF
|
||||||
Loading…
Reference in a new issue