Forked from
Gabrielle Van De Vijver / IA04binôme2A
44 commits behind the upstream repository.
-
Balthazar Wilson authoredBalthazar Wilson authored
ballotagent.go 2.36 KiB
package ballotagent
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"sync"
"time"
rad "gitlab.utc.fr/gvandevi/ia04binome2a" // à remplacer par le nom du dossier actuel
)
type RestServerAgent struct {
sync.Mutex
id string
reqCount int
addr string
}
func NewRestServerAgent(addr string) *RestServerAgent {
return &RestServerAgent{id: addr, addr: addr}
}
// Test de la méthode
func (rsa *RestServerAgent) checkMethod(method string, w http.ResponseWriter, r *http.Request) bool {
if r.Method != method {
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, "method %q not allowed", r.Method)
return false
}
return true
}
func (*RestServerAgent) decodeRequest(r *http.Request) (req rad.BallotRequest, err error) {
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
err = json.Unmarshal(buf.Bytes(), &req)
return
}
func (rsa *RestServerAgent) doCalc(w http.ResponseWriter, r *http.Request) {
// mise à jour du nombre de requêtes
rsa.Lock()
defer rsa.Unlock()
rsa.reqCount++
// vérification de la méthode de la requête
if !rsa.checkMethod("POST", w, r) {
return
}
// décodage de la requête
req, err := rsa.decodeRequest(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err.Error())
return
}
// traitement de la requête
var resp rad.Ballot
resp.BallotID = "ballot1"
switch req.Rule {
case "majority":
case "borda":
case "approval":
case "stv":
default:
w.WriteHeader(http.StatusNotImplemented)
msg := fmt.Sprintf("Unkonwn rule '%s'", req.Rule)
w.Write([]byte(msg))
return
}
w.WriteHeader(http.StatusOK)
serial, _ := json.Marshal(resp)
w.Write(serial)
}
func (rsa *RestServerAgent) doReqcount(w http.ResponseWriter, r *http.Request) {
if !rsa.checkMethod("GET", w, r) {
return
}
w.WriteHeader(http.StatusOK)
rsa.Lock()
defer rsa.Unlock()
serial, _ := json.Marshal(rsa.reqCount)
w.Write(serial)
}
func (rsa *RestServerAgent) Start() {
// création du multiplexer
mux := http.NewServeMux()
mux.HandleFunc("/calculator", rsa.doCalc)
mux.HandleFunc("/reqcount", rsa.doReqcount)
// création du serveur http
s := &http.Server{
Addr: rsa.addr,
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20}
// lancement du serveur
log.Println("Listening on", rsa.addr)
go log.Fatal(s.ListenAndServe())
}