-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
434 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,76 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
|
||
"github.com/devmangel/Yasmine/internal/core/cluster" | ||
"github.com/devmangel/Yasmine/internal/core/commands" | ||
"github.com/devmangel/Yasmine/internal/core/data_structures" | ||
"github.com/devmangel/Yasmine/internal/core/lua" | ||
"github.com/devmangel/Yasmine/internal/core/networking" | ||
"github.com/devmangel/Yasmine/internal/core/persistence" | ||
"github.com/devmangel/Yasmine/internal/core/pubsub" | ||
"github.com/devmangel/Yasmine/internal/core/replication" | ||
"github.com/devmangel/Yasmine/internal/core/security" | ||
) | ||
|
||
func main() { | ||
// Configuración básica de estructuras de datos | ||
hashTable := data_structures.NewHashTable() | ||
list := data_structures.NewList() | ||
set := data_structures.NewSet() | ||
sortedSet := data_structures.NewSortedSet(func(a, b interface{}) bool { | ||
return a.(float64) < b.(float64) | ||
}) | ||
|
||
// Configuración de seguridad | ||
authenticator := security.NewAuthenticator() | ||
authorizer := security.NewAuthorizer() | ||
tlsManager, err := security.NewTLSManager("certs/server.crt", "certs/server.key", "certs/ca.crt") | ||
if err != nil { | ||
log.Fatalf("Error configurando TLS: %v", err) | ||
} | ||
|
||
// Configuración de persistencia | ||
rdbPersistence := persistence.NewRDBPersistence("data/yasmine.rdb") | ||
aofPersistence := persistence.NewAOFPersistence("data/yasmine.aof") | ||
persistenceManager := persistence.NewPersistenceManager(rdbPersistence, aofPersistence) | ||
|
||
// Configuración de replicación | ||
masterNode := replication.NewMasterNode(hashTable) | ||
slaveNode := replication.NewSlaveNode() | ||
replicationManager := replication.NewReplicationManager(masterNode, []*replication.SlaveNode{slaveNode}) | ||
|
||
// Configuración de Pub/Sub | ||
pubSubManager := pubsub.NewPubSubManager() | ||
|
||
// Configuración del clúster | ||
clusterManager := cluster.NewClusterManager() | ||
node := cluster.NewNode("node-1", "localhost") | ||
clusterManager.AddNode(node) | ||
|
||
// Configuración de Lua scripting | ||
scriptManager := lua.NewScriptManager() | ||
|
||
// Configuración de comandos | ||
setStringCommand := commands.NewSetStringCommand(hashTable) | ||
getStringCommand := commands.NewGetStringCommand(hashTable) | ||
lPushCommand := commands.NewLPushCommand(list) | ||
rPushCommand := commands.NewRPushCommand(list) | ||
sAddCommand := commands.NewSAddCommand(set) | ||
zAddCommand := commands.NewZAddCommand(sortedSet) | ||
|
||
// Configuración de networking | ||
httpsConfig := tlsManager.GetTLSConfig() | ||
handler := http.NewServeMux() | ||
handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte("Welcome to Yasmine!")) | ||
}) | ||
|
||
go networking.RedirectToHTTPS() | ||
networking.StartServer(httpsConfig, handler) | ||
|
||
// Iniciar el servidor y otros componentes | ||
log.Println("Yasmine server started successfully.") | ||
} |
This file contains 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,27 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/devmangel/Yasmine/internal/core/commands" | ||
"github.com/devmangel/Yasmine/internal/core/data_structures" | ||
) | ||
|
||
func main() { | ||
// Crear una nueva tabla hash | ||
ht := data_structures.NewHashTable() | ||
|
||
// Crear los comandos SET y GET | ||
setCmd := commands.NewSetStringCommand(ht) | ||
getCmd := commands.NewGetStringCommand(ht) | ||
|
||
// Establecer un valor en la tabla hash | ||
setCmd.Execute("key1", "value1") | ||
|
||
// Recuperar el valor de la tabla hash | ||
value, _ := getCmd.Execute("key1") | ||
fmt.Printf("GET key1: %s\n", value) | ||
|
||
// Mostrar el valor esperado | ||
// Output: GET key1: value1 | ||
} |
This file contains 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,29 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/devmangel/Yasmine/internal/core/data_structures" | ||
"github.com/devmangel/Yasmine/internal/core/persistence" | ||
) | ||
|
||
func main() { | ||
// Crear una nueva tabla hash y establecer un valor | ||
ht := data_structures.NewHashTable() | ||
ht.Set("key1", "value1") | ||
|
||
// Configurar la persistencia RDB | ||
rdbPersistence := persistence.NewRDBPersistence("data/yasmine_example.rdb") | ||
|
||
// Guardar los datos en formato RDB | ||
rdbPersistence.Save(ht) | ||
|
||
// Cargar los datos desde el archivo RDB | ||
loadedHT, _ := rdbPersistence.Load() | ||
|
||
// Mostrar el valor recuperado | ||
value, _ := loadedHT.Get("key1") | ||
fmt.Printf("Loaded key1 from RDB: %s\n", value) | ||
|
||
// Output: Loaded key1 from RDB: value1 | ||
} |
This file contains 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,27 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/devmangel/Yasmine/internal/core/pubsub" | ||
) | ||
|
||
func main() { | ||
// Configurar el gestor de Pub/Sub | ||
pubSubManager := pubsub.NewPubSubManager() | ||
|
||
// Crear un nuevo canal | ||
channel := pubSubManager.CreateChannel("example_channel") | ||
|
||
// Crear un suscriptor con un callback | ||
subscriber := pubsub.NewSubscriber("sub1", func(message interface{}) { | ||
fmt.Printf("Received message: %v\n", message) | ||
}) | ||
|
||
// Suscribir al suscriptor al canal | ||
pubSubManager.Subscribe("example_channel", subscriber) | ||
|
||
// Publicar un mensaje en el canal | ||
pubSubManager.Publish("example_channel", "Hello, Pub/Sub!") | ||
|
||
// Output: Received message: Hello, Pub/Sub! | ||
} |
This file contains 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,32 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/devmangel/Yasmine/internal/core/data_structures" | ||
"github.com/devmangel/Yasmine/internal/core/replication" | ||
) | ||
|
||
func main() { | ||
// Crear la tabla hash para el nodo maestro | ||
masterHT := data_structures.NewHashTable() | ||
masterNode := replication.NewMasterNode(masterHT) | ||
|
||
// Crear un nodo esclavo | ||
slaveNode := replication.NewSlaveNode() | ||
|
||
// Configurar el gestor de replicación | ||
replicationManager := replication.NewReplicationManager(masterNode, []*replication.SlaveNode{slaveNode}) | ||
|
||
// Establecer un valor en el nodo maestro | ||
masterHT.Set("key1", "value1") | ||
|
||
// Sincronizar los datos del maestro al esclavo | ||
replicationManager.SyncToSlaves(masterHT) | ||
|
||
// Recuperar el valor del nodo esclavo | ||
value, _ := slaveNode.GetData().Get("key1") | ||
fmt.Printf("Replicated key1: %s\n", value) | ||
|
||
// Output: Replicated key1: value1 | ||
} |
This file contains 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,32 @@ | ||
package lua | ||
|
||
import ( | ||
"github.com/yuin/gopher-lua" | ||
"log" | ||
) | ||
|
||
// LuaEngine es responsable de ejecutar scripts Lua. | ||
type LuaEngine struct { | ||
state *lua.LState | ||
} | ||
|
||
// NewLuaEngine crea una nueva instancia de LuaEngine. | ||
func NewLuaEngine() *LuaEngine { | ||
return &LuaEngine{ | ||
state: lua.NewState(), | ||
} | ||
} | ||
|
||
// ExecuteScript ejecuta un script Lua desde una cadena de texto. | ||
func (le *LuaEngine) ExecuteScript(script string) error { | ||
if err := le.state.DoString(script); err != nil { | ||
log.Printf("Error ejecutando script Lua: %v", err) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// Close cierra el estado Lua. | ||
func (le *LuaEngine) Close() { | ||
le.state.Close() | ||
} |
This file contains 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,55 @@ | ||
package lua | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// ScriptManager gestiona la ejecución y almacenamiento de scripts Lua. | ||
type ScriptManager struct { | ||
engine *LuaEngine | ||
scripts map[string]string | ||
lock sync.RWMutex | ||
} | ||
|
||
// NewScriptManager crea una nueva instancia de ScriptManager. | ||
func NewScriptManager() *ScriptManager { | ||
return &ScriptManager{ | ||
engine: NewLuaEngine(), | ||
scripts: make(map[string]string), | ||
} | ||
} | ||
|
||
// AddScript añade un nuevo script al gestor. | ||
func (sm *ScriptManager) AddScript(name, script string) { | ||
sm.lock.Lock() | ||
defer sm.lock.Unlock() | ||
sm.scripts[name] = script | ||
} | ||
|
||
// ExecuteScript ejecuta un script Lua almacenado por su nombre. | ||
func (sm *ScriptManager) ExecuteScript(name string) error { | ||
sm.lock.RLock() | ||
script, exists := sm.scripts[name] | ||
sm.lock.RUnlock() | ||
|
||
if !exists { | ||
return ErrScriptNotFound | ||
} | ||
|
||
return sm.engine.ExecuteScript(script) | ||
} | ||
|
||
// RemoveScript elimina un script del gestor. | ||
func (sm *ScriptManager) RemoveScript(name string) { | ||
sm.lock.Lock() | ||
defer sm.lock.Unlock() | ||
delete(sm.scripts, name) | ||
} | ||
|
||
// Close cierra el ScriptManager y libera recursos. | ||
func (sm *ScriptManager) Close() { | ||
sm.engine.Close() | ||
} | ||
|
||
// ErrScriptNotFound es un error que se devuelve cuando un script no se encuentra. | ||
var ErrScriptNotFound = errors.New("script not found") |
This file contains 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,43 @@ | ||
package security | ||
|
||
import "errors" | ||
|
||
var ( | ||
// ErrInvalidCredentials se devuelve cuando las credenciales son inválidas. | ||
ErrInvalidCredentials = errors.New("invalid credentials") | ||
) | ||
|
||
// User representa un usuario en el sistema. | ||
type User struct { | ||
Username string | ||
Password string // En un sistema real, esto estaría hasheado. | ||
} | ||
|
||
// Authenticator maneja la autenticación de usuarios. | ||
type Authenticator struct { | ||
users map[string]*User | ||
} | ||
|
||
// NewAuthenticator crea una nueva instancia de Authenticator. | ||
func NewAuthenticator() *Authenticator { | ||
return &Authenticator{ | ||
users: make(map[string]*User), | ||
} | ||
} | ||
|
||
// AddUser añade un nuevo usuario al sistema. | ||
func (a *Authenticator) AddUser(username, password string) { | ||
a.users[username] = &User{ | ||
Username: username, | ||
Password: password, | ||
} | ||
} | ||
|
||
// Authenticate verifica si las credenciales son correctas. | ||
func (a *Authenticator) Authenticate(username, password string) (*User, error) { | ||
user, exists := a.users[username] | ||
if !exists || user.Password != password { | ||
return nil, ErrInvalidCredentials | ||
} | ||
return user, nil | ||
} |
This file contains 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,47 @@ | ||
package security | ||
|
||
import "errors" | ||
|
||
var ( | ||
// ErrUnauthorized se devuelve cuando el usuario no tiene permisos. | ||
ErrUnauthorized = errors.New("unauthorized access") | ||
) | ||
|
||
// Role representa un rol en el sistema. | ||
type Role struct { | ||
Name string | ||
Permissions []string | ||
} | ||
|
||
// Authorizer maneja la autorización de usuarios. | ||
type Authorizer struct { | ||
roles map[string]*Role | ||
} | ||
|
||
// NewAuthorizer crea una nueva instancia de Authorizer. | ||
func NewAuthorizer() *Authorizer { | ||
return &Authorizer{ | ||
roles: make(map[string]*Role), | ||
} | ||
} | ||
|
||
// AddRole añade un nuevo rol al sistema. | ||
func (az *Authorizer) AddRole(role *Role) { | ||
az.roles[role.Name] = role | ||
} | ||
|
||
// Authorize verifica si un usuario tiene permiso para realizar una acción. | ||
func (az *Authorizer) Authorize(roleName, permission string) error { | ||
role, exists := az.roles[roleName] | ||
if !exists { | ||
return ErrUnauthorized | ||
} | ||
|
||
for _, perm := range role.Permissions { | ||
if perm == permission { | ||
return nil | ||
} | ||
} | ||
|
||
return ErrUnauthorized | ||
} |
Oops, something went wrong.