我不知道如何使用所需的参数发出“访问”、“时钟”和“审核”的请求。您能否指导我如何提出这些请求?
package main
import (
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"sync"
"time"
)
const secret = "galumphing"
// Required parameters for the request
type params struct {
Act string
Nonce string
Signature string
Timeout int64
Offset int
}
var (
auditLock sync.Mutex
auditBase int
auditLog []string
)
var (
seenLock sync.Mutex
seenMap = map[string]bool{}
)
var (
clockAsk = make(chan bool)
clockTime = make(chan int64, 1)
)
func main() {
// Endpoints available
http.HandleFunc("/903/access", handleAccess)
http.HandleFunc("/903/clock", handleClock)
http.HandleFunc("/903/audit", handleAudit)
err := http.ListenAndServe(os.Args[1], nil)
if err != nil {
log.Fatal(err)
}
}
func checkCapacity(w http.ResponseWriter) (ok bool) {
auditLock.Lock()
defer auditLock.Unlock()
if len(auditLog) > 10 {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
ok = true
return
}
func audit(r *http.Request, params params, ok bool) {
auditLock.Lock()
defer auditLock.Unlock()
auditLog = append(auditLog, fmt.Sprintf("%v %q %q", ok, r.URL.Path, params.Act))
}
func parse(w http.ResponseWriter, r *http.Request) (params params, ok bool) {
defer func() {
if !ok {
audit(r, params, false)
}
}()
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Max-Age", "3600")
if r.Method != "POST" {
w.Header().Set("Allow", "POST, OPTIONS")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
}
return
}
慕妹3146593
相关分类