-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: rewrite app with chi and sqlc #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ed6bc0d
chore: deprecate old codebase
steveiliop56 c48fac7
feat: add new app
steveiliop56 f496d7c
chore: remove legacy app
steveiliop56 076fb65
feat: add cors support
steveiliop56 2f2e4f3
refactor: performance improvements in sqlite using wal
steveiliop56 26d589d
refactor: update dockerfile
steveiliop56 82c4fd8
refactor: use map in cache service instead of slice
steveiliop56 4a6025f
Merge branch 'main' into refactor/rewrite
steveiliop56 6bc969b
chore: update readme
steveiliop56 984377c
fix: review comments
steveiliop56 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,8 +1,11 @@ | ||
| # data | ||
| data/analytics.db | ||
| data | ||
|
|
||
| # debug | ||
| __debug_* | ||
|
|
||
| # bench | ||
| # build out | ||
| tinyauth-analytics | ||
|
|
||
| # benchmarks | ||
| bench |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,85 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| type cacheField struct { | ||
| value any | ||
| expire int64 | ||
| } | ||
|
|
||
| type Cache struct { | ||
| cache map[string]cacheField | ||
| mutex sync.RWMutex | ||
| } | ||
|
|
||
| func NewCache() *Cache { | ||
| cache := &Cache{ | ||
| cache: make(map[string]cacheField), | ||
| } | ||
| cache.cleanup() | ||
| return cache | ||
| } | ||
|
|
||
| func (c *Cache) Set(key string, value any, ttl int64) { | ||
| c.mutex.Lock() | ||
| defer c.mutex.Unlock() | ||
|
|
||
| expire := time.Now().Add(time.Duration(ttl) * time.Second).Unix() | ||
|
|
||
| c.cache[key] = cacheField{ | ||
| value: value, | ||
| expire: expire, | ||
| } | ||
| } | ||
|
|
||
| func (c *Cache) Get(key string) (any, bool) { | ||
| c.mutex.RLock() | ||
|
|
||
| field, ok := c.cache[key] | ||
|
|
||
| if !ok { | ||
| c.mutex.RUnlock() | ||
| return nil, false | ||
| } | ||
|
|
||
| if time.Now().Unix() > field.expire { | ||
| c.mutex.RUnlock() | ||
| c.Delete(key) | ||
| return nil, false | ||
| } | ||
|
|
||
| c.mutex.RUnlock() | ||
| return field.value, true | ||
| } | ||
|
|
||
| func (c *Cache) Delete(key string) { | ||
| c.mutex.Lock() | ||
| defer c.mutex.Unlock() | ||
| delete(c.cache, key) | ||
| } | ||
|
|
||
| func (c *Cache) Flush() { | ||
| c.mutex.Lock() | ||
| defer c.mutex.Unlock() | ||
| c.cache = make(map[string]cacheField, 0) | ||
| } | ||
|
|
||
| func (c *Cache) cleanup() { | ||
| go func() { | ||
| ticker := time.NewTicker(24 * time.Hour) | ||
| defer ticker.Stop() | ||
|
|
||
| for range ticker.C { | ||
| c.mutex.Lock() | ||
| for key, field := range c.cache { | ||
| if time.Now().Unix() > field.expire { | ||
| delete(c.cache, key) | ||
| } | ||
| } | ||
| c.mutex.Unlock() | ||
| } | ||
| }() | ||
| } | ||
|
steveiliop56 marked this conversation as resolved.
|
||
Empty file.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.