84 lines
1.8 KiB
Go
84 lines
1.8 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"log"
|
||
|
"net"
|
||
|
"os"
|
||
|
"runtime"
|
||
|
"net/http"
|
||
|
"strings"
|
||
|
"github.com/TylerBrock/colorjson"
|
||
|
)
|
||
|
|
||
|
var port string = "8080"
|
||
|
|
||
|
func getIp() string {
|
||
|
addrs, err := net.InterfaceAddrs()
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return addrs[1].String()
|
||
|
}
|
||
|
|
||
|
func getInfo() {
|
||
|
var ip string = strings.Split(getIp(), "/")[0]
|
||
|
dir, _ := os.Getwd()
|
||
|
var ss [] string
|
||
|
if runtime.GOOS == "windows" {
|
||
|
ss = strings.Split(dir, "\\")
|
||
|
} else {
|
||
|
ss = strings.Split(dir, "/")
|
||
|
}
|
||
|
var path string = strings.Join(ss, "/")
|
||
|
|
||
|
fmt.Println("\n",
|
||
|
"==================================\n",
|
||
|
"iSpindle test server configuration\n",
|
||
|
"==================================\n",
|
||
|
fmt.Sprintf("\n address : %s",ip),
|
||
|
fmt.Sprintf("\n port : %s",port),
|
||
|
fmt.Sprintf("\n path : %s",path),
|
||
|
"\n method : HTTP\n")
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
flag.StringVar(&port, "p", port, "port to use")
|
||
|
flag.Usage = func() {
|
||
|
flag.PrintDefaults() // prints default usage
|
||
|
}
|
||
|
flag.Parse()
|
||
|
var infoArg bool
|
||
|
for _, a := range os.Args[1:] {
|
||
|
if a == "info" { infoArg = true }
|
||
|
}
|
||
|
if infoArg { getInfo()
|
||
|
} else {
|
||
|
http.HandleFunc("/", serve)
|
||
|
log.Fatal(http.ListenAndServe(":"+port, nil))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func serve(w http.ResponseWriter, r *http.Request) {
|
||
|
var obj map[string]interface{}
|
||
|
fmt.Printf("%+v\n", r)
|
||
|
str, err := ioutil.ReadAll(r.Body)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
err = json.Unmarshal([]byte(str), &obj)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
f := colorjson.NewFormatter()
|
||
|
f.Indent = 4
|
||
|
s, _ := f.Marshal(obj)
|
||
|
fmt.Println(string(s))
|
||
|
|
||
|
fmt.Fprint(w, "ok")
|
||
|
}
|
||
|
|