diff --git a/pkg/backend/api/response.go b/pkg/api/response.go similarity index 100% rename from pkg/backend/api/response.go rename to pkg/api/response.go diff --git a/pkg/backend/api/response_neighbors.go b/pkg/api/response_neighbors.go similarity index 100% rename from pkg/backend/api/response_neighbors.go rename to pkg/api/response_neighbors.go diff --git a/pkg/backend/api/response_routes.go b/pkg/api/response_routes.go similarity index 100% rename from pkg/backend/api/response_routes.go rename to pkg/api/response_routes.go diff --git a/pkg/backend/api/response_test.go b/pkg/api/response_test.go similarity index 100% rename from pkg/backend/api/response_test.go rename to pkg/api/response_test.go diff --git a/pkg/backend/api/search_filters.go b/pkg/api/search_filters.go similarity index 100% rename from pkg/backend/api/search_filters.go rename to pkg/api/search_filters.go diff --git a/pkg/backend/api/search_filters_parsers.go b/pkg/api/search_filters_parsers.go similarity index 100% rename from pkg/backend/api/search_filters_parsers.go rename to pkg/api/search_filters_parsers.go diff --git a/pkg/backend/api/search_filters_parsers_test.go b/pkg/api/search_filters_parsers_test.go similarity index 100% rename from pkg/backend/api/search_filters_parsers_test.go rename to pkg/api/search_filters_parsers_test.go diff --git a/pkg/backend/api/search_filters_test.go b/pkg/api/search_filters_test.go similarity index 100% rename from pkg/backend/api/search_filters_test.go rename to pkg/api/search_filters_test.go diff --git a/pkg/backend/api.go b/pkg/backend/api.go index 04ec35f..5b79063 100644 --- a/pkg/backend/api.go +++ b/pkg/backend/api.go @@ -3,14 +3,13 @@ package backend import ( "compress/gzip" "encoding/json" - "net/http" - "log" + "net/http" "strings" - "github.com/alice-lg/alice-lg/pkg/backend/api" - "github.com/julienschmidt/httprouter" + + "github.com/alice-lg/alice-lg/pkg/api" ) // Alice LG Rest API @@ -45,7 +44,7 @@ func endpoint(wrapped apiEndpoint) httprouter.Handle { result, err := wrapped(req, params) if err != nil { // Get affected rs id - rsId, paramErr := validateSourceId(params.ByName("id")) + rsId, paramErr := validateSourceID(params.ByName("id")) if paramErr != nil { rsId = "unknown" } diff --git a/pkg/backend/api_endpoints_meta.go b/pkg/backend/api_endpoints_meta.go index 1e0f2ba..50e87bd 100644 --- a/pkg/backend/api_endpoints_meta.go +++ b/pkg/backend/api_endpoints_meta.go @@ -1,10 +1,11 @@ package backend import ( - "github.com/alice-lg/alice-lg/backend/api" + "net/http" + "github.com/julienschmidt/httprouter" - "net/http" + "github.com/alice-lg/alice-lg/pkg/api" ) // Handle Status Endpoint, this is intended for @@ -16,7 +17,7 @@ func apiStatusShow(_req *http.Request, _params httprouter.Params) (api.Response, // Handle status func apiStatus(_req *http.Request, params httprouter.Params) (api.Response, error) { - rsId, err := validateSourceId(params.ByName("id")) + rsId, err := validateSourceID(params.ByName("id")) if err != nil { return nil, err } diff --git a/pkg/backend/api_endpoints_neighbors.go b/pkg/backend/api_endpoints_neighbors.go index 9745caa..6f775a8 100644 --- a/pkg/backend/api_endpoints_neighbors.go +++ b/pkg/backend/api_endpoints_neighbors.go @@ -1,11 +1,12 @@ package backend import ( - "github.com/alice-lg/alice-lg/backend/api" - "github.com/julienschmidt/httprouter" - "net/http" "sort" + + "github.com/julienschmidt/httprouter" + + "github.com/alice-lg/alice-lg/pkg/api" ) // Handle get neighbors on routeserver @@ -13,7 +14,7 @@ func apiNeighborsList( _req *http.Request, params httprouter.Params, ) (api.Response, error) { - rsId, err := validateSourceId(params.ByName("id")) + rsId, err := validateSourceID(params.ByName("id")) if err != nil { return nil, err } diff --git a/pkg/backend/api_endpoints_routes.go b/pkg/backend/api_endpoints_routes.go index f166997..ec0880e 100644 --- a/pkg/backend/api_endpoints_routes.go +++ b/pkg/backend/api_endpoints_routes.go @@ -1,16 +1,17 @@ package backend import ( - "github.com/alice-lg/alice-lg/backend/api" - "github.com/julienschmidt/httprouter" - "net/http" "time" + + "github.com/julienschmidt/httprouter" + + "github.com/alice-lg/alice-lg/pkg/api" ) // Handle routes func apiRoutesList(_req *http.Request, params httprouter.Params) (api.Response, error) { - rsId, err := validateSourceId(params.ByName("id")) + rsId, err := validateSourceID(params.ByName("id")) if err != nil { return nil, err } @@ -37,7 +38,7 @@ func apiRoutesListReceived( // Measure response time t0 := time.Now() - rsId, err := validateSourceId(params.ByName("id")) + rsId, err := validateSourceID(params.ByName("id")) if err != nil { return nil, err } @@ -110,7 +111,7 @@ func apiRoutesListFiltered( ) (api.Response, error) { t0 := time.Now() - rsId, err := validateSourceId(params.ByName("id")) + rsId, err := validateSourceID(params.ByName("id")) if err != nil { return nil, err } @@ -183,7 +184,7 @@ func apiRoutesListNotExported( ) (api.Response, error) { t0 := time.Now() - rsId, err := validateSourceId(params.ByName("id")) + rsId, err := validateSourceID(params.ByName("id")) if err != nil { return nil, err } diff --git a/pkg/backend/api_endpoints_routeservers.go b/pkg/backend/api_endpoints_routeservers.go index 1230863..9341022 100644 --- a/pkg/backend/api_endpoints_routeservers.go +++ b/pkg/backend/api_endpoints_routeservers.go @@ -1,11 +1,12 @@ package backend import ( - "github.com/alice-lg/alice-lg/backend/api" - "github.com/julienschmidt/httprouter" - "net/http" "sort" + + "github.com/julienschmidt/httprouter" + + "github.com/alice-lg/alice-lg/pkg/api" ) // Handle Routeservers List diff --git a/pkg/backend/api_endpoints_search.go b/pkg/backend/api_endpoints_search.go index 4f1eb60..78e7c8d 100644 --- a/pkg/backend/api_endpoints_search.go +++ b/pkg/backend/api_endpoints_search.go @@ -1,12 +1,13 @@ package backend import ( - "github.com/alice-lg/alice-lg/backend/api" - "github.com/julienschmidt/httprouter" - "net/http" "sort" "time" + + "github.com/julienschmidt/httprouter" + + "github.com/alice-lg/alice-lg/pkg/api" ) // Handle global lookup diff --git a/pkg/backend/api_errors.go b/pkg/backend/api_errors.go index 31f8402..ef5a34f 100644 --- a/pkg/backend/api_errors.go +++ b/pkg/backend/api_errors.go @@ -10,17 +10,24 @@ import ( "net/url" "strings" - "github.com/alice-lg/alice-lg/backend/api" + "github.com/alice-lg/alice-lg/pkg/api" ) +// ResourceNotFoundError is a 404 error type ResourceNotFoundError struct{} -func (self *ResourceNotFoundError) Error() string { +// Error implements the error interface and returns +// the error message +func (err *ResourceNotFoundError) Error() string { return "resource not found" } -var SOURCE_NOT_FOUND_ERROR = &ResourceNotFoundError{} +// Variables +var ( + SOURCE_NOT_FOUND_ERROR = &ResourceNotFoundError{} +) +// Error Constants const ( GENERIC_ERROR_TAG = "GENERIC_ERROR" CONNECTION_REFUSED_TAG = "CONNECTION_REFUSED" @@ -40,6 +47,7 @@ const ( RESOURCE_NOT_FOUND_STATUS = http.StatusNotFound ) +// Handle an error and create a error API response func apiErrorResponse(routeserverId string, err error) (api.ErrorResponse, int) { code := GENERIC_ERROR_CODE message := err.Error() diff --git a/pkg/backend/api_logging.go b/pkg/backend/api_logging.go index 5dbec1a..602495e 100644 --- a/pkg/backend/api_logging.go +++ b/pkg/backend/api_logging.go @@ -7,12 +7,12 @@ import ( ) // Log an api error -func apiLogSourceError(module string, sourceId string, params ...interface{}) { +func apiLogSourceError(module string, sourceID string, params ...interface{}) { var err error args := []string{} // Get source configuration - source := AliceConfig.SourceById(sourceId) + source := AliceConfig.SourceById(sourceID) sourceName := "unknown" if source != nil { sourceName = source.Name diff --git a/pkg/backend/api_pagination.go b/pkg/backend/api_pagination.go index 8c5d9a8..f8e6150 100644 --- a/pkg/backend/api_pagination.go +++ b/pkg/backend/api_pagination.go @@ -5,9 +5,9 @@ Paginate api routes responses */ import ( - "github.com/alice-lg/alice-lg/backend/api" - "math" + + "github.com/alice-lg/alice-lg/pkg/api" ) func apiPaginateRoutes( diff --git a/pkg/backend/api_pagination_test.go b/pkg/backend/api_pagination_test.go index febf3ac..86fa3a3 100644 --- a/pkg/backend/api_pagination_test.go +++ b/pkg/backend/api_pagination_test.go @@ -1,9 +1,9 @@ package backend import ( - "github.com/alice-lg/alice-lg/backend/api" - "testing" + + "github.com/alice-lg/alice-lg/pkg/api" ) func TestApiRoutesPagination(t *testing.T) { diff --git a/pkg/backend/api_query.go b/pkg/backend/api_query.go index d909723..7a67ebc 100644 --- a/pkg/backend/api_query.go +++ b/pkg/backend/api_query.go @@ -5,7 +5,7 @@ import ( "strconv" "strings" - "github.com/alice-lg/alice-lg/backend/api" + "github.com/alice-lg/alice-lg/pkg/api" ) /* diff --git a/pkg/backend/api_query_test.go b/pkg/backend/api_query_test.go index edac950..66d3524 100644 --- a/pkg/backend/api_query_test.go +++ b/pkg/backend/api_query_test.go @@ -5,7 +5,7 @@ import ( "net/url" "testing" - "github.com/alice-lg/alice-lg/backend/api" + "github.com/alice-lg/alice-lg/pkg/api" ) func makeQueryRequest(q string) *http.Request { diff --git a/pkg/backend/api_server.go b/pkg/backend/api_server.go new file mode 100644 index 0000000..c1b1a02 --- /dev/null +++ b/pkg/backend/api_server.go @@ -0,0 +1,28 @@ +package backend + +import ( + "log" + "net/http" + + "github.com/julienschmidt/httprouter" +) + +// StartHTTPServer starts a HTTP server with the config +// in the global AliceConfig. TODO: refactor. +func StartHTTPServer() { + + // Setup request routing + router := httprouter.New() + + // Serve static content + if err := webRegisterAssets(AliceConfig.Ui, router); err != nil { + log.Fatal(err) + } + + if err := apiRegisterEndpoints(router); err != nil { + log.Fatal(err) + } + + // Start http server + log.Fatal(http.ListenAndServe(AliceConfig.Server.Listen, router)) +} diff --git a/pkg/backend/api_validators.go b/pkg/backend/api_validators.go index 1b43e1e..5cb8ca7 100644 --- a/pkg/backend/api_validators.go +++ b/pkg/backend/api_validators.go @@ -8,7 +8,7 @@ import ( ) // Helper: Validate source Id -func validateSourceId(id string) (string, error) { +func validateSourceID(id string) (string, error) { if len(id) > 42 { return "unknown", fmt.Errorf("Source ID too long with length: %d", len(id)) } @@ -20,16 +20,16 @@ func validateQueryString(req *http.Request, key string) (string, error) { query := req.URL.Query() values, ok := query[key] if !ok { - return "", fmt.Errorf("Query param %s is missing.", key) + return "", fmt.Errorf("query param %s is missing", key) } if len(values) != 1 { - return "", fmt.Errorf("Query param %s is ambigous.", key) + return "", fmt.Errorf("query param %s is ambigous", key) } value := values[0] if value == "" { - return "", fmt.Errorf("Query param %s may not be empty.", key) + return "", fmt.Errorf("query param %s may not be empty", key) } return value, nil diff --git a/pkg/backend/backend.go b/pkg/backend/backend.go index 3c073cf..93de590 100644 --- a/pkg/backend/backend.go +++ b/pkg/backend/backend.go @@ -13,3 +13,9 @@ func InitConfig(filename string) error { AliceConfig, err := loadConfig(*configFilenameFlag) return err } + +// InitStores initializes the routes and neighbors cache +func InitStores() { + AliceNeighboursStore = NewNeighboursStore(AliceConfig) + AliceRoutesStore = NewRoutesStore(AliceConfig) +} diff --git a/pkg/backend/caches/lru.go b/pkg/caches/lru.go similarity index 100% rename from pkg/backend/caches/lru.go rename to pkg/caches/lru.go diff --git a/pkg/backend/caches/lru_test.go b/pkg/caches/lru_test.go similarity index 100% rename from pkg/backend/caches/lru_test.go rename to pkg/caches/lru_test.go diff --git a/pkg/backend/caches/neighbors.go b/pkg/caches/neighbors.go similarity index 100% rename from pkg/backend/caches/neighbors.go rename to pkg/caches/neighbors.go diff --git a/pkg/backend/caches/neighbors_test.go b/pkg/caches/neighbors_test.go similarity index 100% rename from pkg/backend/caches/neighbors_test.go rename to pkg/caches/neighbors_test.go diff --git a/pkg/backend/caches/routes.go b/pkg/caches/routes.go similarity index 100% rename from pkg/backend/caches/routes.go rename to pkg/caches/routes.go diff --git a/pkg/backend/caches/routes_test.go b/pkg/caches/routes_test.go similarity index 100% rename from pkg/backend/caches/routes_test.go rename to pkg/caches/routes_test.go diff --git a/pkg/backend/sources/birdwatcher/client.go b/pkg/sources/birdwatcher/client.go similarity index 100% rename from pkg/backend/sources/birdwatcher/client.go rename to pkg/sources/birdwatcher/client.go diff --git a/pkg/backend/sources/birdwatcher/config.go b/pkg/sources/birdwatcher/config.go similarity index 100% rename from pkg/backend/sources/birdwatcher/config.go rename to pkg/sources/birdwatcher/config.go diff --git a/pkg/backend/sources/birdwatcher/parsers.go b/pkg/sources/birdwatcher/parsers.go similarity index 100% rename from pkg/backend/sources/birdwatcher/parsers.go rename to pkg/sources/birdwatcher/parsers.go diff --git a/pkg/backend/sources/birdwatcher/parsers_test.go b/pkg/sources/birdwatcher/parsers_test.go similarity index 100% rename from pkg/backend/sources/birdwatcher/parsers_test.go rename to pkg/sources/birdwatcher/parsers_test.go diff --git a/pkg/backend/sources/birdwatcher/source.go b/pkg/sources/birdwatcher/source.go similarity index 100% rename from pkg/backend/sources/birdwatcher/source.go rename to pkg/sources/birdwatcher/source.go diff --git a/pkg/backend/sources/birdwatcher/source_multitable.go b/pkg/sources/birdwatcher/source_multitable.go similarity index 100% rename from pkg/backend/sources/birdwatcher/source_multitable.go rename to pkg/sources/birdwatcher/source_multitable.go diff --git a/pkg/backend/sources/birdwatcher/source_multitable_test.go b/pkg/sources/birdwatcher/source_multitable_test.go similarity index 100% rename from pkg/backend/sources/birdwatcher/source_multitable_test.go rename to pkg/sources/birdwatcher/source_multitable_test.go diff --git a/pkg/backend/sources/birdwatcher/source_singletable.go b/pkg/sources/birdwatcher/source_singletable.go similarity index 100% rename from pkg/backend/sources/birdwatcher/source_singletable.go rename to pkg/sources/birdwatcher/source_singletable.go diff --git a/pkg/backend/sources/birdwatcher/types.go b/pkg/sources/birdwatcher/types.go similarity index 100% rename from pkg/backend/sources/birdwatcher/types.go rename to pkg/sources/birdwatcher/types.go diff --git a/pkg/backend/sources/birdwatcher/utils.go b/pkg/sources/birdwatcher/utils.go similarity index 100% rename from pkg/backend/sources/birdwatcher/utils.go rename to pkg/sources/birdwatcher/utils.go diff --git a/pkg/backend/sources/birdwatcher/utils_tests.go b/pkg/sources/birdwatcher/utils_tests.go similarity index 100% rename from pkg/backend/sources/birdwatcher/utils_tests.go rename to pkg/sources/birdwatcher/utils_tests.go diff --git a/pkg/backend/sources/gobgp/apiutil/attribute.go b/pkg/sources/gobgp/apiutil/attribute.go similarity index 100% rename from pkg/backend/sources/gobgp/apiutil/attribute.go rename to pkg/sources/gobgp/apiutil/attribute.go diff --git a/pkg/backend/sources/gobgp/apiutil/attribute_test.go b/pkg/sources/gobgp/apiutil/attribute_test.go similarity index 100% rename from pkg/backend/sources/gobgp/apiutil/attribute_test.go rename to pkg/sources/gobgp/apiutil/attribute_test.go diff --git a/pkg/backend/sources/gobgp/apiutil/capability.go b/pkg/sources/gobgp/apiutil/capability.go similarity index 100% rename from pkg/backend/sources/gobgp/apiutil/capability.go rename to pkg/sources/gobgp/apiutil/capability.go diff --git a/pkg/backend/sources/gobgp/apiutil/capability_test.go b/pkg/sources/gobgp/apiutil/capability_test.go similarity index 100% rename from pkg/backend/sources/gobgp/apiutil/capability_test.go rename to pkg/sources/gobgp/apiutil/capability_test.go diff --git a/pkg/backend/sources/gobgp/apiutil/util.go b/pkg/sources/gobgp/apiutil/util.go similarity index 100% rename from pkg/backend/sources/gobgp/apiutil/util.go rename to pkg/sources/gobgp/apiutil/util.go diff --git a/pkg/backend/sources/gobgp/config.go b/pkg/sources/gobgp/config.go similarity index 100% rename from pkg/backend/sources/gobgp/config.go rename to pkg/sources/gobgp/config.go diff --git a/pkg/backend/sources/gobgp/routes.go b/pkg/sources/gobgp/routes.go similarity index 100% rename from pkg/backend/sources/gobgp/routes.go rename to pkg/sources/gobgp/routes.go diff --git a/pkg/backend/sources/gobgp/source.go b/pkg/sources/gobgp/source.go similarity index 100% rename from pkg/backend/sources/gobgp/source.go rename to pkg/sources/gobgp/source.go diff --git a/pkg/backend/sources/gobgp/utils.go b/pkg/sources/gobgp/utils.go similarity index 100% rename from pkg/backend/sources/gobgp/utils.go rename to pkg/sources/gobgp/utils.go diff --git a/pkg/backend/sources/source.go b/pkg/sources/source.go similarity index 100% rename from pkg/backend/sources/source.go rename to pkg/sources/source.go diff --git a/pkg/backend/testdata/api/neighbor_summary.json b/pkg/testdata/api/neighbor_summary.json similarity index 100% rename from pkg/backend/testdata/api/neighbor_summary.json rename to pkg/testdata/api/neighbor_summary.json diff --git a/pkg/backend/testdata/api/routes_response.json b/pkg/testdata/api/routes_response.json similarity index 100% rename from pkg/backend/testdata/api/routes_response.json rename to pkg/testdata/api/routes_response.json