Skip to content

Commit

Permalink
Merge pull request #3 from AstroVerse-Studios/Rust-Migration
Browse files Browse the repository at this point in the history
Updated Rust codebase
  • Loading branch information
tristanpoland authored May 11, 2024
2 parents 39f3912 + 86327e4 commit bd262a8
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 70 deletions.
18 changes: 18 additions & 0 deletions BuntDB/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module horizon/buntdb

go 1.18

require (
github.com/gofrs/uuid v4.0.0+incompatible // indirect
github.com/gomodule/redigo v1.8.4 // indirect
github.com/googollee/go-socket.io v1.7.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/tidwall/btree v1.4.2 // indirect
github.com/tidwall/buntdb v1.3.0 // indirect
github.com/tidwall/gjson v1.14.3 // indirect
github.com/tidwall/grect v0.1.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tidwall/rtred v0.1.2 // indirect
github.com/tidwall/tinyqueue v0.1.1 // indirect
)
33 changes: 33 additions & 0 deletions BuntDB/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gomodule/redigo v1.8.4 h1:Z5JUg94HMTR1XpwBaSH4vq3+PNSIykBLxMdglbw10gg=
github.com/gomodule/redigo v1.8.4/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0=
github.com/googollee/go-socket.io v1.7.0 h1:ODcQSAvVIPvKozXtUGuJDV3pLwdpBLDs1Uoq/QHIlY8=
github.com/googollee/go-socket.io v1.7.0/go.mod h1:0vGP8/dXR9SZUMMD4+xxaGo/lohOw3YWMh2WRiWeKxg=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/tidwall/btree v1.4.2 h1:PpkaieETJMUxYNADsjgtNRcERX7mGc/GP2zp/r5FM3g=
github.com/tidwall/btree v1.4.2/go.mod h1:LGm8L/DZjPLmeWGjv5kFrY8dL4uVhMmzmmLYmsObdKE=
github.com/tidwall/buntdb v1.3.0 h1:gdhWO+/YwoB2qZMeAU9JcWWsHSYU3OvcieYgFRS0zwA=
github.com/tidwall/buntdb v1.3.0/go.mod h1:lZZrZUWzlyDJKlLQ6DKAy53LnG7m5kHyrEHvvcDmBpU=
github.com/tidwall/gjson v1.12.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.14.3 h1:9jvXn7olKEHU1S9vwoMGliaT8jq1vJ7IH/n9zD9Dnlw=
github.com/tidwall/gjson v1.14.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/grect v0.1.4 h1:dA3oIgNgWdSspFzn1kS4S/RDpZFLrIxAZOdJKjYapOg=
github.com/tidwall/grect v0.1.4/go.mod h1:9FBsaYRaR0Tcy4UwefBX/UDcDcDy9V5jUcxHzv2jd5Q=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/rtred v0.1.2 h1:exmoQtOLvDoO8ud++6LwVsAMTu0KPzLTUrMln8u1yu8=
github.com/tidwall/rtred v0.1.2/go.mod h1:hd69WNXQ5RP9vHd7dqekAz+RIdtfBogmglkZSRxCHFQ=
github.com/tidwall/tinyqueue v0.1.1 h1:SpNEvEggbpyN5DIReaJ2/1ndroY8iyEGxPYxoSaymYE=
github.com/tidwall/tinyqueue v0.1.1/go.mod h1:O/QNHwrnjqr6IHItYrzoHAKYhBkLI67Q096fQP5zMYw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
69 changes: 69 additions & 0 deletions BuntDB/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"fmt"
"log"
"net"
"net/http"
"time"

"github.com/googollee/go-socket.io"
"github.com/tidwall/buntdb"
)

func main() {
fmt.Println("Start server...")

// Serve a simple HTML page on root path
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Go server is up and running!")
})

// Listen on port 8000
ln, err := net.Listen("tcp", ":3001")
if err != nil {
log.Fatal(err)
}
defer ln.Close()

// Initialize a new server
server := socketio.NewServer(nil)
if err != nil {
log.Fatal(err)
} else {
fmt.Println("Server listening on port 3001")
}

// Handle '/socket.io/' endpoint
http.Handle("/socket.io/", server)
go http.Serve(ln, nil)

// Handle events
server.OnEvent("/", "update", func(s socketio.Conn, txData string) {
fmt.Println("Received update request:", txData)

// Open the database (use in-memory for this example)
db, err := buntdb.Open(":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()

start := time.Now()
err = db.Update(func(tx *buntdb.Tx) error {
// Perform transaction here based on txData
return nil
})
if err != nil {
log.Println("Error in update transaction:", err)
return
}
elapsed := time.Since(start)

// Emit performance measurement
s.Emit("updateResult", fmt.Sprintf("Update transaction took: %s", elapsed))
})

// Run forever
select {}
}
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ socketioxide = "0.13.0"
tokio = { version = "1.37.0", features = ["rt", "net", "rt-multi-thread"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
rust_socketio = { version = "*", features = ["async"] }
openssl = "0.10.64"
openssl-sys = "0.9.102"

[target.x86_64-unknown-linux-musl.dependencies]
16 changes: 16 additions & 0 deletions Dockerfile.golang
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Use the official GoLang image
FROM golang:latest
USER root

# Set the working directory inside the container
WORKDIR /

# Copy the source code into the container
COPY ./ /go

# Build the GoLang application
# RUN go build -o horizon-memory-db .

# Specify the command to run the GoLang application
#CMD ["go","run","/go/BuntBD/."]
CMD ["sleep", "infinity"]
12 changes: 12 additions & 0 deletions Dockerfile.python
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Use the official Python image
FROM python:latest

# Set the working directory inside the container
WORKDIR /app

# Copy the source code into the container
COPY . .

# Specify the command to run the Python script
CMD ["sleep", "infinity"]
# CMD ["python3", "python_socket_server.py"]
3 changes: 2 additions & 1 deletion Dockerfile → Dockerfile.rust
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ ARG APP_NAME
WORKDIR /app

# Install host build dependencies.
RUN apk add --no-cache clang lld musl-dev git python3
RUN apk add libressl-dev
RUN apk add --no-cache clang lld musl-dev git pkgconfig

# Build the application.
# Leverage a cache mount to /usr/local/cargo/registry/
Expand Down
Empty file added Python-Daemon/Daemon.py
Empty file.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
# Horizon
An easily scalable game server

```RUST
use tracing::info;
Helpes append .route to a specific **mutable** variable so you don't have to.

The first parameter is the variable itself; the value of `axum::Router::new()`.
The second value is the endpoint, eg "/".
The third value is what to return back.

# Example

```rust
let mut app = axum::Router::new()
.layer(layer);

define_routes!(app,
"/", "Hello, World!",
"/goodbye", "Goodbye, World!",
"/function", function());
```
60 changes: 17 additions & 43 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -1,50 +1,24 @@
# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Docker Compose reference guide at
# https://docs.docker.com/go/compose-spec-reference/

# Here the instructions define your application as a service called "server".
# This service is built from the Dockerfile in the current directory.
# You can add other services your application may depend on here, such as a
# database or a cache. For examples, see the Awesome Compose repository:
# https://github.com/docker/awesome-compose
version: '3'
services:
server:
horizon-game-server:
build:
context: .
target: final
dockerfile: Dockerfile.rust
ports:
- 3000:3000

# The commented out section below is an example of how to define a PostgreSQL
# database that your application can use. `depends_on` tells Docker Compose to
# start the database before your application. The `db-data` volume persists the
# database data between container restarts. The `db-password` secret is used
# to set the database password. You must create `db/password.txt` and add
# a password of your choosing to it before running `docker compose up`.
# depends_on:
# db:
# condition: service_healthy
# db:
# image: postgres
# restart: always
# user: postgres
# secrets:
# - db-password
# volumes:
# - db-data:/var/lib/postgresql/data
# environment:
# - POSTGRES_DB=example
# - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
# expose:
# - 5432
# healthcheck:
# test: [ "CMD", "pg_isready" ]
# interval: 10s
# timeout: 5s
# retries: 5
# volumes:
# db-data:
# secrets:
# db-password:
# file: db/password.txt
horizon-memory-db:
build:
context: .
dockerfile: Dockerfile.golang
restart: always
depends_on:
- horizon-game-server

horizon-physics-engine:
build:
context: .
dockerfile: Dockerfile.python
restart: always
depends_on:
- horizon-memory-db
32 changes: 32 additions & 0 deletions installers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
##########################################################################
## /$$ /$$ /$$ ##
## | $$ | $$ |__/ ##
## | $$ | $$ /$$$$$$ /$$$$$$ /$$ /$$$$$$$$ /$$$$$$ /$$$$$$$ ##
## | $$$$$$$$ /$$__ $$ /$$__ $$| $$|____ /$$/ /$$__ $$| $$__ $$ ##
## | $$__ $$| $$ \ $$| $$ \__/| $$ /$$$$/ | $$ \ $$| $$ \ $$ ##
## | $$ | $$| $$ | $$| $$ | $$ /$$__/ | $$ | $$| $$ | $$ ##
## | $$ | $$| $$$$$$/| $$ | $$ /$$$$$$$$| $$$$$$/| $$ | $$ ##
## |__/ |__/ \______/ |__/ |__/|________/ \______/ |__/ |__/ ##
##########################################################################


#! /bin/bash

##################
# Install Python #
##################

apk add python3

##################
# Install Git #
##################

apk add git

##################
# Install GoLang #
##################

# Check GoLang version
go version
Loading

0 comments on commit bd262a8

Please sign in to comment.