even more structural refactoring

This commit is contained in:
Annika Hannig 2021-03-22 16:50:08 +01:00
parent 0af0d9e183
commit 393625ee43
No known key found for this signature in database
GPG Key ID: 62E226E47DDCE58D
52 changed files with 86 additions and 40 deletions

View File

@ -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"
}

View File

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

View File

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

View File

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

View File

@ -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

View File

@ -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

View File

@ -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()

View File

@ -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

View File

@ -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(

View File

@ -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) {

View File

@ -5,7 +5,7 @@ import (
"strconv"
"strings"
"github.com/alice-lg/alice-lg/backend/api"
"github.com/alice-lg/alice-lg/pkg/api"
)
/*

View File

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

View File

@ -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

View File

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