navigator/api/beacon/api.go

40 lines
972 B
Go

package beacon
import (
"flag"
"fmt"
"github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"strconv"
)
var (
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))
}
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")
}
})
}