代码拉取完成,页面将自动刷新
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
"strings"
// tfjson "github.com/hashicorp/terraform-json"
)
func (ro *rover) startServer(ipPort string, frontendFS http.Handler) error {
m := http.NewServeMux()
s := http.Server{Addr: ipPort, Handler: m}
m.Handle("/", frontendFS)
m.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
// simple healthcheck
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"alive": true}`)
})
m.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
fileType := strings.Replace(r.URL.Path, "/api/", "", 1)
var j []byte
var err error
enableCors(&w)
switch fileType {
case "plan":
j, err = json.Marshal(ro.Plan)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error producing plan JSON: %s\n", err))
}
case "rso":
j, err = json.Marshal(ro.RSO)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error producing rso JSON: %s\n", err))
}
case "map":
j, err = json.Marshal(ro.Map)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error producing map JSON: %s\n", err))
}
case "graph":
j, err = json.Marshal(ro.Graph)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error producing graph JSON: %s\n", err))
}
default:
io.WriteString(w, "Please enter a valid file type: plan, rso, map, graph\n")
}
w.Header().Set("Content-Type", "application/json")
io.Copy(w, bytes.NewReader(j))
})
log.Printf("Rover is running on %s", ipPort)
l, err := net.Listen("tcp", ipPort)
if err != nil {
log.Fatal(err)
}
// The browser can connect now because the listening socket is open.
if ro.GenImage {
go screenshot(&s)
}
// Start the blocking server loop.
return s.Serve(l)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。