|
|
package main
import ( "github.com/geplauder/lithium/controllers" "net/http" "os"
"io"
"github.com/geplauder/lithium/middlewares" "github.com/geplauder/lithium/pipelines" "github.com/geplauder/lithium/settings" "github.com/geplauder/lithium/storage" "github.com/gorilla/mux" "github.com/sirupsen/logrus" "github.com/spf13/afero" )
var GitCommit string
func RegisterRoutes(r *mux.Router, appSettings settings.Settings, pipelines []pipelines.IPipeline, storageProvider storage.IStorageProvider) { index := r.Methods(http.MethodGet).Subrouter() index.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { controllers.IndexHandler(pipelines, w, r, GitCommit) })
upload := r.Methods(http.MethodPost).Subrouter() upload.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) { controllers.UploadHandler(w, r, pipelines, storageProvider) })
files := r.Methods(http.MethodGet).Subrouter() files.HandleFunc("/files", func(w http.ResponseWriter, r *http.Request) { controllers.FileHandler(w, r, pipelines, storageProvider, appSettings) })
pipeline := r.Methods(http.MethodGet).Subrouter() pipeline.HandleFunc("/pipelines/{pipeline}", func(w http.ResponseWriter, r *http.Request) { for _, pipeline := range pipelines { if pipeline.GetSlug() == mux.Vars(r)["pipeline"] { controllers.PipelineHandler(pipeline, storageProvider, w, r) return } }
w.WriteHeader(404) })
if appSettings.Authentication.Enabled { authMiddleware := middlewares.CreateAuthenticationMiddleware(appSettings.Authentication.Token)
upload.Use(authMiddleware.Middleware) files.Use(authMiddleware.Middleware) pipeline.Use(authMiddleware.Middleware) } }
func main() { logFile, err := os.OpenFile("lithium.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) if err == nil { multiWriter := io.MultiWriter(os.Stdout, logFile) logrus.SetOutput(multiWriter) } else { logrus.SetOutput(os.Stdout) }
logrus.SetFormatter(&logrus.JSONFormatter{})
appSettings, err := settings.LoadSettings(afero.NewOsFs()) if err != nil { logrus.Fatal("Unexpected error while loading settings: ", err) }
var storageProvider storage.IStorageProvider
if appSettings.StorageProvider.Type == 0 { storageProvider = storage.GetFileSystemStorageProvider(appSettings.StorageProvider.BasePath, "") } else { logrus.WithFields(logrus.Fields{ "StorageProviderType": appSettings.StorageProvider.Type, }).Fatal("Invalid file system provided") }
pipes := pipelines.LoadPipelines()
r := mux.NewRouter()
if appSettings.RateLimiter.Enabled { rateLimiterMiddleware, err := middlewares.CreateRateLimiterMiddleware(appSettings.RateLimiter.RequestsPerMinute, appSettings.RateLimiter.AllowedBurst) if err != nil { logrus.Fatal("Unexpected error while creating rate limiter middleware: ", err) }
r.Use(rateLimiterMiddleware.Middleware) }
RegisterRoutes(r, appSettings, pipes, storageProvider)
logrus.Info("Lithium started, listening for requests...")
err = http.ListenAndServe(appSettings.Endpoint, r) if err != nil { logrus.Fatal("Unexpected error while serving http server: ", err) } }
|