From d47c459406cb47b0ee8c1b2d8a71345d00e46f85 Mon Sep 17 00:00:00 2001 From: Yifu Yu Date: Sat, 1 Feb 2020 15:06:00 +0800 Subject: [PATCH] Refactored API. --- .ijwb/.idea/sqldialects.xml | 6 + BUILD.bazel | 1 + WORKSPACE | 329 ++++++++++++++++++++++++++++++++++++ api/beacon/v1/BUILD.bazel | 1 + api/beacon/v1/api.go | 19 ++- go.mod | 3 + go.sum | 129 ++++++++++++++ main.go | 4 + mapping/BUILD.bazel | 5 +- mapping/firefly/BUILD.bazel | 13 ++ mapping/firefly/core.go | 70 ++++++++ mapping/map.go | 2 + schema.sql | 12 ++ 13 files changed, 591 insertions(+), 3 deletions(-) create mode 100644 .ijwb/.idea/sqldialects.xml create mode 100644 mapping/firefly/BUILD.bazel create mode 100644 mapping/firefly/core.go create mode 100644 schema.sql diff --git a/.ijwb/.idea/sqldialects.xml b/.ijwb/.idea/sqldialects.xml new file mode 100644 index 0000000..288481b --- /dev/null +++ b/.ijwb/.idea/sqldialects.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/BUILD.bazel b/BUILD.bazel index eb96ae4..11117c9 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -14,6 +14,7 @@ go_library( "//api/navigator:go_default_library", "//ipgeo:go_default_library", "//mapping:go_default_library", + "@com_github_sirupsen_logrus//:go_default_library", ], ) diff --git a/WORKSPACE b/WORKSPACE index e4abcc9..c3b68ce 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -41,3 +41,332 @@ go_repository( sum = "h1:ZP11CRSzO9uOTTOVkH6yodtI3kSY69vUID8lx8B0M3s=", version = "v0.0.0-20191113183327-aaf7be003892", ) + +go_repository( + name = "com_github_cockroachdb_apd", + importpath = "github.com/cockroachdb/apd", + sum = "h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=", + version = "v1.1.0", +) + +go_repository( + name = "com_github_coreos_go_systemd", + importpath = "github.com/coreos/go-systemd", + sum = "h1:JOrtw2xFKzlg+cbHpyrpLDmnN1HqhBfnX7WDiW7eG2c=", + version = "v0.0.0-20190719114852-fd7a80b32e1f", +) + +go_repository( + name = "com_github_creack_pty", + importpath = "github.com/creack/pty", + sum = "h1:6pwm8kMQKCmgUg0ZHTm5+/YvRK0s3THD/28+T6/kk4A=", + version = "v1.1.7", +) + +go_repository( + name = "com_github_davecgh_go_spew", + importpath = "github.com/davecgh/go-spew", + sum = "h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=", + version = "v1.1.1", +) + +go_repository( + name = "com_github_go_stack_stack", + importpath = "github.com/go-stack/stack", + sum = "h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=", + version = "v1.8.0", +) + +go_repository( + name = "com_github_gofrs_uuid", + importpath = "github.com/gofrs/uuid", + sum = "h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=", + version = "v3.2.0+incompatible", +) + +go_repository( + name = "com_github_jackc_chunkreader", + importpath = "github.com/jackc/chunkreader", + sum = "h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0=", + version = "v1.0.0", +) + +go_repository( + name = "com_github_jackc_chunkreader_v2", + importpath = "github.com/jackc/chunkreader/v2", + sum = "h1:DUwgMQuuPnS0rhMXenUtZpqZqrR/30NWY+qQvTpSvEs=", + version = "v2.0.0", +) + +go_repository( + name = "com_github_jackc_pgconn", + importpath = "github.com/jackc/pgconn", + sum = "h1:6Q01OMLPPkmcCZVB9oRT6ACgo6nTEaNFQ97O79A8NuA=", + version = "v1.2.0", +) + +go_repository( + name = "com_github_jackc_pgio", + importpath = "github.com/jackc/pgio", + sum = "h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE=", + version = "v1.0.0", +) + +go_repository( + name = "com_github_jackc_pgmock", + importpath = "github.com/jackc/pgmock", + sum = "h1:JVX6jT/XfzNqIjye4717ITLaNwV9mWbJx0dLCpcRzdA=", + version = "v0.0.0-20190831213851-13a1b77aafa2", +) + +go_repository( + name = "com_github_jackc_pgpassfile", + importpath = "github.com/jackc/pgpassfile", + sum = "h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=", + version = "v1.0.0", +) + +go_repository( + name = "com_github_jackc_pgproto3", + importpath = "github.com/jackc/pgproto3", + sum = "h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A=", + version = "v1.1.0", +) + +go_repository( + name = "com_github_jackc_pgproto3_v2", + importpath = "github.com/jackc/pgproto3/v2", + sum = "h1:FApgMJ/GtaXfI0s8Lvd0kaLaRwMOhs4VH92pwkwQQvU=", + version = "v2.0.0", +) + +go_repository( + name = "com_github_jackc_pgtype", + importpath = "github.com/jackc/pgtype", + sum = "h1:aZwrtaSe314VgSGmKvggULa2TavoD1jWVsxj9Zdltek=", + version = "v1.1.0", +) + +go_repository( + name = "com_github_jackc_pgx_v4", + importpath = "github.com/jackc/pgx/v4", + sum = "h1:vcUwfx7/L53CmX3UKANy34iHs69+11iVuwMO+jY68bY=", + version = "v4.2.0", +) + +go_repository( + name = "com_github_jackc_puddle", + importpath = "github.com/jackc/puddle", + sum = "h1:rbjAshlgKscNa7j0jAM0uNQflis5o2XUogPMVAwtcsM=", + version = "v1.0.0", +) + +go_repository( + name = "com_github_konsorten_go_windows_terminal_sequences", + importpath = "github.com/konsorten/go-windows-terminal-sequences", + sum = "h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=", + version = "v1.0.2", +) + +go_repository( + name = "com_github_kr_pretty", + importpath = "github.com/kr/pretty", + sum = "h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=", + version = "v0.1.0", +) + +go_repository( + name = "com_github_kr_pty", + importpath = "github.com/kr/pty", + sum = "h1:AkaSdXYQOWeaO3neb8EM634ahkXXe3jYbVh/F9lq+GI=", + version = "v1.1.8", +) + +go_repository( + name = "com_github_kr_text", + importpath = "github.com/kr/text", + sum = "h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=", + version = "v0.1.0", +) + +go_repository( + name = "com_github_lib_pq", + importpath = "github.com/lib/pq", + sum = "h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=", + version = "v1.2.0", +) + +go_repository( + name = "com_github_mattn_go_colorable", + importpath = "github.com/mattn/go-colorable", + sum = "h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=", + version = "v0.1.2", +) + +go_repository( + name = "com_github_mattn_go_isatty", + importpath = "github.com/mattn/go-isatty", + sum = "h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=", + version = "v0.0.9", +) + +go_repository( + name = "com_github_pkg_errors", + importpath = "github.com/pkg/errors", + sum = "h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=", + version = "v0.8.1", +) + +go_repository( + name = "com_github_pmezard_go_difflib", + importpath = "github.com/pmezard/go-difflib", + sum = "h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=", + version = "v1.0.0", +) + +go_repository( + name = "com_github_rs_xid", + importpath = "github.com/rs/xid", + sum = "h1:mhH9Nq+C1fY2l1XIpgxIiUOfNpRBYH1kKcr+qfKgjRc=", + version = "v1.2.1", +) + +go_repository( + name = "com_github_rs_zerolog", + importpath = "github.com/rs/zerolog", + sum = "h1:uPRuwkWF4J6fGsJ2R0Gn2jB1EQiav9k3S6CSdygQJXY=", + version = "v1.15.0", +) + +go_repository( + name = "com_github_satori_go_uuid", + importpath = "github.com/satori/go.uuid", + sum = "h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=", + version = "v1.2.0", +) + +go_repository( + name = "com_github_shopspring_decimal", + importpath = "github.com/shopspring/decimal", + sum = "h1:pntxY8Ary0t43dCZ5dqY4YTJCObLY1kIXl0uzMv+7DE=", + version = "v0.0.0-20180709203117-cd690d0c9e24", +) + +go_repository( + name = "com_github_sirupsen_logrus", + importpath = "github.com/sirupsen/logrus", + sum = "h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=", + version = "v1.4.2", +) + +go_repository( + name = "com_github_stretchr_objx", + importpath = "github.com/stretchr/objx", + sum = "h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=", + version = "v0.2.0", +) + +go_repository( + name = "com_github_stretchr_testify", + importpath = "github.com/stretchr/testify", + sum = "h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=", + version = "v1.4.0", +) + +go_repository( + name = "com_github_zenazn_goji", + importpath = "github.com/zenazn/goji", + sum = "h1:RSQQAbXGArQ0dIDEq+PI6WqN6if+5KHu6x2Cx/GXLTQ=", + version = "v0.9.0", +) + +go_repository( + name = "in_gopkg_check_v1", + importpath = "gopkg.in/check.v1", + sum = "h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=", + version = "v1.0.0-20180628173108-788fd7840127", +) + +go_repository( + name = "in_gopkg_inconshreveable_log15_v2", + importpath = "gopkg.in/inconshreveable/log15.v2", + sum = "h1:RlWgLqCMMIYYEVcAR5MDsuHlVkaIPDAF+5Dehzg8L5A=", + version = "v2.0.0-20180818164646-67afb5ed74ec", +) + +go_repository( + name = "in_gopkg_yaml_v2", + importpath = "gopkg.in/yaml.v2", + sum = "h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=", + version = "v2.2.2", +) + +go_repository( + name = "org_golang_x_crypto", + importpath = "golang.org/x/crypto", + sum = "h1:nVJ3guKA9qdkEQ3TUdXI9QSINo2CUPM/cySEvw2w8I0=", + version = "v0.0.0-20200109152110-61a87790db17", +) + +go_repository( + name = "org_golang_x_net", + importpath = "golang.org/x/net", + sum = "h1:fHDIZ2oxGnUZRN6WgWFCbYBjH9uqVPRCUVUDhs0wnbA=", + version = "v0.0.0-20190813141303-74dc4d7220e7", +) + +go_repository( + name = "org_golang_x_sync", + importpath = "golang.org/x/sync", + sum = "h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=", + version = "v0.0.0-20190423024810-112230192c58", +) + +go_repository( + name = "org_golang_x_sys", + importpath = "golang.org/x/sys", + sum = "h1:ng0gs1AKnRRuEMZoTLLlbOd+C17zUDepwGQBb/n+JVg=", + version = "v0.0.0-20190826190057-c7b8b68b1456", +) + +go_repository( + name = "org_golang_x_text", + importpath = "golang.org/x/text", + sum = "h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=", + version = "v0.3.2", +) + +go_repository( + name = "org_golang_x_tools", + importpath = "golang.org/x/tools", + sum = "h1:ESK9Jb5JOE+y4u+ozMQeXfMHwEHm6zVbaDQkeaj6wI4=", + version = "v0.0.0-20190823170909-c4a336ef6a2f", +) + +go_repository( + name = "org_golang_x_xerrors", + importpath = "golang.org/x/xerrors", + sum = "h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=", + version = "v0.0.0-20190717185122-a985d3407aa7", +) + +go_repository( + name = "org_uber_go_atomic", + importpath = "go.uber.org/atomic", + sum = "h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=", + version = "v1.4.0", +) + +go_repository( + name = "org_uber_go_multierr", + importpath = "go.uber.org/multierr", + sum = "h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=", + version = "v1.1.0", +) + +go_repository( + name = "org_uber_go_zap", + importpath = "go.uber.org/zap", + sum = "h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM=", + version = "v1.10.0", +) diff --git a/api/beacon/v1/BUILD.bazel b/api/beacon/v1/BUILD.bazel index 5acba9f..2b7b49f 100644 --- a/api/beacon/v1/BUILD.bazel +++ b/api/beacon/v1/BUILD.bazel @@ -8,6 +8,7 @@ go_library( deps = [ "//api/beacon:go_default_library", "//mapping:go_default_library", + "//mapping/firefly:go_default_library", "//utils:go_default_library", ], ) diff --git a/api/beacon/v1/api.go b/api/beacon/v1/api.go index 9d914aa..b66677d 100644 --- a/api/beacon/v1/api.go +++ b/api/beacon/v1/api.go @@ -4,6 +4,7 @@ import ( "encoding/json" "git.eve.moe/jackyyf/navigator/api/beacon" "git.eve.moe/jackyyf/navigator/mapping" + "git.eve.moe/jackyyf/navigator/mapping/firefly" "git.eve.moe/jackyyf/navigator/utils" "net/http" ) @@ -23,11 +24,25 @@ func init() { suffix := mapping.GetSuffix(host) resp.Header().Set("Content-Type", "application/json") resp.WriteHeader(http.StatusOK) - jsonEncoder := json.NewEncoder(resp) - jsonEncoder.Encode(map[string]interface{}{ + json.NewEncoder(resp).Encode(map[string]interface{}{ "nodes": nodes, "suffix": suffix, }) }) + serveMux.HandleFunc("/submit", func(resp http.ResponseWriter, req *http.Request) { + if req.Method != "POST" { + utils.ResponseWithJsonError(resp, http.StatusMethodNotAllowed, "Method is not accepted with this method.") + return + } + request := make(map[string]float64) + decoder := json.NewDecoder(req.Body) + if err := decoder.Decode(&request); err != nil { + utils.ResponseWithJsonError(resp, http.StatusBadRequest, "Malformed JSON payload.") + return + } + remoteIp := utils.GetRemoteIP(req) + go firefly.AddSpeedTestResult(remoteIp, request) + resp.WriteHeader(http.StatusOK) + }) beacon.RegisterApi("v1", serveMux) } diff --git a/go.mod b/go.mod index 017f42c..a5cb8ea 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,8 @@ go 1.13 require ( github.com/ipipdotnet/ipdb-go v1.2.0 + github.com/jackc/pgx/v4 v4.2.0 + github.com/sirupsen/logrus v1.4.2 go.starlark.net v0.0.0-20191113183327-aaf7be003892 + golang.org/x/crypto v0.0.0-20200109152110-61a87790db17 // indirect ) diff --git a/go.sum b/go.sum index 5923efc..82ac2f2 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,133 @@ +github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I= +github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= +github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE= +github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/ipipdotnet/ipdb-go v1.2.0 h1:Afa0qx/SgRevzIK8Qg1TevuD5M28kFLWbzPvU+GQJ08= github.com/ipipdotnet/ipdb-go v1.2.0/go.mod h1:6SFLNyXDBF6q99FQvbOZJQCc2rdPrB1V5DSy4S83RSw= +github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0= +github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo= +github.com/jackc/chunkreader/v2 v2.0.0 h1:DUwgMQuuPnS0rhMXenUtZpqZqrR/30NWY+qQvTpSvEs= +github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= +github.com/jackc/pgconn v0.0.0-20190420214824-7e0022ef6ba3/go.mod h1:jkELnwuX+w9qN5YIfX0fl88Ehu4XC3keFuOJJk9pcnA= +github.com/jackc/pgconn v0.0.0-20190824142844-760dd75542eb/go.mod h1:lLjNuW/+OfW9/pnVKPazfWOgNfH2aPem8YQ7ilXGvJE= +github.com/jackc/pgconn v0.0.0-20190831204454-2fabfa3c18b7/go.mod h1:ZJKsE/KZfsUgOEh9hBm+xYTstcNHg7UPMVJqRfQxq4s= +github.com/jackc/pgconn v1.2.0 h1:6Q01OMLPPkmcCZVB9oRT6ACgo6nTEaNFQ97O79A8NuA= +github.com/jackc/pgconn v1.2.0/go.mod h1:GgY/Lbj1VonNaVdNUHs9AwWom3yP2eymFQ1C8z9r/Lk= +github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE= +github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8= +github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2 h1:JVX6jT/XfzNqIjye4717ITLaNwV9mWbJx0dLCpcRzdA= +github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A= +github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78= +github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA= +github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg= +github.com/jackc/pgproto3/v2 v2.0.0-rc3/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM= +github.com/jackc/pgproto3/v2 v2.0.0-rc3.0.20190831210041-4c03ce451f29/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM= +github.com/jackc/pgproto3/v2 v2.0.0 h1:FApgMJ/GtaXfI0s8Lvd0kaLaRwMOhs4VH92pwkwQQvU= +github.com/jackc/pgproto3/v2 v2.0.0/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM= +github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0/go.mod h1:hdSHsc1V01CGwFsrv11mJRHWJ6aifDLfdV3aVjFF0zg= +github.com/jackc/pgtype v0.0.0-20190824184912-ab885b375b90/go.mod h1:KcahbBH1nCMSo2DXpzsoWOAfFkdEtEJpPbVLq8eE+mc= +github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrUS8lot6TQqcg7mtthZ9T0EoIBFiJcmcyw= +github.com/jackc/pgtype v1.1.0 h1:aZwrtaSe314VgSGmKvggULa2TavoD1jWVsxj9Zdltek= +github.com/jackc/pgtype v1.1.0/go.mod h1:5m2OfMh1wTK7x+Fk952IDmI4nw3nPrvtQdM0ZT4WpC0= +github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08CZQyj1PVQBHy9XOp5p8/SHH6a0psbY9Y= +github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9WuGR0JG/JseM9irFbnEPbuWV2EELPNuM= +github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc= +github.com/jackc/pgx/v4 v4.2.0 h1:vcUwfx7/L53CmX3UKANy34iHs69+11iVuwMO+jY68bY= +github.com/jackc/pgx/v4 v4.2.0/go.mod h1:im3JmBxeFE7meKEgULr08kjebXQOtkr3DP1uVPHuygc= +github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/jackc/puddle v1.0.0 h1:rbjAshlgKscNa7j0jAM0uNQflis5o2XUogPMVAwtcsM= +github.com/jackc/puddle v1.0.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s= +github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= +github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= +github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= +github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= +github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= +github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= +github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24 h1:pntxY8Ary0t43dCZ5dqY4YTJCObLY1kIXl0uzMv+7DE= +github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= +github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= +github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= go.starlark.net v0.0.0-20191113183327-aaf7be003892 h1:ZP11CRSzO9uOTTOVkH6yodtI3kSY69vUID8lx8B0M3s= go.starlark.net v0.0.0-20191113183327-aaf7be003892/go.mod h1:c1/X6cHgvdXj6pUlmWKMkuqRnW4K8x2vwt6JAaaircg= +go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= +go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= +golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200109152110-61a87790db17 h1:nVJ3guKA9qdkEQ3TUdXI9QSINo2CUPM/cySEvw2w8I0= +golang.org/x/crypto v0.0.0-20200109152110-61a87790db17/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456 h1:ng0gs1AKnRRuEMZoTLLlbOd+C17zUDepwGQBb/n+JVg= +golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190823170909-c4a336ef6a2f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index dce42c9..2acd9c6 100644 --- a/main.go +++ b/main.go @@ -6,9 +6,11 @@ import ( _ "git.eve.moe/jackyyf/navigator/api/navigator" "git.eve.moe/jackyyf/navigator/ipgeo" "git.eve.moe/jackyyf/navigator/mapping" + "github.com/sirupsen/logrus" "log" "net/http" _ "net/http/pprof" + "os" ) var ( @@ -17,6 +19,8 @@ var ( func main() { flag.Parse() + logrus.SetReportCaller(true) + logrus.SetOutput(os.Stderr) ipgeo.Initialize() mapping.Initialize() log.Println("HTTP server is running on", *listen_spec) diff --git a/mapping/BUILD.bazel b/mapping/BUILD.bazel index 8a97cb5..63fa16a 100644 --- a/mapping/BUILD.bazel +++ b/mapping/BUILD.bazel @@ -5,5 +5,8 @@ go_library( srcs = ["map.go"], importpath = "git.eve.moe/jackyyf/navigator/mapping", visibility = ["//visibility:public"], - deps = ["//mapping/elf:go_default_library"], + deps = [ + "//mapping/elf:go_default_library", + "//mapping/firefly:go_default_library", + ], ) diff --git a/mapping/firefly/BUILD.bazel b/mapping/firefly/BUILD.bazel new file mode 100644 index 0000000..be64d53 --- /dev/null +++ b/mapping/firefly/BUILD.bazel @@ -0,0 +1,13 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["core.go"], + importpath = "git.eve.moe/jackyyf/navigator/mapping/firefly", + visibility = ["//visibility:public"], + deps = [ + "@com_github_jackc_pgx_v4//:go_default_library", + "@com_github_jackc_pgx_v4//pgxpool:go_default_library", + "@com_github_sirupsen_logrus//:go_default_library", + ], +) diff --git a/mapping/firefly/core.go b/mapping/firefly/core.go new file mode 100644 index 0000000..e36d197 --- /dev/null +++ b/mapping/firefly/core.go @@ -0,0 +1,70 @@ +package firefly + +import ( + "context" + "flag" + "github.com/jackc/pgx/v4" + "github.com/jackc/pgx/v4/pgxpool" + "github.com/sirupsen/logrus" +) + +var ( + pgsqlConn = flag.String("pgsql", "", "Connection spec for postgresql database") + pool *pgxpool.Pool +) + +func Initialize() { + var err error + config, err := pgxpool.ParseConfig(*pgsqlConn) + if err != nil { + panic(err) + } + if config.MaxConns == 0 { + config.MaxConns = 8 + } + if pool, err = pgxpool.Connect(context.Background(), *pgsqlConn); err != nil { + panic(err) + } + if conn, err := pool.Acquire(context.Background()); err != nil { + panic(err) + } else { + conn.Release() + } +} + +func AddSpeedTestResult(ip string, speed map[string]float64) { + conn, err := pool.Acquire(context.Background()) + if err != nil { + logrus.WithError(err).Error("Unable to get database connection") + return + } + defer conn.Release() + tx, err := conn.Begin(context.Background()) + if err != nil { + logrus.WithError(err).Error("Unable to start database transaction") + return + } + row := tx.QueryRow(context.Background(), + "INSERT INTO speedtest_entry (id, ip, timestamp) VALUES (DEFAULT, $1, now()) RETURNING id", ip) + var id int64 + if err = row.Scan(&id); err != nil { + logrus.WithError(err).Error("Unable to insert new speedtest result entry") + _ = tx.Rollback(context.Background()) + return + } + batch := &pgx.Batch{} + for name, value := range speed { + batch.Queue("INSERT INTO speedtest_result (entry_id, node, speed) VALUES ($1, $2, $3)", + id, name, value) + } + results := tx.SendBatch(context.Background(), batch) + if err = results.Close(); err != nil { + logrus.WithError(err).Error("Unable to insert speedtest results") + _ = tx.Rollback(context.Background()) + return + } + if err = tx.Commit(context.Background()); err != nil { + logrus.WithError(err).Error("Unable to commit the database transaction") + _ = tx.Rollback(context.Background()) + } +} diff --git a/mapping/map.go b/mapping/map.go index 92c88d1..d2cafbf 100644 --- a/mapping/map.go +++ b/mapping/map.go @@ -3,6 +3,7 @@ package mapping import ( "flag" "git.eve.moe/jackyyf/navigator/mapping/elf" + "git.eve.moe/jackyyf/navigator/mapping/firefly" ) var ( @@ -15,6 +16,7 @@ var ( func Initialize() { elf.Initialize() + firefly.Initialize() } func Get(ip string) string { diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..cb81f28 --- /dev/null +++ b/schema.sql @@ -0,0 +1,12 @@ +CREATE TABLE speedtest_entry ( + id bigserial PRIMARY KEY, + ip inet, + timestamp timestamptz +); + +CREATE TABLE speedtest_result ( + id bigserial PRIMARY KEY, + entry_id bigint REFERENCES speedtest_entry (id), + node varchar(32), + speed float8 +);