basic skeleton

This commit is contained in:
Nils 2021-12-19 02:16:27 +01:00
parent a219c7c44e
commit e8576273b7
Signed by: byreqz
GPG Key ID: 396A62D7D436749E
1 changed files with 36 additions and 0 deletions

36
main.go Normal file
View File

@ -0,0 +1,36 @@
package main
import (
"fmt"
"os/exec"
"log"
"strings"
"net/http"
)
func runner(command string, args... string) string{
cmd, err := exec.Command(command, args...).Output()
if err != nil {
if ! strings.Contains(err.Error(), "1") { // dont exit if error code is 1
log.Fatal(command, args, "caused an error: ", err)
}
}
return string(cmd)
}
func showhelp(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, "placeholder")
}
func ping(w http.ResponseWriter, req *http.Request) {
geturl := strings.Split(req.URL.String(), "/")
target := geturl[2]
pingres := runner("ping", "-c5", target)
fmt.Fprintln(w, pingres)
}
func main() {
http.HandleFunc("/ping/", ping)
http.HandleFunc("/", showhelp)
fmt.Println("Serving on :8000")
http.ListenAndServe(":8000", nil)
}