58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package routes
|
|
|
|
import (
|
|
"net/http"
|
|
"fmt"
|
|
"github.com/tus/tusd/pkg/filestore"
|
|
tusd "github.com/tus/tusd/pkg/handler"
|
|
)
|
|
|
|
func RegisterRoutes() {
|
|
// Regular Handlers
|
|
http.HandleFunc("/hello", helloHandler)
|
|
|
|
// Uploads Handler
|
|
http.Handle("/uploads/", uploadsHandler())
|
|
}
|
|
|
|
func uploadsHandler() http.Handler {
|
|
store := filestore.FileStore{
|
|
Path: "./uploads",
|
|
}
|
|
composer := tusd.NewStoreComposer()
|
|
store.UseIn(composer)
|
|
|
|
handler, err := tusd.NewHandler(tusd.Config{
|
|
BasePath: "/uploads/",
|
|
StoreComposer: composer,
|
|
NotifyCompleteUploads: true,
|
|
})
|
|
|
|
if err != nil {
|
|
panic(fmt.Errorf("Unable to create handler: %s", err))
|
|
}
|
|
|
|
go func() {
|
|
for {
|
|
event := <-handler.CompleteUploads
|
|
fmt.Printf("Upload %s finished\n", event.Upload.ID)
|
|
}
|
|
}()
|
|
|
|
return http.StripPrefix("/uploads/", handler)
|
|
}
|
|
|
|
func helloHandler(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/hello" {
|
|
http.Error(w, "404 not found.", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
if r.Method != "GET" {
|
|
http.Error(w, "Method is not supported.", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
fmt.Fprintf(w, "Hello!")
|
|
}
|