forked from go-training/drone-golang-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (48 loc) · 1.11 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
)
// HelloWorld for hello world
func HelloWorld() string {
return "Trigger the GitHub Actions, traefik workshop!"
}
func handler(w http.ResponseWriter, r *http.Request) {
log.Printf("Got http request. time: %v", time.Now())
fmt.Fprintf(w, "I love %s!, %s", r.URL.Path[1:], HelloWorld())
}
func pinger(port string) error {
resp, err := http.Get("http://localhost:" + port)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("server returned not-200 status code")
}
return nil
}
func main() {
var port string
var ping bool
flag.StringVar(&port, "port", "8080", "server port")
flag.StringVar(&port, "p", "8080", "server port")
flag.BoolVar(&ping, "ping", false, "check server live")
flag.Parse()
if p, ok := os.LookupEnv("PORT"); ok {
port = p
}
if ping {
if err := pinger(port); err != nil {
log.Printf("ping server error: %v\n", err)
}
return
}
http.HandleFunc("/", handler)
log.Println("http server run on " + port + " port")
log.Fatal(http.ListenAndServe(":"+port, nil))
}