代码拉取完成,页面将自动刷新
同步操作将从 xluohome/smscode 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
log "github.com/golang/glog"
)
type request struct {
Act, Mobile, Code, Service, Uid string `json:",omitempty"`
result chan Result
}
func (r *request) String() string {
return fmt.Sprintf("%s\t%s\t%s\t%s\t%s", r.Act, r.Mobile, r.Code, r.Service, r.Uid)
}
type apiserver struct {
req []chan *request
}
func (api *apiserver) sms(i int) {
var (
infos interface{}
err error
result Result
)
sms := NewSms()
for req := range api.req[i] {
sms.NowTime = time.Now()
sms.SetServiceConfig(req.Service)
infos = nil
switch req.Act {
case "send":
err = sms.Send(req.Mobile)
case "checkcode":
err = sms.CheckCode(req.Mobile, req.Code)
case "setuid":
err = sms.SetUid(req.Mobile, req.Uid)
case "deluid":
err = sms.DelUid(req.Mobile, req.Uid)
case "info":
infos, err = sms.Info(req.Mobile)
default:
err = fmt.Errorf("%s\t%s", "API not found:", req.Act)
}
switch sms.Config.Outformat {
case "mobcent":
result = &Result_mobcent{}
default:
result = &Result_default{}
}
result.Format(err, infos)
req.result <- result
}
return
}
func (api *apiserver) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Info(r.RemoteAddr, r.URL)
defer func() {
if e := recover(); e != nil {
err := fmt.Errorf("Server error :%s", e)
w.WriteHeader(500)
w.Write([]byte(err.Error()))
}
}()
resultchan := make(chan Result)
req := &request{r.URL.Path[1:], r.FormValue("mobile"), r.FormValue("code"), r.FormValue("service"), r.FormValue("uid"), resultchan}
hash := hashFunc([]byte(req.String())) & uint64(*smsworks-1)
for {
select {
case api.req[hash] <- req:
case result := <-resultchan:
w.Header().Set("Content-Type", ContentType)
w.Header().Set("TimeZone", time.Local.String())
str, _ := json.Marshal(result)
w.Write(str)
return
//未在预定时间内完成请求或者接收答复则报超时错误
//如果设置的smsworks过少时,当大量的并发请求因处理量受限无法及时处理。会使得请求或接受时间延长导致超时;
//另外网络不稳定或短信供应商通道出现了问题也会引起超时。
case <-time.After(config.TimeOut * time.Second):
panic("Server timeout error!")
}
}
}
func Apiserver() {
var api = new(apiserver)
api.req = make([]chan *request, *smsworks)
for i := 0; i < *smsworks; i++ {
go func(i int) {
api.req[i] = make(chan *request)
api.sms(i)
}(i)
}
log.Fatal(http.ListenAndServe(config.Bind, api))
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。