even more structural refactoring
This commit is contained in:
parent
0af0d9e183
commit
393625ee43
@ -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"
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
|
@ -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
|
||||
|
@ -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(
|
||||
|
@ -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) {
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/alice-lg/alice-lg/backend/api"
|
||||
"github.com/alice-lg/alice-lg/pkg/api"
|
||||
)
|
||||
|
||||
/*
|
||||
|
@ -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 {
|
||||
|
28
pkg/backend/api_server.go
Normal file
28
pkg/backend/api_server.go
Normal file
@ -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))
|
||||
}
|
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user