-
Notifications
You must be signed in to change notification settings - Fork 0
/
rundefaultserver.go
51 lines (44 loc) · 1.2 KB
/
rundefaultserver.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
package main
import (
"fmt"
"github.com/httpreserve/httpreserve"
"github.com/httpreserve/simplerequest"
"net/http"
"os"
"os/exec"
"strings"
"time"
)
func launchRemoteBackgroundProcess() {
cmd := exec.Command("httpreserve-app", "-demo")
err := cmd.Start()
if err != nil {
fmt.Fprintln(os.Stderr, "Error starting myself in the background")
}
time.Sleep(2)
}
func testDefaultServer() bool {
// no cost to us to try and start ourselves...
launchRemoteBackgroundProcess()
// setup a simple call to the server to look for a 200 OK response
sr, err := simplerequest.Create(http.MethodHead, defaulthpserver)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
return false
}
// send a request to the server to see if it's there...
ls, err := httpreserve.HTTPFromSimpleRequest(sr, "")
if err != nil {
if strings.Contains(err.Error(), "getsockopt: connection refused") {
fmt.Fprintln(os.Stderr, "httpreserve server is not running.")
}
fmt.Fprintf(os.Stderr, "%s\n", err)
return false
}
// if our response is anything other than 200 OK return to exit...
if ls.ResponseCode != http.StatusOK {
fmt.Fprintln(os.Stderr, "httpreserve server is not running.")
return false
}
return true
}