From 469d1dfcb33d88d6abbb4396241e78e7f676939a Mon Sep 17 00:00:00 2001 From: Joseph Manley Date: Sun, 27 Dec 2020 04:27:57 -0500 Subject: [PATCH] Start development work on webserver runtime --- go.mod | 3 ++- go.sum | 4 ++++ main.go | 12 ++++++++-- runtime/webserver.go | 56 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 runtime/webserver.go diff --git a/go.mod b/go.mod index ed4c485..e1cf770 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ 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.0-20201227072620-649a4ef8820a github.com/sirupsen/logrus v1.7.0 ) diff --git a/go.sum b/go.sum index 3758d83..29c9d2c 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ 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= @@ -10,6 +12,8 @@ github.com/josephbmanley/OpenSkins-Common v0.0.0-20201227060121-852c176c7219 h1: 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/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= diff --git a/main.go b/main.go index 3077421..982d552 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,14 @@ 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() { @@ -37,7 +40,12 @@ func main() { os.Exit(1) } - log.Fatalln("Runtime is currently not implemented!") - os.Exit(1) + switch appRuntime { + case "webserver": + runtime.StartWebserver() + default: + log.Fatalln("Runtime is currently not implemented!") + os.Exit(1) + } } diff --git a/runtime/webserver.go b/runtime/webserver.go new file mode 100644 index 0000000..87eeaa3 --- /dev/null +++ b/runtime/webserver.go @@ -0,0 +1,56 @@ +package runtime + +import ( + "encoding/json" + "fmt" + "github.com/gorilla/mux" + "github.com/josephbmanley/OpenSkins-Common/datastore" + "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) { + 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.Panicf("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 + } + + for _, character := range user.Characters { + if character.UID == charID { + json.NewEncoder(w).Encode(character) + return + } + } + + w.WriteHeader(404) + return +} + +// StartWebserver starts the webserver +func StartWebserver() { + myRouter := mux.NewRouter().StrictSlash(true) + myRouter.HandleFunc("/health", healthCheck) + myRouter.HandleFunc("/get/character/{user}/{char}", getCharacter) + log.Fatal(http.ListenAndServe(":8081", myRouter)) +}