navigator/api/beacon/api.go

40 lines
972 B
Go
Raw Normal View History

package beacon
import (
2020-02-02 16:39:15 +08:00
"flag"
"fmt"
2020-02-02 16:39:15 +08:00
"github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
2020-02-02 16:39:15 +08:00
"strconv"
)
var (
2020-02-02 16:39:15 +08:00
apiServeMux = http.NewServeMux()
beaconFrontendPath = flag.String("beacon-html", "./api/beacon/beacon.html", "Path to the html file of beacon frontend")
)
func init() {
http.Handle("/beacon/", http.StripPrefix("/beacon", apiServeMux))
}
func RegisterApi(version string, handler http.Handler) {
apiServeMux.Handle(fmt.Sprintf("/%s/", version), http.StripPrefix(fmt.Sprint("/", version), handler))
}
2020-02-02 16:39:15 +08:00
func Initialize() {
content, err := ioutil.ReadFile(*beaconFrontendPath)
if err != nil {
panic(err)
}
apiServeMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Content-Length", strconv.Itoa(len(content)))
w.WriteHeader(200)
_, err := w.Write(content)
if err != nil {
logrus.WithError(err).Error("Unable to write html data")
}
})
}