@@ -10,47 +10,70 @@ package main
1010
1111import (
1212 "encoding/json"
13+ "fmt"
1314 "io/ioutil"
1415 "log"
1516 "net/http"
17+ "sync/atomic"
1618)
1719
1820type ctSubmissionRequest struct {
1921 Chain []string `json:"chain"`
2022}
2123
22- func handler (w http.ResponseWriter , r * http.Request ) {
23- if r .Method != "POST" || r .URL .Path != "/ct/v1/add-chain" {
24+ type integrationSrv struct {
25+ submissions int64
26+ }
27+
28+ func (is * integrationSrv ) handler (w http.ResponseWriter , r * http.Request ) {
29+ switch r .URL .Path {
30+ case "/ct/v1/add-chain" :
31+ if r .Method != "POST" {
32+ http .NotFound (w , r )
33+ return
34+ }
35+ bodyBytes , err := ioutil .ReadAll (r .Body )
36+ if err != nil {
37+ http .Error (w , err .Error (), http .StatusBadRequest )
38+ }
39+
40+ var addChainReq ctSubmissionRequest
41+ err = json .Unmarshal (bodyBytes , & addChainReq )
42+ if err != nil {
43+ http .Error (w , err .Error (), http .StatusBadRequest )
44+ }
45+
46+ w .WriteHeader (http .StatusOK )
47+ // id is a sha256 of a random EC key. Generate your own with:
48+ // openssl ecparam -name prime256v1 -genkey -outform der | openssl sha256 -binary | base64
49+ w .Write ([]byte (`{
50+ "sct_version": 0,
51+ "id": "8fjM8cvLPOhzCFwI62IYJhjkOcvWFLx1dMJbs0uhxJU=",
52+ "timestamp": 1442400000,
53+ "extensions": "",
54+ "signature": "BAMARzBFAiBB5wKED8KqKhADT37n0y28fZIPiGbCfZRVKq0wNo0hrwIhAOIa2tPBF/rB1y30Y/ROh4LBmJ0mItAbTWy8XZKh7Wcp"
55+ }` ))
56+ atomic .AddInt64 (& is .submissions , 1 )
57+ case "/submissions" :
58+ if r .Method != "GET" {
59+ http .NotFound (w , r )
60+ return
61+ }
62+
63+ submissions := atomic .LoadInt64 (& is .submissions )
64+ w .WriteHeader (http .StatusOK )
65+ w .Write ([]byte (fmt .Sprintf ("%d" , submissions )))
66+ default :
2467 http .NotFound (w , r )
2568 return
2669 }
27- bodyBytes , err := ioutil .ReadAll (r .Body )
28- if err != nil {
29- http .Error (w , err .Error (), http .StatusBadRequest )
30- }
31-
32- var addChainReq ctSubmissionRequest
33- err = json .Unmarshal (bodyBytes , & addChainReq )
34- if err != nil {
35- http .Error (w , err .Error (), http .StatusBadRequest )
36- }
37-
38- w .WriteHeader (http .StatusOK )
39- // id is a sha256 of a random EC key. Generate your own with:
40- // openssl ecparam -name prime256v1 -genkey -outform der | openssl sha256 -binary | base64
41- w .Write ([]byte (`{
42- "sct_version": 0,
43- "id": "8fjM8cvLPOhzCFwI62IYJhjkOcvWFLx1dMJbs0uhxJU=",
44- "timestamp": 1442400000,
45- "extensions": "",
46- "signature": "BAMARzBFAiBB5wKED8KqKhADT37n0y28fZIPiGbCfZRVKq0wNo0hrwIhAOIa2tPBF/rB1y30Y/ROh4LBmJ0mItAbTWy8XZKh7Wcp"
47- }` ))
4870}
4971
5072func main () {
73+ is := integrationSrv {}
5174 s := & http.Server {
52- Addr : ":4500" ,
53- Handler : http .HandlerFunc (handler ),
75+ Addr : "localhost :4500" ,
76+ Handler : http .HandlerFunc (is . handler ),
5477 }
5578 log .Fatal (s .ListenAndServe ())
5679}
0 commit comments