Skip to content

Commit

Permalink
Merge pull request #8 from Lumerin-protocol/main
Browse files Browse the repository at this point in the history
Add
  • Loading branch information
rcondron authored Apr 5, 2024
2 parents 2aa70d4 + 537175c commit 12bbc44
Show file tree
Hide file tree
Showing 72 changed files with 10,503 additions and 755 deletions.
13 changes: 13 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: '3.8'
services:
ollama:
image: ollama/ollama
container_name: ollama
volumes:
- ollama:/root/.ollama
ports:
- "11434:11434"
restart: unless-stopped

volumes:
ollama:
13 changes: 11 additions & 2 deletions proxy-router/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ import (
"syscall"
"time"

"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/aiengine"
"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/apibus"
"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/config"
"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/handlers/httphandlers"
"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/handlers/tcphandlers"
"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/interfaces"
"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/lib"
"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/proxyapi"
"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/repositories/transport"
"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/rpcproxy"
"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/system"
"github.com/ethereum/go-ethereum/ethclient"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -186,8 +190,13 @@ func start() error {
)
tcpServer.SetConnectionHandler(tcpHandler)

handl := httphandlers.NewHTTPHandler(sysConfig, publicUrl, &cfg, derived, time.Now(), contractLogStorage, log)
httpServer := transport.NewServer(cfg.Web.Address, handl, log.Named("HTP"))
proxyRouterApi := proxyapi.NewProxyRouterApi(sysConfig, publicUrl, cfg.Marketplace.WalletPrivateKey, &cfg, derived, time.Now(), contractLogStorage, log)
rpcProxy := rpcproxy.NewRpcProxy(ethClient)
aiEngine := aiengine.NewAiEngine()
apiBus := apibus.NewApiBus(rpcProxy, aiEngine, proxyRouterApi)

handl := httphandlers.NewHTTPHandler(apiBus)
httpServer := transport.NewServer(cfg.Web.Address, handl, log.Named("HTTP"))

ctx, cancel = context.WithCancel(ctx)
g, errCtx := errgroup.WithContext(ctx)
Expand Down
14 changes: 14 additions & 0 deletions proxy-router/internal/aiengine/ai_engine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package aiengine

import "context"

type AiEngine struct {
}

func NewAiEngine() *AiEngine {
return &AiEngine{}
}

func (aiEngine *AiEngine) Prompt(ctx context.Context) (string, error) {
return "Hello!", nil
}
47 changes: 47 additions & 0 deletions proxy-router/internal/apibus/api_bus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package apibus

import (
"context"

"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/aiengine"
"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/proxyapi"
"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/rpcproxy"
"github.com/gin-gonic/gin"
)

type ApiBus struct {
rpcProxy *rpcproxy.RpcProxy
aiEngine *aiengine.AiEngine
proxyRouterApi *proxyapi.ProxyRouterApi
}

func NewApiBus(rpcProxy *rpcproxy.RpcProxy, aiEngine *aiengine.AiEngine, proxyRouterApi *proxyapi.ProxyRouterApi) *ApiBus {
return &ApiBus{
rpcProxy: rpcProxy,
aiEngine: aiEngine,
proxyRouterApi: proxyRouterApi,
}
}

// Proxy Router Api
func (apiBus *ApiBus) GetConfig(ctx context.Context) interface{} {
return apiBus.proxyRouterApi.GetConfig(ctx)
}

func (apiBus *ApiBus) GetFiles(ctx *gin.Context) (int, interface{}) {
return apiBus.proxyRouterApi.GetFiles(ctx)
}

func (apiBus *ApiBus) HealthCheck(ctx context.Context) interface{} {
return apiBus.proxyRouterApi.HealthCheck(ctx)
}

// AiEngine
func (apiBus *ApiBus) Prompt(ctx context.Context) (string, error) {
return apiBus.aiEngine.Prompt(ctx)
}

// RpcProxy
func (apiBus *ApiBus) GetLatestBlock(ctx context.Context) (uint64, error) {
return apiBus.rpcProxy.GetLatestBlock(ctx)
}
15 changes: 0 additions & 15 deletions proxy-router/internal/handlers/httphandlers/config.go

This file was deleted.

115 changes: 13 additions & 102 deletions proxy-router/internal/handlers/httphandlers/http.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,28 @@
package httphandlers

import (
"context"
"encoding/json"
"fmt"
"io"
"net/url"
"os"
"time"

"net/http/pprof"

"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/config"
"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/interfaces"
"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/lib"
"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/system"
"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/apibus"
"github.com/gin-gonic/gin"
)

type Proxy interface {
SetDest(ctx context.Context, newDestURL *url.URL, onSubmit func(diff float64)) error
}

type Sanitizable interface {
GetSanitized() any
}

type HTTPHandler struct {
sysConfig *system.SystemConfigurator
publicUrl *url.URL
pubKey string
config Sanitizable
derivedConfig *config.DerivedConfig
appStartTime time.Time
logStorage *lib.Collection[*interfaces.LogStorage]
log interfaces.ILogger
}

func NewHTTPHandler(sysConfig *system.SystemConfigurator, publicUrl *url.URL, config Sanitizable, derivedConfig *config.DerivedConfig, appStartTime time.Time, logStorage *lib.Collection[*interfaces.LogStorage], log interfaces.ILogger) *gin.Engine {
handl := &HTTPHandler{
sysConfig: sysConfig,
publicUrl: publicUrl,
config: config,
derivedConfig: derivedConfig,
appStartTime: appStartTime,
logStorage: logStorage,
log: log,
}
type HTTPHandler struct{}

func NewHTTPHandler(apiBus *apibus.ApiBus) *gin.Engine {
gin.SetMode(gin.ReleaseMode)
r := gin.New()

r.GET("/healthcheck", handl.HealthCheck)
r.GET("/config", handl.GetConfig)
r.GET("/files", handl.GetFiles)
r.GET("/healthcheck", (func(ctx *gin.Context) {
ctx.JSON(200, apiBus.HealthCheck(ctx))
}))
r.GET("/config", (func(ctx *gin.Context) {
ctx.JSON(200, apiBus.GetConfig(ctx))
}))
r.GET("/files", (func(ctx *gin.Context) {
status, files := apiBus.GetFiles(ctx)
ctx.JSON(status, files)
}))

r.Any("/debug/pprof/*action", gin.WrapF(pprof.Index))

Expand All @@ -64,61 +33,3 @@ func NewHTTPHandler(sysConfig *system.SystemConfigurator, publicUrl *url.URL, co

return r
}

func (h *HTTPHandler) HealthCheck(ctx *gin.Context) {
ctx.JSON(200, gin.H{
"status": "healthy",
"version": config.BuildVersion,
"uptime": time.Since(h.appStartTime).Round(time.Second).String(),
})
}

func (h *HTTPHandler) GetFiles(ctx *gin.Context) {
files, err := h.sysConfig.GetFileDescriptors(ctx, os.Getpid())
if err != nil {
ctx.JSON(500, gin.H{"error": err.Error()})
return
}
ctx.Status(200)

systemCfg, err := h.sysConfig.GetConfig()
if err != nil {
fmt.Fprintf(ctx.Writer, "failed to get system config: %s\n", err)
} else {
json, err := json.Marshal(systemCfg)
if err != nil {
fmt.Fprintf(ctx.Writer, "failed to marshal system config: %s\n", err)
} else {
fmt.Fprintf(ctx.Writer, "system config: %s\n", json)
}
}

fmt.Fprintf(ctx.Writer, "\n")

err = writeFiles(ctx.Writer, files)
if err != nil {
h.log.Errorf("failed to write files: %s", err)
_ = ctx.Error(err)
ctx.Abort()
}
}

func writeFiles(writer io.Writer, files []system.FD) error {
text := fmt.Sprintf("Total: %d\n", len(files))
text += "\n"
text += "fd\tpath\n"

_, err := fmt.Fprintf(writer, text)
if err != nil {
return err
}

for _, f := range files {
_, err := fmt.Fprintf(writer, "%s\t%s\n", f.ID, f.Path)
if err != nil {
return err
}
}

return nil
}
8 changes: 0 additions & 8 deletions proxy-router/internal/handlers/httphandlers/schemas.go

This file was deleted.

118 changes: 118 additions & 0 deletions proxy-router/internal/proxyapi/proxy_router_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package proxyapi

import (
"context"
"encoding/json"
"fmt"
"io"
"net/url"
"os"
"time"

"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/config"
"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/interfaces"
"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/lib"
"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/internal/system"
"github.com/gin-gonic/gin"
)

type Sanitizable interface {
GetSanitized() any
}

type ConfigResponse struct {
Version string
Commit string
DerivedConfig interface{}
Config interface{}
}

type ProxyRouterApi struct {
sysConfig *system.SystemConfigurator
publicUrl *url.URL
pubKey string
config Sanitizable
derivedConfig *config.DerivedConfig
appStartTime time.Time
logStorage *lib.Collection[*interfaces.LogStorage]
log interfaces.ILogger
}

func NewProxyRouterApi(sysConfig *system.SystemConfigurator, publicUrl *url.URL, pubKey string, config Sanitizable, derivedConfig *config.DerivedConfig, appStartTime time.Time, logStorage *lib.Collection[*interfaces.LogStorage], log interfaces.ILogger) *ProxyRouterApi {
return &ProxyRouterApi{
sysConfig: sysConfig,
publicUrl: publicUrl,
pubKey: pubKey,
config: config,
derivedConfig: derivedConfig,
appStartTime: appStartTime,
logStorage: logStorage,
log: log,
}
}

func (p *ProxyRouterApi) GetConfig(ctx context.Context) ConfigResponse {
return ConfigResponse{
Version: config.BuildVersion,
Commit: config.Commit,
Config: p.config.GetSanitized(),
DerivedConfig: p.derivedConfig,
}
}

func (p *ProxyRouterApi) HealthCheck(ctx context.Context) gin.H {
return gin.H{
"status": "healthy",
"version": config.BuildVersion,
"uptime": time.Since(p.appStartTime).Round(time.Second).String(),
}
}

func (p *ProxyRouterApi) GetFiles(ctx *gin.Context) (int, gin.H) {
files, err := p.sysConfig.GetFileDescriptors(ctx, os.Getpid())
if err != nil {
return 500, gin.H{"error": err.Error()}
}

systemCfg, err := p.sysConfig.GetConfig()
if err != nil {
fmt.Fprintf(ctx.Writer, "failed to get system config: %s\n", err)
} else {
json, err := json.Marshal(systemCfg)
if err != nil {
fmt.Fprintf(ctx.Writer, "failed to marshal system config: %s\n", err)
} else {
fmt.Fprintf(ctx.Writer, "system config: %s\n", json)
}
}

fmt.Fprintf(ctx.Writer, "\n")

err = writeFiles(ctx.Writer, files)
if err != nil {
p.log.Errorf("failed to write files: %s", err)
_ = ctx.Error(err)
ctx.Abort()
}
return 200, gin.H{}
}

func writeFiles(writer io.Writer, files []system.FD) error {
text := fmt.Sprintf("Total: %d\n", len(files))
text += "\n"
text += "fd\tpath\n"

_, err := fmt.Fprintf(writer, text)
if err != nil {
return err
}

for _, f := range files {
_, err := fmt.Fprintf(writer, "%s\t%s\n", f.ID, f.Path)
if err != nil {
return err
}
}

return nil
}
Loading

0 comments on commit 12bbc44

Please sign in to comment.