-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
42 lines (36 loc) · 772 Bytes
/
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
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"time"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
resp := struct {
DateTime string `json:"date_time"`
}{time.Now().Format(time.RFC3339)}
buf, err := json.Marshal(resp)
if err != nil {
log.Printf("failed to marshal response: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err = w.Write(buf)
if err != nil {
log.Printf("failed to write response: %v", err)
}
})
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
err := http.ListenAndServe(":"+port, nil)
if err != nil {
panic(err)
return
}
}