Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 0.1.0 #3

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# SpiderMILK-Backend
# SpiderMILK-Backend v0.1.0
11 changes: 11 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
@@ -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"
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module SpiderMILKBackend

go 1.20

require github.com/joho/godotenv v1.5.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
6 changes: 6 additions & 0 deletions json/help.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"helpMsg": "\"clear\" to clear the screen<br>\"exit\" to exit the game<br>\"help\" to print help message",
"clear": "\"clear\" to clear the screen",
"exit": "\"exit\" to exit the game",
"help": "\"help\" to print help message"
}
50 changes: 50 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
@@ -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)
}