Merge branch 'feature/webserverRuntime' into main

This commit is contained in:
Layla 2021-01-29 23:20:26 -05:00
commit d7808a2214
9 changed files with 430 additions and 16 deletions

6
.gitignore vendored
View File

@ -4,6 +4,8 @@
*.dll
*.so
*.dylib
bin/
.env/
# Test binary, built with `go test -c`
*.test
@ -11,6 +13,6 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
# Dependency directories
vendor/
plugins/

24
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,24 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Build & Launch",
"type": "go",
"request": "launch",
"mode": "exec",
"program": "${workspaceRoot}/bin/OpenSkins",
"preLaunchTask": "compile",
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": "Build & Launch - Standalone",
"type": "go",
"request": "launch",
"mode": "exec",
"program": "${workspaceRoot}/bin/OpenSkins",
"preLaunchTask": "standalone",
"internalConsoleOptions": "openOnSessionStart"
}
]
}

40
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,40 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "compile",
"type": "shell",
"command": "make compile"
},
{
"label": "install",
"type": "shell",
"command": "make install"
},
{
"label": "clean",
"type": "shell",
"command": "make clean"
},
{
"label": "build",
"type": "shell",
"command": "make build"
},
{
"label": "run",
"type": "shell",
"command": "make run"
},
{
"label": "test",
"type": "shell",
"command": "make test"
},
{
"label": "standalone",
"type": "shell",
"command": "make standalone"
}
]
}

94
Makefile Normal file
View File

@ -0,0 +1,94 @@
#include .env
PROJECTNAME="OpenSkins"
STANDALONE_LOCATION="../OpenSkins-Standalone-Plugin/bin/OpenSkins-Standalone-Plugin"
# Go related variables.
GOBASE=$(shell pwd)
GOPATH=$(GOBASE)/vendor:$(GOBASE)
GOBIN=$(GOBASE)/bin
GOFILES=$(wildcard *.go)
# Redirect error output to a file, so we can show it in development mode.
STDERR=/tmp/.$(PROJECTNAME)-stderr.txt
# PID file will store the server process id when it's running on development mode
PID=/tmp/.$(PROJECTNAME)-api-server.pid
# Make is verbose in Linux. Make it silent.
MAKEFLAGS += --silent
go-compile: go-clean go-get go-build
go-build:
@echo " > Building binary..."
@go build -o $(GOBIN)/$(PROJECTNAME) $(GOFILES)
go-generate:
@echo " > Generating dependency files..."
@go generate $(generate)
go-get:
@echo " > Checking if there is any missing dependencies..."
@go get $(get)
go-install:
@echo " > Running go install..."
@go install $(GOFILES)
go-clean:
@echo " > Cleaning build cache"
@go clean
go-test:
@echo " > Running tests..."
@go test
go-run:
@echo " > Running ${PROJECTNAME}"
@-(cd $(GOBIN); ./$(PROJECTNAME))
openskins-common:
@echo " > Updating common library..."
@go get -u github.com/josephbmanley/OpenSkins-Common
## install: downloads and installs dependencies
install: openskins-common go-get
## clean: Runs go clean
clean:
@(MAKEFILE) go-clean
## compile: cleans project, installs dependencies, and builds project
compile:
@-touch $(STDERR)
@-rm $(STDERR)
@-$(MAKE) -s go-compile 2> $(STDERR)
@cat $(STDERR) | sed -e '1s/.*/\nError:\n/' | sed 's/make\[.*/ /' | sed "/^/s/^/ /" 1>&2
## watch: Runs command on code update
watch:
@yolo -i . -e vendor -e bin -c $(run)
## build: Runs go build
build: go-build
## run: Compiles and executes project binary
run: go-compile go-run
## test: Run unit tests
test: go-test
## standalone: Compile project & install standalone plugin *REQUIRES BUILD IN `../OpenSkins-Standalone-Plugin/bin`*
standalone: compile
@mkdir -p ./bin/plugins
@rm -f ./bin/plugins/standalone.so
@cp $(STANDALONE_LOCATION) ./bin/plugins/standalone.so
## help: Displays help text for make commands
.DEFAULT_GOAL := help
all: help
help: Makefile
@echo " Choose a command run in "$(PROJECTNAME)":"
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'

7
go.mod
View File

@ -3,6 +3,11 @@ module github.com/josephbmanley/OpenSkins
go 1.13
require (
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227061123-22199a5c0ab9
github.com/gorilla/mux v1.8.0
github.com/josephbmanley/OpenSkins-Common v0.0.1
github.com/sirupsen/logrus v1.7.0
github.com/stretchr/objx v0.3.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

31
go.sum
View File

@ -1,20 +1,31 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201226022245-12099a28475a h1:bW/ahOovqRP2r1Ynp4WXxrOCiHq1vqye8RFyYLIZ6hg=
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201226022245-12099a28475a/go.mod h1:xmRLNQLOMLqiQ2hzP81mdsNbu6ZxyLozoVWaR2A0BMY=
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227051624-01b75eb56779 h1:ArCBRyblBk/x0BGgNu6gJ7j7kNvn91K7OmpOHpHUwAs=
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227051624-01b75eb56779/go.mod h1:xmRLNQLOMLqiQ2hzP81mdsNbu6ZxyLozoVWaR2A0BMY=
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227052902-2d6c9664ec1e h1:5nRhRlgg/IX2yp4R/NDG1ku65ldkl5/LKxZxg4UcWw4=
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227052902-2d6c9664ec1e/go.mod h1:xmRLNQLOMLqiQ2hzP81mdsNbu6ZxyLozoVWaR2A0BMY=
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227060121-852c176c7219 h1:PHEi0kL7sC2ERY34grA1bsObqXRcZzMZVDisCB4xm3Q=
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227060121-852c176c7219/go.mod h1:xmRLNQLOMLqiQ2hzP81mdsNbu6ZxyLozoVWaR2A0BMY=
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227061123-22199a5c0ab9 h1:rEfQZyiAKIKchefT4uUGojwLVPDEylORTOU1/l+VEQU=
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227061123-22199a5c0ab9/go.mod h1:xmRLNQLOMLqiQ2hzP81mdsNbu6ZxyLozoVWaR2A0BMY=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/josephbmanley/OpenSkins-Common v0.0.0-20210129235549-eea81c7820d0 h1:nrARrp3he8wkT1P+ryMjJbMxhlwjmVWLhDWvxU5iHoo=
github.com/josephbmanley/OpenSkins-Common v0.0.0-20210129235549-eea81c7820d0/go.mod h1:xmRLNQLOMLqiQ2hzP81mdsNbu6ZxyLozoVWaR2A0BMY=
github.com/josephbmanley/OpenSkins-Common v0.0.1 h1:rOvP3YMgx1XNJ9JQU76AMWkypiWKHQw4Wxr76dhsMME=
github.com/josephbmanley/OpenSkins-Common v0.0.1/go.mod h1:xmRLNQLOMLqiQ2hzP81mdsNbu6ZxyLozoVWaR2A0BMY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

15
main.go
View File

@ -3,18 +3,22 @@ package main
import (
"fmt"
"github.com/josephbmanley/OpenSkins/pluginmanager"
"github.com/josephbmanley/OpenSkins/runtime"
log "github.com/sirupsen/logrus"
"os"
"plugin"
)
var appRuntime = "webserver"
const plugindirectory = "./plugins"
func main() {
pluginFiles, err := pluginmanager.GetPlugins(plugindirectory)
if err != nil {
log.Warningln(fmt.Sprintf("Failed to read plugins directory: %v", err.Error()))
log.Fatalln(fmt.Sprintf("Failed to read plugins directory: %v", err.Error()))
os.Exit(1)
}
loadedPlugins := []*plugin.Plugin{}
@ -37,7 +41,12 @@ func main() {
os.Exit(1)
}
log.Fatalln("Runtime is currently not implemented!")
os.Exit(1)
switch appRuntime {
case "webserver":
runtime.StartWebserver(8080) // Start on port 8080
default:
log.Fatalln("Runtime is currently not implemented!")
os.Exit(1)
}
}

View File

@ -16,6 +16,7 @@ var LoadedSkinstores []datastore.Skinstore = []datastore.Skinstore{}
func LoadSkinstores(plugins []*plugin.Plugin) error {
for _, plugin := range plugins {
log.Debug("Looking for skinstore in plugin...")
symSkinstore, err := plugin.Lookup("SkinstoreModule")
if err != nil {
@ -33,6 +34,7 @@ func LoadSkinstores(plugins []*plugin.Plugin) error {
return err
}
log.Info("Loaded new skinstore!")
LoadedSkinstores = append(LoadedSkinstores, skinstore)
}
return nil

227
runtime/webserver.go Normal file
View File

@ -0,0 +1,227 @@
package runtime
import (
"bytes"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/josephbmanley/OpenSkins-Common/datastore"
"github.com/josephbmanley/OpenSkins-Common/datatypes"
"github.com/josephbmanley/OpenSkins/pluginmanager"
"io"
"log"
"net/http"
)
var activeSkinstore datastore.Skinstore
var activeUserstore datastore.Userstore
func healthCheck(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Service is healthy!")
}
func getCharacter(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(501)
return
vars := mux.Vars(r)
userID := vars["user"]
charID := vars["char"]
user, err := activeUserstore.GetUser(userID)
// Check if there was an error getting the user
if err != nil {
log.Printf("An error occured when getting a character: %v", err.Error())
w.WriteHeader(500)
return
}
// Check if user exists
if user == nil {
w.WriteHeader(404)
return
}
// Find character
for _, character := range user.Characters {
if character.UID == charID {
json.NewEncoder(w).Encode(character) // Return character Json
return // Return 200
}
}
// Return 404
w.WriteHeader(404)
return
}
func createCharacter(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(501)
return
vars := mux.Vars(r)
userID := vars["user"]
charID := vars["char"]
user, err := activeUserstore.GetUser(userID)
// Check if there was an error getting the user
if err != nil {
log.Printf("CREATE CHARACTER - Failed to get user: %v", err.Error())
w.WriteHeader(500)
return
}
// Check if user exists
if user == nil {
w.WriteHeader(400)
w.Write([]byte("User does not exist!"))
return
}
// Check if character already exists
for _, character := range user.Characters {
if character.UID == charID {
w.WriteHeader(400)
w.Write([]byte("Character already exists!"))
return
}
}
newCharacter := datatypes.Character{
UID: charID,
Skin: datatypes.Skin{
UID: "none",
},
}
// Add character to user object
user.Characters = append(user.Characters, newCharacter)
err = activeUserstore.SetUser(user)
if err != nil {
log.Printf("CREATE CHARACTER - Failed to set user %v", err.Error())
w.WriteHeader(500)
}
return
}
func getSkin(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
skinID := vars["skin"]
// Get skin
skin, err := activeSkinstore.GetSkin(skinID)
if err != nil {
log.Printf("GET SKIN - Failed to get skin %v", err.Error())
w.WriteHeader(500)
}
// Check if skin exists
if skin == nil {
w.WriteHeader(404)
return
}
json.NewEncoder(w).Encode(skin) // Return skin Json
return // Return 200
}
func createSkin(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
skinID := vars["skin"]
// Lookup skin object
skin, err := activeSkinstore.GetSkin(skinID)
if err != nil {
log.Printf("CREATE SKIN - Failed to get skin: %v", err.Error())
w.WriteHeader(500)
return
}
// Check if skin exists
if skin != nil {
w.WriteHeader(400)
w.Write([]byte("Skin already exists!"))
return
}
// Load skin data from request
skinBytes, err := readfile(r, "skin")
if err != nil {
log.Printf("CREATE SKIN - Failed to read upload: %v", err.Error())
}
// Verify data exists
if skinBytes == nil {
w.WriteHeader(400)
w.Write([]byte("Missing skin data!"))
return
}
// Create new skin object
err = activeSkinstore.AddSkin(skinID, skinBytes)
// Validate skin creation
if err != nil {
log.Printf("CREATE SKIN - Failed to add skin: %v", err.Error())
w.WriteHeader(500)
return
}
return // Return 200
}
// Helper function to read a file from a request form
func readfile(r *http.Request, fileName string) ([]byte, error) {
err := r.ParseMultipartForm(32 << 20) // Limit upload size
if err != nil {
return nil, err
}
var buf bytes.Buffer
file, _, err := r.FormFile(fileName)
if err != nil {
return nil, err
}
defer file.Close()
// Copy the file data to my buffer
_, err = io.Copy(&buf, file)
if err != nil {
return nil, err
}
contents := buf.Bytes() // Get data
buf.Reset() // Clear buffer
return contents, nil
}
// StartWebserver starts the webserver
func StartWebserver(port int) {
activeSkinstore = pluginmanager.LoadedSkinstores[0] // Load first skinstore
// Intialize router
myRouter := mux.NewRouter().StrictSlash(true)
// Health check path
myRouter.HandleFunc("/health", healthCheck).Methods("GET")
// Character routes
myRouter.HandleFunc("/get/character/{user}/{char}", getCharacter).Methods("GET")
myRouter.HandleFunc("/create/character/{user}/{char}", createCharacter).Methods("POST")
// Skin routes
myRouter.HandleFunc("/get/skin/{skin}", getSkin).Methods("GET")
myRouter.HandleFunc("/create/skin/{skin}", createSkin).Methods("POST")
// Run webserver
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), myRouter))
}