restructuring

This commit is contained in:
Nils Stinnesbeck 2020-12-11 21:32:30 +01:00
parent 0fec126ac4
commit 199aa57c96
Signed by: nils
GPG Key ID: 86D4882C6C6CA48B
3 changed files with 59 additions and 53 deletions

View File

@ -1,15 +1,10 @@
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"git.nils.zone/clientserverapi/server/storage"
t "git.nils.zone/clientserverapi/server/types"
"git.nils.zone/nils/prettify"
"github.com/elastic/go-sysinfo/types"
)
func check(err error) {
@ -22,47 +17,3 @@ func check(err error) {
func homePage(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Nothing here...")
}
func commands(w http.ResponseWriter, r *http.Request) {
// Client requests instructions
log.Println("Client requesting instructions, sending the following:")
var o t.OSInfo
err := json.NewDecoder(r.Body).Decode(&o)
check(err)
log.Println("Received the following OS info:")
prettify.Print(o)
var i t.Instruction
// check the OS and send the appropriate command
switch o.OSType {
case "linux":
// i = instruction{Command: "uname", Args: []string{"-a"}}
i = t.Instruction{Command: "poweroff"}
case "windows":
i = t.Instruction{Command: "winver"}
default:
return
}
log.Println("Sending the following instructions:")
prettify.Print(i)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(i)
}
func response(w http.ResponseWriter, r *http.Request) {
log.Println("Got the following output from client:")
var p t.CmdResponse
err := json.NewDecoder(r.Body).Decode(&p)
check(err)
prettify.Print(p)
}
func register(w http.ResponseWriter, r *http.Request) {
log.Println("Got the following output from client:")
var h types.HostInfo
err := json.NewDecoder(r.Body).Decode(&h)
check(err)
prettify.Print(h)
}

55
requests.go Normal file
View File

@ -0,0 +1,55 @@
package main
import (
"encoding/json"
"log"
"net/http"
s "git.nils.zone/clientserverapi/server/shared"
"git.nils.zone/nils/prettify"
"github.com/elastic/go-sysinfo/types"
)
func commands(w http.ResponseWriter, r *http.Request) {
// Client requests instructions
log.Println("Client requesting instructions, sending the following:")
var o s.OSInfo
err := json.NewDecoder(r.Body).Decode(&o)
check(err)
log.Println("Received the following OS info:")
prettify.Print(o)
var i s.Instruction
// check the OS and send the appropriate command
switch o.OSType {
case "linux":
// i = instruction{Command: "uname", Args: []string{"-a"}}
i = s.Instruction{Command: "poweroff"}
case "windows":
i = s.Instruction{Command: "winver"}
default:
return
}
log.Println("Sending the following instructions:")
prettify.Print(i)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(i)
}
func response(w http.ResponseWriter, r *http.Request) {
log.Println("Got the following output from client:")
var p s.CmdResponse
err := json.NewDecoder(r.Body).Decode(&p)
check(err)
prettify.Print(p)
}
func register(w http.ResponseWriter, r *http.Request) {
log.Println("Got the following output from client:")
var h types.HostInfo
err := json.NewDecoder(r.Body).Decode(&h)
check(err)
prettify.Print(h)
}

View File

@ -1,19 +1,19 @@
package types
package shared
// instruction is a structured way of sending commands
// Instruction is a structured way of sending commands
type Instruction struct {
Command string `json:"command"`
Args []string `json:"args"`
}
// cmdResponse is a response from the client to the server
// CmdResponse is a response from the client to the server
type CmdResponse struct {
Stdout string `json:"stdout"`
Stderr string `json:"stderr"`
ExitCode int `json:"exitcode"`
}
// osInfo needs a comment
// OSInfo needs a comment
type OSInfo struct {
OSType string `json:"os_type"`
OSArch string `json:"os_arch"`