Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/resttls/60 #62

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,18 @@ go get oss.nandlabs.io/golly
- General consumer interface for receiving and processing messages from
different messaging platforms.
- A local provider interface for messaging using channels
- [rest](clients/rest/README.md)
- rest
[server](rest/server/README.md)

- HTTP methods: GET, POST, PUT, DELETE
- Query parameters
- Request headers
- Middleware support
- TLS Configuration
- Transport Layer Configuration

[client](clients/rest/README.md)

- HTTP methods: GET, POST, PUT, DELETE
- Query parameters
- Request headers
Expand All @@ -57,6 +68,7 @@ go get oss.nandlabs.io/golly
- Transport Layer Configuration
- SSL Configuration
- Error handling

- [semver](semver/README.md)
- Adheres to the [SemVer 2.0.0](https://semver.org/spec/v2.0.0.html)
specification
Expand Down
30 changes: 15 additions & 15 deletions rest/server/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Options struct {
}

// Validate validates the server options
func (o Options) Validate() error {
func (o *Options) Validate() error {
if o.Id == "" {
return ErrInvalidID
}
Expand All @@ -36,69 +36,69 @@ func (o Options) Validate() error {
}

// GetListenHost returns the listen host
func (o Options) GetListenHost() string {
func (o *Options) GetListenHost() string {
return o.ListenHost
}

// GetListenPort returns the listen port
func (o Options) GetListenPort() int16 {
func (o *Options) GetListenPort() int16 {
return o.ListenPort
}

// GetEnableTLS returns the enable TLS value
func (o Options) GetEnableTLS() bool {
func (o *Options) GetEnableTLS() bool {
return o.EnableTLS
}

// GetPrivateKeyPath returns the private key path
func (o Options) GetPrivateKeyPath() string {
func (o *Options) GetPrivateKeyPath() string {
return o.PrivateKeyPath
}

// GetCertPath returns the cert path
func (o Options) GetCertPath() string {
func (o *Options) GetCertPath() string {
return o.CertPath
}

// SetListenHost sets the listen host
func (o Options) SetListenHost(host string) Options {
func (o *Options) SetListenHost(host string) *Options {
o.ListenHost = host
return o
}

// SetListenPort sets the listen port
func (o Options) SetListenPort(port int16) Options {
func (o *Options) SetListenPort(port int16) *Options {

o.ListenPort = port
return o
}

// SetEnableTLS sets the enable TLS value
func (o Options) SetEnableTLS(enableTLS bool) Options {
func (o *Options) SetEnableTLS(enableTLS bool) *Options {
o.EnableTLS = enableTLS
return o
}

// SetPrivateKeyPath sets the private key path
func (o Options) SetPrivateKeyPath(privateKeyPath string) Options {
func (o *Options) SetPrivateKeyPath(privateKeyPath string) *Options {
o.PrivateKeyPath = privateKeyPath
return o
}

// SetCertPath sets the cert path
func (o Options) SetCertPath(certPath string) Options {
func (o *Options) SetCertPath(certPath string) *Options {
o.CertPath = certPath
return o
}

// NewOptions returns a new server options
func NewOptions() Options {
return Options{}
func NewOptions() *Options {
return &Options{}
}

// NewOptionsWithDefaults returns a new server options with default values
func NewOptionsWithDefaults() Options {
return Options{
func NewOptionsWithDefaults() *Options {
return &Options{
ListenHost: "localhost",
ListenPort: 8080,
}
Expand Down
2 changes: 1 addition & 1 deletion rest/server/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestOptions_Getters(t *testing.T) {

// TestOptions_Setters tests the setter functions
func TestOptions_Setters(t *testing.T) {
opts := Options{}
opts := &Options{}
opts = opts.SetListenHost("localhost")
if opts.ListenHost != "localhost" {
t.Errorf("SetListenHost() = %v, want %v", opts.ListenHost, "localhost")
Expand Down
25 changes: 23 additions & 2 deletions rest/server/rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,41 @@ func New(opts *Options) (rServer Server, err error) {
SimpleComponent: &lifecycle.SimpleComponent{
CompId: opts.Id,
StartFunc: func() error {

listener, err = net.Listen("tcp", httpServer.Addr)
if err != nil {
logger.ErrorF("Error starting server: %v", err)
}
return err
},
AfterStart: func(err error) {

if err == nil {
logger.Info("Ready to server requests on ", httpServer.Addr)
httpServer.Serve(listener)

if opts.EnableTLS && opts.CertPath != textutils.EmptyStr && opts.PrivateKeyPath != textutils.EmptyStr {
logger.Info("starting to accept https requests on ", httpServer.Addr)
err = httpServer.ServeTLS(listener, opts.CertPath, opts.PrivateKeyPath)
if err != nil {
// if the server was closed intentionally, do not log the error
if err != http.ErrServerClosed {
logger.ErrorF("Error starting https server: %v", err)
}
}
ioutils.CloserFunc(listener)

} else {
logger.Info("starting to accept http requests on ", httpServer.Addr)
err = httpServer.Serve(listener)
if err != nil {
logger.ErrorF("Error starting http server: %v", err)
}
ioutils.CloserFunc(listener)
}
}
},

StopFunc: func() error {
logger.Info("Stopping server at ", httpServer.Addr)
return httpServer.Shutdown(context.Background())
},
},
Expand Down
19 changes: 19 additions & 0 deletions rest/server/rest_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,22 @@ func TestRestServer_Lifecycle(t *testing.T) {
}()
assert.NoError(t, err)
}

// TestRestServer_TLS tests the TLS functions
func TestRestServer_TLS(t *testing.T) {

opts := DefaultOptions().SetEnableTLS(true).
SetCertPath("testdata/server.crt").
SetPrivateKeyPath("testdata/server.key")
server, err := New(opts)
assert.NoError(t, err)
mgr := lifecycle.NewSimpleComponentManager()
mgr.Register(server)
go func() {
time.Sleep(3 * time.Second)
err := mgr.StopAll()
assert.NoError(t, err)
}()
mgr.StartAndWait()

}
15 changes: 15 additions & 0 deletions rest/server/testdata/server.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-----BEGIN CERTIFICATE-----
MIICYTCCAeagAwIBAgIUT2NDXvl1eWM+o6jQqGnR/5eBQ0AwCgYIKoZIzj0EAwIw
ZzELMAkGA1UEBhMCQVUxDDAKBgNVBAgMA05TVzEZMBcGA1UECgwQTmFuZGxhYnMg
UHR5IEx0ZDEOMAwGA1UECwwFR29sbHkxHzAdBgNVBAMMFmdvbGx5LXRlc3QubmFu
ZGxhYnMuaW8wHhcNMjQwOTI2MjMzNTM1WhcNMzQwOTI0MjMzNTM1WjBnMQswCQYD
VQQGEwJBVTEMMAoGA1UECAwDTlNXMRkwFwYDVQQKDBBOYW5kbGFicyBQdHkgTHRk
MQ4wDAYDVQQLDAVHb2xseTEfMB0GA1UEAwwWZ29sbHktdGVzdC5uYW5kbGFicy5p
bzB2MBAGByqGSM49AgEGBSuBBAAiA2IABO/GTCc4y/0gSXeS4wxM6fGwSADmR/og
ddlqWSoTavBiRmxn6ou1G1Ox4uxbippS9E7L0LVvEJo2Ka/lzI88jojPSBpgAVA5
Y3aSVclP2Xba5CeUiNuRw/0psALWSohd1aNTMFEwHQYDVR0OBBYEFPJvnojefXp/
UtBJJiMabSLzbGZGMB8GA1UdIwQYMBaAFPJvnojefXp/UtBJJiMabSLzbGZGMA8G
A1UdEwEB/wQFMAMBAf8wCgYIKoZIzj0EAwIDaQAwZgIxAO9BmpuO7d2CJI2unGGL
gxABBsAM9FFroJ1rxe3Lv8q4vUoX+j5nrun3QBGVA1qMVwIxALxVR5oNiwBAcha8
/VmiPPvom6KpvYqCS/Otjq8B3QvtgXRlYDtNYGT5pEKJwY/WuQ==
-----END CERTIFICATE-----
9 changes: 9 additions & 0 deletions rest/server/testdata/server.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-----BEGIN EC PARAMETERS-----
BgUrgQQAIg==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MIGkAgEBBDDk3PFoX1Flxj2Y4L2DjOkTvUQozXzYFCG6CxN+Vm9PVALDL4W7HtVW
75xf9IDYdsmgBwYFK4EEACKhZANiAATvxkwnOMv9IEl3kuMMTOnxsEgA5kf6IHXZ
alkqE2rwYkZsZ+qLtRtTseLsW4qaUvROy9C1bxCaNimv5cyPPI6Iz0gaYAFQOWN2
klXJT9l22uQnlIjbkcP9KbAC1kqIXdU=
-----END EC PRIVATE KEY-----
Loading