save
This commit is contained in:
@@ -3,7 +3,7 @@ package handlers
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"lash"
|
"lash"
|
||||||
"lash/internal/errx"
|
"strconv"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,19 +17,23 @@ type ValidateRequest struct {
|
|||||||
|
|
||||||
func (h FileData) APIHandler(w http.ResponseWriter, r *http.Request) {
|
func (h FileData) APIHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
decoder := json.NewDecoder(r.Body)
|
decoder := json.NewDecoder(r.Body)
|
||||||
|
|
||||||
var t ValidateRequest
|
var t ValidateRequest
|
||||||
err := decoder.Decode(&t)
|
err := decoder.Decode(&t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errx.FatalPerror(err)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.Token == lash.Token {
|
if t.Token != lash.Token {
|
||||||
errx.Log("Got token")
|
http.Error(w, "Invalid Token", http.StatusUnauthorized)
|
||||||
} else {
|
return
|
||||||
errx.Log("No Token")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h FileData) DownloadHandler(w http.ResponseWriter, r *http.Request) {
|
w.Header().Set("Content-Disposition", "attachment; filename=file.bin")
|
||||||
|
w.Header().Set("Content-Type", "application/octet-stream")
|
||||||
|
w.Header().Set("Content-Length", strconv.Itoa(len(h.Contents)))
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write(h.Contents)
|
w.Write(h.Contents)
|
||||||
}
|
}
|
||||||
|
|||||||
5
lash.go
5
lash.go
@@ -3,7 +3,7 @@ package lash
|
|||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
|
|
||||||
// "github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed templates/*.html
|
//go:embed templates/*.html
|
||||||
@@ -12,5 +12,4 @@ var Templates embed.FS
|
|||||||
//go:embed version
|
//go:embed version
|
||||||
var Version string
|
var Version string
|
||||||
|
|
||||||
// var Token string = uuid.New().String()
|
var Token string = uuid.New().String()
|
||||||
var Token string = "foo"
|
|
||||||
|
|||||||
@@ -1,20 +1,47 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title>Lash v{{.Version}}</title>
|
<title>Lash v{{.Version}}</title>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
</body>
|
<h1>Enter Token</h1>
|
||||||
|
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
id="tokenInput"
|
||||||
|
placeholder="Enter token"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<button id="submitBtn">
|
||||||
|
Submit
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<p id="status"></p>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
await fetch('/api/receive-token', {
|
const tokenInput = document.getElementById('tokenInput');
|
||||||
|
const submitBtn = document.getElementById('submitBtn');
|
||||||
|
const status = document.getElementById('status');
|
||||||
|
|
||||||
|
submitBtn.addEventListener('click', async () => {
|
||||||
|
const token = tokenInput.value.trim();
|
||||||
|
|
||||||
|
if (!token) {
|
||||||
|
status.textContent = 'Please enter a token.';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch('/api/receive-token', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: {
|
||||||
body: JSON.stringify({ token: "foo" })
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ token })
|
||||||
|
});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user