First commit | 1.0.0 | Stable
pushing repo to gitea
This commit is contained in:
commit
1d724cd5b3
10
.vscode/settings.json
vendored
Normal file
10
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"spellright.language": [
|
||||||
|
"en-US-10-1."
|
||||||
|
],
|
||||||
|
"spellright.documentTypes": [
|
||||||
|
"markdown",
|
||||||
|
"latex",
|
||||||
|
"plaintext"
|
||||||
|
]
|
||||||
|
}
|
3
README.md
Normal file
3
README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# go-rest-cctweaked
|
||||||
|
|
||||||
|
Small REST api manipulation program for my little experiment with CC:Tweaked minecraft mod.
|
70
cli/ArgsProcessor.go
Normal file
70
cli/ArgsProcessor.go
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
const PROJECT_NAME string = "go-rest-cctweaked"
|
||||||
|
|
||||||
|
type knownArguments int
|
||||||
|
|
||||||
|
const (
|
||||||
|
enumStart knownArguments = iota
|
||||||
|
HelpArg
|
||||||
|
SendArg
|
||||||
|
RecieveArg
|
||||||
|
WrongArg
|
||||||
|
)
|
||||||
|
|
||||||
|
var argumentsDescription = map[knownArguments]string{
|
||||||
|
HelpArg: "help - shows help message.",
|
||||||
|
SendArg: "send <url> <content> <type> - sends cctweaked message to specified restfull endpoint.",
|
||||||
|
RecieveArg: "recieve <url> - fetches current message on server",
|
||||||
|
}
|
||||||
|
|
||||||
|
func gracefullExit(msg string) {
|
||||||
|
fmt.Println(msg)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BasicInterface() {
|
||||||
|
args := os.Args[1:]
|
||||||
|
|
||||||
|
if len(args) == 0 {
|
||||||
|
gracefullExit("Can't find argument. Please refer to --help")
|
||||||
|
}
|
||||||
|
|
||||||
|
knownArg := getArgType(args[0])
|
||||||
|
|
||||||
|
switch knownArg {
|
||||||
|
case HelpArg:
|
||||||
|
processHelp()
|
||||||
|
case SendArg:
|
||||||
|
processSend(args)
|
||||||
|
case RecieveArg:
|
||||||
|
processRecieve(args)
|
||||||
|
case WrongArg:
|
||||||
|
gracefullExit("Can't find argument. Please refer to --help")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getArgType(argument string) knownArguments {
|
||||||
|
switch argument {
|
||||||
|
|
||||||
|
case "help":
|
||||||
|
return HelpArg
|
||||||
|
case "--help":
|
||||||
|
return HelpArg
|
||||||
|
case "-h":
|
||||||
|
return HelpArg
|
||||||
|
|
||||||
|
case "send":
|
||||||
|
return SendArg
|
||||||
|
case "recieve":
|
||||||
|
return RecieveArg
|
||||||
|
|
||||||
|
default:
|
||||||
|
return WrongArg
|
||||||
|
}
|
||||||
|
}
|
10
cli/HelpArgument.go
Normal file
10
cli/HelpArgument.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func processHelp() {
|
||||||
|
fmt.Printf("--%v help menu--\n", PROJECT_NAME)
|
||||||
|
for i := enumStart + 1; i < WrongArg; i++ {
|
||||||
|
fmt.Printf("| %v\n", argumentsDescription[knownArguments(i)])
|
||||||
|
}
|
||||||
|
}
|
15
cli/RecieveArgument.go
Normal file
15
cli/RecieveArgument.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"go-rest-cctweaked/networking"
|
||||||
|
)
|
||||||
|
|
||||||
|
func processRecieve(args []string) {
|
||||||
|
if len(args) < 2 {
|
||||||
|
gracefullExit("Recieve take additional url argument. Check --help")
|
||||||
|
}
|
||||||
|
body := networking.GetJsonBody(args[1])
|
||||||
|
|
||||||
|
fmt.Println(string(body))
|
||||||
|
}
|
25
cli/SendArgument.go
Normal file
25
cli/SendArgument.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"go-rest-cctweaked/networking"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func processSend(args []string) {
|
||||||
|
if len(args) < 4 {
|
||||||
|
gracefullExit("Send take additional url, content and type arguments. Check --help")
|
||||||
|
}
|
||||||
|
url := args[1]
|
||||||
|
content := args[2]
|
||||||
|
msgType, err := strconv.Atoi(args[3])
|
||||||
|
if err != nil {
|
||||||
|
panic("Failed converting message type to integer.")
|
||||||
|
}
|
||||||
|
|
||||||
|
json := networking.EncodeJson(content, msgType)
|
||||||
|
|
||||||
|
statusCode := networking.PostJsonBody(url, json)
|
||||||
|
|
||||||
|
fmt.Printf("Server responded with: %v\n", statusCode)
|
||||||
|
}
|
10
main.go
Normal file
10
main.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go-rest-cctweaked/cli"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
cli.BasicInterface()
|
||||||
|
}
|
39
networking/jsonFactory.go
Normal file
39
networking/jsonFactory.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package networking
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type cctweakedMessage struct {
|
||||||
|
Content string `json:"content"`
|
||||||
|
Msg_type int `json:"msg_type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func createMessage(messageContent string, messageType int) cctweakedMessage {
|
||||||
|
return cctweakedMessage{
|
||||||
|
messageContent,
|
||||||
|
messageType,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func DecodeJson(jsonBody []byte) cctweakedMessage {
|
||||||
|
var message cctweakedMessage
|
||||||
|
|
||||||
|
err := json.Unmarshal(jsonBody, &message)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to decode json from body %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return message
|
||||||
|
}
|
||||||
|
|
||||||
|
func EncodeJson(messageContent string, messageType int) []byte {
|
||||||
|
msg := createMessage(messageContent, messageType)
|
||||||
|
json, err := json.Marshal(msg)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to encode message in json: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return json
|
||||||
|
}
|
31
networking/websiteController.go
Normal file
31
networking/websiteController.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package networking
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetJsonBody(url string) []byte {
|
||||||
|
req, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to send request %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := io.ReadAll(req.Body)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to read request body %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return body
|
||||||
|
}
|
||||||
|
|
||||||
|
func PostJsonBody(url string, json []byte) int {
|
||||||
|
req, err := http.Post(url, "application/json", bytes.NewBuffer(json))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to send post request on server: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return req.StatusCode
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user