Code refactor, adding GoLand workspace with Bazel plugin.

This commit is contained in:
2020-01-12 22:09:30 +08:00
parent 6ac03c18cc
commit 2dbb47f519
25 changed files with 519 additions and 194 deletions

8
api/beacon/BUILD.bazel Normal file
View File

@ -0,0 +1,8 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["api.go"],
importpath = "git.eve.moe/jackyyf/navigator/api/beacon",
visibility = ["//visibility:public"],
)

18
api/beacon/api.go Normal file
View File

@ -0,0 +1,18 @@
package beacon
import (
"fmt"
"net/http"
)
var (
apiServeMux = http.NewServeMux()
)
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))
}

13
api/beacon/v1/BUILD.bazel Normal file
View File

@ -0,0 +1,13 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["api.go"],
importpath = "git.eve.moe/jackyyf/navigator/api/beacon/v1",
visibility = ["//visibility:public"],
deps = [
"//api/beacon:go_default_library",
"//mapping:go_default_library",
"//utils:go_default_library",
],
)

33
api/beacon/v1/api.go Normal file
View File

@ -0,0 +1,33 @@
package v1
import (
"encoding/json"
"git.eve.moe/jackyyf/navigator/api/beacon"
"git.eve.moe/jackyyf/navigator/mapping"
"git.eve.moe/jackyyf/navigator/utils"
"net/http"
)
var (
serveMux = http.NewServeMux()
)
func init() {
serveMux.HandleFunc("/getNodes", func(resp http.ResponseWriter, req *http.Request) {
host := utils.GetRemoteIP(req)
nodes := mapping.GetNodes()
if nodes == nil {
utils.ResponseWithJsonError(resp, http.StatusInternalServerError, "Unable to get nodes")
return
}
suffix := mapping.GetSuffix(host)
resp.Header().Set("Content-Type", "application/json")
resp.WriteHeader(http.StatusOK)
jsonEncoder := json.NewEncoder(resp)
jsonEncoder.Encode(map[string]interface{}{
"nodes": nodes,
"suffix": suffix,
})
})
beacon.RegisterApi("v1", serveMux)
}