/
gowebserver.go
128 lines (111 loc) · 3.71 KB
/
gowebserver.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
// Copyright 2022 Jeremy Edwards
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gowebserver
import (
"fmt"
"os"
"github.com/jeremyje/gomain"
"github.com/jeremyje/gowebserver/v2/pkg/certtool"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func Run() {
_, syncFunc := configLogger(true)
defer syncFunc()
gomain.Run(runInteractive, gomain.Config{
ServiceName: "gowebserver",
ServiceDescription: "A simple, convenient, reliable, well tested HTTP/HTTPS web server to host static files.",
Command: "",
})
}
func configLogger(verbose bool) (*zap.Logger, func() error) {
zapConfig := zap.NewDevelopmentConfig()
zapConfig.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
zapConfig.Encoding = "console"
if verbose {
zapConfig.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
} else {
zapConfig.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
}
logger, err := zapConfig.Build()
if err == nil {
zap.ReplaceGlobals(logger)
}
if logger == nil {
return nil, func() error { return nil }
}
return logger, logger.Sync
}
func runInteractive(wait func()) error {
return runApplication(wait)
}
func runApplication(wait func()) error {
conf, err := Load()
if err != nil {
return err
}
logger, syncFunc := configLogger(conf.Verbose)
defer syncFunc()
logger.Sugar().Debug(conf)
checkError(createCertificate(conf))
httpServer, err := New(conf)
if err != nil {
return err
}
return httpServer.Serve(wait)
}
func createCertificate(conf *Config) error {
dir, err := os.Getwd()
zap.S().With("certificate", conf.HTTPS.Certificate, "directory", dir, "error", err).Debug("createCertificate")
_, certErr := os.Stat(conf.HTTPS.Certificate.CertificateFilePath)
_, privateKeyErr := os.Stat(conf.HTTPS.Certificate.PrivateKeyFilePath)
if conf.HTTPS.Certificate.ForceOverwrite || (os.IsNotExist(certErr) && os.IsNotExist(privateKeyErr)) {
var parentKP *certtool.KeyPair
if conf.HTTPS.Certificate.RootCertificateFilePath != "" {
rootCertPath := conf.HTTPS.Certificate.RootCertificateFilePath
rootKeyPath := conf.HTTPS.Certificate.RootPrivateKeyFilePath
kp, err := certtool.GenerateAndWriteKeyPair(&certtool.Args{
KeyType: &certtool.KeyType{
Algorithm: "RSA",
KeyLength: 2048,
},
Validity: conf.HTTPS.Certificate.CertificateValidDuration,
CA: true,
},
rootCertPath,
rootKeyPath)
zap.S().With("error", err, "certificateFile", rootCertPath, "privateKeyFile", rootKeyPath).Debug("GenerateAndWriteKeyPair")
if err != nil {
return fmt.Errorf("cannot write public certificate, %w", err)
}
parentKP = kp
}
_, err := certtool.GenerateAndWriteKeyPair(&certtool.Args{
KeyType: &certtool.KeyType{
Algorithm: "RSA",
KeyLength: 2048,
},
Validity: conf.HTTPS.Certificate.CertificateValidDuration,
CA: false,
ParentKeyPair: parentKP,
},
conf.HTTPS.Certificate.CertificateFilePath,
conf.HTTPS.Certificate.PrivateKeyFilePath)
zap.S().With("error", err, "certificateFile", conf.HTTPS.Certificate.CertificateFilePath, "privateKeyFile", conf.HTTPS.Certificate.PrivateKeyFilePath).Debug("GenerateAndWriteKeyPair")
if err != nil {
return fmt.Errorf("cannot write public certificate, %w", err)
}
}
return nil
}