main.go 1.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. )
  9. const _accessKeyId string = "LTAI5tGjnZY6k799BHxhmqcm"
  10. const _accessKeySecret string = "eU1DmULbgHe2dnIg3P93634PO2vEh5"
  11. const _addr string = ":8081"
  12. func smsHandler(w http.ResponseWriter, r *http.Request) {
  13. defer r.Body.Close()
  14. body, err := io.ReadAll(r.Body)
  15. if err != nil {
  16. http.Error(w, err.Error()+"\n访问 /help 查看使用帮助", http.StatusInternalServerError)
  17. }
  18. if err := SendSMS(&body); err != nil {
  19. http.Error(w, err.Error()+"\n访问 /help 查看使用帮助", http.StatusInternalServerError)
  20. }
  21. fmt.Fprintf(w, "success")
  22. }
  23. func helpHandler(w http.ResponseWriter, r *http.Request) {
  24. content, err := ioutil.ReadFile("./help.html")
  25. if err != nil {
  26. http.Error(w, err.Error(), http.StatusInternalServerError)
  27. }
  28. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  29. w.Write(content)
  30. }
  31. func main() {
  32. http.HandleFunc("/sms", smsHandler)
  33. http.HandleFunc("/help", helpHandler)
  34. log.Fatal(http.ListenAndServe(_addr, nil))
  35. }