-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
60 lines (49 loc) · 1.08 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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"os/signal"
"time"
)
// struct 정의
type datas struct {
Data string `json:"data"`
Version string `json:"version"`
}
func Index(wr http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
fmt.Println("requested: /")
hostname, _ := os.Hostname()
d1 := datas{Data: "Welcome! " + hostname, Version: "v2.0"}
res, _ := json.Marshal(d1)
fmt.Fprintln(wr, string(res))
fmt.Println(string(res))
}
}
func main() {
pid := os.Getpid()
fmt.Println("pid", pid)
server := &http.Server{Addr: ":8080"}
http.HandleFunc("/", Index)
go func() {
if err := server.ListenAndServe(); err != nil {
// handle err
}
}()
// Setting up signal capturing
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
// Waiting for SIGINT (kill -2)
<-stop
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
fmt.Println("exit")
defer cancel()
if err := server.Shutdown(ctx); err != nil {
// handle err
}
// Wait for ListenAndServe goroutine to close.
}