save
This commit is contained in:
20
cmd/lash/main.go
Normal file
20
cmd/lash/main.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"lash"
|
||||
"lash/internal/app"
|
||||
"lash/internal/errx"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
srv := app.New()
|
||||
|
||||
errx.Log("Your token is \033[1;92m%s\033[0m", lash.Token)
|
||||
|
||||
errx.Log("starting server at http://127.0.0.1:1337")
|
||||
if err := http.ListenAndServe(":1337", srv); err != nil {
|
||||
errx.FatalPerror(err)
|
||||
}
|
||||
}
|
||||
19
internal/app/filePath.go
Normal file
19
internal/app/filePath.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetFilePath() (string, error) {
|
||||
fp := ""
|
||||
|
||||
for _, v := range os.Args[1:] {
|
||||
if !strings.HasPrefix(v, "-") {
|
||||
fp = v
|
||||
return fp, nil
|
||||
}
|
||||
}
|
||||
return "", errors.New("not enough arguments")
|
||||
}
|
||||
29
internal/app/routes.go
Normal file
29
internal/app/routes.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"lash"
|
||||
"lash/internal/errx"
|
||||
"lash/internal/handlers"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func New() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
fp, err := GetFilePath()
|
||||
if err != nil {
|
||||
errx.FatalPerror(err)
|
||||
}
|
||||
|
||||
share := handlers.ShareData{
|
||||
Version: lash.Version,
|
||||
FileName: fp,
|
||||
}
|
||||
|
||||
file := handlers.FileData{}
|
||||
|
||||
mux.HandleFunc("/", share.Handler)
|
||||
mux.HandleFunc("/api/receive-token", file.APIHandler)
|
||||
|
||||
return mux
|
||||
}
|
||||
7
internal/errx/error.go
Normal file
7
internal/errx/error.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package errx
|
||||
|
||||
import "fmt"
|
||||
|
||||
func Error(err string) {
|
||||
fmt.Println("error:", "lash:", err)
|
||||
}
|
||||
15
internal/errx/perror.go
Normal file
15
internal/errx/perror.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package errx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Perror(err error) {
|
||||
fmt.Println("error:", "lash:", err)
|
||||
}
|
||||
|
||||
func FatalPerror(err error) {
|
||||
Perror(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
7
internal/errx/print.go
Normal file
7
internal/errx/print.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package errx
|
||||
|
||||
import "fmt"
|
||||
|
||||
func Log(msg string, k... any) {
|
||||
fmt.Printf("lash: " + msg + "\n", k...)
|
||||
}
|
||||
35
internal/handlers/file.go
Normal file
35
internal/handlers/file.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"lash"
|
||||
"lash/internal/errx"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type FileData struct {
|
||||
Contents []byte
|
||||
}
|
||||
|
||||
type ValidateRequest struct {
|
||||
Token string
|
||||
}
|
||||
|
||||
func (h FileData) APIHandler(w http.ResponseWriter, r *http.Request) {
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
var t ValidateRequest
|
||||
err := decoder.Decode(&t)
|
||||
if err != nil {
|
||||
errx.FatalPerror(err)
|
||||
}
|
||||
|
||||
if t.Token == lash.Token {
|
||||
errx.Log("Got token")
|
||||
} else {
|
||||
errx.Log("No Token")
|
||||
}
|
||||
}
|
||||
|
||||
func (h FileData) DownloadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write(h.Contents)
|
||||
}
|
||||
17
internal/handlers/share.go
Normal file
17
internal/handlers/share.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"lash"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ShareData struct {
|
||||
Version string
|
||||
FileName string
|
||||
}
|
||||
|
||||
func (h ShareData) Handler(w http.ResponseWriter, r *http.Request) {
|
||||
tmpl := template.Must(template.ParseFS(lash.Templates, "share.html"))
|
||||
tmpl.ExecuteTemplate(w, "share.html", h)
|
||||
}
|
||||
11
lash.go
Normal file
11
lash.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package lash
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed templates/*
|
||||
var Templates embed.FS
|
||||
|
||||
//go:embed version
|
||||
var Version string
|
||||
|
||||
var Token string = "SICK-COOL-TOKEN"
|
||||
65
main.go
65
main.go
@@ -1,65 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
//go:embed version
|
||||
var version string
|
||||
|
||||
var fp string
|
||||
var read []byte
|
||||
var id string = uuid.New().String()
|
||||
|
||||
type Data struct {
|
||||
Version string
|
||||
FileName string
|
||||
Contents []byte
|
||||
UUID string
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Println("error: lash: not enough arguments")
|
||||
return
|
||||
}
|
||||
fp = os.Args[1]
|
||||
var err error
|
||||
read, err = os.ReadFile(fp)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
fmt.Printf("error: lash: file `%s` does not exist", fp)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
http.HandleFunc("/", shareHandler)
|
||||
http.HandleFunc(fmt.Sprintf("/%s", id), fileHandler)
|
||||
fmt.Println("lash: starting server at http://127.0.0.1:1337")
|
||||
log.Fatal(http.ListenAndServe("127.0.0.1:1337", nil))
|
||||
}
|
||||
|
||||
func shareHandler(w http.ResponseWriter, r *http.Request) {
|
||||
tmpl := template.Must(template.ParseFiles("templates/share.html"))
|
||||
|
||||
data := Data{
|
||||
Version: version,
|
||||
FileName: fp,
|
||||
UUID: id,
|
||||
}
|
||||
|
||||
tmpl.ExecuteTemplate(w, "share.html", data)
|
||||
}
|
||||
|
||||
func fileHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write(read)
|
||||
}
|
||||
@@ -7,6 +7,20 @@
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1><a href="/{{.UUID}}" download>{{.FileName}}</a></h1>
|
||||
|
||||
<form id="tform" action="localpage.html" method="POST">
|
||||
<input name="token" id="token" placeholder="Enter token">
|
||||
<button type="submit">Go</button>
|
||||
</form>
|
||||
|
||||
</body>
|
||||
|
||||
<script>
|
||||
await fetch('/api/receive-token', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ token: USER_TOKEN })
|
||||
});
|
||||
</script>
|
||||
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user