1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00

123 lines
3.3 KiB
Go
Raw Normal View History

2021-10-22 22:40:03 +02:00
package http
2017-05-18 14:44:50 +02:00
import (
"compress/gzip"
"encoding/json"
"log"
2021-03-22 16:50:08 +01:00
"net/http"
2017-05-18 14:44:50 +02:00
"strings"
"github.com/julienschmidt/httprouter"
2021-03-22 16:50:08 +01:00
"github.com/alice-lg/alice-lg/pkg/api"
2021-10-25 22:03:10 +02:00
"github.com/alice-lg/alice-lg/pkg/config"
2017-05-18 14:44:50 +02:00
)
// Alice LG Rest API
//
// The API provides endpoints for getting
// information from the routeservers / alice datasources.
//
// Endpoints:
//
// Config
2018-10-03 17:59:07 +02:00
// Show /api/v1/config
2017-05-18 14:44:50 +02:00
//
// Routeservers
2018-10-03 17:59:07 +02:00
// List /api/v1/routeservers
// Status /api/v1/routeservers/:id/status
2018-10-03 18:02:56 +02:00
// Neighbors /api/v1/routeservers/:id/neighbors
// Routes /api/v1/routeservers/:id/neighbors/:neighborId/routes
2017-05-18 14:44:50 +02:00
//
2017-05-23 13:58:58 +02:00
// Querying
2019-09-25 01:53:21 +02:00
// LookupPrefix /api/v1/lookup/prefix?q=<prefix>
// LookupNeighbor /api/v1/lookup/neighbor?asn=1235
2017-05-18 14:44:50 +02:00
2017-05-18 15:26:22 +02:00
type apiEndpoint func(*http.Request, httprouter.Params) (api.Response, error)
2017-05-18 14:44:50 +02:00
// Wrap handler for access controll, throtteling and compression
func endpoint(wrapped apiEndpoint) httprouter.Handle {
return func(res http.ResponseWriter,
req *http.Request,
params httprouter.Params) {
// Get result from handler
result, err := wrapped(req, params)
if err != nil {
2018-08-05 18:25:59 +02:00
// Get affected rs id
2021-10-15 12:03:16 +02:00
rsID, paramErr := validateSourceID(params.ByName("id"))
2018-08-05 18:25:59 +02:00
if paramErr != nil {
2021-10-15 12:03:16 +02:00
rsID = "unknown"
2017-05-18 14:44:50 +02:00
}
2018-08-05 18:25:59 +02:00
// Make error response
2021-10-15 12:03:16 +02:00
result, status := apiErrorResponse(rsID, err)
2017-05-18 14:44:50 +02:00
payload, _ := json.Marshal(result)
2019-01-11 20:06:03 +01:00
http.Error(res, string(payload), status)
2017-05-18 14:44:50 +02:00
return
}
// Encode json
payload, err := json.Marshal(result)
if err != nil {
msg := "Could not encode result as json"
http.Error(res, msg, http.StatusInternalServerError)
log.Println(err)
log.Println("This is most likely due to an older version of go.")
log.Println("Consider upgrading to golang > 1.8")
return
}
2017-05-18 14:44:50 +02:00
// Set response header
res.Header().Set("Content-Type", "application/json")
// Check if compression is supported
if strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
// Compress response
res.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(res)
defer gz.Close()
gz.Write(payload)
} else {
res.Write(payload) // Fall back to uncompressed response
}
}
}
// Register api endpoints
2021-10-26 23:11:42 +02:00
func (s *Server) apiRegisterEndpoints(
cfg *config.Config,
router *httprouter.Router,
) error {
2017-05-18 14:44:50 +02:00
2017-05-19 11:32:22 +02:00
// Meta
2021-10-26 23:11:42 +02:00
router.GET("/api/v1/status", endpoint(s.apiStatusShow))
router.GET("/api/v1/config", endpoint(s.apiConfigShow))
2017-05-18 14:44:50 +02:00
// Routeservers
2018-10-03 17:59:07 +02:00
router.GET("/api/v1/routeservers",
2021-10-26 23:11:42 +02:00
endpoint(s.apiRouteserversList))
2018-10-03 17:59:07 +02:00
router.GET("/api/v1/routeservers/:id/status",
2021-10-26 23:11:42 +02:00
endpoint(s.apiStatus))
2018-10-03 18:02:56 +02:00
router.GET("/api/v1/routeservers/:id/neighbors",
2021-10-26 23:11:42 +02:00
endpoint(s.apiNeighborsList))
2018-10-03 18:02:56 +02:00
router.GET("/api/v1/routeservers/:id/neighbors/:neighborId/routes",
2021-10-26 23:11:42 +02:00
endpoint(s.apiRoutesList))
2018-10-03 18:02:56 +02:00
router.GET("/api/v1/routeservers/:id/neighbors/:neighborId/routes/received",
2021-10-26 23:11:42 +02:00
endpoint(s.apiRoutesListReceived))
2018-10-03 18:02:56 +02:00
router.GET("/api/v1/routeservers/:id/neighbors/:neighborId/routes/filtered",
2021-10-26 23:11:42 +02:00
endpoint(s.apiRoutesListFiltered))
2018-10-03 18:02:56 +02:00
router.GET("/api/v1/routeservers/:id/neighbors/:neighborId/routes/not-exported",
2021-10-26 23:11:42 +02:00
endpoint(s.apiRoutesListNotExported))
2017-05-18 14:44:50 +02:00
2017-05-23 13:58:58 +02:00
// Querying
2021-10-25 22:03:10 +02:00
if cfg.Server.EnablePrefixLookup == true {
2018-10-03 17:59:07 +02:00
router.GET("/api/v1/lookup/prefix",
2021-10-26 23:11:42 +02:00
endpoint(s.apiLookupPrefixGlobal))
2019-10-07 18:13:08 +02:00
router.GET("/api/v1/lookup/neighbors",
2021-10-26 23:11:42 +02:00
endpoint(s.apiLookupNeighborsGlobal))
2017-07-04 12:36:48 +02:00
}
2017-06-19 16:44:24 +02:00
2017-05-18 14:44:50 +02:00
return nil
}