diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2838448 --- /dev/null +++ b/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2024 Patial Tech. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Patial Tech. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/cmd/migrate-up/main.go b/cmd/migrate-up/main.go index bdea489..b5e9a82 100644 --- a/cmd/migrate-up/main.go +++ b/cmd/migrate-up/main.go @@ -1,3 +1,6 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. package main import ( diff --git a/cmd/server/handler/request.go b/cmd/server/handler/request.go index c25687b..6043968 100644 --- a/cmd/server/handler/request.go +++ b/cmd/server/handler/request.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package handler import ( @@ -31,7 +35,7 @@ func Request() func(http.Handler) http.Handler { // ID requestID := r.Header.Get("X-Request-Id") if requestID == "" { - requestID = uid.ULID() + requestID = uid.NewUUID() } ctx = context.WithValue(ctx, RequestIDKey, requestID) diff --git a/cmd/server/main.go b/cmd/server/main.go index 320f7d9..d91c128 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package main import ( diff --git a/config/config.go b/config/config.go index 09ffe0d..f5b75ab 100644 --- a/config/config.go +++ b/config/config.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package config import ( diff --git a/config/dotenv/parser.go b/config/dotenv/parser.go index 5b3651a..1d68e21 100644 --- a/config/dotenv/parser.go +++ b/config/dotenv/parser.go @@ -11,7 +11,7 @@ import ( ) // -// copied from https://github.com/joho/godotenv/blob/main/parser.go +// ported from https://github.com/joho/godotenv/blob/main/parser.go // const ( diff --git a/config/dotenv/read.go b/config/dotenv/read.go index d2b833f..c07830c 100644 --- a/config/dotenv/read.go +++ b/config/dotenv/read.go @@ -10,7 +10,7 @@ import ( ) // -// copied from https://github.com/joho/godotenv/blob/main/godotenv.go +// ported from https://github.com/joho/godotenv/blob/main/godotenv.go // // Read all env (with same file loading semantics as Load) but return values as diff --git a/config/dotenv/write.go b/config/dotenv/write.go index 02e4928..14f454d 100644 --- a/config/dotenv/write.go +++ b/config/dotenv/write.go @@ -9,7 +9,7 @@ import ( ) // -// copied from https://github.com/joho/godotenv/blob/main/godotenv.go +// ported from https://github.com/joho/godotenv/blob/main/godotenv.go // const doubleQuoteSpecialChars = "\\\n\r\"!$`" diff --git a/config/urls.go b/config/urls.go index cb35c66..7a63bf1 100644 --- a/config/urls.go +++ b/config/urls.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package config import "fmt" diff --git a/db/client.go b/db/client.go index 2a6ef95..3180cf2 100644 --- a/db/client.go +++ b/db/client.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package db import ( @@ -54,48 +58,52 @@ func Client() *ent.Client { return cl } +type entity interface { + GetID() (int64, error) +} + // A AuditHook is an example for audit-log hook. func AuditHook(next ent.Mutator) ent.Mutator { - type Entity interface { - GetID() (int64, error) - } return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (v ent.Value, err error) { - var id int64 start := time.Now() defer func() { - saveAudit(ctx, id, m.Type(), m.Op().String(), time.Since(start), err) + saveAudit(ctx, m.Type(), m.Op(), v, err, time.Since(start)) }() v, err = next.Mutate(ctx, m) - - if en, ok := v.(Entity); ok { - id, _ = en.GetID() - } - logger.Info("** %v", v) return }) } -func saveAudit(_ context.Context, entID int64, entT, op string, d time.Duration, err error) { - logger.Info("audit: %d, %s, %s, %s, %v", entID, entT, op, d, err) - // ml.SetCreatedAt(time.Now()) - // if usr := auth.CtxUser(ctx); usr != nil { - // ml.SetByID(usr.ID) - // ml.SetBy(usr.DisplayName) - // } +func saveAudit(ctx context.Context, t string, op ent.Op, v ent.Value, err error, d time.Duration) { + if t == "Audit" { + // skip Audit table operations + return + } - // switch op := m.Op(); { - // case op.Is(ent.OpCreate): - // ml.SetOperation("Create") + var entOp string + switch { + case op.Is(ent.OpCreate): + entOp = "Create" + case op.Is(ent.OpDelete): + entOp = "Delete" + case op.Is(ent.OpDeleteOne): + entOp = "DeleteOne" + case op.Is(ent.OpUpdate): + entOp = "Update" + case op.Is(ent.OpUpdateOne): + entOp = "UpdateOne" + } - // case op.Is(ent.OpUpdate): - // ml.SetOperation("Update") - // case op.Is(ent.OpUpdateOne): - // ml.SetOperation("UpdateOne") + big. + reqID := ctx.Value("RequestID") + ip := ctx.Value("RequestIP") + ua := ctx.Value("RequestUA") - // case op.Is(ent.OpDelete): - // ml.SetOperation("Delete") - // case op.Is(ent.OpDeleteOne): - // ml.SetOperation("DeleteOne") - // } + if en, ok := v.(entity); ok { + id, _ := en.GetID() + logger.Info("%s %s %s-%s(%d) ip=%s ua=%s t=%v error=%e", reqID, status, op.String(), t, id, ip, ua, d, err) + } else { + logger.Info("%s %s %s-%s ip=%s ua=%s t=%v error=%e", reqID, status, op.String(), t, ip, ua, d, err) + } } diff --git a/db/migrations/index.go b/db/migrations/index.go index 6913262..a1fc6c0 100644 --- a/db/migrations/index.go +++ b/db/migrations/index.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package migrations import ( diff --git a/mailer/mailer.go b/mailer/mailer.go index 81f99de..c33271d 100644 --- a/mailer/mailer.go +++ b/mailer/mailer.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package mailer import ( diff --git a/mailer/message/render.go b/mailer/message/render.go index f180d06..217875a 100644 --- a/mailer/message/render.go +++ b/mailer/message/render.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package message import ( diff --git a/mailer/message/welcome.go b/mailer/message/welcome.go index 9c44db7..cfd5424 100644 --- a/mailer/message/welcome.go +++ b/mailer/message/welcome.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package message import _ "embed" diff --git a/mailer/transport_dev.go b/mailer/transport_dev.go index be6a2b7..c3dac6d 100644 --- a/mailer/transport_dev.go +++ b/mailer/transport_dev.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package mailer import ( diff --git a/pkg/user/create.go b/pkg/user/create.go index a44aac2..9c2bb34 100644 --- a/pkg/user/create.go +++ b/pkg/user/create.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package user import ( diff --git a/pkg/user/create_test.go b/pkg/user/create_test.go index 3967aaf..68ade84 100644 --- a/pkg/user/create_test.go +++ b/pkg/user/create_test.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package user import ( diff --git a/pkg/user/password.go b/pkg/user/password.go index 93e5b08..16e8e24 100644 --- a/pkg/user/password.go +++ b/pkg/user/password.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package user // EmailResetPWD link to user to reset password diff --git a/pkg/user/session.go b/pkg/user/session.go index e7b55ee..4861268 100644 --- a/pkg/user/session.go +++ b/pkg/user/session.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package user import ( diff --git a/pkg/user/token.go b/pkg/user/token.go index 6a83f75..eca4689 100644 --- a/pkg/user/token.go +++ b/pkg/user/token.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package user import ( diff --git a/pkg/user/token_test.go b/pkg/user/token_test.go index e887bbf..a66544c 100644 --- a/pkg/user/token_test.go +++ b/pkg/user/token_test.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package user import ( diff --git a/pkg/user/verify.go b/pkg/user/verify.go index cd116a2..b514639 100644 --- a/pkg/user/verify.go +++ b/pkg/user/verify.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package user import ( diff --git a/util/crypto/hash.go b/util/crypto/hash.go index a1017a9..fbde359 100644 --- a/util/crypto/hash.go +++ b/util/crypto/hash.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package crypto import ( diff --git a/util/crypto/hash_test.go b/util/crypto/hash_test.go index 8de3ecf..2f878d7 100644 --- a/util/crypto/hash_test.go +++ b/util/crypto/hash_test.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package crypto import "testing" diff --git a/util/logger/logger.go b/util/logger/logger.go index c6b3cda..d6fbdbe 100644 --- a/util/logger/logger.go +++ b/util/logger/logger.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package logger import ( diff --git a/util/open/darwin.go b/util/open/darwin.go index 06c891a..72671a5 100644 --- a/util/open/darwin.go +++ b/util/open/darwin.go @@ -1,5 +1,9 @@ //go:build darwin +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package open import ( diff --git a/util/open/linux.go b/util/open/linux.go index a432b58..803b77a 100644 --- a/util/open/linux.go +++ b/util/open/linux.go @@ -1,5 +1,9 @@ //go:build linux +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package open import ( diff --git a/util/open/open.go b/util/open/open.go index 0044286..69c9b35 100644 --- a/util/open/open.go +++ b/util/open/open.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package open // WithDefaultApp open a file, directory, or URI using the OS's default application for that object type. diff --git a/util/open/windows.go b/util/open/windows.go index a0b0324..dfdfb28 100644 --- a/util/open/windows.go +++ b/util/open/windows.go @@ -1,5 +1,9 @@ //go:build windows +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package open import ( diff --git a/util/structs/structs.go b/util/structs/structs.go index bf5f209..9780bcd 100644 --- a/util/structs/structs.go +++ b/util/structs/structs.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package structs import ( diff --git a/util/uid/sqid.go b/util/uid/sqid.go index 99ecf08..70e016e 100644 --- a/util/uid/sqid.go +++ b/util/uid/sqid.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package uid import "github.com/sqids/sqids-go" diff --git a/util/uid/uuid.go b/util/uid/uuid.go new file mode 100644 index 0000000..1f6ef0d --- /dev/null +++ b/util/uid/uuid.go @@ -0,0 +1,23 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package uid + +import ( + "context" + "fmt" + "time" + + "github.com/google/uuid" + "gitserver.in/patialtech/rano/util/logger" +) + +func NewUUID() string { + id, err := uuid.NewV7() + if err != nil { + logger.Incident(context.Background(), "New UUID", err.Error()) + return fmt.Sprintf("%d", time.Now().UTC().UnixNano()) + } + return id.String() +} diff --git a/util/validate/validate.go b/util/validate/validate.go index 20c9516..36c295d 100644 --- a/util/validate/validate.go +++ b/util/validate/validate.go @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package validate import ( diff --git a/web/lib/gql/account.gql.ts b/web/lib/gql/account.gql.ts index eeed9e0..1484b7c 100644 --- a/web/lib/gql/account.gql.ts +++ b/web/lib/gql/account.gql.ts @@ -1,3 +1,7 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + import { gql } from '@urql/svelte'; export const Me = gql` query Me { diff --git a/web/lib/gql/client.ts b/web/lib/gql/client.ts index 0fe1191..baf878e 100644 --- a/web/lib/gql/client.ts +++ b/web/lib/gql/client.ts @@ -1,8 +1,12 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + import { Client, cacheExchange, fetchExchange } from '@urql/svelte'; export function newClient(url: string) { return new Client({ url, - exchanges: [ fetchExchange] + exchanges: [fetchExchange] }); } diff --git a/web/lib/stores/alert.svelte.ts b/web/lib/stores/alert.svelte.ts index e69de29..15889d8 100644 --- a/web/lib/stores/alert.svelte.ts +++ b/web/lib/stores/alert.svelte.ts @@ -0,0 +1,3 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. diff --git a/web/lib/stores/auth.svelte.ts b/web/lib/stores/auth.svelte.ts index 1a42083..0ae9a29 100644 --- a/web/lib/stores/auth.svelte.ts +++ b/web/lib/stores/auth.svelte.ts @@ -1 +1,5 @@ +// Copyright 2024 Patial Tech. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + export let authUser = $state(); diff --git a/web/lib/ui/Btn.svelte b/web/lib/ui/Btn.svelte index b49c21d..cf592ca 100644 --- a/web/lib/ui/Btn.svelte +++ b/web/lib/ui/Btn.svelte @@ -1,12 +1,15 @@ +// Copyright 2024 Patial Tech. All rights reserved. // Use of this source code is governed by a BSD-style // license +that can be found in the LICENSE file. +