diff --git a/BUILD.bazel b/BUILD.bazel index 11117c9..fa41a73 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -10,6 +10,7 @@ go_library( importpath = "git.eve.moe/jackyyf/navigator", visibility = ["//visibility:private"], deps = [ + "//api/beacon:go_default_library", "//api/beacon/v1:go_default_library", "//api/navigator:go_default_library", "//ipgeo:go_default_library", diff --git a/api/beacon/BUILD.bazel b/api/beacon/BUILD.bazel index a6f0a81..1461fb0 100644 --- a/api/beacon/BUILD.bazel +++ b/api/beacon/BUILD.bazel @@ -3,6 +3,8 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "go_default_library", srcs = ["api.go"], + data = ["beacon.html"], importpath = "git.eve.moe/jackyyf/navigator/api/beacon", visibility = ["//visibility:public"], + deps = ["@com_github_sirupsen_logrus//:go_default_library"], ) diff --git a/api/beacon/api.go b/api/beacon/api.go index 5a53e06..53c51b0 100644 --- a/api/beacon/api.go +++ b/api/beacon/api.go @@ -1,12 +1,17 @@ package beacon import ( + "flag" "fmt" + "github.com/sirupsen/logrus" + "io/ioutil" "net/http" + "strconv" ) var ( - apiServeMux = http.NewServeMux() + apiServeMux = http.NewServeMux() + beaconFrontendPath = flag.String("beacon-html", "./api/beacon/beacon.html", "Path to the html file of beacon frontend") ) func init() { @@ -16,3 +21,19 @@ func init() { 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") + } + }) +} diff --git a/api/beacon/beacon.html b/api/beacon/beacon.html new file mode 100644 index 0000000..303e160 --- /dev/null +++ b/api/beacon/beacon.html @@ -0,0 +1,256 @@ + + +
+ + + + +Please click the button below to start speedtest.
+ +