-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgo-apns-server.go
59 lines (47 loc) · 1.3 KB
/
go-apns-server.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
// Copyright (c) 2016, CleverTap
// All rights reserved.
// Licensed under the New 3-Clause BSD License
//
// A mock server for the HTTP2 Apple push gateway
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"strconv"
)
func main() {
serverPort := flag.Int("port", 8443, "The port for the mock server to listen to")
serverCert := flag.String("cert", "cert.pem", "The path to the certificate")
serverKey := flag.String("key", "key.pem", "The path to the certificate key")
flag.Parse()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println("Error:", err)
}
n := rand.Int31n(10)
statusCode := 200
var body string
if n == 7 {
statusCode = 400
body = "{\"reason\": \"BadDeviceToken\"}"
} else if n > 7 {
statusCode = 410
body = "{\"reason\": \"Unregistered\"}"
}
w.WriteHeader(statusCode)
if statusCode != 200 {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, body)
}
})
log.Printf("Using certificate %#v with key %#v", *serverCert, *serverKey)
log.Println("Starting server on port", *serverPort)
log.Println("Press Ctrl+C to stop...")
port := ":" + strconv.Itoa(*serverPort)
log.Fatal(http.ListenAndServeTLS(port, *serverCert, *serverKey, nil))
}