Inital version
This commit is contained in:
65
main.go
Normal file
65
main.go
Normal file
@@ -0,0 +1,65 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user