Skip to content

Commit

Permalink
feat(app): add GoosyMock codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Icikowski committed Nov 14, 2022
1 parent 0ea0e31 commit 325863d
Show file tree
Hide file tree
Showing 32 changed files with 2,128 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package config

// Config represents the application's configuration
type Config struct {
Logging LoggingConfig `json:"logging"`
AdminAPIService ServiceConfig `envPrefix:"ADMIN_API_" json:"adminApi"`
ContentService ServiceConfig `envPrefix:"CONTENT_" json:"content"`
HealthProbesPort int `env:"HEALTH_PROBES_PORT" envDefault:"8888" json:"healthProbesPort"`
MaximumPayloadSize int64 `env:"MAX_PAYLOAD_SIZE" envDefault:"64" json:"maxPayloadSize"`
}
7 changes: 7 additions & 0 deletions src/config/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package config

// LoggingConfig represents the configuration of application's logger
type LoggingConfig struct {
Level string `env:"LOG_LEVEL" envDefault:"info" json:"level"`
Pretty bool `env:"PRETTY_LOG" envDefault:"false" json:"pretty"`
}
71 changes: 71 additions & 0 deletions src/config/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package config

import (
"crypto/tls"
"crypto/x509"
"fmt"
"os"
)

// ServiceConfig represents the configuration of application's service
// and its security
type ServiceConfig struct {
Port int `env:"PORT" json:"port"`
SecuredPort int `env:"SECURED_PORT" json:"securedPort"`
SSLEnabled bool `env:"SSL_ENABLED" envDefault:"false" json:"sslEnabled"`
CACertPath string `env:"CA_CERT_PATH" json:"caCertPath"`
TLSCertPath string `env:"TLS_CERT_PATH" json:"tlsCertPath"`
TLSKeyPath string `env:"TLS_KEY_PATH" json:"tlsKeyPath"`

caCertPool *x509.CertPool
tlsCert tls.Certificate
}

// LoadCerts attempts to load CA & TLS certificates defined in configuration
func (c *ServiceConfig) LoadCerts() error {
if !c.SSLEnabled {
return nil
}

ca, err := os.ReadFile(c.CACertPath)
if err != nil {
return err
}

caCerts := x509.NewCertPool()
if !caCerts.AppendCertsFromPEM(ca) {
return fmt.Errorf("failed to append CA certificates to pool")
}

c.caCertPool = caCerts

tlsCert, err := tls.LoadX509KeyPair(c.TLSCertPath, c.TLSKeyPath)
if err != nil {
return err
}

c.tlsCert = tlsCert

return nil
}

// GetTLSConfig returns the *tls.Config based on given configuration
func (c *ServiceConfig) GetTLSConfig() *tls.Config {
if !c.SSLEnabled {
return nil
}

return &tls.Config{
Certificates: []tls.Certificate{c.tlsCert},
RootCAs: c.caCertPool,
MinVersion: tls.VersionTLS12,
CipherSuites: []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
},
}
}
17 changes: 17 additions & 0 deletions src/constants/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package constants

import "net/http"

// Configuration defaults
const (
DefaultCfgAdminAPIPort int = 8081
DefaultCfgAdminAPISecuredPort int = 8444
DefaultCfgContentPort int = 8080
DefaultCfgContentSecuredPort int = 8443
)

// Response defaults
const (
DefaultResponseStatusCode int = http.StatusOK
DefaultResponseContentType string = "text/plain"
)
15 changes: 15 additions & 0 deletions src/constants/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package constants

// HTTP header names
const (
HeaderContentType string = "Content-Type"
HeaderXSentBy string = "X-SentBy"
HeaderAccept string = "Accept"
)

// Content types' names
const (
ContentTypeJSON string = "application/json"
ContentTypeYAML string = "text/yaml"
ContentTypeRaw string = "application/octet-stream"
)
13 changes: 13 additions & 0 deletions src/constants/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package constants

// Application components' names
const (
ComponentInit string = "init"
ComponentCLI string = "cli"
ComponentAdminAPIService string = "admin-api-service"
ComponentContentService string = "content-service"
ComponentHealthProbesService string = "health-probes-service"
ComponentServicesManager string = "services-manager"
ComponentRoutesStore string = "routes-store"
ComponentPayloadsStore string = "payloads-store"
)
59 changes: 59 additions & 0 deletions src/data/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package data

import "io"

// Store is generic definition of key-value storage adapter
type Store[T any] interface {
// GetAll returns all elements from the store
GetAll() map[string]T
// Get returns the element with given ID from the store
Get(string) (T, error)
// Set sets the contents of the store
Set(map[string]T) error
// Insert inserts the element with given ID into the store
Insert(string, T) error
// Update updates the element with given ID in the store
Update(string, T) error
// Upsert inserts or updates the element with given ID
// into/in the store
Upsert(string, T) error
// Delete deletes the element with given ID from the store
Delete(string) error
// DeleteAll deletes all elements from the store
DeleteAll() error
// Count returns the number of elements in the store
Count() int
}

// Subscribable is generic definition of subscribable object
type Subscribable interface {
// Subscribe subscribes to the channel which notifies about
// changes being made to the object with particular
// subscriber's ID
Subscribe(string) <-chan struct{}
// Unsubscribe removes the subscription for notifications
// of particular receiver
Unsubscribe(string)
}

// SubscribableStore is a definition of store that can indicate
// whether the contents of the store has changed
type SubscribableStore[T any] interface {
Store[T]
Subscribable
}

// FilesStore is a definition of store that is capable of storing
// files and defines Close method for cleanup
type FilesStore[T any] interface {
Store[T]
io.Closer
// InsertFile inserts the file into store with random UUID
// as a key
InsertFile(string, io.ReadCloser) (string, error)
// UpdateFile updates the file item with given ID in the store
UpdateFile(string, string, io.ReadCloser) error
// UpsertFile inserts or updates the file item with given ID
// into/in the store
UpsertFile(string, string, io.ReadCloser) error
}
Loading

0 comments on commit 325863d

Please sign in to comment.