mirror of
https://github.com/pikami/cosmium.git
synced 2025-02-08 08:56:01 +00:00
Add more error handling and mutex guards
This commit is contained in:
parent
d6b816b55a
commit
125f10d8a2
@ -58,15 +58,15 @@ func (c *ServerConfig) PopulateCalculatedFields() {
|
|||||||
|
|
||||||
switch c.LogLevel {
|
switch c.LogLevel {
|
||||||
case "debug":
|
case "debug":
|
||||||
logger.LogLevel = logger.LogLevelDebug
|
logger.SetLogLevel(logger.LogLevelDebug)
|
||||||
case "info":
|
case "info":
|
||||||
logger.LogLevel = logger.LogLevelInfo
|
logger.SetLogLevel(logger.LogLevelInfo)
|
||||||
case "error":
|
case "error":
|
||||||
logger.LogLevel = logger.LogLevelError
|
logger.SetLogLevel(logger.LogLevelError)
|
||||||
case "silent":
|
case "silent":
|
||||||
logger.LogLevel = logger.LogLevelSilent
|
logger.SetLogLevel(logger.LogLevelSilent)
|
||||||
default:
|
default:
|
||||||
logger.LogLevel = logger.LogLevelInfo
|
logger.SetLogLevel(logger.LogLevelInfo)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/pikami/cosmium/api/handlers"
|
"github.com/pikami/cosmium/api/handlers"
|
||||||
@ -13,15 +14,19 @@ import (
|
|||||||
tlsprovider "github.com/pikami/cosmium/internal/tls_provider"
|
tlsprovider "github.com/pikami/cosmium/internal/tls_provider"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ginMux sync.Mutex
|
||||||
|
|
||||||
func (s *ApiServer) CreateRouter(repository *repositories.DataRepository) {
|
func (s *ApiServer) CreateRouter(repository *repositories.DataRepository) {
|
||||||
routeHandlers := handlers.NewHandlers(repository, s.config)
|
routeHandlers := handlers.NewHandlers(repository, s.config)
|
||||||
|
|
||||||
|
ginMux.Lock()
|
||||||
gin.DefaultWriter = logger.InfoWriter()
|
gin.DefaultWriter = logger.InfoWriter()
|
||||||
gin.DefaultErrorWriter = logger.ErrorWriter()
|
gin.DefaultErrorWriter = logger.ErrorWriter()
|
||||||
|
|
||||||
if s.config.LogLevel != "debug" {
|
if s.config.LogLevel != "debug" {
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
}
|
}
|
||||||
|
ginMux.Unlock()
|
||||||
|
|
||||||
router := gin.Default(func(e *gin.Engine) {
|
router := gin.Default(func(e *gin.Engine) {
|
||||||
e.RedirectTrailingSlash = false
|
e.RedirectTrailingSlash = false
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LogLevelType int
|
type LogLevelType int
|
||||||
@ -21,67 +22,68 @@ type LogWriter struct {
|
|||||||
WriterLevel LogLevelType
|
WriterLevel LogLevelType
|
||||||
}
|
}
|
||||||
|
|
||||||
var LogLevel = LogLevelInfo
|
var logLevelMutex sync.RWMutex
|
||||||
|
var logLevel = LogLevelInfo
|
||||||
|
|
||||||
var DebugLogger = log.New(os.Stdout, "", log.Ldate|log.Ltime)
|
var DebugLogger = log.New(os.Stdout, "", log.Ldate|log.Ltime)
|
||||||
var InfoLogger = log.New(os.Stdout, "", log.Ldate|log.Ltime)
|
var InfoLogger = log.New(os.Stdout, "", log.Ldate|log.Ltime)
|
||||||
var ErrorLogger = log.New(os.Stderr, "", log.Ldate|log.Ltime)
|
var ErrorLogger = log.New(os.Stderr, "", log.Ldate|log.Ltime)
|
||||||
|
|
||||||
func DebugLn(v ...any) {
|
func DebugLn(v ...any) {
|
||||||
if LogLevel <= LogLevelDebug {
|
if GetLogLevel() <= LogLevelDebug {
|
||||||
prefix := getCallerPrefix()
|
prefix := getCallerPrefix()
|
||||||
DebugLogger.Println(append([]interface{}{prefix}, v...)...)
|
DebugLogger.Println(append([]interface{}{prefix}, v...)...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Debug(v ...any) {
|
func Debug(v ...any) {
|
||||||
if LogLevel <= LogLevelDebug {
|
if GetLogLevel() <= LogLevelDebug {
|
||||||
prefix := getCallerPrefix()
|
prefix := getCallerPrefix()
|
||||||
DebugLogger.Println(append([]interface{}{prefix}, v...)...)
|
DebugLogger.Println(append([]interface{}{prefix}, v...)...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Debugf(format string, v ...any) {
|
func Debugf(format string, v ...any) {
|
||||||
if LogLevel <= LogLevelDebug {
|
if GetLogLevel() <= LogLevelDebug {
|
||||||
prefix := getCallerPrefix()
|
prefix := getCallerPrefix()
|
||||||
DebugLogger.Printf(prefix+format, v...)
|
DebugLogger.Printf(prefix+format, v...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func InfoLn(v ...any) {
|
func InfoLn(v ...any) {
|
||||||
if LogLevel <= LogLevelInfo {
|
if GetLogLevel() <= LogLevelInfo {
|
||||||
InfoLogger.Println(v...)
|
InfoLogger.Println(v...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Info(v ...any) {
|
func Info(v ...any) {
|
||||||
if LogLevel <= LogLevelInfo {
|
if GetLogLevel() <= LogLevelInfo {
|
||||||
InfoLogger.Print(v...)
|
InfoLogger.Print(v...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Infof(format string, v ...any) {
|
func Infof(format string, v ...any) {
|
||||||
if LogLevel <= LogLevelInfo {
|
if GetLogLevel() <= LogLevelInfo {
|
||||||
InfoLogger.Printf(format, v...)
|
InfoLogger.Printf(format, v...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ErrorLn(v ...any) {
|
func ErrorLn(v ...any) {
|
||||||
if LogLevel <= LogLevelError {
|
if GetLogLevel() <= LogLevelError {
|
||||||
prefix := getCallerPrefix()
|
prefix := getCallerPrefix()
|
||||||
ErrorLogger.Println(append([]interface{}{prefix}, v...)...)
|
ErrorLogger.Println(append([]interface{}{prefix}, v...)...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Error(v ...any) {
|
func Error(v ...any) {
|
||||||
if LogLevel <= LogLevelError {
|
if GetLogLevel() <= LogLevelError {
|
||||||
prefix := getCallerPrefix()
|
prefix := getCallerPrefix()
|
||||||
ErrorLogger.Print(append([]interface{}{prefix}, v...)...)
|
ErrorLogger.Print(append([]interface{}{prefix}, v...)...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Errorf(format string, v ...any) {
|
func Errorf(format string, v ...any) {
|
||||||
if LogLevel <= LogLevelError {
|
if GetLogLevel() <= LogLevelError {
|
||||||
prefix := getCallerPrefix()
|
prefix := getCallerPrefix()
|
||||||
ErrorLogger.Printf(prefix+format, v...)
|
ErrorLogger.Printf(prefix+format, v...)
|
||||||
}
|
}
|
||||||
@ -112,11 +114,25 @@ func DebugWriter() *LogWriter {
|
|||||||
return &LogWriter{WriterLevel: LogLevelDebug}
|
return &LogWriter{WriterLevel: LogLevelDebug}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SetLogLevel(level LogLevelType) {
|
||||||
|
logLevelMutex.Lock()
|
||||||
|
defer logLevelMutex.Unlock()
|
||||||
|
logLevel = level
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetLogLevel() LogLevelType {
|
||||||
|
logLevelMutex.RLock()
|
||||||
|
defer logLevelMutex.RUnlock()
|
||||||
|
return logLevel
|
||||||
|
}
|
||||||
|
|
||||||
func getCallerPrefix() string {
|
func getCallerPrefix() string {
|
||||||
_, file, line, ok := runtime.Caller(2)
|
_, file, line, ok := runtime.Caller(2)
|
||||||
if ok {
|
if ok {
|
||||||
parts := strings.Split(file, "/")
|
parts := strings.Split(file, "/")
|
||||||
file = parts[len(parts)-1]
|
if len(parts) > 0 {
|
||||||
|
file = parts[len(parts)-1]
|
||||||
|
}
|
||||||
return fmt.Sprintf("%s:%d - ", file, line)
|
return fmt.Sprintf("%s:%d - ", file, line)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,8 +46,8 @@ func (r *DataRepository) LoadStateFS(filePath string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *DataRepository) LoadStateJSON(jsonData string) error {
|
func (r *DataRepository) LoadStateJSON(jsonData string) error {
|
||||||
r.storeState.RLock()
|
r.storeState.Lock()
|
||||||
defer r.storeState.RUnlock()
|
defer r.storeState.Unlock()
|
||||||
|
|
||||||
var state repositorymodels.State
|
var state repositorymodels.State
|
||||||
if err := json.Unmarshal([]byte(jsonData), &state); err != nil {
|
if err := json.Unmarshal([]byte(jsonData), &state); err != nil {
|
||||||
|
@ -47,7 +47,10 @@ func GetCollection(serverName *C.char, databaseId *C.char, collectionId *C.char)
|
|||||||
return C.CString("")
|
return C.CString("")
|
||||||
}
|
}
|
||||||
|
|
||||||
collectionJson, _ := json.Marshal(collection)
|
collectionJson, err := json.Marshal(collection)
|
||||||
|
if err != nil {
|
||||||
|
return C.CString("")
|
||||||
|
}
|
||||||
return C.CString(string(collectionJson))
|
return C.CString(string(collectionJson))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +70,10 @@ func GetAllCollections(serverName *C.char, databaseId *C.char) *C.char {
|
|||||||
return C.CString("")
|
return C.CString("")
|
||||||
}
|
}
|
||||||
|
|
||||||
collectionsJson, _ := json.Marshal(collections)
|
collectionsJson, err := json.Marshal(collections)
|
||||||
|
if err != nil {
|
||||||
|
return C.CString("")
|
||||||
|
}
|
||||||
return C.CString(string(collectionsJson))
|
return C.CString(string(collectionsJson))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,10 @@ func GetDatabase(serverName *C.char, databaseId *C.char) *C.char {
|
|||||||
return C.CString("")
|
return C.CString("")
|
||||||
}
|
}
|
||||||
|
|
||||||
databaseJson, _ := json.Marshal(database)
|
databaseJson, err := json.Marshal(database)
|
||||||
|
if err != nil {
|
||||||
|
return C.CString("")
|
||||||
|
}
|
||||||
return C.CString(string(databaseJson))
|
return C.CString(string(databaseJson))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,7 +49,10 @@ func GetDocument(serverName *C.char, databaseId *C.char, collectionId *C.char, d
|
|||||||
return C.CString("")
|
return C.CString("")
|
||||||
}
|
}
|
||||||
|
|
||||||
documentJson, _ := json.Marshal(document)
|
documentJson, err := json.Marshal(document)
|
||||||
|
if err != nil {
|
||||||
|
return C.CString("")
|
||||||
|
}
|
||||||
return C.CString(string(documentJson))
|
return C.CString(string(documentJson))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,7 +73,10 @@ func GetAllDocuments(serverName *C.char, databaseId *C.char, collectionId *C.cha
|
|||||||
return C.CString("")
|
return C.CString("")
|
||||||
}
|
}
|
||||||
|
|
||||||
documentsJson, _ := json.Marshal(documents)
|
documentsJson, err := json.Marshal(documents)
|
||||||
|
if err != nil {
|
||||||
|
return C.CString("")
|
||||||
|
}
|
||||||
return C.CString(string(documentsJson))
|
return C.CString(string(documentsJson))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ type ServerInstance struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var serverInstances map[string]*ServerInstance
|
var serverInstances map[string]*ServerInstance
|
||||||
var mutex sync.RWMutex
|
var mutex sync.Mutex
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ResponseSuccess = 0
|
ResponseSuccess = 0
|
||||||
@ -32,8 +32,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func getInstance(serverName string) (*ServerInstance, bool) {
|
func getInstance(serverName string) (*ServerInstance, bool) {
|
||||||
mutex.RLock()
|
mutex.Lock()
|
||||||
defer mutex.RUnlock()
|
defer mutex.Unlock()
|
||||||
|
|
||||||
if serverInstances == nil {
|
if serverInstances == nil {
|
||||||
serverInstances = make(map[string]*ServerInstance)
|
serverInstances = make(map[string]*ServerInstance)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user