Browse Source

Exclude index page from authentication

feature/fix-routes
Fabian Vowie 3 years ago
parent
commit
1209539d40
No known key found for this signature in database GPG Key ID: C27317C33B27C410
  1. 17
      main.go
  2. 11
      main_test.go
  3. 2
      settings/settings.go

17
main.go

@ -130,7 +130,7 @@ func UploadHandler(w http.ResponseWriter, r *http.Request, pipes []pipelines.IPi
logrus.Info("Pipeline routes registered successfully") logrus.Info("Pipeline routes registered successfully")
} }
func RegisterRoutes(r *mux.Router, pipelines []pipelines.IPipeline, storageProvider storage.IStorageProvider) {
func RegisterRoutes(r *mux.Router, appSettings settings.Settings, pipelines []pipelines.IPipeline, storageProvider storage.IStorageProvider) {
index := r.Methods(http.MethodGet).Subrouter() index := r.Methods(http.MethodGet).Subrouter()
index.HandleFunc("/", IndexHandler) index.HandleFunc("/", IndexHandler)
@ -150,6 +150,13 @@ func RegisterRoutes(r *mux.Router, pipelines []pipelines.IPipeline, storageProvi
w.WriteHeader(404) w.WriteHeader(404)
}) })
if appSettings.Authentication.Enabled {
authMiddleware := middlewares.CreateAuthenticationMiddleware(appSettings.Authentication.Token)
upload.Use(authMiddleware.Middleware)
pipeline.Use(authMiddleware.Middleware)
}
} }
func main() { func main() {
@ -182,12 +189,6 @@ func main() {
r := mux.NewRouter() r := mux.NewRouter()
if appSettings.Authentication.Enabled {
authMiddleware := middlewares.CreateAuthenticationMiddleware(appSettings.Authentication.Token)
r.Use(authMiddleware.Middleware)
}
if appSettings.RateLimiter.Enabled { if appSettings.RateLimiter.Enabled {
rateLimiterMiddleware, err := middlewares.CreateRateLimiterMiddleware(appSettings.RateLimiter.RequestsPerMinute, appSettings.RateLimiter.AllowedBurst) rateLimiterMiddleware, err := middlewares.CreateRateLimiterMiddleware(appSettings.RateLimiter.RequestsPerMinute, appSettings.RateLimiter.AllowedBurst)
if err != nil { if err != nil {
@ -197,7 +198,7 @@ func main() {
r.Use(rateLimiterMiddleware.Middleware) r.Use(rateLimiterMiddleware.Middleware)
} }
RegisterRoutes(r, pipes, storageProvider)
RegisterRoutes(r, appSettings, pipes, storageProvider)
logrus.Info("Lithium started, listening for requests...") logrus.Info("Lithium started, listening for requests...")

11
main_test.go

@ -10,8 +10,10 @@ import (
"github.com/bxcodec/faker/v3" "github.com/bxcodec/faker/v3"
"github.com/geplauder/lithium/pipelines" "github.com/geplauder/lithium/pipelines"
"github.com/geplauder/lithium/settings"
"github.com/geplauder/lithium/storage" "github.com/geplauder/lithium/storage"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -35,8 +37,9 @@ func TestEndpointRoute(t *testing.T) {
t.Run("Registered pipelines are valid routes", func(t *testing.T) { t.Run("Registered pipelines are valid routes", func(t *testing.T) {
router := mux.NewRouter() router := mux.NewRouter()
fs := storage.GetMemoryStorageProvider() fs := storage.GetMemoryStorageProvider()
appSettings, _ := settings.LoadSettings(afero.NewMemMapFs())
RegisterRoutes(router, []pipelines.IPipeline{data}, fs)
RegisterRoutes(router, appSettings, []pipelines.IPipeline{data}, fs)
request, _ := http.NewRequest("GET", "/pipelines/"+data.Slug, nil) request, _ := http.NewRequest("GET", "/pipelines/"+data.Slug, nil)
responseRecorder := httptest.NewRecorder() responseRecorder := httptest.NewRecorder()
@ -64,8 +67,9 @@ func TestUploadRoute(t *testing.T) {
t.Run("Test uploads missing multipart boundary", func(t *testing.T) { t.Run("Test uploads missing multipart boundary", func(t *testing.T) {
router := mux.NewRouter() router := mux.NewRouter()
fs := storage.GetMemoryStorageProvider() fs := storage.GetMemoryStorageProvider()
appSettings, _ := settings.LoadSettings(afero.NewMemMapFs())
RegisterRoutes(router, []pipelines.IPipeline{pipelines.Pipeline{
RegisterRoutes(router, appSettings, []pipelines.IPipeline{pipelines.Pipeline{
Name: "", Name: "",
Slug: "", Slug: "",
Type: 0, Type: 0,
@ -92,8 +96,9 @@ func TestUploadRoute(t *testing.T) {
t.Run("Test uploads missing multipart boundary", func(t *testing.T) { t.Run("Test uploads missing multipart boundary", func(t *testing.T) {
router := mux.NewRouter() router := mux.NewRouter()
fs := storage.GetMemoryStorageProvider() fs := storage.GetMemoryStorageProvider()
appSettings, _ := settings.LoadSettings(afero.NewMemMapFs())
RegisterRoutes(router, []pipelines.IPipeline{pipelines.Pipeline{
RegisterRoutes(router, appSettings, []pipelines.IPipeline{pipelines.Pipeline{
Name: "", Name: "",
Slug: "", Slug: "",
Type: 0, Type: 0,

2
settings/settings.go

@ -63,7 +63,7 @@ func LoadSettings(fileSystem afero.Fs) (Settings, error) {
defaultSettings := Settings{ defaultSettings := Settings{
Endpoint: "127.0.0.1:8000", Endpoint: "127.0.0.1:8000",
Authentication: AuthenticationSettings{ Authentication: AuthenticationSettings{
Enabled: true,
Enabled: false,
Token: "changeme", Token: "changeme",
}, },
RateLimiter: RateLimiterSettings{ RateLimiter: RateLimiterSettings{

Loading…
Cancel
Save