diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/README.md b/README.md index eeff3d0..b849bb9 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# SpiderMILK-Backend \ No newline at end of file +# SpiderMILK-Backend v0.1.0 diff --git a/commands.go b/commands.go new file mode 100644 index 0000000..bdd453d --- /dev/null +++ b/commands.go @@ -0,0 +1,11 @@ +package main + +func commandHandler(command string) string { + switch(command) { + case "help": + case "clear": + case "exit": + return "Don't do that" + } + return "Command not found" +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..62ec806 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module SpiderMILKBackend + +go 1.20 + +require github.com/joho/godotenv v1.5.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d61b19e --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= diff --git a/json/help.json b/json/help.json new file mode 100644 index 0000000..489bb47 --- /dev/null +++ b/json/help.json @@ -0,0 +1,6 @@ +{ + "helpMsg": "\"clear\" to clear the screen
\"exit\" to exit the game
\"help\" to print help message", + "clear": "\"clear\" to clear the screen", + "exit": "\"exit\" to exit the game", + "help": "\"help\" to print help message" +} diff --git a/server.go b/server.go new file mode 100644 index 0000000..3747695 --- /dev/null +++ b/server.go @@ -0,0 +1,50 @@ +package main + +import ( + "encoding/json" + "io/ioutil" + "net/http" + "os" + "fmt" + "github.com/joho/godotenv" +) + +func main() { + godotenv.Load() + serverPort := os.Getenv("PORT") + + http.HandleFunc("/", requestHandler) + http.HandleFunc("/help", helpHandler) + http.ListenAndServe(":"+serverPort, nil) +} + +func requestHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Access-Control-Allow-Headers", "Content-Type") + w.Header().Add("Access-Control-Allow-Methods", "POST") + w.Header().Add("Access-Control-Allow-Origin", "*") + w.Header().Add("Content-Type", "application/json") + + var reqBody map[string]interface{} + json.NewDecoder(r.Body).Decode(&reqBody) + result, err := json.Marshal(map[string]string{"responseText": commandHandler(reqBody["action"].(string))}) + if err != nil { + fmt.Println("Error marshalling response JSON in requestHandler(): ", err) + return + } + w.Write(result) +} + +func helpHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Access-Control-Allow-Headers", "Content-Type") + w.Header().Add("Access-Control-Allow-Methods", "POST") + w.Header().Add("Access-Control-Allow-Origin", "*") + w.Header().Add("Content-Type", "application/json") + + content, err := ioutil.ReadFile("json/help.json") + if(err != nil) { + fmt.Println("Error reading help.json in helpHandler(): ", err) + return + } + + w.Write(content) +}