You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.9 KiB
72 lines
1.9 KiB
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/geplauder/lithium/auth"
|
|
"github.com/geplauder/lithium/pipelines"
|
|
"github.com/geplauder/lithium/settings"
|
|
"github.com/geplauder/lithium/storage"
|
|
"github.com/gorilla/mux"
|
|
"github.com/spf13/afero"
|
|
)
|
|
|
|
const Name string = "Lithium"
|
|
const Version string = "0.1.0"
|
|
|
|
type Metadata struct {
|
|
Name string `json:"name"`
|
|
Version string `json:"version"`
|
|
}
|
|
|
|
func PipelineHandler(pipeline pipelines.IPipeline, storageProvider storage.IStorageProvider, w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
err := json.NewEncoder(w).Encode(pipeline)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
err := json.NewEncoder(w).Encode(Metadata{Name, Version})
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline, storageProvider storage.IStorageProvider) {
|
|
for _, pipeline := range pipelines {
|
|
r.HandleFunc("/"+pipeline.GetSlug(), func(w http.ResponseWriter, r *http.Request) {
|
|
PipelineHandler(pipeline, storageProvider, w, r)
|
|
})
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
settings := settings.LoadSettings(afero.NewOsFs())
|
|
|
|
var storageProvider storage.IStorageProvider
|
|
|
|
if settings.StorageProvider.Type == 0 {
|
|
storageProvider = storage.GetFileSystemStorageProvider(settings.StorageProvider.BasePath, "")
|
|
} else {
|
|
panic("Invalid file system provided!")
|
|
}
|
|
|
|
pipes := pipelines.LoadPipelines()
|
|
|
|
authMiddleware := auth.CreateAuthenticationMiddleware(settings.Token)
|
|
|
|
r := mux.NewRouter()
|
|
r.Use(authMiddleware.Middleware)
|
|
r.HandleFunc("/", IndexHandler)
|
|
|
|
RegisterPipelineRoutes(r, pipes, storageProvider)
|
|
|
|
err := http.ListenAndServe(":8000", r)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|