/
server.go
3024 lines (2510 loc) · 92.7 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package diecast
//go:generate esc -o static.go -pkg diecast -modtime 1500000000 -prefix ui ui
//go:generate make favicon.go
import (
"bytes"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"errors"
"fmt"
"html/template"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
"sync"
"syscall"
"time"
"github.com/ghetzel/go-stockutil/fileutil"
"github.com/ghetzel/go-stockutil/httputil"
"github.com/ghetzel/go-stockutil/log"
"github.com/ghetzel/go-stockutil/maputil"
"github.com/ghetzel/go-stockutil/netutil"
"github.com/ghetzel/go-stockutil/pathutil"
"github.com/ghetzel/go-stockutil/sliceutil"
"github.com/ghetzel/go-stockutil/stringutil"
"github.com/ghetzel/go-stockutil/timeutil"
"github.com/ghetzel/go-stockutil/typeutil"
"github.com/ghetzel/ratelimit"
"github.com/gobwas/glob"
jwt "github.com/golang-jwt/jwt"
"github.com/husobee/vestigo"
"github.com/mattn/go-shellwords"
opentracing "github.com/opentracing/opentracing-go"
"github.com/signalsciences/tlstext"
"github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"golang.org/x/text/language"
"gopkg.in/yaml.v2"
)
var ITotallyUnderstandRunningArbitraryCommandsAsRootIsRealRealBad = false
var DirectoryErr = errors.New(`is a directory`)
var DefaultLocale = language.AmericanEnglish
var DefaultLogFormat = `common`
var DefaultProtocol = `http`
const weirdPathsInHostnamesPlaceholder = "\u2044"
func init() {
maputil.UnmarshalStructTag = `json`
stringutil.ExpandEnvPreserveIfEmpty = true
}
var logFormats = map[string]string{
`common`: "${remote_address} - - [${request_started_at:%02/Jan/2006:15:04:05 -0700}] \"${method} ${url} ${protocol}\" ${status_code} ${response_length}\n",
}
// Registers a new named format for log output
func RegisterLogFormat(name string, format string) {
logFormats[name] = format
}
func IsDirectoryErr(err error) bool {
return (err == DirectoryErr)
}
const DefaultQueryJoiner = `,`
const DefaultHeaderJoiner = `,`
const DefaultAddress = `127.0.0.1:28419`
const DefaultRoutePrefix = `/`
const DefaultConfigFile = `diecast.yml`
const DefaultLayoutsPath = `/_layouts`
const DefaultErrorsPath = `/_errors`
const DebuggingQuerystringParam = `__viewsource`
const LayoutTemplateName = `layout`
const ContentTemplateName = `content`
const ContextRequestKey = `diecast-request-id`
const ContextResponseKey = `diecast-response`
const JaegerSpanKey = `jaeger-span`
const RequestBodyKey = `request-body`
var HeaderSeparator = []byte{'-', '-', '-'}
var DefaultIndexFile = `index.html`
var DefaultVerifyFile = `/` + DefaultIndexFile
var DefaultTemplatePatterns = []string{`*.html`, `*.md`, `*.scss`}
var DefaultAutocompressPatterns = []string{`*.zip`, `*.docx`, `*.xlsx`, `*.pptx`}
var DefaultTryExtensions = []string{`html`, `md`}
var DefaultAutoindexFilename = `/autoindex.html`
var DefaultRequestBodyPreload int64 = 1048576
var DefaultAutolayoutPatterns = []string{
`*.html`,
`*.md`,
}
var DefaultRendererMappings = map[string]string{
`md`: `markdown`,
`scss`: `sass`,
`pptx`: `ooxml`,
`xlsx`: `ooxml`,
`docx`: `ooxml`,
}
var DefaultFilterEnvVars = []string{
`_*`, // treat vars starting with "_" as internal/hidden
`*KEY*`, // omit api keys and whatnot
`*PASSWORD*`, // omit things that explicitly call themselves a "password"
`*PID*`, // avoid PID leakage for weird paranoid reasons
`*HOST*`, // while we're at it, don't leak hostnames either
`*URL*`, // ...or URLs
`*SECRET*`, // not a secret if you go blabbing about it...
`*TOKEN*`, // ditto.
`AWS_ACCESS_KEY_ID`, // very specifically omit AWS credentials
`AWS_SECRET_ACCESS_KEY`, // very specifically omit AWS credentials
`PROMPT_COMMAND`, // people keep weird stuff in here sometimes
`PWD`, // WHO WANTS TO KNOW?
}
type ServeFunc func(*Server) error
type Serveable interface {
ListenAndServe() error
ListenAndServeTLS(string, string) error
Serve(net.Listener) error
ServeTLS(net.Listener, string, string) error
}
type RedirectTo string
func (self RedirectTo) Error() string {
return string(self)
}
type StartCommand struct {
Command string `yaml:"command" json:"command"` // The shell command line to execute on start
Directory string `yaml:"directory" json:"directory"` // The working directory the command should be run from
Environment map[string]interface{} `yaml:"env" json:"env"` // A map of environment variables to expose to the command
WaitBefore string `yaml:"delay" json:"delay"` // How long to delay before running the command
Wait string `yaml:"timeout" json:"timeout"` // How long to wait before killing the command
ExitOnCompletion bool `yaml:"exitOnCompletion" json:"exitOnCompletion"` // Whether Diecast should exit upon command completion
cmd *exec.Cmd
}
type TlsConfig struct {
Enable bool `yaml:"enable" json:"enable"` // Whether to enable SSL/TLS on the server.
CertFile string `yaml:"cert" json:"cert"` // path to a PEM-encoded (.crt) file containing the server's TLS public key.
KeyFile string `yaml:"key" json:"key"` // path to a PEM-encoded (.key) file containing the server's TLS private key.
ClientCertMode string `yaml:"clients" json:"clients"` // If set, TLS Client certificates will be requested/accepted. If set, may be one of: "request", "any", "verify", "require"
ClientCAFile string `yaml:"clientCA" json:"clientCA"` // Path to a PEM-encoded file containing the CA that client certificates are issued and verify against.
}
type LogConfig struct {
Format string `yaml:"format" json:"format"` // configure the output format for logging requests
Destination string `yaml:"destination" json:"destination"` // specify where logs should be written to
Truncate bool `yaml:"truncate" json:"truncate"` // if true, the output log file will be truncated on startup
Colorize bool `yaml:"colorize" json:"colorize"` // if false, log output will not be colorized
Disable bool `yaml:"disable" json:"disable"` // if true, no log output will be written
}
type RateLimitConfig struct {
Enable bool `yaml:"enable" json:"enable"`
Limit string `yaml:"limit" json:"limit"` // Specify a rate limit string (e.g.: "1r/s", "200r/m")
PerClient bool `yaml:"per_client" json:"per_client"` // Specify that the limit should be applied per-client instead of globally.
Penalty string `yaml:"penalty" json:"penalty"` // An amount of time to sleep instead of returning an HTTP 429 error on rate limited requests
}
func (self *RateLimitConfig) KeyFor(req *http.Request) string {
if self.PerClient {
return req.RemoteAddr
} else {
return `__global__`
}
}
type TraceMapping struct {
Match string `yaml:"match" json:"match"` // A regular expression used to match candidate trace operation names
Replace string `yaml:"replace" json:"replace"` // A string that will replace matching operation names.
rx *regexp.Regexp
}
func (self *TraceMapping) TraceName(candidate string) (string, bool) {
if self.Match != `` {
if self.rx == nil {
if rx, err := regexp.Compile(self.Match); err == nil {
self.rx = rx
}
}
}
if self.rx != nil {
if self.rx.MatchString(candidate) {
return self.rx.ReplaceAllString(candidate, self.Replace), true
}
}
return ``, false
}
type JWTConfig struct {
Algorithm string `yaml:"alg" json:"alg"` // The JWT signing algorithm to use (default: HS256)
Secret string `yaml:"secret" json:"secret"` // The JWT secret used to sign payloads
Claims map[string]interface{} `yaml:"claims" json:"claims"` // The claims being made (i.e.: the payload that will be converted to JSON)
Expires interface{} `yaml:"expires" json:"expires"` // A duration string representing how long issued tokens will be valid for (default: 60s)
Issuer string `yaml:"issuer" json:"issuer"` // The JWT issuer
Subject string `yaml:"subject" json:"subject"`
}
func (self *JWTConfig) SignedString(input string) (string, error) {
var alg = typeutil.OrString(self.Algorithm, `HS256`)
if signer := jwt.GetSigningMethod(alg); signer != nil {
return signer.Sign(input, []byte(self.Secret))
} else {
return ``, fmt.Errorf("invalid signing algorithm %q", alg)
}
}
func (self *JWTConfig) Issue(tpldata map[string]interface{}, funcs FuncMap) (string, error) {
var now = time.Now()
var alg = typeutil.OrString(self.Algorithm, `HS256`)
var expiry = typeutil.OrDuration(self.Expires, `60s`)
if signer := jwt.GetSigningMethod(alg); signer != nil {
var claims jwt.Claims
if len(self.Claims) == 0 {
claims = jwt.StandardClaims{
Id: stringutil.UUID().String(),
IssuedAt: now.Unix(),
ExpiresAt: now.Add(expiry).Unix(),
Issuer: ShouldEvalInline(self.Issuer, tpldata, funcs).String(),
Subject: ShouldEvalInline(self.Subject, tpldata, funcs).String(),
NotBefore: now.Unix(),
}
} else {
var c = jwt.MapClaims(self.Claims)
if self.Issuer != `` {
c[`iss`] = self.Issuer
}
c[`iat`] = now.Unix()
c[`exp`] = now.Add(expiry).Unix()
c[`jti`] = stringutil.UUID().String()
for k, v := range c {
c[k] = ShouldEvalInline(v, tpldata, funcs).Value
}
claims = c
}
// put it all together: sign the JSONified claims using the given signing method and secret
return jwt.NewWithClaims(signer, claims).SignedString([]byte(self.Secret))
} else {
return ``, fmt.Errorf("invalid signing algorithm %q", alg)
}
}
type JaegerConfig struct {
Enable bool `yaml:"enable" json:"enable"` // Explicitly enable or disable Jaeger tracing
ServiceName string `yaml:"service" json:"service"` // Set the service name that traces will fall under.
Agent string `yaml:"agent" json:"agent"` // Specify the host:port of a local UDP agent to send traces to.
Collector string `yaml:"collector" json:"collector"` // Specify the collector address to sent traces to. Overrides "agent" if set.
Username string `yaml:"username" json:"username"` // Provides a username to authenticate with the collector.
Password string `yaml:"password" json:"password"` // Provides a password to authenticate with the collector.
QueueSize int `yaml:"queueSize" json:"queueSize"` // Specify the size of the queue for outgoing reports.
FlushInterval string `yaml:"flushInterval" json:"flushInterval"` // Duration specifying how frequently queued reports should be flushed.
Tags map[string]interface{} `yaml:"tags" json:"tags"` // A set of key-value pairs that are included in every trace.
SamplingType string `yaml:"sampling" json:"sampling"` // Specifies the type of sampling to use: const, probabilistic, rateLimiting, or remote.
SamplingParam float64 `yaml:"samplingParam" json:"samplingParam"` // A type-specific parameter used to configure that type of sampling; const: 0 or 1, probabilistic: 0.0-1.0, rateLimiting: max number of spans per seconds, remote: same as probabilistic.
SamplingServerURL string `yaml:"samplingUrl" json:"samplingUrl"` // The sampling server URL for the "remote" sampling type.
SamplingRefreshInterval string `yaml:"samplingRefreshInterval" json:"samplingRefreshInterval"` // How frequently to poll the remote sampling server.
SamplingMaxOperations int `yaml:"samplingMaxOps" json:"samplingMaxOps"` // A maximum number of operations for certain sampling modes.
OperationsMappings []*TraceMapping `yaml:"operations" json:"operations"` // Maps regular expressions used to match specific routes to the operation name that will be emitted in traces. Without a matching expression, traces will be named by the calling HTTP method and Request URI. The string being tested by these regular expressions is the one that would be emitted otherwise; so "GET /path/to/file"
}
type Server struct {
Actions []*Action `yaml:"actions" json:"actions"` // Configure routes and actions to execute when those routes are requested.
AdditionalFunctions template.FuncMap `yaml:"-" json:"-"` // Allow for the programmatic addition of extra functions for use in templates.
Address string `yaml:"address" json:"address"` // The host:port address the server is listening on
Authenticators AuthenticatorConfigs `yaml:"authenticators" json:"authenticators"` // A set of authenticator configurations used to protect some or all routes.
Autoindex bool `yaml:"autoindex" json:"autoindex"` // Specify that requests that terminate at a filesystem directory should automatically generate an index listing of that directory.
AutoindexTemplate string `yaml:"autoindexTemplate" json:"autoindexTemplate"` // If Autoindex is enabled, this allows the template used to generate the index page to be customized.
AutolayoutPatterns []string `yaml:"autolayoutPatterns" json:"autolayoutPatterns"` // Which types of files will automatically have layouts applied.
BaseHeader *TemplateHeader `yaml:"header" json:"header"` // A default header that all templates will inherit from.
BinPath string `yaml:"-" json:"-"` // Exposes the location of the diecast binary
BindingPrefix string `yaml:"bindingPrefix" json:"bindingPrefix"` // Specify a string to prefix all binding resource values that start with "/"
Bindings SharedBindingSet `yaml:"bindings" json:"bindings"` // Top-level bindings that apply to every rendered template
DefaultPageObject map[string]interface{} `yaml:"-" json:"-"` //
DisableCommands bool `yaml:"disable_commands" json:"disable_commands"` // Disable the execution of PrestartCommands and StartCommand .
DisableTimings bool `yaml:"disableTimings" json:"disableTimings"` // Disable emitting per-request Server-Timing headers to aid in tracing bottlenecks and performance issues.
EnableDebugging bool `yaml:"debug" json:"debug"` // Enables additional options for debugging applications. Caution: can expose secrets and other sensitive data.
DebugDumpRequests map[string]string `yaml:"debugDumpRequests" json:"debugDumpRequests"` // An object keyed on path globs whose values are a directory where matching requests are dumped in their entirety as text files.
EnableLayouts bool `yaml:"enableLayouts" json:"enableLayouts"` // Specifies whether layouts are enabled
Environment string `yaml:"environment" json:"environment"` // Specify the environment for loading environment-specific configuration files in the form "diecast.env.yml"
ErrorsPath string `yaml:"errors" json:"errors"` // The path to the errors template directory
ExposeEnvVars []string `yaml:"exposeEnvVars" json:"exposeEnvVars"` // a list of glob patterns matching environment variable names that should always be exposed
FaviconPath string `yaml:"favicon" json:"favicon"` // TODO: favicon autogenerator: Specifies the relative path to the file containing the /favicon.ico file. This path can point to a Windows Icon (.ico), GIF, PNG, JPEG, or Bitmap (.bmp). If necessary, the file will be converted and stored in memory to the ICO format.
FilterEnvVars []string `yaml:"filterEnvVars" json:"filterEnvVars"` // a list of glob patterns matching environment variable names that should not be exposed
GlobalHeaders map[string]interface{} `yaml:"globalHeaders,omitempty" json:"globalHeaders,omitempty"` // A set of HTTP headers that should be added to EVERY response Diecast returns, regardless of whether it originates from a template, mount, or other configuration.
IndexFile string `yaml:"indexFile" json:"indexFile"` // The name of the template file to use when a directory is requested.
LayoutPath string `yaml:"layouts" json:"layouts"` // The path to the layouts template directory
Locale string `yaml:"locale" json:"locale"` // Specify the default locale for pages being served.
MountConfigs []MountConfig `yaml:"mounts" json:"mounts"` // A list of mount configurations read from the diecast.yml config file.
Mounts []Mount `yaml:"-" json:"-"` // The set of all registered mounts.
OnAddHandler AddHandlerFunc `yaml:"-" json:"-"` // A function that can be used to intercept handlers being added to the server.
OverridePageObject map[string]interface{} `yaml:"-" json:"-"` //
PrestartCommands []*StartCommand `yaml:"prestart" json:"prestart"` // A command that will be executed before the server is started.
Protocols map[string]ProtocolConfig `yaml:"protocols" json:"protocols"` // Setup global configuration details for Binding Protocols
RendererMappings map[string]string `yaml:"rendererMapping" json:"rendererMapping"` // Map file extensions to preferred renderers for a given file type.
RootPath string `yaml:"root" json:"root"` // The filesystem location where templates and files are served from
RoutePrefix string `yaml:"routePrefix" json:"routePrefix"` // If specified, all requests must be prefixed with this string.
StartCommands []*StartCommand `yaml:"start" json:"start"` // A command that will be executed after the server is confirmed running.
TLS *TlsConfig `yaml:"tls" json:"tls"` // where SSL/TLS configuration is stored
TemplatePatterns []string `yaml:"patterns" json:"patterns"` // A set of glob patterns specifying which files will be rendered as templates.
Translations map[string]interface{} `yaml:"translations,omitempty" json:"translations,omitempty"` // Stores translations for use with the i18n and l10n functions. Keys values represent the
TrustedRootPEMs []string `yaml:"trustedRootPEMs" json:"trustedRootPEMs"` // List of filenames containing PEM-encoded X.509 TLS certificates that represent trusted authorities. Use to validate certificates signed by an internal, non-public authority.
TryExtensions []string `yaml:"tryExtensions" json:"tryExtensions"` // Try these file extensions when looking for default (i.e.: "index") files. If IndexFile has an extension, it will be stripped first.
TryLocalFirst bool `yaml:"localFirst" json:"localFirst"` // Whether to attempt to locate a local file matching the requested path before attempting to find a template.
VerifyFile string `yaml:"verifyFile" json:"verifyFile"` // A file that must exist and be readable before starting the server.
PreserveConnections bool `yaml:"preserveConnections" json:"preserveConnections"` // Don't add the "Connection: close" header to every response.
CSRF *CSRF `yaml:"csrf" json:"csrf"` // configures CSRF protection
Log LogConfig `yaml:"log" json:"log"` // configure logging
BeforeHandlers []Middleware `yaml:"-" json:"-"` // contains a stack of Middleware functions that are run before handling the request
AfterHandlers []http.HandlerFunc `yaml:"-" json:"-"` // contains a stack of HandlerFuncs that are run after handling the request. These functions cannot stop the request, as it's already been written to the client.
Protocol string `yaml:"protocol" json:"protocol"` // Specify which HTTP protocol to use ("http", "http2")
RateLimit *RateLimitConfig `yaml:"ratelimit" json:"ratelimit"` // Specify a rate limiting configuration.
BindingTimeout interface{} `yaml:"bindingTimeout" json:"bindingTimeout"` // Sets the default timeout for bindings that don't explicitly set one.
JaegerConfig *JaegerConfig `yaml:"jaeger" json:"jaeger"` // Configures distributed tracing using Jaeger.
AutocompressPatterns []string `yaml:"autocompress" json:"autocompress"` // A set of glob patterns indicating directories whose contents will be delivered as ZIP files
RequestBodyPreload int64 `yaml:"requestPreload" json:"requestPreload"` // Maximum number of bytes to read from a request body for the purpose of automatically parsing it. Requests larger than this will not be available to templates.
JWT map[string]*JWTConfig `yaml:"jwt" json:"jwt"` // Contains configurations for generating JSON Web Tokens in templates.
altRootCaPool *x509.CertPool
faviconImageIco []byte
fs http.FileSystem
hasUserRoutes bool
initialized bool
precmd *exec.Cmd
mux *http.ServeMux
userRouter *vestigo.Router
logwriter io.Writer
isTerminalOutput bool
rateLimiter *ratelimit.Limit
jaegerCfg *jaegercfg.Configuration
opentrace opentracing.Tracer
otcloser io.Closer
viaConstructor bool
sharedBindingData sync.Map
lockGetFunctions sync.Mutex
}
func NewServer(root interface{}, patterns ...string) *Server {
describeTimer(`tpl`, `Diecast Template Rendering`)
var server = &Server{
RootPath: `.`,
TemplatePatterns: patterns,
Authenticators: make(AuthenticatorConfigs, 0),
Bindings: make(SharedBindingSet, 0),
DefaultPageObject: make(map[string]interface{}),
Mounts: make([]Mount, 0),
OverridePageObject: make(map[string]interface{}),
GlobalHeaders: make(map[string]interface{}),
EnableLayouts: true,
RequestBodyPreload: DefaultRequestBodyPreload,
mux: http.NewServeMux(),
viaConstructor: true,
}
server.populateDefaults()
if str, ok := root.(string); ok {
server.RootPath = str
} else if fs, ok := root.(http.FileSystem); ok {
server.SetFileSystem(fs)
}
return server
}
func (self *Server) ShouldReturnSource(req *http.Request) bool {
if self.EnableDebugging {
if httputil.QBool(req, DebuggingQuerystringParam) {
return true
}
}
return false
}
func (self *Server) LoadConfig(filename string) error {
if pathutil.FileExists(filename) {
if file, err := os.Open(filename); err == nil {
defer file.Close()
return self.LoadConfigFromReader(file, filename)
} else {
return err
}
}
return nil
}
func (self *Server) LoadConfigFromReader(file io.Reader, filename string) error {
if data, err := ioutil.ReadAll(file); err == nil && len(data) > 0 {
data = []byte(stringutil.ExpandEnv(string(data)))
if err := yaml.UnmarshalStrict(data, self); err == nil {
// apply environment-specific overrides
if self.Environment != `` && filename != `` {
eDir, eFile := filepath.Split(filename)
var base = strings.TrimSuffix(eFile, filepath.Ext(eFile))
var ext = filepath.Ext(eFile)
eFile = fmt.Sprintf("%s.%s%s", base, self.Environment, ext)
var envPath = filepath.Join(eDir, eFile)
if fileutil.IsNonemptyFile(envPath) {
if err := self.LoadConfig(envPath); err != nil {
return fmt.Errorf("failed to load %s: %v", eFile, err)
}
}
}
// process mount configs into mount instances
for i, config := range self.MountConfigs {
if mount, err := NewMountFromSpec(fmt.Sprintf("%s:%s", config.Mount, config.To)); err == nil {
var mountOverwriteIndex = -1
for i, existing := range self.Mounts {
if IsSameMount(mount, existing) {
mountOverwriteIndex = i
break
}
}
if err := maputil.TaggedStructFromMap(config.Options, mount, `json`); err != nil {
return fmt.Errorf("mount %d options: %v", i, err)
}
if mountOverwriteIndex >= 0 {
log.Debugf("mount: overwriting mountpoint with new configuration: %v", mount)
self.Mounts[mountOverwriteIndex] = mount
} else {
self.Mounts = append(self.Mounts, mount)
}
} else {
return fmt.Errorf("invalid mount %d: %v", i, err)
}
}
} else {
return err
}
} else {
return err
}
return nil
}
// Append the specified mounts to the current server.
func (self *Server) SetMounts(mounts []Mount) {
if len(self.Mounts) > 0 {
self.Mounts = append(self.Mounts, mounts...)
} else {
self.Mounts = mounts
}
}
func (self *Server) SetFileSystem(fs http.FileSystem) {
self.fs = fs
}
// Read a file from the underlying root filesystem, satisfying the http.FileSystem interface.
func (self *Server) Open(name string) (http.File, error) {
if self.fs == nil {
return nil, fmt.Errorf("no filesystem")
} else {
return self.fs.Open(name)
}
}
func (self *Server) IsInRootPath(path string) bool {
if absR, err := filepath.Abs(self.RootPath); err == nil {
if absP, err := filepath.Abs(path); err == nil {
absR, _ := filepath.EvalSymlinks(absR)
absP, _ := filepath.EvalSymlinks(absP)
if absP == absR || strings.HasPrefix(absP, absR) {
return true
}
}
}
return false
}
func (self *Server) populateDefaults() {
if self.mux == nil {
self.mux = http.NewServeMux()
}
self.handlersEnsureRouter()
if self.Log.Format == `` {
self.Log.Format = logFormats[`common`]
self.Log.Destination = `-`
self.Log.Colorize = true
}
if !self.viaConstructor {
self.EnableLayouts = true
}
if len(self.AutolayoutPatterns) == 0 {
self.AutolayoutPatterns = DefaultAutolayoutPatterns
}
if len(self.AutocompressPatterns) == 0 {
self.AutocompressPatterns = DefaultAutocompressPatterns
}
if len(self.TemplatePatterns) == 0 {
self.TemplatePatterns = DefaultTemplatePatterns
}
if len(self.RendererMappings) == 0 {
self.RendererMappings = DefaultRendererMappings
}
if len(self.TryExtensions) == 0 {
self.TryExtensions = DefaultTryExtensions
}
if len(self.FilterEnvVars) == 0 {
self.FilterEnvVars = DefaultFilterEnvVars
}
if self.Address == `` {
self.Address = DefaultAddress
}
if self.ErrorsPath == `` {
self.ErrorsPath = DefaultErrorsPath
}
if self.IndexFile == `` {
self.IndexFile = DefaultIndexFile
}
if self.LayoutPath == `` {
self.LayoutPath = DefaultLayoutsPath
}
if self.RoutePrefix == `` {
self.RoutePrefix = DefaultRoutePrefix
}
if self.VerifyFile == `` {
self.VerifyFile = DefaultVerifyFile
}
if self.AutoindexTemplate == `` {
self.AutoindexTemplate = DefaultAutoindexFilename
}
if self.Protocol == `` {
self.Protocol = DefaultProtocol
}
if self.BindingTimeout == `` {
self.BindingTimeout = DefaultBindingTimeout
}
if len(self.JWT) == 0 {
self.JWT = make(map[string]*JWTConfig)
}
}
func (self *Server) Initialize() error {
self.populateDefaults()
// if we haven't explicitly set a filesystem, create it
if self.fs == nil {
if strings.Contains(self.RootPath, `://`) {
if mnt, err := NewMountFromSpec(`/:` + self.RootPath); err == nil {
self.SetFileSystem(mnt)
} else {
return fmt.Errorf("root mount: %v", err)
}
} else {
if v, err := fileutil.ExpandUser(self.RootPath); err == nil {
self.RootPath = v
}
if v, err := filepath.Abs(self.RootPath); err == nil {
self.RootPath = v
} else {
return fmt.Errorf("root path: %v", err)
}
self.SetFileSystem(http.Dir(self.RootPath))
}
}
log.Debugf("rootfs: %T(%v)", self.fs, self.RootPath)
// allocate ephemeral address if we're supposed to
if addr, port, err := net.SplitHostPort(self.Address); err == nil {
if port == `0` {
if allocated, err := netutil.EphemeralPort(); err == nil {
self.Address = fmt.Sprintf("%v:%d", addr, allocated)
} else {
return err
}
}
}
if err := self.initJaegerTracing(); err != nil {
return fmt.Errorf("jaeger: %v", err)
}
// if configured, this path must exist (relative to RootPath or the root filesystem) or Diecast will refuse to start
if self.VerifyFile != `` {
if verify, err := self.fs.Open(self.VerifyFile); err == nil {
verify.Close()
} else {
return fmt.Errorf("Failed to open verification file %q: %v.", self.VerifyFile, err)
}
}
if err := self.setupServer(); err != nil {
return err
}
if err := self.Bindings.init(self); err != nil {
return fmt.Errorf("async bindings: %v", err)
}
self.initialized = true
if self.DisableCommands {
log.Noticef("Not executing PrestartCommand because DisableCommands is set")
return nil
} else if _, err := self.RunStartCommand(self.PrestartCommands, false); err != nil {
return err
} else {
return nil
}
}
func (self *Server) prestart() error {
if !self.initialized {
if err := self.Initialize(); err != nil {
return err
}
}
go func() {
if self.DisableCommands {
log.Noticef("Not executing StartCommand because DisableCommands is set")
return
}
eoc, err := self.RunStartCommand(self.StartCommands, true)
if eoc {
defer func() {
self.cleanupCommands()
os.Exit(0)
}()
}
if err != nil {
log.Errorf("start command failed: %v", err)
}
}()
return nil
}
func (self *Server) initJaegerTracing() error {
// if enabled, initialize tracing (Jaeger/OpenTracing)
if jc := self.JaegerConfig; jc != nil && jc.Enable {
if cfg, err := jaegercfg.FromEnv(); err == nil {
self.jaegerCfg = cfg
} else {
return fmt.Errorf("config: %v", err)
}
if self.jaegerCfg.ServiceName == `` {
self.jaegerCfg.ServiceName = sliceutil.OrString(jc.ServiceName, `diecast`)
}
if self.jaegerCfg.Sampler == nil {
self.jaegerCfg.Sampler = new(jaegercfg.SamplerConfig)
}
if self.jaegerCfg.Reporter == nil {
self.jaegerCfg.Reporter = new(jaegercfg.ReporterConfig)
}
if r := self.jaegerCfg.Reporter; r != nil {
if jc.QueueSize > 0 {
r.QueueSize = jc.QueueSize
}
if jc.Agent != `` {
r.LocalAgentHostPort = jc.Agent
}
if jc.Collector != `` {
r.CollectorEndpoint = jc.Collector
}
if jc.Username != `` {
r.User = jc.Username
}
if jc.Password != `` {
r.Password = jc.Password
}
}
if s := self.jaegerCfg.Sampler; s != nil {
if s.Type == `` {
s.Type = jaeger.SamplerTypeConst
s.Param = 1
} else if jc.SamplingType != `` {
s.Type = jc.SamplingType
s.Param = jc.SamplingParam
}
if jc.SamplingServerURL != `` {
s.SamplingServerURL = jc.SamplingServerURL
}
if jc.SamplingMaxOperations > 0 {
s.MaxOperations = jc.SamplingMaxOperations
}
if jc.SamplingRefreshInterval != `` {
s.SamplingRefreshInterval = typeutil.Duration(jc.SamplingRefreshInterval)
}
}
if jc.FlushInterval != `` {
if bfi := typeutil.Duration(jc.FlushInterval); bfi >= (1 * time.Millisecond) {
self.jaegerCfg.Reporter.BufferFlushInterval = bfi
} else {
return fmt.Errorf("invalid flush interval (minimum: 1ms)")
}
}
if len(jc.Tags) > 0 {
for k, v := range jc.Tags {
self.jaegerCfg.Tags = append(self.jaegerCfg.Tags, opentracing.Tag{
Key: k,
Value: v,
})
}
}
self.jaegerCfg.Tags = append(self.jaegerCfg.Tags, opentracing.Tag{
Key: `diecast-version`,
Value: ApplicationVersion,
})
if ott, otc, err := self.jaegerCfg.NewTracer(); err == nil {
self.opentrace = ott
self.otcloser = otc
opentracing.SetGlobalTracer(self.opentrace)
var logline string
if v := self.jaegerCfg.Reporter.CollectorEndpoint; v != `` {
logline = fmt.Sprintf("collector at %s", v)
} else if v := self.jaegerCfg.Reporter.LocalAgentHostPort; v != `` {
logline = fmt.Sprintf("agent at %s", v)
}
if logline != `` {
log.Debugf("trace: Jaeger tracing enabled: service=%s send to %s", self.jaegerCfg.ServiceName, logline)
if len(self.jaegerCfg.Tags) > 0 {
log.Debugf("trace: global tags:")
for _, tag := range self.jaegerCfg.Tags {
log.Debugf("trace: + %s: %v", tag.Key, typeutil.V(tag.Value))
}
}
}
} else {
return err
}
}
return nil
}
// Perform an end-to-end render of a single path, writing the output to the given writer,
// then exit.
func (self *Server) RenderPath(w io.Writer, path string) error {
path = `/` + strings.TrimPrefix(path, `/`)
var rw = httptest.NewRecorder()
var req = httptest.NewRequest(http.MethodGet, path, nil)
self.ServeHTTP(rw, req)
if !rw.Flushed {
rw.Flush()
}
if res := rw.Result(); res.StatusCode < 400 {
_, err := io.Copy(w, res.Body)
return err
} else {
errbody, _ := ioutil.ReadAll(res.Body)
return fmt.Errorf("render failed: %v", sliceutil.Or(string(errbody), res.Status))
}
}
// Perform a single request to the server and return an http.Response.
func (self *Server) GetResponse(method string, path string, body io.Reader, params map[string]interface{}, headers map[string]interface{}) *http.Response {
path = `/` + strings.TrimPrefix(path, `/`)
var rw = httptest.NewRecorder()
var req = httptest.NewRequest(method, path, body)
for k, v := range params {
httputil.SetQ(req.URL, k, v)
}
for k, v := range headers {
req.Header.Set(k, typeutil.String(v))
}
self.ServeHTTP(rw, req)
if !rw.Flushed {
rw.Flush()
}
return rw.Result()
}
// Return a URL string that can be used to perform requests from the local machine.
func (self *Server) LocalURL() string {
return self.bestInternalLoopbackUrl(nil)
}
// Start a long-running webserver. If provided, the functions provided will be run in parallel
// after the server has started. If any of them return a non-nil error, the server will stop and
// this method will return that error.
func (self *Server) Serve(workers ...ServeFunc) error {
var serveable Serveable
var useTLS bool
var useUDP bool
var useSocket string
var servechan = make(chan error)
// fire off some goroutines for the prestart and start commands (if configured)
if err := self.prestart(); err != nil {
return err
}
var srv = &http.Server{
Handler: self,
}
// work out if we're starting a UNIX socket server
if addr := self.Address; strings.HasPrefix(addr, `unix:`) {
useSocket = strings.TrimPrefix(addr, `unix:`)
if useSocket == `` {
useSocket = `diecast.` + typeutil.String(os.Getpid()) + `.sock`
}
} else {
srv.Addr = addr
}
// setup TLSConfig
if ssl := self.TLS; ssl != nil && ssl.Enable {
var tc = new(tls.Config)
ssl.CertFile = fileutil.MustExpandUser(ssl.CertFile)
ssl.KeyFile = fileutil.MustExpandUser(ssl.KeyFile)
if !fileutil.IsNonemptyFile(ssl.CertFile) {
return fmt.Errorf("ssl: cert file %q is not readable or does not exist", ssl.CertFile)
}
if !fileutil.IsNonemptyFile(ssl.KeyFile) {
return fmt.Errorf("ssl: key file %q is not readable or does not exist", ssl.KeyFile)
}
if mode := ssl.ClientCertMode; mode != `` {
switch mode {
case `request`:
tc.ClientAuth = tls.RequestClientCert
case `any`:
tc.ClientAuth = tls.RequireAnyClientCert
case `verify`:
tc.ClientAuth = tls.VerifyClientCertIfGiven
case `require`:
tc.ClientAuth = tls.RequireAndVerifyClientCert
default:
return fmt.Errorf(
"Invalid value %q for 'ssl_client_certs': must be one of %q, %q, %q, %q.",
mode,
`request`,
`any`,
`verify`,
`require`,
)
}
ssl.ClientCAFile = fileutil.MustExpandUser(ssl.ClientCAFile)
if !fileutil.IsNonemptyFile(ssl.ClientCAFile) {
return fmt.Errorf("ssl: client CA file %q is not readable or does not exist", ssl.ClientCAFile)
}
if pool, err := httputil.LoadCertPool(ssl.ClientCAFile); err == nil {
tc.ClientCAs = pool
} else {
return fmt.Errorf("ssl: client CA: %v", err)
}
}
srv.TLSConfig = tc
useTLS = true
}
// wrap all the various protocol implementations in a common interface
switch strings.ToLower(self.Protocol) {
case ``, `http`:
serveable = srv
case `http2`:
var h2s = new(http2.Server)
if useTLS {
http2.ConfigureServer(srv, h2s)
} else {
var hnd = srv.Handler
srv.Handler = h2c.NewHandler(hnd, h2s)
}
serveable = srv
default:
return fmt.Errorf("unknown protocol %q", self.Protocol)
}
// take a wildly different path if we're listening on a unix socket
if useSocket != `` {
if x, err := fileutil.ExpandUser(useSocket); err == nil {
useSocket = x
} else {
return err
}
var network = `unix`
if useUDP {
network = `unixpacket`
}
if _, err := os.Stat(useSocket); err == nil {
if err := os.Remove(useSocket); err != nil {
return err
}
}
if listener, err := net.Listen(network, useSocket); err == nil {
go func() {
if useTLS {
servechan <- serveable.ServeTLS(listener, self.TLS.CertFile, self.TLS.KeyFile)
} else {
servechan <- serveable.Serve(listener)
}
os.Remove(useSocket)