From f3c5b266d803f8ef7c3508e3dca67512f8ebaf10 Mon Sep 17 00:00:00 2001 From: Mans Ziesel Date: Thu, 25 Jan 2024 20:40:36 +0100 Subject: [PATCH] add simple html page and bit of functionality --- app/app.go | 63 +++++++++++++++++++++++++++++++++++++++++--- cmd/server/go.mod | 3 --- go.mod | 5 +++- go.sum | 2 ++ gomod2nix.toml | 3 +++ static/script.js | 23 ++++++++++++++++ static/style.css | 20 ++++++++++++++ templates/index.html | 36 +++++++++++++++++++++++++ 8 files changed, 148 insertions(+), 7 deletions(-) delete mode 100644 cmd/server/go.mod create mode 100644 static/script.js create mode 100644 static/style.css create mode 100644 templates/index.html diff --git a/app/app.go b/app/app.go index 66876f6..f672b1b 100644 --- a/app/app.go +++ b/app/app.go @@ -4,9 +4,13 @@ import ( "context" "database/sql" "fmt" + "html/template" "log" - "time" "net/http" + "time" + + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" _ "modernc.org/sqlite" ) @@ -19,17 +23,70 @@ type App struct { } func New() *App { - db, err := sql.Open("sqlite3", filePath) + db, err := sql.Open("sqlite", filePath) if err != nil { log.Fatalf("failed to open db: %s", err) } - return &App{ + app := &App{ db: db, } + + app.loadRoutes() + + return app } +func (a *App) loadRoutes() { + router := chi.NewRouter() + + router.Use(middleware.Recoverer) + + fs := http.FileServer(http.Dir("static")) + router.Handle("/static/*", http.StripPrefix("/static/", fs)) + + router.Get("/", func(w http.ResponseWriter, r *http.Request) { + t, err := template.ParseFiles("templates/index.html") + if err != nil { + fmt.Println(err) + return + } + err = t.Execute(w, nil) + if err != nil { + fmt.Println(err) + return + } + }) + + router.Post("/api/upload", uploadFile) + + a.router = router +} + +func uploadFile(w http.ResponseWriter, r *http.Request) { + if r.ContentLength < 10 << 20 { + http.Error(w, "Request size is too big", http.StatusRequestEntityTooLarge) + } + + err := r.ParseMultipartForm(10 << 20) + + if err != nil { + log.Fatal(err) + return + } + + file, handler, err := r.FormFile("file") + if err != nil { + log.Fatal(err) + return + } + defer file.Close() + + fmt.Printf("%s %d %s", handler.Filename, handler.Size, handler.Header) +} + + func (a *App) Start(ctx context.Context) error { server := &http.Server{ Addr: ":5454", diff --git a/cmd/server/go.mod b/cmd/server/go.mod deleted file mode 100644 index f7a4cff..0000000 --- a/cmd/server/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module git.mzsl.nl/mans/goshare/cmd/goshare-server - -go 1.21.6 diff --git a/go.mod b/go.mod index cbe05f6..e4d9f7d 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module git.mzsl.nl/mans/goshare go 1.21 -require modernc.org/sqlite v1.28.0 +require ( + github.com/go-chi/chi/v5 v5.0.11 + modernc.org/sqlite v1.28.0 +) require ( github.com/dustin/go-humanize v1.0.1 // indirect diff --git a/go.sum b/go.sum index 71afbfd..4246aca 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA= +github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ= diff --git a/gomod2nix.toml b/gomod2nix.toml index b5d6cb1..3304939 100644 --- a/gomod2nix.toml +++ b/gomod2nix.toml @@ -4,6 +4,9 @@ schema = 3 [mod."github.com/dustin/go-humanize"] version = "v1.0.1" hash = "sha256-yuvxYYngpfVkUg9yAmG99IUVmADTQA0tMbBXe0Fq0Mc=" + [mod."github.com/go-chi/chi/v5"] + version = "v5.0.11" + hash = "sha256-95LKg/OVzhik2HUz6cirHH3eAT4qbHSg52bSvkc+XOY=" [mod."github.com/google/uuid"] version = "v1.3.0" hash = "sha256-QoR55eBtA94T2tBszyxfDtO7/pjZZSGb5vm7U0Xhs0Y=" diff --git a/static/script.js b/static/script.js new file mode 100644 index 0000000..e00f933 --- /dev/null +++ b/static/script.js @@ -0,0 +1,23 @@ +function dropHandler(e) { + e.preventDefault(); + + if (e.dataTransfer.items) { + // Use DataTransferItemList interface to access the file(s) + [...e.dataTransfer.items].forEach((item, i) => { + // If dropped items aren't files, reject them + if (item.kind === "file") { + const file = item.getAsFile(); + console.log(`… file[${i}].name = ${file.name}`); + } + }); + } else { + // Use DataTransfer interface to access the file(s) + [...e.dataTransfer.files].forEach((file, i) => { + console.log(`… file[${i}].name = ${file.name}`); + }); + } +} + +function dragOverHandler(e) { + e.preventDefault(); +} diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..6c78399 --- /dev/null +++ b/static/style.css @@ -0,0 +1,20 @@ +.container { + max-width: 600px; + margin: auto; +} + +#upload-box { + display: flex; + flex-direction: column; + align-items: center; + padding: 100px; + border: dashed; +} + +/* #file-input { */ +/* } */ + +#retention-select { + margin-top: 1rem; +} + diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..fca8259 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,36 @@ + + + + + + Upload files | goshare + + + +
+

Upload files

+
+
+
+ +
+ + + + + +
+
+ + +