Skip to content

Commit

Permalink
Merge pull request #224 from damongolding/task/release
Browse files Browse the repository at this point in the history
0.14.4
  • Loading branch information
damongolding authored Dec 11, 2024
2 parents 3da2ca3 + 9fe76a7 commit 203d4b4
Show file tree
Hide file tree
Showing 11 changed files with 655 additions and 440 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/fogleman/gg v1.3.0
github.com/google/go-querystring v1.1.0
github.com/google/uuid v1.6.0
github.com/labstack/echo/v4 v4.13.0
github.com/labstack/echo/v4 v4.13.1
github.com/mcuadros/go-defaults v1.2.0
github.com/oapi-codegen/runtime v1.1.1
github.com/patrickmn/go-cache v2.1.0+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/labstack/echo/v4 v4.13.0 h1:8DjSi4H/k+RqoOmwXkxW14A2H1pdPdS95+qmdJ4q1Tg=
github.com/labstack/echo/v4 v4.13.0/go.mod h1:61j7WN2+bp8V21qerqRs4yVlVTGyOagMBpF0vE7VcmM=
github.com/labstack/echo/v4 v4.13.1 h1:q3+CpQlYhJwpRr9+08pX0IdlabTIpnIhrg2AKPSKhFE=
github.com/labstack/echo/v4 v4.13.1/go.mod h1:61j7WN2+bp8V21qerqRs4yVlVTGyOagMBpF0vE7VcmM=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
Expand Down
88 changes: 64 additions & 24 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,70 @@
package common

import (
"context"
"fmt"
"net/url"
"os"
"os/signal"
"sync"
"syscall"

"github.com/charmbracelet/log"
"github.com/damongolding/immich-kiosk/internal/config"
"github.com/damongolding/immich-kiosk/internal/immich"
"github.com/damongolding/immich-kiosk/internal/utils"
)

// SharedSecret stores the application-wide shared secret string
var SharedSecret string
var (
initOnce sync.Once

// SharedSecretInit ensures SharedSecret is initialized only once
var SharedSecretInit sync.Once
// shared context
Context context.Context
cancel context.CancelFunc

// SharedSecret stores the application-wide shared secret string
SharedSecret string
)

// Initialize sets up the application context and shared secret.
// It ensures initialization occurs only once using sync.Once.
// Returns any errors that occurred during initialization.
func Initialize() error {
var err error

initOnce.Do(func() {
err = initialize()
})

return err
}

// initialize performs the actual initialization work:
// - Creates cancellable context
// - Initializes shared secret
// - Sets up graceful shutdown handling
// Returns any errors that occurred during initialization.
func initialize() error {
Context, cancel = context.WithCancel(context.Background())

if err := InitializeSecret(); err != nil {
log.Fatal("failed to initialize shared secret", "error", err)
}

// Handle graceful shutdown on interrupt signals
go func() {
sigChan := make(chan os.Signal, 2)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
select {
case <-sigChan:
cancel()
case <-Context.Done():
}
signal.Stop(sigChan)
}()

return nil
}

// RouteRequestData contains request metadata and configuration used across routes
type RouteRequestData struct {
Expand All @@ -26,29 +75,20 @@ type RouteRequestData struct {
ClientName string // Name of the client making the request
}

// InitializeSecret generates and sets the shared secret for the application.
// It uses sync.Once to ensure the secret is only generated once.
// Returns an error if secret generation fails.
// InitializeSecret generates and sets the shared secret used for application security.
// The shared secret is used for authenticating and validating requests between components.
// Generation occurs only once through sync.Once synchronization to prevent duplicate secrets.
// The generated secret is stored in the SharedSecret global variable.
// Returns an error if the secret generation process fails.
func InitializeSecret() error {
var initErr error

SharedSecretInit.Do(func() {
secret, err := utils.GenerateSharedSecret()
if err != nil {
initErr = fmt.Errorf("failed to generate shared secret: %w", err)
return
}
SharedSecret = secret
})

return initErr
}

// init initializes the package by generating the shared secret
func init() {
if err := InitializeSecret(); err != nil {
log.Fatal("initialising secret", "error", err)
secret, err := utils.GenerateSharedSecret()
if err != nil {
return fmt.Errorf("failed to generate shared secret: %w", err)
}
SharedSecret = secret

return nil
}

// ViewImageData contains the image data and metadata for displaying an image in the view
Expand Down
Loading

0 comments on commit 203d4b4

Please sign in to comment.