generated from jidicula/template-go
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
72 lines (63 loc) · 2.23 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// vci-check checks if a vaccine credential issuer is in the VCI Directory
// Copyright (C) 2022 Johanan Idicula
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"fmt"
"log"
"net/http"
"strings"
"github.com/jidicula/vci-check/checker"
flag "github.com/spf13/pflag"
)
func main() {
port := flag.String("port", "8080", "Port to listen to")
flag.Parse()
log.Printf("listening on %s", *port)
http.HandleFunc("/", checkHandler)
log.Fatal(http.ListenAndServe(":"+*port, nil))
}
func checkHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
log.Printf("incorrect request method: %s", r.Method)
w.Header().Set("Allow", http.MethodGet)
http.Error(w, `{"message": "expect method GET at /?iss=<url>"}`, http.StatusMethodNotAllowed)
return
}
// serveraddress:port?iss=<url>
issURL := r.URL.Query().Get("iss")
if issURL == "" {
msg := "No issuer URL provided"
log.Print(msg)
http.Error(w, fmt.Sprintf(`{"message": "%s"}`, msg), http.StatusBadRequest)
return
}
il, err := checker.NewIssuerList()
if err != nil {
log.Print(err.Error())
http.Error(w, `{"message": "Error retrieving VCI issuer list"}`, http.StatusInternalServerError)
return
}
response := fmt.Sprintf(`{"message": %t}`, il.IsTrusted(issURL))
escapedIssURL := strings.Replace(issURL, "\n", " ", -1)
escapedIssURL = strings.Replace(escapedIssURL, "\r", " ", -1)
log.Printf("issURL %s : %s", escapedIssURL, response)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
x, err := w.Write([]byte(response))
if err != nil {
log.Printf("%s, %d bytes written", err, x)
}
}