-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
49 lines (43 loc) · 959 Bytes
/
main.go
File metadata and controls
49 lines (43 loc) · 959 Bytes
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
package main
import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
)
func main() {
http.HandleFunc("/err", HandleError)
http.HandleFunc("/ok", HandleOK)
http.HandleFunc("/", HandleRoot)
http.ListenAndServe(port(), nil)
}
func port() string {
p := os.Getenv("PORT")
if p == "" {
return ":3333"
}
return ":" + p
}
func HandleError(w http.ResponseWriter, r *http.Request) {
http.Error(w, "some err", http.StatusBadRequest)
}
func HandleOK(w http.ResponseWriter, r *http.Request) {
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
fmt.Fprintf(w, "userip: %q is not IP:port", r.RemoteAddr)
}
userIP := net.ParseIP(ip)
log.Printf("%+v\n", userIP)
log.Println(string(bodyBytes))
w.WriteHeader(http.StatusOK)
}
func HandleRoot(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Levpay rmock project"))
}