-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
main.go
59 lines (50 loc) 路 1.38 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
// This program is intended to run as a sidecar of other, temporary containers.
// After startup, it begins to listen to incoming HTTP connections on port
// :8008. Requests to `/sync/:id` endpoint hang until canceled, and then
// trigger container `:id` to be terminated.
//
// If this program doesn't get any input for 10 seconds, it halts. It also
// terminates after an attempt to stop the requested container.
//
// Inspired by testcontainers/moby-ryuk project.
package main
import (
"log"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/orlangure/gnomock"
)
func main() {
var (
once sync.Once
connected = make(chan bool)
)
go func() {
log.Println("waiting for input")
select {
case <-connected:
case <-time.After(time.Second * 10):
log.Fatalln("no incoming connections, stopping")
}
}()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
http.HandleFunc("/sync/", func(w http.ResponseWriter, r *http.Request) {
once.Do(func() { connected <- true })
id := strings.TrimPrefix(r.URL.Path, "/sync/")
log.Println("got request to kill", id)
<-r.Context().Done()
if err := gnomock.Stop(&gnomock.Container{
ID: id,
}); err != nil {
log.Fatalf("can't stop container %s: %s\n", id, err.Error())
}
log.Println(id, "killed, exiting")
os.Exit(0)
})
log.Fatalln(http.ListenAndServe(":8008", nil))
}