diff --git a/.gitignore b/.gitignore index 6f27dc3..3c497e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .vscode .env +.idea diff --git a/go/l17.go b/go/l17.go index 11a7c5a..f7e4647 100644 --- a/go/l17.go +++ b/go/l17.go @@ -1,22 +1,23 @@ package main import ( - "log" - "github.com/joho/godotenv" - "os" - "strings" - "net/http" - "time" "encoding/json" - mqtt "github.com/eclipse/paho.mqtt.golang" "fmt" + "log" + "net/http" + "os" "strconv" + "strings" + "time" + + mqtt "github.com/eclipse/paho.mqtt.golang" + "github.com/joho/godotenv" ) -type OpenWeather struct{ +type OpenWeather struct { City string `json:"name"` Main struct { - Temp float64 `json:"temp"` + Temp float64 `json:"temp"` FeelLike float64 `json:"feels_like"` } `json:"main"` Weather []struct { @@ -44,8 +45,8 @@ func WeatherReq() (string, float64, string, float64, float64, string) { client := &http.Client{ Timeout: 20 * time.Second, } - link := "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=metric&appid=" + api - url := strings.ReplaceAll(link, " ", "%20") + lin := "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=metric&appid=" + api + url := strings.ReplaceAll(lin, " ", "%20") req, err := http.NewRequest("GET", url, nil) if err != nil { log.Fatalf("%v\n", err) @@ -83,17 +84,29 @@ func main() { opts := mqtt.NewClientOptions() err := godotenv.Load() - if err != nil { log.Fatalf("%v\n", err) } + if err != nil { + log.Fatalf("%v\n", err) + } 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") - 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") - 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") - if username != "" { opts.SetUsername(username) } + if username != "" { + opts.SetUsername(username) + } password := os.Getenv("password_mqtt") - if password != "" { opts.SetPassword(password) } + if password != "" { + opts.SetPassword(password) + } TimeHour := os.Getenv("TimeHourSendMQTT") opts.AddBroker(broker) @@ -101,14 +114,18 @@ func main() { opts.SetConnectTimeout(5 * time.Second) 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) fmt.Println("Connect to a broker") var ticker *time.Ticker if 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) } else { ticker = time.NewTicker(2 * time.Hour) @@ -119,4 +136,4 @@ func main() { for range ticker.C { Send(client, topic) } -} \ No newline at end of file +} diff --git a/go/l19.go b/go/l19.go deleted file mode 100644 index e69de29..0000000 diff --git a/go/l19/.gitignore b/go/l19/.gitignore new file mode 100644 index 0000000..5991b1f --- /dev/null +++ b/go/l19/.gitignore @@ -0,0 +1,4 @@ +.env +.*.*.swp +.idea +.vscode diff --git a/go/l19/l1.go b/go/l19/l1.go new file mode 100644 index 0000000..3d7d756 --- /dev/null +++ b/go/l19/l1.go @@ -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) + } +} diff --git a/go/l19/output/l1 b/go/l19/output/l1 new file mode 100755 index 0000000..9d9acb7 Binary files /dev/null and b/go/l19/output/l1 differ diff --git a/go/output/go_build_my_app b/go/output/go_build_my_app new file mode 100755 index 0000000..bb7da68 --- /dev/null +++ b/go/output/go_build_my_app @@ -0,0 +1 @@ +ELF \ No newline at end of file