Commit 5d5cbb3c authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Use chi and refactor/cleanup server


Co-authored-by: Tomas's avatarConfiks <confiks@scriptbase.org>
parent 90f0ac50
...@@ -68,3 +68,17 @@ func JsonResponse(v interface{}, err *irma.RemoteError) (int, []byte) { ...@@ -68,3 +68,17 @@ func JsonResponse(v interface{}, err *irma.RemoteError) (int, []byte) {
} }
return status, b return status, b
} }
func WriteError(w http.ResponseWriter, err Error, msg string) {
status, bts := JsonResponse(nil, RemoteError(err, msg))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
w.Write(bts)
}
func WriteJson(w http.ResponseWriter, object interface{}) {
status, bts := JsonResponse(object, nil)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
w.Write(bts)
}
package server package server
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"github.com/go-chi/chi"
"github.com/go-errors/errors" "github.com/go-errors/errors"
"github.com/privacybydesign/irmago" "github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/irmaserver" "github.com/privacybydesign/irmago/irmaserver"
...@@ -15,73 +15,22 @@ import ( ...@@ -15,73 +15,22 @@ import (
var server *http.Server var server *http.Server
func Start(port int, conf *irmaserver.Configuration) error { func Start(port int, conf *irmaserver.Configuration) error {
mux := http.NewServeMux()
if err := irmarequestor.Initialize(conf); err != nil { if err := irmarequestor.Initialize(conf); err != nil {
return err return err
} }
mux.HandleFunc("/irma/", irmarequestor.HttpHandlerFunc("/irma/")) router := chi.NewRouter()
mux.HandleFunc("/create", func(w http.ResponseWriter, r *http.Request) { // Mount server for irmaclient
if r.Method != http.MethodPost { router.Mount("/irma/", irmarequestor.HttpHandlerFunc("/irma/"))
writeError(w, irmaserver.ErrorInvalidRequest, "")
return
}
body, err := ioutil.ReadAll(r.Body) // Server routes
if err != nil { router.Post("/create", handleCreate)
writeError(w, irmaserver.ErrorInvalidRequest, err.Error()) router.Get("/status/{token}", handleStatus)
return router.Get("/result/{token}", handleResult)
}
request, err := parseRequest(body)
if err != nil {
writeError(w, irmaserver.ErrorInvalidRequest, err.Error())
return
}
qr, _, err := irmarequestor.StartSession(request, nil) // Start server
if err != nil { server = &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: router}
writeError(w, irmaserver.ErrorInvalidRequest, err.Error())
return
}
b, _ := json.Marshal(qr)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(b)
})
mux.HandleFunc("/status/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeError(w, irmaserver.ErrorInvalidRequest, "")
return
}
token := r.URL.Path[len("/status/"):]
res := irmarequestor.GetSessionResult(token)
if res == nil {
writeError(w, irmaserver.ErrorSessionUnknown, "")
return
}
b, _ := json.Marshal(res.Status)
w.Write(b)
})
mux.HandleFunc("/result/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeError(w, irmaserver.ErrorInvalidRequest, "")
return
}
token := r.URL.Path[len("/result/"):]
res := irmarequestor.GetSessionResult(token)
if res == nil {
writeError(w, irmaserver.ErrorInvalidRequest, "")
return
}
b, _ := json.Marshal(res)
w.Write(b)
})
server = &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: mux}
server.ListenAndServe() server.ListenAndServe()
return nil return nil
} }
...@@ -90,10 +39,43 @@ func Stop() { ...@@ -90,10 +39,43 @@ func Stop() {
server.Close() server.Close()
} }
func writeError(w http.ResponseWriter, err irmaserver.Error, msg string) { func handleCreate(w http.ResponseWriter, r *http.Request) {
status, bts := irmaserver.JsonResponse(nil, irmaserver.RemoteError(err, msg)) body, err := ioutil.ReadAll(r.Body)
w.WriteHeader(status) if err != nil {
w.Write(bts) irmaserver.WriteError(w, irmaserver.ErrorInvalidRequest, err.Error())
return
}
request, err := parseRequest(body)
if err != nil {
irmaserver.WriteError(w, irmaserver.ErrorInvalidRequest, err.Error())
return
}
qr, _, err := irmarequestor.StartSession(request, nil)
if err != nil {
irmaserver.WriteError(w, irmaserver.ErrorInvalidRequest, err.Error())
return
}
irmaserver.WriteJson(w, qr)
}
func handleStatus(w http.ResponseWriter, r *http.Request) {
res := irmarequestor.GetSessionResult(chi.URLParam(r, "token"))
if res == nil {
irmaserver.WriteError(w, irmaserver.ErrorSessionUnknown, "")
return
}
irmaserver.WriteJson(w, res.Status)
}
func handleResult(w http.ResponseWriter, r *http.Request) {
res := irmarequestor.GetSessionResult(chi.URLParam(r, "token"))
if res == nil {
irmaserver.WriteError(w, irmaserver.ErrorSessionUnknown, "")
return
}
irmaserver.WriteJson(w, res)
} }
func parseRequest(bts []byte) (request irma.SessionRequest, err error) { func parseRequest(bts []byte) (request irma.SessionRequest, err error) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment