-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.go
176 lines (145 loc) · 3.82 KB
/
server.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package docs
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"os"
"strings"
"syscall"
"time"
)
const (
defaultRoute = "/api"
defaultDirectory = "./internal/dist"
defaultIndexPath = "/index.html"
fwSlashSuffix = "/"
sigContSleeperMilliseconds = 20
readHeaderTimeout = 60 * time.Second // same than nginx
)
// ConfigSwaggerUI represents a structure which will be used to pass required configuration params to
//
// the ServeSwaggerUI func.
type ConfigSwaggerUI struct {
Route string
Port string
httpServer *http.Server
initFS fileSystem
stopper chan os.Signal
}
// ServeSwaggerUI does what its name implies - runs Swagger UI on mentioned set port and route.
func ServeSwaggerUI(conf *ConfigSwaggerUI) error {
if conf == nil {
return errors.New("swagger config is required")
}
if conf.Route == "" {
conf.Route = defaultRoute
}
if conf.initFS.isNil() {
conf.initFS = *initializeStandardFS()
}
if conf.httpServer == nil {
conf.initializeDefaultHTTPServer()
}
log.Printf("Serving SwaggerIU on HTTP port: %s\n", conf.Port)
conf.sigCont()
val := <-conf.stopper
switch val {
case syscall.SIGINT, syscall.SIGKILL:
log.Printf("SwaggerUI did not shut down properly: %v", conf.httpServer.Shutdown(context.Background()))
default:
log.Printf("SwaggerUI server experienced an unexpected error: %v", conf.httpServer.ListenAndServe())
}
return nil
}
// fileSystem represents a wrapper for http.FileSystem, with relevant type func implementations.
type fileSystem struct {
fileSysInit http.FileSystem
fsOpenFn fsOpenFn
getStatFn getStatFn
getIsDir getIsDirFn
}
type (
fsOpenFn func(name string) (http.File, error)
fsIsDirFn func() bool
fileStatFn func() (os.FileInfo, error)
getStatFn func(file http.File) fileStatFn
getIsDirFn func(file os.FileInfo) fsIsDirFn
)
func initializeStandardFS() *fileSystem {
fsInit := http.Dir(defaultDirectory)
return &fileSystem{
fileSysInit: fsInit,
fsOpenFn: newFSOpen(fsInit),
getStatFn: newGetStatFn(),
getIsDir: newGetIsDirFn(),
}
}
func newFSOpen(fis http.FileSystem) fsOpenFn {
return func(name string) (http.File, error) {
return fis.Open(name)
}
}
func newGetStatFn() getStatFn {
return func(file http.File) fileStatFn {
return func() (os.FileInfo, error) {
return file.Stat()
}
}
}
func newGetIsDirFn() getIsDirFn {
return func(file os.FileInfo) fsIsDirFn {
return func() bool {
return file.IsDir()
}
}
}
func (fis *fileSystem) isNil() bool {
if fis == nil {
return true
}
if fis.getStatFn == nil ||
fis.getIsDir == nil ||
fis.fsOpenFn == nil ||
fis.fileSysInit == nil {
return true
}
return false
}
// Open opens file. Returns http.File, and error if there is any.
func (fis fileSystem) Open(path string) (http.File, error) {
f, err := fis.fsOpenFn(path)
if err != nil {
return nil, fmt.Errorf("failed to open file in path %s :%w", path, err)
}
fileInfo, err := fis.getStatFn(f)()
if err != nil {
return f, fmt.Errorf("failed to fetch file info :%w", err)
}
if fis.getIsDir(fileInfo)() {
index := strings.TrimSuffix(path, fwSlashSuffix) + defaultIndexPath
if _, err = fis.fileSysInit.Open(index); err != nil {
return nil, fmt.Errorf("failed trimming path sufix :%w", err)
}
}
return f, nil
}
func (c *ConfigSwaggerUI) initializeDefaultHTTPServer() {
fileServer := http.FileServer(c.initFS)
c.httpServer = &http.Server{
Addr: fmt.Sprintf(":%s", c.Port),
Handler: http.StripPrefix(strings.TrimRight(c.Route, fwSlashSuffix), fileServer),
ReadHeaderTimeout: readHeaderTimeout,
}
}
func (c *ConfigSwaggerUI) sigCont() {
if c.stopper == nil {
osSignal := make(chan os.Signal)
c.stopper = osSignal
go func() {
time.Sleep(sigContSleeperMilliseconds * time.Millisecond)
osSignal <- syscall.SIGCONT
}()
}
}