-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: code refactor, cache impl, logger fix (#4)
- Loading branch information
Showing
19 changed files
with
308 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
apps: | ||
log_level: "DEBUG" | ||
rest: | ||
port: 8080 | ||
validation: | ||
jwt_header_name: "<YOUR_JWT_HEADER_NAME>" | ||
jwt_validation_url: "<YOUR_JWT_VALIDATION_URL>" | ||
board_validation_url: "<YOUR_BOARD_VALIDATION_URL>" | ||
|
||
logging: | ||
level: "DEBUG" | ||
write_to_file: false | ||
|
||
storage: | ||
users: | ||
type: "in-memory" | ||
rooms: | ||
type: "in-memory" | ||
|
||
cache: | ||
type: "in-memory" | ||
ttl: 300 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
module github.com/Icerzack/excalidraw-ws-go | ||
module github.com/Icerzack/excaliroom | ||
|
||
go 1.21 | ||
|
||
require ( | ||
github.com/go-chi/chi v1.5.5 | ||
github.com/go-chi/chi/v5 v5.0.12 | ||
github.com/gorilla/websocket v1.5.1 | ||
go.uber.org/zap v1.27.0 | ||
gopkg.in/yaml.v3 v3.0.1 | ||
) | ||
|
||
require ( | ||
go.uber.org/multierr v1.11.0 // indirect | ||
golang.org/x/net v0.17.0 // indirect | ||
golang.org/x/net v0.25.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package cache | ||
|
||
type Cache interface { | ||
Set(key string, value interface{}) error | ||
Get(key string) (interface{}, error) | ||
SetWithTTL(key string, value interface{}, ttl int64) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package inmemory | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
type Item struct { | ||
Value interface{} | ||
Expiration int64 | ||
} | ||
|
||
type Cache struct { | ||
mu sync.RWMutex | ||
items map[string]*Item | ||
logger *zap.Logger | ||
} | ||
|
||
func NewCache(logger *zap.Logger) *Cache { | ||
return &Cache{ | ||
items: make(map[string]*Item), | ||
logger: logger, | ||
} | ||
} | ||
|
||
func (c *Cache) Set(key string, value interface{}) error { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
c.items[key] = &Item{ | ||
Value: value, | ||
} | ||
c.logger.Debug("User added to cache", zap.String("key", key)) | ||
return nil | ||
} | ||
|
||
func (c *Cache) SetWithTTL(key string, value interface{}, ttl int64) error { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
c.items[key] = &Item{ | ||
Value: value, | ||
Expiration: time.Now().UnixNano() + ttl*int64(time.Second), | ||
} | ||
c.logger.Debug("User added to cache", zap.String("key", key)) | ||
return nil | ||
} | ||
|
||
func (c *Cache) Get(key string) (interface{}, error) { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
|
||
item, ok := c.items[key] | ||
if !ok || item.Expiration < time.Now().UnixNano() { | ||
c.logger.Debug("User not found in cache", zap.String("key", key)) | ||
return nil, nil | ||
} | ||
|
||
return item.Value, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package user | ||
package models | ||
|
||
import "github.com/gorilla/websocket" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.