12345678910111213141516171819202122232425262728293031323334353637
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "net/http"
  7. )
  8. const _addr string = ":8081"
  9. func smsHandler(w http.ResponseWriter, r *http.Request) {
  10. if err := authHeader(r); err != nil {
  11. http.Error(w, err.Error()+"\n访问 /help 查看使用帮助", http.StatusInternalServerError)
  12. return
  13. }
  14. defer r.Body.Close()
  15. body, err := io.ReadAll(r.Body)
  16. if err != nil {
  17. http.Error(w, err.Error()+"\n访问 /help 查看使用帮助", http.StatusInternalServerError)
  18. return
  19. }
  20. if err := SendSMS(&body); err != nil {
  21. http.Error(w, err.Error()+"\n访问 /help 查看使用帮助", http.StatusInternalServerError)
  22. return
  23. }
  24. fmt.Fprintf(w, "success")
  25. }
  26. func main() {
  27. http.HandleFunc("/sms", smsHandler)
  28. http.HandleFunc("/help", helpHandler)
  29. log.Fatal(http.ListenAndServe(_addr, nil))
  30. }