2021-05-22 13:12:14 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/fs"
|
2021-05-23 00:46:15 +08:00
|
|
|
"log"
|
2021-05-22 13:12:14 +08:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
listen := os.Getenv("LISTEN")
|
|
|
|
if listen == "" {
|
|
|
|
listen = ":8080"
|
|
|
|
}
|
|
|
|
|
|
|
|
assetsWeb, err := fs.Sub(assets, "web")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
setupHandlers()
|
2021-05-23 00:46:15 +08:00
|
|
|
setupS3()
|
2021-05-22 13:12:14 +08:00
|
|
|
|
|
|
|
router := mux.NewRouter()
|
2021-05-23 00:46:15 +08:00
|
|
|
router.Use(middlewareLogger)
|
|
|
|
|
|
|
|
router.Methods(http.MethodGet).PathPrefix("/assets").Handler(http.FileServer(http.FS(assetsWeb)))
|
|
|
|
router.Methods(http.MethodGet).Path("/favicon.ico").Handler(http.FileServer(http.FS(assetsWeb)))
|
|
|
|
router.Methods(http.MethodGet).Path("/").HandlerFunc(handleCreate)
|
2021-05-22 23:53:00 +08:00
|
|
|
uploadRouter := router.PathPrefix("/{id}").Subrouter()
|
|
|
|
|
2021-05-23 00:46:15 +08:00
|
|
|
uploadTemplateRouter := uploadRouter.Path("").Subrouter()
|
2021-05-22 23:53:00 +08:00
|
|
|
s3Router := uploadRouter.PathPrefix("/s3/multipart").Subrouter()
|
|
|
|
|
2021-05-23 00:46:15 +08:00
|
|
|
uploadTemplateRouter.Methods(http.MethodGet).Path("").HandlerFunc(handleUpload)
|
|
|
|
|
2021-05-22 23:53:00 +08:00
|
|
|
s3Router.Methods(http.MethodPost).Path("").HandlerFunc(handleCreateMultipartUpload)
|
|
|
|
s3Router.Methods(http.MethodGet).Path("/{uploadID}").HandlerFunc(handleGetUploadedParts)
|
|
|
|
s3Router.Methods(http.MethodGet).Path("/{uploadID}/{uploadPart}").HandlerFunc(handleSignPartUpload)
|
|
|
|
s3Router.Methods(http.MethodPost).Path("/{uploadID}/complete").HandlerFunc(handleCompleteMultipartUpload)
|
|
|
|
s3Router.Methods(http.MethodDelete).Path("/{uploadID}").HandlerFunc(handleAbortMultipartUpload)
|
2021-05-22 13:12:14 +08:00
|
|
|
|
|
|
|
server := &http.Server{
|
|
|
|
Handler: router,
|
|
|
|
Addr: listen,
|
|
|
|
ReadTimeout: 5 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
}
|
2021-05-23 00:46:15 +08:00
|
|
|
log.Printf("listeining on %s", listen)
|
2021-05-22 13:12:14 +08:00
|
|
|
err = server.ListenAndServe()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|