add simple html page and bit of functionality
This commit is contained in:
parent
2890aed889
commit
f3c5b266d8
63
app/app.go
63
app/app.go
@ -4,9 +4,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
|
|
||||||
_ "modernc.org/sqlite"
|
_ "modernc.org/sqlite"
|
||||||
)
|
)
|
||||||
@ -19,17 +23,70 @@ type App struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func New() *App {
|
func New() *App {
|
||||||
db, err := sql.Open("sqlite3", filePath)
|
db, err := sql.Open("sqlite", filePath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to open db: %s", err)
|
log.Fatalf("failed to open db: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &App{
|
app := &App{
|
||||||
db: db,
|
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 {
|
func (a *App) Start(ctx context.Context) error {
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
Addr: ":5454",
|
Addr: ":5454",
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
module git.mzsl.nl/mans/goshare/cmd/goshare-server
|
|
||||||
|
|
||||||
go 1.21.6
|
|
5
go.mod
5
go.mod
@ -2,7 +2,10 @@ module git.mzsl.nl/mans/goshare
|
|||||||
|
|
||||||
go 1.21
|
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 (
|
require (
|
||||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
|
2
go.sum
2
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 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
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 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
|
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
|
||||||
|
@ -4,6 +4,9 @@ schema = 3
|
|||||||
[mod."github.com/dustin/go-humanize"]
|
[mod."github.com/dustin/go-humanize"]
|
||||||
version = "v1.0.1"
|
version = "v1.0.1"
|
||||||
hash = "sha256-yuvxYYngpfVkUg9yAmG99IUVmADTQA0tMbBXe0Fq0Mc="
|
hash = "sha256-yuvxYYngpfVkUg9yAmG99IUVmADTQA0tMbBXe0Fq0Mc="
|
||||||
|
[mod."github.com/go-chi/chi/v5"]
|
||||||
|
version = "v5.0.11"
|
||||||
|
hash = "sha256-95LKg/OVzhik2HUz6cirHH3eAT4qbHSg52bSvkc+XOY="
|
||||||
[mod."github.com/google/uuid"]
|
[mod."github.com/google/uuid"]
|
||||||
version = "v1.3.0"
|
version = "v1.3.0"
|
||||||
hash = "sha256-QoR55eBtA94T2tBszyxfDtO7/pjZZSGb5vm7U0Xhs0Y="
|
hash = "sha256-QoR55eBtA94T2tBszyxfDtO7/pjZZSGb5vm7U0Xhs0Y="
|
||||||
|
23
static/script.js
Normal file
23
static/script.js
Normal file
@ -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();
|
||||||
|
}
|
20
static/style.css
Normal file
20
static/style.css
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
|
36
templates/index.html
Normal file
36
templates/index.html
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Upload files | goshare</title>
|
||||||
|
<link rel="stylesheet" href="/static/style.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<h1>Upload files</h1>
|
||||||
|
<form
|
||||||
|
action="/api/upload"
|
||||||
|
method="post"
|
||||||
|
enctype="multipart/form-data"
|
||||||
|
>
|
||||||
|
<div id="upload-box"
|
||||||
|
ondrop="dropHandler(event)"
|
||||||
|
ondragover="dragOverHandler(event)"
|
||||||
|
>
|
||||||
|
<label for="file-input">Choose file to upload</label><br/>
|
||||||
|
<input id="file-input" type="file" name="file">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<label for="retention-select">Retention period</label>
|
||||||
|
<select name="retention" id="retention-select">
|
||||||
|
<option value="1_day">One Day</option>
|
||||||
|
<option value="2_day">Two Days</option>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<button id="upload-btn" type="submit">Upload</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<script src="/static/script.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user