2020-01-12 22:09:30 +08:00
|
|
|
package beacon
|
|
|
|
|
|
|
|
import (
|
2020-02-02 16:39:15 +08:00
|
|
|
"flag"
|
2020-01-12 22:09:30 +08:00
|
|
|
"fmt"
|
2020-02-02 16:39:15 +08:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"io/ioutil"
|
2020-01-12 22:09:30 +08:00
|
|
|
"net/http"
|
2020-02-02 16:39:15 +08:00
|
|
|
"strconv"
|
2020-01-12 22:09:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
2020-01-12 22:09:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|