Initial refactoring, WIP
This commit is contained in:
parent
be8535b206
commit
0e87800ddc
|
@ -1,10 +1,12 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
tushttp "github.com/tus/tusd/src/http"
|
tushttp "github.com/tus/tusd/src/http"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -12,12 +14,41 @@ func main() {
|
||||||
log.Printf("tusd started")
|
log.Printf("tusd started")
|
||||||
|
|
||||||
addr := ":1080"
|
addr := ":1080"
|
||||||
if port := os.Getenv("TUSD_PORT"); port != "" {
|
if envPort := os.Getenv("TUSD_PORT"); envPort != "" {
|
||||||
addr = ":" + port
|
addr = ":" + envPort
|
||||||
}
|
}
|
||||||
log.Printf("servering clients at http://localhost%s", addr)
|
|
||||||
|
|
||||||
handler := tushttp.NewHandler()
|
maxSize := int64(1024 * 1024 * 1024)
|
||||||
|
if envMaxSize := os.Getenv("TUSD_DATA_STORE_MAXSIZE"); envMaxSize != "" {
|
||||||
|
parsed, err := strconv.ParseInt(envMaxSize, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
panic("bad TUSD_DATA_STORE_MAXSIZE: " + err.Error())
|
||||||
|
}
|
||||||
|
maxSize = parsed
|
||||||
|
}
|
||||||
|
|
||||||
|
dir := os.Getenv("TUSD_DATA_DIR")
|
||||||
|
if dir == "" {
|
||||||
|
if workingDir, err := os.Getwd(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
} else {
|
||||||
|
dir = filepath.Join(workingDir, "tus_data")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
config := tushttp.HandlerConfig{
|
||||||
|
Dir: dir,
|
||||||
|
MaxSize: maxSize,
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("handler config: %+v", config)
|
||||||
|
|
||||||
|
handler, err := tushttp.NewHandler(config)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("servering clients at http://localhost%s", addr)
|
||||||
if err := http.ListenAndServe(addr, handler); err != nil {
|
if err := http.ListenAndServe(addr, handler); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sort"
|
"sort"
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
@ -18,7 +18,7 @@ type DataStore struct {
|
||||||
maxSize int64
|
maxSize int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDataStore(dir string, maxSize int64) *DataStore {
|
func newDataStore(dir string, maxSize int64) *DataStore {
|
||||||
store := &DataStore{dir: dir, maxSize: maxSize}
|
store := &DataStore{dir: dir, maxSize: maxSize}
|
||||||
go store.gcLoop()
|
go store.gcLoop()
|
||||||
return store
|
return store
|
||||||
|
@ -139,6 +139,10 @@ func (s *DataStore) logPath(id string) string {
|
||||||
return path.Join(s.dir, id) + ".log"
|
return path.Join(s.dir, id) + ".log"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: This works for now, but it would be better if we would trigger gc()
|
||||||
|
// manually whenever a storage operation will need more space, telling gc() how
|
||||||
|
// much space we need. If the amount of space required fits into the max, we
|
||||||
|
// can simply ignore the gc request, otherwise delete just as much as we need.
|
||||||
func (s *DataStore) gcLoop() {
|
func (s *DataStore) gcLoop() {
|
||||||
for {
|
for {
|
||||||
if before, after, err := s.gc(); err != nil {
|
if before, after, err := s.gc(); err != nil {
|
|
@ -1,14 +1,37 @@
|
||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewHandler() *Handler {
|
type HandlerConfig struct{
|
||||||
return &Handler{}
|
// Dir points to a filesystem path used by tus to store uploaded and partial
|
||||||
|
// files. Will be created if does not exist yet. Required.
|
||||||
|
Dir string
|
||||||
|
|
||||||
|
// MaxSize defines how many bytes may be stored inside Dir. Exceeding this
|
||||||
|
// limit will cause the oldest upload files to be deleted until enough space
|
||||||
|
// is available again. Required.
|
||||||
|
MaxSize int64
|
||||||
}
|
}
|
||||||
|
|
||||||
type Handler struct {}
|
func NewHandler(config HandlerConfig) (*Handler, error) {
|
||||||
|
// Ensure the data store directory exists
|
||||||
|
if err := os.MkdirAll(config.Dir, 0777); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Handler) ServeHTTP(http.ResponseWriter, *http.Request) {
|
return &Handler{
|
||||||
|
store: newDataStore(config.Dir, config.MaxSize),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type Handler struct{
|
||||||
|
store *DataStore
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Printf("request: %s %s", r.Method, r.URL.RequestURI())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue