Skip to content

Commit

Permalink
Feat/63/cors (#66)
Browse files Browse the repository at this point in the history
 [#63] Added Cors support
  • Loading branch information
nandagopalan authored Dec 14, 2024
1 parent 8982bcc commit ff399d4
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 27 deletions.
13 changes: 6 additions & 7 deletions assertion/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package assertion

import (
"reflect"
"testing"
)

// Equal compares the expected and actual values and logs an error if they are not equal
Expand Down Expand Up @@ -52,19 +51,19 @@ func HasValue(m map[string]any, value any) bool {
}

// ListHas logs an error if the list does not contain the value
func ListHas(value any, list ...any) bool {
for _, v := range list {
if reflect.DeepEqual(v, value) {
func ListHas[S ~[]E, E any](val any, list S) bool {
for _, e := range list {
if reflect.DeepEqual(val, e) {
return true
}
}
return false
}

// ListMissing logs an error if the list contains the value
func ListMissing(t *testing.T, value any, list ...any) bool {
for _, v := range list {
if reflect.DeepEqual(v, value) {
func ListMissing[S ~[]E, E any](val any, list S) bool {
for _, e := range list {
if reflect.DeepEqual(val, e) {
return false
}
}
Expand Down
8 changes: 4 additions & 4 deletions assertion/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ func TestListHas(t *testing.T) {
// Test case 1
value := 1
list := []interface{}{1, 2, 3}
if !ListHas(value, list...) {
if !ListHas(value, list) {
t.Errorf("ListHas() = false, want true")
}

// Test case 2
value = 4
list = []interface{}{1, 2, 3}
if ListHas(value, list...) {
if ListHas(value, list) {
t.Errorf("ListHas() = true, want false")
}
}
Expand All @@ -154,14 +154,14 @@ func TestListMissing(t *testing.T) {
// Test case 1
value := 1
list := []interface{}{1, 2, 3}
if ListMissing(t, value, list...) {
if ListMissing(value, list) {
t.Errorf("ListMissing() = true, want false")
}

// Test case 2
value = 4
list = []interface{}{1, 2, 3}
if !ListMissing(t, value, list...) {
if !ListMissing(value, list) {
t.Errorf("ListMissing() = false, want true")
}
}
Expand Down
44 changes: 35 additions & 9 deletions rest/server/opts.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package server

import (
"net/http"

"oss.nandlabs.io/golly/turbo/filters"
)

// Options is the configuration for the server
type Options struct {
Id string `json:"id" yaml:"id" bson:"id" mapstructure:"id"`
PathPrefix string `json:"path_prefix,omitempty" yaml:"path_prefix,omitempty" bson:"path_prefix,omitempty" mapstructure:"path_prefix,omitempty"`
ListenHost string `json:"listen_host" yaml:"listen_host" bson:"listen_host" mapstructure:"listen_host"`
ListenPort int16 `json:"listen_port" yaml:"listen_port" bson:"listen_port" mapstructure:"listen_port"`
ReadTimeout int64 `json:"read_timeout,omitempty" yaml:"read_timeout,omitempty" bson:"read_timeout,omitempty" mapstructure:"read_timeout,omitempty"`
WriteTimeout int64 `json:"write_timeout,omitempty" yaml:"write_timeout,omitempty" bson:"write_timeout,omitempty" mapstructure:"write_timeout,omitempty"`
EnableTLS bool `json:"enable_tls" yaml:"enable_tls" bson:"enable_tls" mapstructure:"enable_tls"`
PrivateKeyPath string `json:"private_key_path,omitempty" yaml:"private_key_path,omitempty" bson:"private_key_path,omitempty" mapstructure:"private_key,omitempty"`
CertPath string `json:"cert_path,omitempty" yaml:"cert_path,omitempty" bson:"cert_path,omitempty" mapstructure:"cert,omitempty"`
Id string `json:"id" yaml:"id" bson:"id" mapstructure:"id"`
PathPrefix string `json:"path_prefix,omitempty" yaml:"path_prefix,omitempty" bson:"path_prefix,omitempty" mapstructure:"path_prefix,omitempty"`
ListenHost string `json:"listen_host" yaml:"listen_host" bson:"listen_host" mapstructure:"listen_host"`
ListenPort int16 `json:"listen_port" yaml:"listen_port" bson:"listen_port" mapstructure:"listen_port"`
ReadTimeout int64 `json:"read_timeout,omitempty" yaml:"read_timeout,omitempty" bson:"read_timeout,omitempty" mapstructure:"read_timeout,omitempty"`
WriteTimeout int64 `json:"write_timeout,omitempty" yaml:"write_timeout,omitempty" bson:"write_timeout,omitempty" mapstructure:"write_timeout,omitempty"`
EnableTLS bool `json:"enable_tls" yaml:"enable_tls" bson:"enable_tls" mapstructure:"enable_tls"`
PrivateKeyPath string `json:"private_key_path,omitempty" yaml:"private_key_path,omitempty" bson:"private_key_path,omitempty" mapstructure:"private_key,omitempty"`
CertPath string `json:"cert_path,omitempty" yaml:"cert_path,omitempty" bson:"cert_path,omitempty" mapstructure:"cert,omitempty"`
Cors *filters.CorsOptions `json:"cors,omitempty" yaml:"cors,omitempty" bson:"cors,omitempty" mapstructure:"cors,omitempty"`
}

// Validate validates the server options
Expand Down Expand Up @@ -105,6 +112,19 @@ func NewOptionsWithDefaults() *Options {
}

// DefaultOptions returns the default options for the server
// The default options are:
// - PathPrefix: "/"
// - Id: "default-http-server"
// - ListenHost: "localhost"
// - ListenPort: 8080
// - ReadTimeout: 20000
// - WriteTimeout: 20000
// - Cors: &filters.CorsOptions{
// MaxAge: 0,
// AllowedOrigins: []string{"*"},
// AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
// ResponseStatus: http.StatusNoContent,
// }
func DefaultOptions() *Options {
return &Options{
PathPrefix: "/",
Expand All @@ -113,5 +133,11 @@ func DefaultOptions() *Options {
ListenPort: 8080,
ReadTimeout: 20000,
WriteTimeout: 20000,
Cors: &filters.CorsOptions{
MaxAge: filters.DefaultAccessControlMaxAge,
AllowedOrigins: []string{filters.AccessControlAllowAllOrigins},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
ResponseStatus: http.StatusNoContent,
},
}
}
2 changes: 2 additions & 0 deletions rest/server/rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ func New(opts *Options) (rServer Server, err error) {
return
}
router := turbo.NewRouter()
router.AddCorsFilter(opts.Cors)

httpServer := &http.Server{
Handler: router,
Addr: opts.ListenHost + ":" + strconv.Itoa(int(opts.ListenPort)),
Expand Down
8 changes: 4 additions & 4 deletions testing/assert/asserts.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@ func HasValue(t *testing.T, m map[string]any, value any) bool {
}

// ListHas logs an error if the list does not contain the value
func ListHas(t *testing.T, value any, list ...any) bool {
val := assertion.ListHas(value, list...)
func ListHas[S ~[]E, E any](t *testing.T, value any, list S) bool {
val := assertion.ListHas(value, list)
if !val {
t.Errorf("Expected: %v to be in %v", value, list)
}
return val
}

// ListMissing logs an error if the list contains the value
func ListMissing(t *testing.T, value any, list ...any) bool {
val := assertion.ListMissing(t, value, list...)
func ListMissing[S ~[]E, E any](t *testing.T, value any, list S) bool {
val := assertion.ListMissing(value, list)
if !val {
t.Errorf("Expected: %v not to be in %v", value, list)
}
Expand Down
4 changes: 2 additions & 2 deletions testing/assert/asserts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ func TestHasValue(t *testing.T) {
func TestListHas(t *testing.T) {
// Test when the list contains the value
list := []interface{}{"value1", "value2", "value3"}
if !ListHas(t, "value2", list...) {
if !ListHas(t, "value2", list) {
t.Errorf("ListHas failed: expected value to be present in the list, but it is missing")
}
}

func TestListMissing(t *testing.T) {
// Test when the list does not contain the value
list := []interface{}{"value1", "value2", "value3"}
if !ListMissing(t, "value4", list...) {
if !ListMissing(t, "value4", list) {
t.Errorf("ListMissing failed: expected value to be missing from the list, but it is present")
}
}
Expand Down
Loading

0 comments on commit ff399d4

Please sign in to comment.