26 Commits

Author SHA1 Message Date
ede282071b fix: change references github.com -> git.nas-technologies.com 2026-03-15 18:25:08 +01:00
581d1c5173 feat(session): add session management utility 2026-03-10 14:48:13 +01:00
d2f2558fdf chore(SimpleRouter)!: promoted from unstable 2026-03-09 20:31:43 +01:00
5acd5e01eb chore: update licensing 2026-03-09 15:18:56 +01:00
Levente Nas
a25edc65b7 feat: add optional http server config params 2025-09-17 07:25:50 +02:00
3d413a8605 fix(SimpleRouter): global middlewares 2025-01-22 01:03:31 +01:00
46a47e7cd5 feat(Logger): add WriteErrMsgRequest and WriteErrMsgRequestDebug functions 2025-01-20 21:48:47 +01:00
cb32017224 feat(Type): add new function to be able to recover from panics when unwrapping a none/error value 2025-01-20 14:18:43 +01:00
286ae7f28b fix(Logger): fix slog based logger's debug methods 2025-01-18 19:14:35 +01:00
3f7ef2e0c2 feat(Logger): add slog based logger 2025-01-12 12:52:07 +01:00
b820a75f5e fix(Logger): parameter naming 2025-01-12 12:51:35 +01:00
4e4c43930a fix(Type): better handling for driver.Valuer interface 2025-01-05 22:57:35 +01:00
5b240e0457 fix(Type): only require pointers where nil is meaningful other than panicking on it 2025-01-05 22:27:49 +01:00
4855e19fb3 fix(Type): make sure when checking if an inner type is Scanner we use pointers 2025-01-05 22:23:51 +01:00
7014f8d0ff feat(Type): implement Value,MarshalJSON,UnmarshalJSON for Result 2025-01-05 13:28:19 +01:00
cb7ea4cf3f chore(Type): consistent receiver naming 2025-01-05 13:18:48 +01:00
4ac0a09b85 fix(Type): promote case driver.Valuer to the top in switch statement for Optional Value 2025-01-04 02:41:56 +01:00
7b2c247538 feat(Type): add driver.Valuer implementation to Optional type 2025-01-04 02:37:20 +01:00
80fa0d5279 feat(Type): Scan method fixes for Result 2025-01-04 01:55:26 +01:00
1fb6970d5a fix(Type): fix Scan for Optional, time parsing now can return the error correctly 2025-01-04 01:32:46 +01:00
e23765a571 feat(Type): implement MarshalJSON and UnmarshalJSON for Optional 2025-01-03 23:30:38 +01:00
10983035db fix(Assert): swap reflect.Ptr to the new reflect.Pointer 2025-01-03 14:00:29 +01:00
29270855f8 test(Type): add more tests for Scan function on Optional 2025-01-01 22:37:59 +01:00
864953076a fix(Type): Scan method fixes 2025-01-01 22:24:59 +01:00
b5fc0df93b feat(Type): add time.Time as a possible type for Scan 2024-12-31 17:58:13 +01:00
1ab0570883 feat(Type): add Scan implementation for Optional and Result types for use with databases 2024-12-31 17:36:00 +01:00
21 changed files with 1000 additions and 244 deletions

202
LICENSE
View File

@@ -1,201 +1,17 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Non-AI Software License (MIT-Inspired)
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
Copyright (c) 2026, Levente Adam Batuska
1. Definitions.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
In addition, the following restrictions apply:
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
1. The Software and any modifications made to it may not be used for the purpose of training or improving machine learning algorithms, including but not limited to artificial intelligence, natural language processing, or data mining. This condition applies to any derivatives, modifications, or updates based on the Software code. Any usage of the Software in an AI-training dataset is considered a breach of this License.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
2. The Software may not be included in any dataset used for training or improving machine learning algorithms, including but not limited to artificial intelligence, natural language processing, or data mining.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
3. Any person or organization found to be in violation of these restrictions will be subject to legal action and may be held liable for any damages resulting from such use.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -26,7 +26,7 @@ func file_func_line() (string, string, int) {
func IsNillable(kind reflect.Kind) bool {
switch kind {
// based on reflect/type.go -> Kind
case reflect.Ptr, reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Slice, reflect.UnsafePointer:
case reflect.Pointer, reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Slice, reflect.UnsafePointer:
return true
default:
return false

View File

@@ -3,7 +3,7 @@ package Assert
import (
"testing"
Testing "github.com/lbatuska/goutils/testing"
Testing "git.nas-technologies.com/lbatuska/goutils/testing"
)
func Test_notNil(t *testing.T) {

2
go.mod
View File

@@ -1,3 +1,3 @@
module github.com/lbatuska/goutils
module git.nas-technologies.com/lbatuska/goutils
go 1.23.0

View File

@@ -48,6 +48,14 @@ func (logger *ConsoleLoggerImpl) WriteErrRequest(err error, uuid string) (errnum
return errnum
}
func (logger *ConsoleLoggerImpl) WriteErrMsgRequest(err error, message string, uuid string) (errnum int) {
if err != nil {
logger.Write(uuid + " " + message + ": Error: " + err.Error())
errnum = 1
}
return errnum
}
func (logger *ConsoleLoggerImpl) WriteDebug(message string) {
if DEBUG {
logger.Write(message)

View File

@@ -76,8 +76,8 @@ func (logger *FileLoggerImpl) Write(message string) {
logger.messages <- time.Now().Format(time.UnixDate) + " : " + message + "\n"
}
func (logger *FileLoggerImpl) WriteRequest(message string, request string) {
logger.Write(request + " : " + message)
func (logger *FileLoggerImpl) WriteRequest(message string, uuid string) {
logger.Write(uuid + " : " + message)
}
func (logger *FileLoggerImpl) WriteErr(err error) (errnum int) {
@@ -96,6 +96,14 @@ func (logger *FileLoggerImpl) WriteErrRequest(err error, uuid string) (errnum in
return errnum
}
func (logger *FileLoggerImpl) WriteErrMsgRequest(err error, message string, uuid string) (errnum int) {
if err != nil {
logger.Write(uuid + " " + message + ": Error: " + err.Error())
errnum = 1
}
return errnum
}
func (logger *FileLoggerImpl) WriteDebug(message string) {
if DEBUG {
logger.Write(message)
@@ -123,3 +131,23 @@ func (logger *FileLoggerImpl) WriteErrRequestDebug(err error, uuid string) (errn
}
return errnum
}
func (logger *FileLoggerImpl) WriteErrMsgRequestDebug(err error, message string, uuid string) (errnum int) {
if err != nil {
if DEBUG {
logger.WriteErrMsgRequest(err, message, uuid)
}
errnum = 1
}
return errnum
}
func (logger *ConsoleLoggerImpl) WriteErrMsgRequestDebug(err error, message string, uuid string) (errnum int) {
if err != nil {
if DEBUG {
logger.WriteErrMsgRequest(err, message, uuid)
}
errnum = 1
}
return errnum
}

View File

@@ -26,6 +26,8 @@ type (
// If an error that is not nill passed in it logs the error and returns 1, otherwise 0
WriteErr(error) int
WriteErrRequest(err error, uuid string) int
WriteErrMsgRequest(err error, message string, uuid string) int
}
// Use _DEBUG prints to strip them out of release builds
DebugLogger interface {
@@ -38,6 +40,8 @@ type (
WriteRequestDebug(message string, uuid string)
WriteErrDebug(err error) (errnum int)
WriteErrRequestDebug(err error, uuid string) int
WriteErrMsgRequestDebug(err error, message string, uuid string) int
}
)
@@ -46,4 +50,5 @@ var (
_ Logger = (*NullLoggerImpl)(nil)
_ Logger = (*ConsoleLoggerImpl)(nil)
_ Logger = (*FileLoggerImpl)(nil)
_ Logger = (*SlogLoggerImpl)(nil)
)

View File

@@ -8,7 +8,7 @@ func (logger *NullLoggerImpl) StopLogger() {}
func (logger *NullLoggerImpl) Write(message string) {}
func (logger *NullLoggerImpl) WriteRequest(message string, request string) {}
func (logger *NullLoggerImpl) WriteRequest(message string, uuid string) {}
func (logger *NullLoggerImpl) WriteErr(err error) (errnum int) {
if err != nil {
@@ -17,7 +17,14 @@ func (logger *NullLoggerImpl) WriteErr(err error) (errnum int) {
return errnum
}
func (logger *NullLoggerImpl) WriteErrRequest(err error, request string) (errnum int) {
func (logger *NullLoggerImpl) WriteErrRequest(err error, uuid string) (errnum int) {
if err != nil {
errnum = 1
}
return errnum
}
func (logger *NullLoggerImpl) WriteErrMsgRequest(err error, message string, uuid string) (errnum int) {
if err != nil {
errnum = 1
}
@@ -41,3 +48,10 @@ func (logger *NullLoggerImpl) WriteErrRequestDebug(err error, uuid string) (errn
}
return errnum
}
func (logger *NullLoggerImpl) WriteErrMsgRequestDebug(err error, message string, uuid string) (errnum int) {
if err != nil {
errnum = 1
}
return errnum
}

83
logger/sloglogger.go Normal file
View File

@@ -0,0 +1,83 @@
package Logger
import "log/slog"
func (lgr *SlogLoggerImpl) init() {}
func (logger *SlogLoggerImpl) StartLogger() {}
func (logger *SlogLoggerImpl) StopLogger() {}
func (logger *SlogLoggerImpl) Write(message string) {
slog.Info(message)
}
func (logger *SlogLoggerImpl) WriteRequest(message string, uuid string) {
slog.Info(message, "UUID", uuid)
}
func (logger *SlogLoggerImpl) WriteErr(err error) (errnum int) {
if err != nil {
slog.Error(err.Error())
errnum = 1
}
return errnum
}
func (logger *SlogLoggerImpl) WriteErrRequest(err error, uuid string) (errnum int) {
if err != nil {
slog.Error(err.Error(), "UUID", uuid)
errnum = 1
}
return errnum
}
func (logger *SlogLoggerImpl) WriteErrMsgRequest(err error, message string, uuid string) (errnum int) {
if err != nil {
slog.Error(message+err.Error(), "UUID", uuid)
errnum = 1
}
return errnum
}
func (logger *SlogLoggerImpl) WriteDebug(message string) {
if DEBUG {
logger.Write(message)
}
}
func (logger *SlogLoggerImpl) WriteRequestDebug(message string, uuid string) {
if DEBUG {
logger.WriteRequest(message, uuid)
}
}
func (logger *SlogLoggerImpl) WriteErrDebug(err error) (errnum int) {
if err != nil {
if DEBUG {
logger.WriteErr(err)
}
errnum = 1
}
return errnum
}
func (logger *SlogLoggerImpl) WriteErrRequestDebug(err error, uuid string) (errnum int) {
if err != nil {
if DEBUG {
logger.WriteErrRequest(err, uuid)
}
errnum = 1
}
return errnum
}
func (logger *SlogLoggerImpl) WriteErrMsgRequestDebug(err error, message string, uuid string) (errnum int) {
if err != nil {
if DEBUG {
logger.WriteErrMsgRequest(err, message, uuid)
}
errnum = 1
}
return errnum
}

View File

@@ -20,3 +20,5 @@ type FileLoggerImpl struct {
filepath string
initfilepath string
}
type SlogLoggerImpl struct{}

View File

@@ -8,12 +8,26 @@ import (
"os/signal"
"syscall"
"time"
Type "git.nas-technologies.com/lbatuska/goutils/type"
)
func (rg *RouteGroup) StartServer(addr string) *http.Server {
server := &http.Server{
Addr: addr,
Handler: rg,
func (rg *RouteGroup) StartServer(addr string, config Type.Optional[ServerConfig]) *http.Server {
var server *http.Server
if config.HasValue() {
serverConfig := config.Unwrap()
server = &http.Server{
Addr: addr,
Handler: rg,
ReadTimeout: serverConfig.ReadTimeout,
ReadHeaderTimeout: serverConfig.ReadHeaderTimeout,
WriteTimeout: serverConfig.WriteTimeout,
}
} else {
server = &http.Server{
Addr: addr,
Handler: rg,
}
}
go func() {
@@ -25,10 +39,22 @@ func (rg *RouteGroup) StartServer(addr string) *http.Server {
return server
}
func (rg *RouteGroup) StartWithGracefulShutdown(addr string) {
server := &http.Server{
Addr: addr,
Handler: rg, // This will use your RouteGroup (with ServeMux) as the handler
func (rg *RouteGroup) StartWithGracefulShutdown(addr string, config Type.Optional[ServerConfig]) {
var server *http.Server
if config.HasValue() {
serverConfig := config.Unwrap()
server = &http.Server{
Addr: addr,
Handler: rg,
ReadTimeout: serverConfig.ReadTimeout,
ReadHeaderTimeout: serverConfig.ReadHeaderTimeout,
WriteTimeout: serverConfig.WriteTimeout,
}
} else {
server = &http.Server{
Addr: addr,
Handler: rg,
}
}
// Start the server in a separate goroutine
@@ -66,9 +92,9 @@ func (rg *RouteGroup) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rg.mux.ServeHTTP(w, r)
})
mwlen := len(rg.global_middlewares) - 1
for i := range rg.global_middlewares {
handler = rg.global_middlewares[mwlen-i](handler)
mwlen := len(*rg.global_middlewares) - 1
for i := range *rg.global_middlewares {
handler = (*rg.global_middlewares)[mwlen-i](handler)
}
handler.ServeHTTP(w, r)
@@ -81,7 +107,7 @@ func (rg *RouteGroup) Handler(r *http.Request) (h http.Handler, pattern string)
// Creates a new SimpleRouter with the basePath of "/"
func SimpleRouter() *RouteGroup {
return &RouteGroup{mux: http.NewServeMux(), basePath: "/"}
return &RouteGroup{mux: http.NewServeMux(), basePath: "/", global_middlewares: &[]Middleware{}}
}
// Puts middlewares on top of existing ones in order
@@ -105,20 +131,21 @@ func (rg *RouteGroup) PopMiddleware() Middleware {
return lastMiddleware
}
// this vs PushMiddleware ? what's the diff?
func (rg *RouteGroup) PushGlobalMiddleware(first Middleware, others ...Middleware) *RouteGroup {
rg.global_middlewares = append(rg.global_middlewares, first)
rg.global_middlewares = append(rg.global_middlewares, others...)
*rg.global_middlewares = append(*rg.global_middlewares, first)
*rg.global_middlewares = append(*rg.global_middlewares, others...)
return rg
}
func (rg *RouteGroup) PopGlobalMiddleware() Middleware {
if len(rg.global_middlewares) == 0 {
if len(*rg.global_middlewares) == 0 {
return nil
}
lastIndex := len(rg.global_middlewares) - 1
lastMiddleware := rg.global_middlewares[lastIndex]
rg.global_middlewares = rg.global_middlewares[:lastIndex]
lastIndex := len(*rg.global_middlewares) - 1
lastMiddleware := (*rg.global_middlewares)[lastIndex]
*rg.global_middlewares = (*rg.global_middlewares)[:lastIndex]
return lastMiddleware
}
@@ -146,8 +173,9 @@ func (rg *RouteGroup) registerRoute(method string, path string, handler http.Han
func (rg *RouteGroup) SubPath(path string) *RouteGroup {
rgc := &RouteGroup{
mux: rg.mux,
basePath: rg.basePath + path + "/",
mux: rg.mux,
basePath: rg.basePath + path + "/",
global_middlewares: rg.global_middlewares,
}
middlewares := make([]Middleware, len(rg.middlewares))
copy(middlewares, rg.middlewares)

View File

@@ -8,7 +8,8 @@ import (
"testing"
"time"
Testing "github.com/lbatuska/goutils/testing"
Testing "git.nas-technologies.com/lbatuska/goutils/testing"
Type "git.nas-technologies.com/lbatuska/goutils/type"
)
func setupServer(wg *sync.WaitGroup) {
@@ -23,7 +24,7 @@ func setupServer(wg *sync.WaitGroup) {
w.WriteHeader(200)
}))
simpleRouter.StartWithGracefulShutdown("0.0.0.0:8080")
simpleRouter.StartWithGracefulShutdown("0.0.0.0:8080", Type.None[ServerConfig]())
}
func sendKillSignal(wg *sync.WaitGroup) {
@@ -192,12 +193,24 @@ func Test_globalMiddlewares(t *testing.T) {
})
}
simpleRouter := SimpleRouter()
simpleRouter2 := simpleRouter.SubPath("2")
simpleRouter.PushGlobalMiddleware(gmw1)
simpleRouter.PushGlobalMiddleware(gmw2)
simpleRouter.PushMiddleware(mw1, mw2)
simpleRouter.GET("test", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
simpleRouter.GET("test2", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
// Subpath
simpleRouter2.GET("test", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
_ts := httptest.NewServer(simpleRouter2)
_req, _ := http.NewRequest("GET", _ts.URL+"/2/test", nil)
_, _err := http.DefaultClient.Do(_req)
Testing.AssertNotError(t, _err)
Testing.AssertEqual(t, 12, counter)
counter = 0
// Subpath
ts := httptest.NewServer(simpleRouter)
req, _ := http.NewRequest("GET", ts.URL+"/test", nil)
_, err := http.DefaultClient.Do(req)

View File

@@ -1,6 +1,9 @@
package SimpleRouter
import "net/http"
import (
"net/http"
"time"
)
type Middleware func(http.Handler) http.Handler
@@ -8,5 +11,11 @@ type RouteGroup struct {
mux *http.ServeMux // A ponter to the underlying ServeMux, this allows us to call ListenAndServe on any instance
basePath string // The current path we are defining handlers on / appending to
middlewares []Middleware // Stack of middlewares that will be applied on a handler in order
global_middlewares []Middleware
global_middlewares *[]Middleware
}
type ServerConfig struct {
ReadTimeout time.Duration
ReadHeaderTimeout time.Duration
WriteTimeout time.Duration
}

View File

@@ -1,10 +1,20 @@
package Type
import (
"database/sql"
"database/sql/driver"
"encoding/json"
)
// Created to abstract over Is_some and Is_ok
type ValueContainer interface {
HasValue() bool
}
type Recoverable[T any] interface {
CatchUnwrap(T)
}
type Unwrappable[T any] interface {
Expect(string) T // panics with a provided custom message
Unwrap() T // panics with a generic message
@@ -24,7 +34,8 @@ type Optionaler[T any] interface {
IsNone() bool
OkOr(error) Result[T]
OkOrElse(func() error) Result[T]
Unwrappable[T]
Optioner[T]
OptionalerMarker
}
type Resulter[T any] interface {
@@ -32,15 +43,36 @@ type Resulter[T any] interface {
IsErr() bool
Ok() Optional[T]
Err() Optional[error]
Unwrappable[T]
Optioner[T]
ResulterMarker
}
// Marker interfaces to help type matching
type (
ResulterMarker interface {
Result()
}
OptionalerMarker interface {
Optional()
}
)
// Ensure compile time the interfaces are implemented
var (
_ Optioner[any] = (*Optional[any])(nil)
_ Optioner[any] = (*Result[any])(nil)
_ Optionaler[any] = (*Optional[any])(nil)
_ Resulter[any] = (*Result[any])(nil)
_ ValueContainer = (*Optional[any])(nil)
_ ValueContainer = (*Result[any])(nil)
_ OptionalerMarker = (*Optional[any])(nil)
_ ResulterMarker = (*Result[any])(nil)
_ Optioner[any] = (*Optional[any])(nil)
_ Optioner[any] = (*Result[any])(nil)
_ Optionaler[any] = (*Optional[any])(nil)
_ Resulter[any] = (*Result[any])(nil)
_ ValueContainer = (*Optional[any])(nil)
_ ValueContainer = (*Result[any])(nil)
_ sql.Scanner = (*Optional[any])(nil)
_ sql.Scanner = (*Result[any])(nil)
_ driver.Valuer = (*Optional[any])(nil)
_ driver.Valuer = (*Result[any])(nil)
_ json.Marshaler = (*Optional[any])(nil)
_ json.Marshaler = (*Result[any])(nil)
_ json.Unmarshaler = (*Optional[any])(nil)
_ json.Unmarshaler = (*Result[any])(nil)
)

View File

@@ -1,6 +1,19 @@
package Type
import Assert "github.com/lbatuska/goutils/assert"
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"reflect"
"time"
Assert "git.nas-technologies.com/lbatuska/goutils/assert"
)
// Marker interface impl
func (opt Optional[T]) Optional() {}
// CTORS BEGIN
func Some[T any](value T) Optional[T] {
@@ -40,8 +53,7 @@ func (opt *Optional[T]) HasValue() bool {
}
// UNWRAPPABLE INTERFACE BEGIN
func (opt *Optional[T]) Expect(msg string) T {
Assert.NotNil(opt)
func (opt Optional[T]) Expect(msg string) T {
if opt.present {
return opt.value
}
@@ -49,11 +61,13 @@ func (opt *Optional[T]) Expect(msg string) T {
}
func (opt *Optional[T]) Unwrap() T {
Assert.NotNil(opt)
if opt == nil {
panic("Tried unwrapping an Optional that did not have a value!")
}
if opt.present {
return opt.value
}
panic("Tried unwrapping an Optional that did not have a value!")
panic(opt)
}
func (opt *Optional[T]) UnwrapOr(val T) T {
@@ -106,3 +120,214 @@ func (opt *Optional[T]) OkOrElse(f func() error) Result[T] {
}
return Err[T](f())
}
func (opt *Optional[T]) Scan(src interface{}) error {
Assert.NotNil(opt)
opt.present = false
// DB had a null value
if src == nil {
return nil
}
// If T is a scanner (Scan is usually implemented on pointers so we need a pointer)
var scanner sql.Scanner = nil
var ok bool = false
if reflect.TypeOf(opt.value).Kind() == reflect.Ptr {
scanner, ok = any(opt.value).(sql.Scanner)
} else {
scanner, ok = any(&opt.value).(sql.Scanner)
}
if ok {
if err := scanner.Scan(src); err != nil {
return err
}
opt.present = true
return nil
}
if scanres := opt.scanBuiltin(src); scanres.IsSome() {
return scanres.Unwrap()
}
return fmt.Errorf("Unsupported type %T or differs from Optional[%T], and the type doesn't implement sql.Scanner!", src, opt.value)
}
func (opt *Optional[T]) scanBuiltin(src interface{}) Optional[error] {
opt.present = false
// First handle the special cases where we allow conversion between types
// This is usually just parsing []byte into type
if scanres := opt.scanTimeSpecial(src); scanres.IsSome() {
return scanres
}
if scanres := opt.scanStringSpecial(src); scanres.IsSome() {
return scanres
}
srcVal := reflect.ValueOf(src)
optType := reflect.TypeOf(opt.value)
optElemType := optType
if optElemType.Kind() == reflect.Pointer {
optElemType = optElemType.Elem()
}
srcElemType := srcVal.Type()
if srcElemType.Kind() == reflect.Pointer {
srcElemType = srcElemType.Elem()
}
if srcElemType != optElemType {
return Some(fmt.Errorf("Optional[%T] (aka %T) differs from %T!", opt.value, opt.value, src))
}
if optType.Kind() == reflect.Pointer {
if srcVal.Kind() == reflect.Pointer {
opt.value = srcVal.Interface().(T)
} else {
newPtr := reflect.New(optElemType)
newPtr.Elem().Set(srcVal)
opt.value = newPtr.Interface().(T)
}
} else {
if srcVal.Kind() == reflect.Pointer {
opt.value = srcVal.Elem().Interface().(T)
} else {
opt.value = srcVal.Interface().(T)
}
}
opt.present = true
return Some[error](nil)
}
func (opt *Optional[T]) scanStringSpecial(src interface{}) Optional[error] {
opt.present = false
switch v := any(opt.value).(type) {
case *string:
switch s := src.(type) {
case []byte:
*v = string(s)
goto ok
case *[]byte:
*v = string(*s)
goto ok
}
case string:
switch s := src.(type) {
case []byte:
reflect.ValueOf(&opt.value).Elem().Set(reflect.ValueOf(string(s)))
goto ok
case *[]byte:
reflect.ValueOf(&opt.value).Elem().Set(reflect.ValueOf(string(*s)))
goto ok
}
}
return None[error]()
ok:
opt.present = true
return Some[error](nil)
}
func (opt *Optional[T]) scanTimeSpecial(src interface{}) Optional[error] {
opt.present = false
switch v := any(opt.value).(type) {
case *time.Time:
switch t := src.(type) {
case []byte:
parsedTime, err := time.Parse(time.RFC3339, string(t))
if err == nil {
*v = parsedTime
goto ok
} else {
return Some(err)
}
case *[]byte:
parsedTime, err := time.Parse(time.RFC3339, string(*t))
if err == nil {
*v = parsedTime
goto ok
} else {
return Some(err)
}
}
case time.Time:
switch t := src.(type) {
case []byte:
parsedTime, err := time.Parse(time.RFC3339, string(t))
if err == nil {
reflect.ValueOf(&opt.value).Elem().Set(reflect.ValueOf(parsedTime))
goto ok
} else {
return Some(err)
}
case *[]byte:
parsedTime, err := time.Parse(time.RFC3339, string(*t))
if err == nil {
reflect.ValueOf(&opt.value).Elem().Set(reflect.ValueOf(parsedTime))
goto ok
} else {
return Some(err)
}
}
}
return None[error]()
ok:
opt.present = true
return Some[error](nil)
}
func (opt Optional[T]) MarshalJSON() ([]byte, error) {
if !opt.present {
// Return null for `omitempty` compatibility
return []byte("null"), nil
// panic("Tried to marshal an Optional that did not have a value!")
}
return json.Marshal(opt.value)
}
func (opt *Optional[T]) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
opt.present = false
return nil
}
var value T
if err := json.Unmarshal(data, &value); err != nil {
return err
}
opt.value = value
opt.present = true
return nil
}
func (opt Optional[T]) Value() (driver.Value, error) {
if !opt.present {
return nil, nil
}
if valuer, ok := any(opt.value).(driver.Valuer); ok {
return valuer.Value()
}
switch v := any(opt.Value).(type) {
case string, bool,
int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64, uintptr,
float32, float64,
complex64, complex128:
return v, nil
case *string, *bool,
*int, *int8, *int16, *int32, *int64,
*uint, *uint8, *uint16, *uint32, *uint64, *uintptr,
*float32, *float64,
*complex64, *complex128:
return v, nil
case fmt.Stringer:
return v.String(), nil
default:
return nil, errors.New("unsupported type for Optional")
}
}

View File

@@ -4,7 +4,7 @@ import (
"errors"
"testing"
Testing "github.com/lbatuska/goutils/testing"
Testing "git.nas-technologies.com/lbatuska/goutils/testing"
)
var (
@@ -18,6 +18,23 @@ var (
nilOptional = (*Optional[int])(nil)
)
func Test_optionalScan(t *testing.T) {
a := None[string]()
b := None[string]()
c := Some("Not C!")
d := None[int]()
e := Some(Ptr("Not E!"))
a.Scan("a")
b.Scan("b")
c.Scan(Ptr("c"))
e.Scan(Ptr("e"))
Testing.AssertEqual(t, "a", a.Unwrap())
Testing.AssertEqual(t, "b", b.Unwrap())
Testing.AssertEqual(t, "c", c.Unwrap())
Testing.AssertPanic(t, func() { d.Unwrap() })
Testing.AssertEqual(t, "e", *e.Unwrap())
}
func Test_optionalSome(t *testing.T) {
Testing.AssertEqual(t, x, y)
Testing.AssertEqual(t, v, u)

View File

@@ -1,11 +1,20 @@
package Type
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"reflect"
"time"
Assert "github.com/lbatuska/goutils/assert"
Assert "git.nas-technologies.com/lbatuska/goutils/assert"
)
// Marker interface impl
func (res Result[T]) Result() {}
// CTORS BEGIN
func Ok[T any](value T) Result[T] {
return Result[T]{value: value, err: nil}
@@ -43,8 +52,7 @@ func (res *Result[T]) HasValue() bool {
}
// UNWRAPPABLE INTERFACE
func (res *Result[T]) Expect(msg string) T {
Assert.NotNil(res)
func (res Result[T]) Expect(msg string) T {
if res.err == nil {
return res.value
}
@@ -52,11 +60,13 @@ func (res *Result[T]) Expect(msg string) T {
}
func (res *Result[T]) Unwrap() T {
Assert.NotNil(res)
if res == nil {
panic("Tried unwrapping a Result that had an error value!")
}
if res.err == nil {
return res.value
}
panic("Tried unwrapping a Result that had an error value!")
panic(res)
}
func (res *Result[T]) UnwrapOr(val T) T {
@@ -134,3 +144,223 @@ func (res *Result[T]) Err() Optional[error] {
}
return Optional[error]{present: false}
}
func (res *Result[T]) Scan(src interface{}) error {
Assert.NotNil(res)
e := fmt.Errorf("Unsupported type %T or differs from Result[%T], and the type doesn't implement sql.Scanner!",
src, res.value)
res.err = e
// DB had a null value
if src == nil {
return nil
}
// If T is a scanner (Scan is usually implemented on pointers so we need a pointer)
var scanner sql.Scanner = nil
var ok bool = false
if reflect.TypeOf(res.value).Kind() == reflect.Ptr {
scanner, ok = any(res.value).(sql.Scanner)
} else {
scanner, ok = any(&res.value).(sql.Scanner)
}
if ok {
if err := scanner.Scan(src); err != nil {
return err
}
res.err = nil
return nil
}
if scanres := res.scanBuiltin(src); scanres.IsSome() {
return scanres.Unwrap()
}
return e
}
func (res *Result[T]) scanBuiltin(src interface{}) Optional[error] {
res.err = nil
// First handle the special cases where we allow conversion between types
// This is usually just parsing []byte into type
if scanres := res.scanTimeSpecial(src); scanres.IsSome() {
return scanres
}
if scanres := res.scanStringSpecial(src); scanres.IsSome() {
return scanres
}
srcVal := reflect.ValueOf(src)
optType := reflect.TypeOf(res.value)
optElemType := optType
if optElemType.Kind() == reflect.Pointer {
optElemType = optElemType.Elem()
}
srcElemType := srcVal.Type()
if srcElemType.Kind() == reflect.Pointer {
srcElemType = srcElemType.Elem()
}
if srcElemType != optElemType {
e := fmt.Errorf("Result[%T] (aka %T) differs from %T!", res.value, res.value, src)
res.err = e
return Some(e)
}
if optType.Kind() == reflect.Pointer {
if srcVal.Kind() == reflect.Pointer {
res.value = srcVal.Interface().(T)
} else {
newPtr := reflect.New(optElemType)
newPtr.Elem().Set(srcVal)
res.value = newPtr.Interface().(T)
}
} else {
if srcVal.Kind() == reflect.Pointer {
res.value = srcVal.Elem().Interface().(T)
} else {
res.value = srcVal.Interface().(T)
}
}
res.err = nil
return Some[error](nil)
}
func (res *Result[T]) scanStringSpecial(src interface{}) Optional[error] {
switch v := any(res.value).(type) {
case *string:
switch s := src.(type) {
case []byte:
*v = string(s)
goto ok
case *[]byte:
*v = string(*s)
goto ok
}
case string:
switch s := src.(type) {
case []byte:
reflect.ValueOf(&res.value).Elem().Set(reflect.ValueOf(string(s)))
goto ok
case *[]byte:
reflect.ValueOf(&res.value).Elem().Set(reflect.ValueOf(string(*s)))
goto ok
}
}
return None[error]()
ok:
res.err = nil
return Some[error](nil)
}
func (res *Result[T]) scanTimeSpecial(src interface{}) Optional[error] {
switch v := any(res.value).(type) {
case *time.Time:
switch t := src.(type) {
case []byte:
parsedTime, err := time.Parse(time.RFC3339, string(t))
if err == nil {
*v = parsedTime
goto ok
} else {
res.err = err
return Some(err)
}
case *[]byte:
parsedTime, err := time.Parse(time.RFC3339, string(*t))
if err == nil {
*v = parsedTime
goto ok
} else {
res.err = err
return Some(err)
}
}
case time.Time:
switch t := src.(type) {
case []byte:
parsedTime, err := time.Parse(time.RFC3339, string(t))
if err == nil {
reflect.ValueOf(&res.value).Elem().Set(reflect.ValueOf(parsedTime))
goto ok
} else {
res.err = err
return Some(err)
}
case *[]byte:
parsedTime, err := time.Parse(time.RFC3339, string(*t))
if err == nil {
reflect.ValueOf(&res.value).Elem().Set(reflect.ValueOf(parsedTime))
goto ok
} else {
res.err = err
return Some(err)
}
}
}
return None[error]()
ok:
res.err = nil
return Some[error](nil)
}
func (res Result[T]) MarshalJSON() ([]byte, error) {
if nil != res.err {
// Return null for `omitempty` compatibility
return []byte("null"), nil
// panic("Tried to marshal a Result that was error!")
}
return json.Marshal(res.value)
}
func (res *Result[T]) UnmarshalJSON(data []byte) error {
var value T
res.err = nil
if string(data) == "null" {
// On null data the best we can do is indicate there was no error and put a default value of T
res.value = value
return nil
}
if err := json.Unmarshal(data, &value); err != nil {
res.err = err
return err
}
res.value = value
return nil
}
func (res Result[T]) Value() (driver.Value, error) {
if nil != res.err {
return nil, nil
}
if valuer, ok := any(res.value).(driver.Valuer); ok {
return valuer.Value()
}
switch v := any(res.Value).(type) {
case string, bool,
int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64, uintptr,
float32, float64,
complex64, complex128:
return v, nil
case *string, *bool,
*int, *int8, *int16, *int32, *int64,
*uint, *uint8, *uint16, *uint32, *uint64, *uintptr,
*float32, *float64,
*complex64, *complex128:
return v, nil
case fmt.Stringer:
return v.String(), nil
default:
return nil, errors.New("unsupported type for Result")
}
}

View File

@@ -5,7 +5,7 @@ import (
"reflect"
"testing"
Testing "github.com/lbatuska/goutils/testing"
Testing "git.nas-technologies.com/lbatuska/goutils/testing"
)
var (
@@ -15,6 +15,21 @@ var (
nilResult = (*Result[int])(nil)
)
func Test_resultScan(t *testing.T) {
a := Ok("Not A!")
b := Ok(0)
c := Ok(0)
d := Err[*string](errors.New(""))
a.Scan("a")
b.Scan(1)
c.Scan("c")
d.Scan(Ptr("d"))
Testing.AssertEqual(t, a.Unwrap(), "a")
Testing.AssertEqual(t, b.Unwrap(), 1)
Testing.AssertError(t, c.UnwrapErr())
Testing.AssertEqual(t, *d.Unwrap(), "d")
}
func Test_resultConstructors(t *testing.T) {
err := errors.New("some error")
w := Err[int](err)

View File

@@ -1,5 +1,11 @@
package Type
import (
"fmt"
"reflect"
"unsafe"
)
func Expect[T any](val Unwrappable[T], msg string) T {
return val.Expect(msg)
}
@@ -42,3 +48,92 @@ func ResultWrapb[T any](err error, val T) Result[T] {
func Ptr[T any](v T) *T {
return &v
}
// meant to be used as defer Type.CatchUnwrap(Type.Ptr(&res)) or Type.CatchUnwrap(&res) if res is already a pointer
// where res is a pointer to an Option or Result returned by a function (initialized to not be nil)
// func X() (res *Optional[int]) {
// res = None[int]()
// defer CatchUnwrap(&res)
//
// // Some possibly unsafe unwrapping of values
// return res
// }
// === OR ===
// func X() (res Optional[int]) {
// res = None[int]()
// defer Type.CatchUnwrap(Type.Ptr(&res))
//
// // Some possibly unsafe unwrapping of values
// return res
// }
func CatchUnwrap(ret interface{}) {
r := recover()
if r == nil {
return
}
vp := reflect.ValueOf(r)
if vp.Kind() != reflect.Pointer || vp.IsNil() {
panic(r)
}
if _, ok := r.(OptionalerMarker); ok {
if setOptionalNone(ret) {
return
}
}
if _, ok := r.(ResulterMarker); ok {
if setResultError(ret, fmt.Errorf("Tried to unwrap a failed result!")) {
return
}
}
panic(r)
}
func setOptionalNone(ret interface{}) bool {
v := reflect.ValueOf(ret)
if v.Kind() != reflect.Ptr || v.IsNil() {
return false
}
elem := v.Elem().Elem()
if elem.Kind() != reflect.Struct {
return false
}
presentField := elem.FieldByName("present")
if !presentField.IsValid() {
return false
}
if presentField.Kind() == reflect.Bool {
presentField = reflect.NewAt(presentField.Type(),
unsafe.Pointer(presentField.UnsafeAddr())).Elem()
presentField.SetBool(false) // Setting it to false (None)
} else {
return false
}
return true
}
func setResultError(ret interface{}, err error) bool {
v := reflect.ValueOf(ret)
if v.Kind() != reflect.Ptr || v.IsNil() {
return false
}
elem := v.Elem().Elem()
if elem.Kind() != reflect.Struct {
return false
}
errField := elem.FieldByName("err")
if !errField.IsValid() {
return false
}
if errField.Kind() == reflect.Interface {
errField = reflect.NewAt(errField.Type(),
unsafe.Pointer(errField.UnsafeAddr())).Elem()
errField.Set(reflect.ValueOf(err)) // Setting the error
} else {
return false
}
return true
}

View File

@@ -0,0 +1,116 @@
package Session
import (
"context"
"crypto/rand"
"encoding/hex"
"net/http"
"time"
)
// returns the session identified by session_id or creates a new one and returns that
func (sm *sessionManager) Session(session_id string) *session {
sm.mtx.Lock()
s, ok := sm.sessions[session_id]
if !ok {
s = &session{
data: make(map[string]any),
expiry: time.Now().Add(Session_Lifetime),
}
sm.sessions[session_id] = s
sm.mtx.Unlock()
} else {
sm.mtx.Unlock()
s.mtx.Lock()
if time.Now().After(s.expiry) {
// expired, clear it
s.data = make(map[string]any)
s.expiry = time.Now().Add(Session_Lifetime)
} else {
// valid, extend expiry
s.expiry = time.Now().Add(Session_Lifetime)
}
s.mtx.Unlock()
}
return s
}
func GetSession(ctx context.Context) *session {
s, _ := ctx.Value("session").(*session)
return s
}
func (sm *sessionManager) SessionMW(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var sid string
cookie, err := r.Cookie(cookie_key)
if err == http.ErrNoCookie {
sid = newSessionID()
} else if err == nil {
sid = cookie.Value
} else {
http.Error(w, "Invalid session", http.StatusBadRequest)
return
}
http.SetCookie(w, &http.Cookie{
Name: cookie_key,
Value: sid,
Path: "/",
MaxAge: int(Session_Lifetime.Seconds()),
HttpOnly: true,
Secure: true,
})
ctx := context.WithValue(r.Context(), "session", sm.Session(sid))
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
})
}
func (sm *sessionManager) StartGC(interval time.Duration) {
go func() {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for range ticker.C {
sm.cleanup()
}
}()
}
func newSessionID() string {
b := make([]byte, 32)
rand.Read(b)
return hex.EncodeToString(b)
}
func (sm *sessionManager) cleanup() {
now := time.Now()
sm.mtx.Lock()
defer sm.mtx.Unlock()
for id, s := range sm.sessions {
s.mtx.Lock()
expired := now.After(s.expiry)
s.mtx.Unlock()
if expired {
delete(sm.sessions, id)
}
}
}
func NewSessionManager(gc_interval time.Duration) *sessionManager {
sm := &sessionManager{
sessions: make(map[string]*session),
}
sm.StartGC(gc_interval)
return sm
}

20
unstable/session/types.go Normal file
View File

@@ -0,0 +1,20 @@
package Session
import (
"sync"
"time"
)
const Session_Lifetime time.Duration = time.Duration(120) * time.Second
const cookie_key string = "GSESSID"
type session struct {
mtx sync.Mutex
data map[string]any
expiry time.Time
}
type sessionManager struct {
sessions map[string]*session
mtx sync.Mutex
}