Start development work on webserver runtime
This commit is contained in:
parent
fd2814d77a
commit
469d1dfcb3
3
go.mod
3
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
|
||||
)
|
||||
|
4
go.sum
4
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=
|
||||
|
12
main.go
12
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)
|
||||
}
|
||||
|
||||
}
|
||||
|
56
runtime/webserver.go
Normal file
56
runtime/webserver.go
Normal file
@ -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))
|
||||
}
|
Reference in New Issue
Block a user