Skip to content

Commit

Permalink
options update for tls config and files; server adjustments for tls
Browse files Browse the repository at this point in the history
  • Loading branch information
redecs committed Apr 18, 2024
1 parent 242bf8a commit 58373c9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
27 changes: 25 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fuego

import (
"crypto/tls"
"fmt"
"html/template"
"io"
Expand Down Expand Up @@ -42,7 +43,7 @@ type Server struct {
// [http.ServeMux.Handle] can also be used to register routes.
Mux *http.ServeMux

// Not stored with the oter middlewares because it is a special case :
// Not stored with the other middlewares because it is a special case :
// it applies on routes that are not registered.
// For example, it allows OPTIONS /foo even if it is not declared (only GET /foo is declared).
corsMiddleware func(http.Handler) http.Handler
Expand All @@ -67,14 +68,17 @@ type Server struct {
template *template.Template // TODO: use preparsed templates

DisallowUnknownFields bool // If true, the server will return an error if the request body contains unknown fields. Useful for quick debugging in development.
DisableOpenapi bool // If true, the the routes within the server will not generate an openapi spec.
DisableOpenapi bool // If true, the routes within the server will not generate an openapi spec.
maxBodySize int64
Serialize func(w http.ResponseWriter, ans any) // Used to serialize the response. Defaults to [SendJSON].
SerializeError func(w http.ResponseWriter, err error) // Used to serialize the error response. Defaults to [SendJSONError].
ErrorHandler func(err error) error // Used to transform any error into a unified error type structure with status code. Defaults to [ErrorHandler]
startTime time.Time

OpenAPIConfig OpenAPIConfig

tlsCertFile string
tlsKeyFile string
}

// NewServer creates a new server with the given options.
Expand Down Expand Up @@ -378,3 +382,22 @@ func (s *Server) RemoveTags(tags ...string) *Server {
}
return s
}

// WithTLSConfig allows setting a custom TLS configuration, and it will make the underling Server run in TLS mode.
func WithTLSConfig(tlsConfig *tls.Config) func(*Server) {
return func(s *Server) {
s.Server.TLSConfig = tlsConfig
}
}

// WithTLS allows setting the certificate and key files, and it will make the underling Server run in TLS mode.
func WithTLS(certFile, keyFile string) func(*Server) {
return func(s *Server) {
s.tlsCertFile = certFile
s.tlsKeyFile = keyFile
}
}

func (s *Server) isTLS() bool {
return s.Server.TLSConfig != nil || (s.tlsCertFile != "" && s.tlsKeyFile != "")
}
22 changes: 22 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fuego

import (
"crypto/tls"
"errors"
"html/template"
"io"
Expand Down Expand Up @@ -365,3 +366,24 @@ func TestServerTags(t *testing.T) {
require.Equal(t, subGroup.tags, []string{"my-server-tag"})
})
}

func TestWithTLSConfig(t *testing.T) {
testTlsConfig := &tls.Config{}
s := NewServer(
WithTLSConfig(testTlsConfig),
)

require.NotNil(t, s.Server.TLSConfig)
require.Equal(t, testTlsConfig, s.Server.TLSConfig)
}

func TestWithTLS(t *testing.T) {
cert := "foo.pem"
key := "bar.key"
s := NewServer(
WithTLS(cert, key),
)

require.Equal(t, s.tlsCertFile, cert)
require.Equal(t, s.tlsKeyFile, key)
}
4 changes: 2 additions & 2 deletions serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s *Server) setupRun(isTLS bool) {
// It returns an error if the server could not start (it could not bind to the port for example).
// It also generates the OpenAPI spec and outputs it to a file, the UI, and a handler (if enabled).
func (s *Server) Run() error {
s.setupRun(false)
s.setupRun(s.isTLS())
return s.Server.ListenAndServe()
}

Expand All @@ -40,7 +40,7 @@ func (s *Server) Run() error {
// It returns an error if the server could not start (it could not bind to the port for example).
// It also generates the OpenAPI spec and outputs it to a file, the UI, and a handler (if enabled).
func (s *Server) RunTLS(certFile, keyFile string) error {
s.setupRun(true)
s.setupRun(s.isTLS())
return s.Server.ListenAndServeTLS(certFile, keyFile)
}

Expand Down

0 comments on commit 58373c9

Please sign in to comment.