Intial webserver runtime

This commit is contained in:
Layla 2021-01-29 23:19:27 -05:00
parent f17f3d160c
commit 05ad9a50c3
8 changed files with 240 additions and 30 deletions

11
.vscode/launch.json vendored
View File

@ -2,13 +2,22 @@
"version": "0.2.0",
"configurations": [
{
"name": "Launch executable",
"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"
}
]
}

5
.vscode/tasks.json vendored
View File

@ -30,6 +30,11 @@
"label": "test",
"type": "shell",
"command": "make test"
},
{
"label": "standalone",
"type": "shell",
"command": "make standalone"
}
]
}

View File

@ -1,10 +1,11 @@
#include .env
PROJECTNAME="OpenSkins"
STANDALONE_LOCATION="../OpenSkins-Standalone-Plugin/bin/OpenSkins-Standalone-Plugin"
# Go related variables.
GOBASE=$(shell pwd)
GOPATH=$(GOBASE)/vendor:$(GOBASE):/home/azer/code/golang # You can remove or change the path after last colon.
GOPATH=$(GOBASE)/vendor:$(GOBASE)
GOBIN=$(GOBASE)/bin
GOFILES=$(wildcard *.go)
@ -21,36 +22,41 @@ go-compile: go-clean go-get go-build
go-build:
@echo " > Building binary..."
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go build -o $(GOBIN)/$(PROJECTNAME) $(GOFILES)
@go build -o $(GOBIN)/$(PROJECTNAME) $(GOFILES)
go-generate:
@echo " > Generating dependency files..."
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go generate $(generate)
@go generate $(generate)
go-get:
@echo " > Checking if there is any missing dependencies..."
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go get $(get)
@go get $(get)
go-install:
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go install $(GOFILES)
@echo " > Running go install..."
@go install $(GOFILES)
go-clean:
@echo " > Cleaning build cache"
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go clean
@go clean
go-test:
@echo " > Running tests..."
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go test
@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: go-get
install: openskins-common go-get
## clean: test
## clean: Runs go clean
clean:
@(MAKEFILE) go-clean
@ -61,7 +67,7 @@ compile:
@-$(MAKE) -s go-compile 2> $(STDERR)
@cat $(STDERR) | sed -e '1s/.*/\nError:\n/' | sed 's/make\[.*/ /' | sed "/^/s/^/ /" 1>&2
## watch: Runs go clean
## watch: Runs command on code update
watch:
@yolo -i . -e vendor -e bin -c $(run)
@ -74,6 +80,12 @@ 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

6
go.mod
View File

@ -4,6 +4,10 @@ go 1.13
require (
github.com/gorilla/mux v1.8.0
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227072620-649a4ef8820a
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,24 +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/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-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/josephbmanley/OpenSkins-Common v0.0.0-20201227072620-649a4ef8820a h1:BVoRW4lVzSDTUGanOxoKnPIvVcRhY5v6h3j0pOlV/DY=
github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227072620-649a4ef8820a/go.mod h1:xmRLNQLOMLqiQ2hzP81mdsNbu6ZxyLozoVWaR2A0BMY=
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=

View File

@ -43,7 +43,7 @@ func main() {
switch appRuntime {
case "webserver":
runtime.StartWebserver(8081)
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

View File

@ -1,10 +1,14 @@
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"
)
@ -17,6 +21,9 @@ func healthCheck(w http.ResponseWriter, r *http.Request) {
}
func getCharacter(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(501)
return
vars := mux.Vars(r)
userID := vars["user"]
charID := vars["char"]
@ -25,7 +32,7 @@ func getCharacter(w http.ResponseWriter, r *http.Request) {
// Check if there was an error getting the user
if err != nil {
log.Panicf("An error occured when getting a character: %v", err.Error())
log.Printf("An error occured when getting a character: %v", err.Error())
w.WriteHeader(500)
return
}
@ -36,21 +43,185 @@ func getCharacter(w http.ResponseWriter, r *http.Request) {
return
}
// Find character
for _, character := range user.Characters {
if character.UID == charID {
json.NewEncoder(w).Encode(character)
return
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)
myRouter.HandleFunc("/health", healthCheck)
myRouter.HandleFunc("/get/character/{user}/{char}", getCharacter)
// 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))
}