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)
}

13
api/navigator/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/navigator",
visibility = ["//visibility:public"],
deps = [
"//ipgeo:go_default_library",
"//mapping:go_default_library",
"//utils:go_default_library",
],
)

109
api/navigator/api.go Normal file
View File

@ -0,0 +1,109 @@
package navigator
import (
"encoding/json"
"fmt"
"git.eve.moe/jackyyf/navigator/ipgeo"
"git.eve.moe/jackyyf/navigator/mapping"
"git.eve.moe/jackyyf/navigator/utils"
"log"
"net"
"net/http"
"strings"
)
const (
errIPv4Only = "Navigator works for valid IPv4 only :)"
)
func init() {
http.HandleFunc("/healthz", func(resp http.ResponseWriter, req *http.Request) {
resp.WriteHeader(200)
resp.Write([]byte("ok"))
})
http.HandleFunc("/info", func(resp http.ResponseWriter, req *http.Request) {
var host string
if argIp := req.FormValue("ip"); argIp != "" {
host = argIp
if net.ParseIP(host).To4() == nil {
utils.ResponseWithError(resp, http.StatusPreconditionFailed, errIPv4Only)
return
}
} else {
host = utils.GetRemoteIP(req)
}
db := ipgeo.Get()
info_cn, err := db.FindInfo(host, "CN")
if err != nil {
fmt.Fprintf(resp, "IP %s not found in the database.", host)
return
}
info_en, err := db.FindInfo(host, "EN")
if err != nil {
fmt.Fprintf(resp, "IP %s not found in the database.", host)
return
}
resp.Header().Set("Content-Type", "text/plain")
resp.WriteHeader(http.StatusOK)
server := mapping.Get(host)
fmt.Fprintln(resp, "您的IP:", host)
fmt.Fprintln(resp, "数据库中IP所属位置:", utils.BuildLocation(info_cn))
if info_cn.IspDomain != "" {
fmt.Fprintln(resp, "数据库中IP所属运营商:", info_cn.IspDomain)
}
fmt.Fprintln(resp, "您被分配的CDN节点为:", server)
fmt.Fprintln(resp)
fmt.Fprintln(resp, strings.Repeat("=", 72))
fmt.Fprintln(resp, "Your IP:", host)
fmt.Fprintln(resp, "Location for your IP according our database:", utils.BuildLocation(info_en))
if info_en.IspDomain != "" {
fmt.Fprintln(resp, "ISP for your IP according our database:", info_en.IspDomain)
}
fmt.Fprintln(resp, "Allocated CDN node for you:", server)
fmt.Fprintln(resp)
})
http.HandleFunc("/mapping", func(resp http.ResponseWriter, req *http.Request) {
host := utils.GetRemoteIP(req)
resp.Header().Set("Content-Type", "text/plain")
resp.WriteHeader(http.StatusOK)
server := mapping.Get(host)
fmt.Fprint(resp, server)
})
http.HandleFunc("/getMapping", func(resp http.ResponseWriter, req *http.Request) {
ip := req.FormValue("ip")
if net.ParseIP(ip).To4() == nil {
utils.ResponseWithError(resp, http.StatusBadRequest, errIPv4Only)
return
}
resp.Header().Set("Content-Type", "text/plain")
resp.WriteHeader(http.StatusOK)
server := mapping.Get(ip)
log.Printf("%s => %s\n", ip, server)
fmt.Fprint(resp, server)
})
http.HandleFunc("/getNodes", func(resp http.ResponseWriter, req *http.Request) {
ip := req.FormValue("ip")
if net.ParseIP(ip).To4() == nil {
utils.ResponseWithError(resp, http.StatusBadRequest, errIPv4Only)
return
}
nodes := mapping.GetNodes()
if nodes == nil {
utils.ResponseWithJsonError(resp, http.StatusInternalServerError, "Unable to get nodes")
return
}
suffix := mapping.GetSuffix(ip)
resp.Header().Set("Content-Type", "application/json")
resp.WriteHeader(http.StatusOK)
jsonEncoder := json.NewEncoder(resp)
ret := make([]string, 0, len(nodes))
for _, node := range nodes {
ret = append(ret, node+suffix)
}
jsonEncoder.Encode(ret)
})
}