/
restApi.go
126 lines (113 loc) · 3.48 KB
/
restApi.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package restAPI
import (
"context"
"fmt"
"net/http"
"strings"
"sync"
"time"
"github.com/exasol/extension-manager/pkg/extensionController"
log "github.com/sirupsen/logrus"
)
// RestAPI is the interface that provides the REST API server of the extension-manager.
type RestAPI interface {
// Serve starts the server. This method blocks until the server is stopped or fails.
Serve()
// StartInBackground starts the server in the background and blocks until it is ready, i.e. reacts to HTTP requests.
StartInBackground()
// Stop stops the server.
Stop()
}
// Create creates a new RestAPI.
func Create(controller extensionController.TransactionController, serverAddress string, addCauseToInternalServerError bool) RestAPI {
return &restAPIImpl{
controller: controller,
serverAddress: serverAddress,
addCauseToInternalServerError: addCauseToInternalServerError,
server: nil,
stopped: nil,
stoppedMutex: nil,
}
}
type restAPIImpl struct {
controller extensionController.TransactionController
addCauseToInternalServerError bool
serverAddress string
server *http.Server
stopped *bool
stoppedMutex *sync.Mutex
}
func (api *restAPIImpl) StartInBackground() {
go api.Serve()
api.waitUntilServerReplies()
}
func (api *restAPIImpl) Serve() {
if api.server != nil {
panic("server already running")
}
api.setStopped(false)
handler, _, err := setupStandaloneAPI(api.controller, api.addCauseToInternalServerError)
if err != nil {
log.Fatalf("failed to setup api: %v", err)
}
//nolint:exhaustruct // Default values are ok for other fields
api.server = &http.Server{
Addr: api.serverAddress,
Handler: handler,
ReadHeaderTimeout: 3 * time.Second,
}
api.startServer()
}
// startServer starts the server on the given serverAddress. This method blocks until the server is stopped or fails.
func (api *restAPIImpl) startServer() {
log.Printf("Starting server on %s...\n", api.serverAddress)
err := api.server.ListenAndServe() // blocking
if err != nil && !api.isStopped() {
log.Fatalf("failed to start server: %v", err)
}
}
func (api *restAPIImpl) waitUntilServerReplies() {
request, err := http.NewRequestWithContext(context.Background(), "GET", "http://"+api.serverAddress, strings.NewReader(""))
if err != nil {
log.Fatalf("failed to create request: %v", err)
}
timeout := time.Now().Add(1 * time.Second)
for {
response, err := http.DefaultClient.Do(request)
if err == nil {
response.Body.Close()
if response.StatusCode == 404 {
return
}
}
if time.Now().After(timeout) {
log.Fatalf("Server did not reply within 1s, error: %v, response: %d %s", err, response.StatusCode, response.Status)
}
}
}
func (api *restAPIImpl) setStopped(stopped bool) {
if api.stopped == nil {
stopped := false
api.stopped = &stopped
api.stoppedMutex = &sync.Mutex{}
}
api.stoppedMutex.Lock()
defer api.stoppedMutex.Unlock()
*api.stopped = stopped
}
func (api *restAPIImpl) isStopped() bool {
api.stoppedMutex.Lock()
defer api.stoppedMutex.Unlock()
return *api.stopped
}
func (api *restAPIImpl) Stop() {
if api.server == nil {
panic("cant stop server since it's not running")
}
api.setStopped(true)
err := api.server.Shutdown(context.Background())
if err != nil {
panic(fmt.Sprintf("failed to shutdown rest API server. Cause: %v", err))
}
api.server = nil
}