Compare commits
6 commits
c8f4f4961f
...
dfb8fb800c
| Author | SHA1 | Date | |
|---|---|---|---|
| dfb8fb800c | |||
| f4476d3e89 | |||
| fc57baff78 | |||
| df80e29f6e | |||
| f5ab92b1cf | |||
| 911c1ce734 |
13 changed files with 111 additions and 198 deletions
2
.env-dev
2
.env-dev
|
|
@ -29,7 +29,7 @@ COOKIE_SECRET=13d6b4dff8f84a10851021ec8608f814570d562c92fe6b5ec4c9f595bcb3234b
|
|||
###### JWT ######
|
||||
#######################################
|
||||
JWT_SECRET=dkfTgonmgaAdlgkw
|
||||
JWT_LIFESPAN_MINUTES=4320 # expires after 3 days
|
||||
JWT_LIFESPAN_MINUTES=1440 # expires after 1 day
|
||||
|
||||
#######################################
|
||||
###### DATABASE ######
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ CDNEnable=false
|
|||
###### JWT ######
|
||||
#######################################
|
||||
JWT_SECRET=dkfTgonmgaAdlgkw
|
||||
JWT_LIFESPAN_MINUTES=4320 # expires after 3 days
|
||||
JWT_LIFESPAN_MINUTES=1440 # expires after 1 day
|
||||
|
||||
#######################################
|
||||
###### DATABASE ######
|
||||
|
|
|
|||
|
|
@ -16,5 +16,15 @@ func GetQueueConfig() core.QueueConfig {
|
|||
// For enabling and disabling the queue system
|
||||
// set to true to enable it, set to false to disable
|
||||
EnableQueue: false,
|
||||
|
||||
// Number of concurrent workers processing tasks
|
||||
Concurrency: 10,
|
||||
|
||||
// Queue names with priority weights (higher number = higher priority)
|
||||
Queues: map[string]int{
|
||||
"critical": 6,
|
||||
"default": 3,
|
||||
"low": 1,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ func AdminUsersList(c *core.Context) *core.Response {
|
|||
|
||||
// initiate authority
|
||||
auth := new(utils.Authority)
|
||||
var session = new(utils.SessionUser)
|
||||
session := c.GetSession()
|
||||
// true if session is active
|
||||
hassession := session.Init(c)
|
||||
|
||||
|
|
@ -135,7 +135,7 @@ func AdminUsersAdd(c *core.Context) *core.Response {
|
|||
|
||||
// initiate authority
|
||||
auth := new(utils.Authority)
|
||||
var session = new(utils.SessionUser)
|
||||
session := c.GetSession()
|
||||
// true if session is active
|
||||
hassession := session.Init(c)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
// Copyright 2023 Harran Ali <harran.m@gmail.com>. All rights reserved.
|
||||
// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
|
||||
// Copyright (c) 2026 Zeni Kim <zenik@smarteching.com>
|
||||
// Use of this source code is governed by MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
|
|
@ -19,7 +19,6 @@ import (
|
|||
"git.smarteching.com/goffee/core"
|
||||
"git.smarteching.com/goffee/cup/events"
|
||||
"git.smarteching.com/goffee/cup/models"
|
||||
"git.smarteching.com/goffee/cup/utils"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
|
@ -101,7 +100,7 @@ func Signup(c *core.Context) *core.Response {
|
|||
|
||||
// cache the token
|
||||
userAgent := c.GetUserAgent()
|
||||
hashedCacheKey := utils.CreateAuthTokenHashedCacheKey(user.ID, userAgent)
|
||||
hashedCacheKey := core.CreateAuthTokenHashedCacheKey(user.ID, userAgent)
|
||||
err = c.GetCache().Set(hashedCacheKey, token)
|
||||
if err != nil {
|
||||
c.GetLogger().Error(err.Error())
|
||||
|
|
@ -223,12 +222,12 @@ func Signin(c *core.Context) *core.Response {
|
|||
}
|
||||
// cache the token
|
||||
userAgent := c.GetUserAgent()
|
||||
hashedCacheKey := utils.CreateAuthTokenHashedCacheKey(user.ID, userAgent)
|
||||
hashedCacheKey := core.CreateAuthTokenHashedCacheKey(user.ID, userAgent)
|
||||
err = c.GetCache().Set(hashedCacheKey, token)
|
||||
|
||||
// delete data from old sessions
|
||||
sessionKey := fmt.Sprintf("sess_%v", userAgent)
|
||||
hashedSessionKey := utils.CreateAuthTokenHashedCacheKey(user.ID, sessionKey)
|
||||
hashedSessionKey := core.CreateAuthTokenHashedCacheKey(user.ID, sessionKey)
|
||||
_ = c.GetCache().Delete(hashedSessionKey)
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -463,7 +462,7 @@ func Signout(c *core.Context) *core.Response {
|
|||
}))
|
||||
}
|
||||
userAgent := c.GetUserAgent()
|
||||
hashedCacheKey := utils.CreateAuthTokenHashedCacheKey(uint(c.CastToInt(payload["userID"])), userAgent)
|
||||
hashedCacheKey := core.CreateAuthTokenHashedCacheKey(uint(c.CastToInt(payload["userID"])), userAgent)
|
||||
|
||||
err = c.GetCache().Delete(hashedCacheKey)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -47,8 +47,27 @@ func WelcomeHome(c *core.Context) *core.Response {
|
|||
|
||||
}
|
||||
|
||||
// Show dashboard
|
||||
|
||||
func WelcomeToDashboard(c *core.Context) *core.Response {
|
||||
message := "{\"message\": \"Welcome to Dashboard\"}"
|
||||
return c.Response.Json(message)
|
||||
}
|
||||
|
||||
// Show basic app login
|
||||
func AppLogin(c *core.Context) *core.Response {
|
||||
|
||||
// first, include all compoments
|
||||
// first, include all compoments
|
||||
type templateData struct {
|
||||
PageCard components.PageCard
|
||||
}
|
||||
|
||||
// now fill data of the components
|
||||
tmplData := templateData{
|
||||
PageCard: components.PageCard{
|
||||
CardTitle: "Card title",
|
||||
CardBody: "Loerm ipsum at deim",
|
||||
},
|
||||
}
|
||||
return c.Response.Template("login.html", tmplData)
|
||||
}
|
||||
|
|
|
|||
55
controllers/queuesample.go
Normal file
55
controllers/queuesample.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
// Copyright (c) 2025 Zeni Kim <zenik@smarteching.com>
|
||||
// Use of this source code is governed by MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.smarteching.com/goffee/core"
|
||||
"git.smarteching.com/goffee/cup/workers"
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
// Make samples queues
|
||||
func Queuesample(c *core.Context) *core.Response {
|
||||
|
||||
// Get client queue asynq
|
||||
client := c.GetQueueClient()
|
||||
|
||||
// Create a task with typename and payload.
|
||||
payload, err := json.Marshal(workers.EmailTaskPayload{UserID: 42})
|
||||
if err != nil {
|
||||
c.GetLogger().Error(err.Error())
|
||||
return c.Response.SetStatusCode(500).Json(`{"message": "internal error"}`)
|
||||
}
|
||||
|
||||
t1 := asynq.NewTask(workers.TypeWelcomeEmail, payload)
|
||||
|
||||
t2 := asynq.NewTask(workers.TypeReminderEmail, payload)
|
||||
|
||||
// Process the task immediately.
|
||||
info, err := client.Enqueue(t1)
|
||||
if err != nil {
|
||||
c.GetLogger().Error(err.Error())
|
||||
return c.Response.SetStatusCode(500).Json(`{"message": "internal error"}`)
|
||||
}
|
||||
c.GetLogger().Info(fmt.Sprintf(" [*] Successfully enqueued task: %+v", info))
|
||||
|
||||
// Process 2 task 1 min later.
|
||||
for i := 1; i < 3; i++ {
|
||||
info, err = client.Enqueue(t2, asynq.ProcessIn(1*time.Minute))
|
||||
if err != nil {
|
||||
c.GetLogger().Error(err.Error())
|
||||
return c.Response.SetStatusCode(500).Json(`{"message": "internal error"}`)
|
||||
}
|
||||
c.GetLogger().Info(fmt.Sprintf(" [*] Successfully enqueued task: %+v", info))
|
||||
}
|
||||
|
||||
message := "{\"message\": \"Task queued\"}"
|
||||
return c.Response.Json(message)
|
||||
|
||||
}
|
||||
|
|
@ -9,7 +9,6 @@ import (
|
|||
|
||||
"git.smarteching.com/goffee/core"
|
||||
"git.smarteching.com/goffee/cup/models"
|
||||
"git.smarteching.com/goffee/cup/utils"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
|
@ -31,7 +30,7 @@ var CheckSessionCookie core.Hook = func(c *core.Context) {
|
|||
} else {
|
||||
|
||||
userAgent := c.GetUserAgent()
|
||||
hashedCacheKey := utils.CreateAuthTokenHashedCacheKey(uint(c.CastToInt(payload["userID"])), userAgent)
|
||||
hashedCacheKey := core.CreateAuthTokenHashedCacheKey(uint(c.CastToInt(payload["userID"])), userAgent)
|
||||
|
||||
cachedToken, err := c.GetCache().Get(hashedCacheKey)
|
||||
if err != nil {
|
||||
|
|
@ -102,7 +101,7 @@ var AuthCheck core.Hook = func(c *core.Context) {
|
|||
return
|
||||
}
|
||||
userAgent := c.GetUserAgent()
|
||||
hashedCacheKey := utils.CreateAuthTokenHashedCacheKey(uint(c.CastToInt(payload["userID"])), userAgent)
|
||||
hashedCacheKey := core.CreateAuthTokenHashedCacheKey(uint(c.CastToInt(payload["userID"])), userAgent)
|
||||
|
||||
cachedToken, err := c.GetCache().Get(hashedCacheKey)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ package main
|
|||
|
||||
import (
|
||||
"git.smarteching.com/goffee/core"
|
||||
"git.smarteching.com/goffee/cup/config"
|
||||
"git.smarteching.com/goffee/cup/workers"
|
||||
)
|
||||
|
||||
|
|
@ -26,5 +27,5 @@ func registerQueues() {
|
|||
//########################################
|
||||
// Start queue server, DO NOT TOUCH
|
||||
//########################################
|
||||
go queque.RunQueueserver()
|
||||
go queque.RunQueueserver(config.GetQueueConfig())
|
||||
}
|
||||
|
|
|
|||
11
routes.go
11
routes.go
|
|
@ -8,6 +8,7 @@ package main
|
|||
import (
|
||||
"git.smarteching.com/goffee/core"
|
||||
"git.smarteching.com/goffee/cup/controllers"
|
||||
"git.smarteching.com/goffee/cup/hooks"
|
||||
)
|
||||
|
||||
// Register the app controllers
|
||||
|
|
@ -19,7 +20,7 @@ func registerRoutes() {
|
|||
|
||||
// Define your routes here...
|
||||
controller.Get("/", controllers.WelcomeHome)
|
||||
|
||||
|
||||
// Uncomment the lines below to enable authentication API
|
||||
controller.Post("/signup", controllers.Signup)
|
||||
controller.Post("/signin", controllers.Signin)
|
||||
|
|
@ -27,6 +28,9 @@ func registerRoutes() {
|
|||
controller.Post("/reset-password", controllers.ResetPasswordRequest)
|
||||
controller.Post("/reset-password/code/:code", controllers.SetNewPassword)
|
||||
|
||||
// queue sample route
|
||||
controller.Get("/queuesample", controllers.Queuesample)
|
||||
|
||||
// Uncomment the lines below to enable user administration
|
||||
controller.Get("/admin/users", controllers.AdminUsersList)
|
||||
controller.Post("/admin/users", controllers.AdminUsersList)
|
||||
|
|
@ -37,5 +41,8 @@ func registerRoutes() {
|
|||
controller.Post("/admin/users/delete", controllers.AdminUsersDelete)
|
||||
controller.Post("/admin/users/deleteconfirm", controllers.AdminUsersDelConfirm)
|
||||
|
||||
|
||||
controller.Get("/dashboard", controllers.WelcomeToDashboard, hooks.AuthCheck)
|
||||
controller.Get("/signout", controllers.Signout)
|
||||
controller.Get("/applogin", controllers.AppLogin, hooks.CheckSessionCookie)
|
||||
controller.Post("/applogin", controllers.AppLogin, hooks.CheckSessionCookie)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
|
|
@ -71,13 +69,7 @@ func CreateSeedData() {
|
|||
}
|
||||
}
|
||||
|
||||
// generate a hashed string to be used as key for caching auth jwt token
|
||||
func CreateAuthTokenHashedCacheKey(userID uint, userAgent string) string {
|
||||
cacheKey := fmt.Sprintf("userid:_%v_useragent:_%v_jwt_token", userID, userAgent)
|
||||
hashedCacheKey := fmt.Sprintf("%v", fmt.Sprintf("%x", md5.Sum([]byte(cacheKey))))
|
||||
|
||||
return hashedCacheKey
|
||||
}
|
||||
|
||||
func FormatUnix(value int64) string {
|
||||
return time.Unix(value, 0).Format("2006-01-02 15:04:05")
|
||||
|
|
|
|||
170
utils/session.go
170
utils/session.go
|
|
@ -1,170 +0,0 @@
|
|||
// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
|
||||
// Use of this source code is governed by MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"encoding/json"
|
||||
|
||||
"git.smarteching.com/goffee/core"
|
||||
"git.smarteching.com/goffee/cup/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type SessionUser struct {
|
||||
mu sync.RWMutex
|
||||
context *core.Context
|
||||
userID uint
|
||||
hashedSessionKey string
|
||||
authenticated bool
|
||||
sessionStart time.Time
|
||||
values map[string]interface{}
|
||||
}
|
||||
|
||||
// start the struct
|
||||
func (s *SessionUser) Init(c *core.Context) bool {
|
||||
|
||||
// check session cookie
|
||||
pass := true
|
||||
token := ""
|
||||
s.context = c
|
||||
|
||||
payload := make(map[string]interface{})
|
||||
// get cookie
|
||||
usercookie, err := c.GetCookie()
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
|
||||
token = usercookie.Token
|
||||
|
||||
if token == "" {
|
||||
|
||||
pass = false
|
||||
|
||||
} else {
|
||||
|
||||
payload, err = c.GetJWT().DecodeToken(token)
|
||||
|
||||
if err != nil {
|
||||
|
||||
pass = false
|
||||
|
||||
} else {
|
||||
|
||||
userID := uint(c.CastToInt(payload["userID"]))
|
||||
userAgent := c.GetUserAgent()
|
||||
|
||||
// get data from redis
|
||||
hashedCacheKey := CreateAuthTokenHashedCacheKey(userID, userAgent)
|
||||
cachedToken, err := c.GetCache().Get(hashedCacheKey)
|
||||
|
||||
if err != nil {
|
||||
pass = false
|
||||
} else if cachedToken != token {
|
||||
pass = false
|
||||
} else {
|
||||
var user models.User
|
||||
res := c.GetGorm().Where("id = ?", userID).First(&user)
|
||||
if res.Error != nil && !errors.Is(res.Error, gorm.ErrRecordNotFound) {
|
||||
pass = false
|
||||
}
|
||||
// if have session start the struct
|
||||
if pass {
|
||||
userAgent := c.GetUserAgent()
|
||||
sessionKey := fmt.Sprintf("sess_%v", userAgent)
|
||||
s.hashedSessionKey = CreateAuthTokenHashedCacheKey(userID, sessionKey)
|
||||
|
||||
s.values = make(map[string]interface{})
|
||||
s.authenticated = true
|
||||
s.userID = userID
|
||||
value, _ := c.GetCache().Get(s.hashedSessionKey)
|
||||
|
||||
if len(value) > 0 {
|
||||
_ = json.Unmarshal([]byte(value), &s.values)
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
} else {
|
||||
|
||||
s.hashedSessionKey = ""
|
||||
s.authenticated = false
|
||||
s.userID = 0
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *SessionUser) Set(key string, value interface{}) error {
|
||||
s.mu.Lock()
|
||||
s.values[key] = value
|
||||
s.mu.Unlock()
|
||||
return s.Save()
|
||||
}
|
||||
|
||||
func (s *SessionUser) Get(key string) (interface{}, bool) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
val, ok := s.values[key]
|
||||
return val, ok
|
||||
}
|
||||
|
||||
func (s *SessionUser) Delete(key string) interface{} {
|
||||
s.mu.RLock()
|
||||
v, ok := s.values[key]
|
||||
s.mu.RUnlock()
|
||||
if ok {
|
||||
s.mu.Lock()
|
||||
delete(s.values, key)
|
||||
s.mu.Unlock()
|
||||
}
|
||||
s.Save()
|
||||
return v
|
||||
}
|
||||
|
||||
func (s *SessionUser) Flush() error {
|
||||
s.mu.Lock()
|
||||
s.context.GetCache().Delete(s.hashedSessionKey)
|
||||
s.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SessionUser) Save() error {
|
||||
|
||||
var value string
|
||||
|
||||
s.mu.RLock()
|
||||
|
||||
if len(s.values) > 0 {
|
||||
buf, err := json.Marshal(&s.values)
|
||||
if err != nil {
|
||||
s.mu.RUnlock()
|
||||
return err
|
||||
}
|
||||
value = string(buf)
|
||||
}
|
||||
|
||||
if len(value) > 0 {
|
||||
s.context.GetCache().Set(s.hashedSessionKey, value)
|
||||
} else {
|
||||
s.context.GetCache().Delete(s.hashedSessionKey)
|
||||
}
|
||||
s.mu.RUnlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SessionUser) GetUserID() uint {
|
||||
|
||||
return s.userID
|
||||
}
|
||||
|
|
@ -3,8 +3,9 @@ package workers
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"fmt"
|
||||
|
||||
"git.smarteching.com/goffee/core/logger"
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
|
|
@ -25,7 +26,7 @@ func HandleWelcomeEmailTask(ctx context.Context, t *asynq.Task) error {
|
|||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf(" [*] Send Welcome Email to User %d", p.UserID)
|
||||
logger.ResolveLogger().Info(fmt.Sprintf(" [*] Send Welcome Email to User %d", p.UserID))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -34,6 +35,6 @@ func HandleReminderEmailTask(ctx context.Context, t *asynq.Task) error {
|
|||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf(" [*] Send Reminder Email to User %d", p.UserID)
|
||||
logger.ResolveLogger().Info(fmt.Sprintf(" [*] Send Reminder Email to User %d", p.UserID))
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue