feat: create v2 for module support
All checks were successful
Deploy / deploy (push) Successful in 1m21s
All checks were successful
Deploy / deploy (push) Successful in 1m21s
This commit is contained in:
26
v2/app.artifact.go
Normal file
26
v2/app.artifact.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.sour.is/paste/v2/artifact"
|
||||
"go.sour.is/pkg/env"
|
||||
"go.sour.is/pkg/lg"
|
||||
"go.sour.is/pkg/service"
|
||||
)
|
||||
|
||||
var _ = apps.Register(40, func(ctx context.Context, svc *service.Harness) error {
|
||||
_, span := lg.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
store := env.Default("ARTIFACT_STORE", "data/")
|
||||
|
||||
a, err := artifact.New(store, -1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
svc.Add(a)
|
||||
span.AddEvent("register artifact")
|
||||
|
||||
return nil
|
||||
})
|
||||
29
v2/app.info.go
Normal file
29
v2/app.info.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"go.sour.is/pkg/lg"
|
||||
"go.sour.is/pkg/service"
|
||||
)
|
||||
|
||||
var _ = apps.Register(50, func(ctx context.Context, svc *service.Harness) error {
|
||||
_, span := lg.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
svc.Add(&info{})
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
type info struct{}
|
||||
|
||||
func (info) RegisterHTTP(mux *http.ServeMux) {
|
||||
mux.HandleFunc("/app-info", func(w http.ResponseWriter, r *http.Request) {
|
||||
name, version := service.AppName()
|
||||
fmt.Fprint(w, name, version)
|
||||
})
|
||||
|
||||
}
|
||||
131
v2/app.paste.go
Normal file
131
v2/app.paste.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"go.sour.is/pkg/env"
|
||||
"go.sour.is/pkg/lg"
|
||||
"go.sour.is/pkg/service"
|
||||
|
||||
"go.sour.is/paste/assets"
|
||||
"go.sour.is/paste/v2/paste"
|
||||
)
|
||||
|
||||
var _ = apps.Register(50, func(ctx context.Context, svc *service.Harness) error {
|
||||
_, span := lg.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
store := env.Default("PASTE_STORE", "data/")
|
||||
randBytes, err := strconv.ParseInt(env.Default("PASTE_RANDOM", "4096"), 10, 32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p, err := paste.New(store)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
svc.Add(&pasteSRV{
|
||||
ui: http.FileServer(http.FS(assets.GetUI())),
|
||||
paste: p,
|
||||
randBytes: int(randBytes),
|
||||
})
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
type pasteSRV struct {
|
||||
ui http.Handler
|
||||
paste *paste.Paste
|
||||
randBytes int
|
||||
}
|
||||
|
||||
func (a *pasteSRV) RegisterHTTP(mux *http.ServeMux) {
|
||||
mux.Handle("/ui/", lg.Htrace(http.StripPrefix("/ui/", a.ui), "paste-assets"))
|
||||
|
||||
mux.Handle("/api", http.StripPrefix("/api", a))
|
||||
mux.Handle("/api/", http.StripPrefix("/api/", a))
|
||||
|
||||
mux.Handle("/paste", http.StripPrefix("/paste", a))
|
||||
mux.Handle("/paste/", http.StripPrefix("/paste/", a))
|
||||
}
|
||||
|
||||
func (p *pasteSRV) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
switch {
|
||||
case strings.HasPrefix(r.URL.Path, "rng"):
|
||||
p.getRandom(w)
|
||||
|
||||
case strings.HasPrefix(r.URL.Path, "get"), r.URL.Path != "":
|
||||
p.getPaste(w, strings.TrimPrefix(r.URL.Path, "get/"))
|
||||
|
||||
default:
|
||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||
}
|
||||
|
||||
case http.MethodPost:
|
||||
switch {
|
||||
case r.URL.Path == "":
|
||||
p.postPaste(w, r)
|
||||
|
||||
default:
|
||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||
}
|
||||
|
||||
default:
|
||||
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *pasteSRV) getRandom(w http.ResponseWriter) {
|
||||
log.Println("get random")
|
||||
s := make([]byte, p.randBytes)
|
||||
_, _ = rand.Read(s)
|
||||
|
||||
w.Header().Set("content-type", "application/octet-stream")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(s)
|
||||
}
|
||||
|
||||
func (p *pasteSRV) getPaste(w http.ResponseWriter, id string) {
|
||||
log.Println("get paste", id)
|
||||
w.Header().Set("content-type", "application/octet-stream")
|
||||
|
||||
err := p.paste.Get(w, id)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, paste.ErrGone):
|
||||
w.WriteHeader(http.StatusGone)
|
||||
case errors.Is(err, paste.ErrNotFound):
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
case errors.Is(err, paste.ErrReadingContent):
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "ERR %s", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (p *pasteSRV) postPaste(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("content-type", "application/octet-stream")
|
||||
|
||||
id, err := p.paste.Set(r.Body)
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "ERR %s", err)
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
log.Println("post paste", id)
|
||||
fmt.Fprint(w, "OK ", id)
|
||||
}
|
||||
328
v2/artifact/artifact.go
Normal file
328
v2/artifact/artifact.go
Normal file
@@ -0,0 +1,328 @@
|
||||
package artifact
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"go.sour.is/paste/src/pkg/readutil"
|
||||
"golang.org/x/sys/unix"
|
||||
"sour.is/x/toolbox/httpsrv"
|
||||
"sour.is/x/toolbox/log"
|
||||
|
||||
"github.com/gomarkdown/markdown"
|
||||
"github.com/gomarkdown/markdown/html"
|
||||
"github.com/gomarkdown/markdown/parser"
|
||||
)
|
||||
|
||||
// func init() {
|
||||
// a := &Artifact{}
|
||||
// httpsrv.RegisterModule("artifact", a.config)
|
||||
|
||||
// httpsrv.HttpRegister("artifact", httpsrv.HttpRoutes{
|
||||
// {Name: "get-path", Method: "GET", Pattern: "/a/{name}/{path:.*}", HandlerFunc: a.get},
|
||||
// {Name: "get", Method: "GET", Pattern: "/a/{name}", HandlerFunc: a.get},
|
||||
// {Name: "put", Method: "PUT", Pattern: "/a", HandlerFunc: a.put},
|
||||
// {Name: "get", Method: "GET", Pattern: "/a", HandlerFunc: a.list},
|
||||
// })
|
||||
// }
|
||||
|
||||
// artifact stores items to disk
|
||||
type artifact struct {
|
||||
store string
|
||||
maxSize int64
|
||||
}
|
||||
|
||||
const DefaultMaxSize = 500 * 1024 * 1024
|
||||
|
||||
func New(store string, maxSize int64) (a *artifact, err error) {
|
||||
a = &artifact{
|
||||
store: store,
|
||||
maxSize: DefaultMaxSize,
|
||||
}
|
||||
|
||||
if maxSize > 0 {
|
||||
a.maxSize = maxSize
|
||||
}
|
||||
|
||||
if !chkStore(a.store) {
|
||||
return nil, fmt.Errorf("artifact Store location [%s] does not exist or is not writable", a.store)
|
||||
}
|
||||
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func (a *artifact) RegisterHTTP(mux *http.ServeMux) {
|
||||
mux.Handle("/a", http.StripPrefix("/a", a))
|
||||
mux.Handle("/a/", http.StripPrefix("/a/", a))
|
||||
}
|
||||
func (a *artifact) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
if r.URL.Path == "" {
|
||||
a.list(w)
|
||||
return
|
||||
}
|
||||
|
||||
name := strings.TrimPrefix(r.URL.Path, "/")
|
||||
name, path, _ := strings.Cut(name, "/")
|
||||
|
||||
a.get(w, name, path)
|
||||
case http.MethodPost:
|
||||
length := 0
|
||||
if h := r.Header.Get("Content-Length"); h != "" {
|
||||
if i, err := strconv.Atoi(h); err != nil {
|
||||
length = i
|
||||
}
|
||||
}
|
||||
id, err := a.put(r.Body, length)
|
||||
switch {
|
||||
case errors.Is(err, ErrGone):
|
||||
w.WriteHeader(http.StatusGone)
|
||||
case errors.Is(err, ErrNotFound):
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
case errors.Is(err, ErrReadingContent):
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "OK %s", id)
|
||||
|
||||
default:
|
||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
func (a *artifact) get(w http.ResponseWriter, name string, path string) error {
|
||||
hasPath := path != ""
|
||||
|
||||
ext := filepath.Ext(name)
|
||||
id := strings.TrimSuffix(name, ext)
|
||||
|
||||
fname := filepath.Join(a.store, id)
|
||||
|
||||
if !chkFile(fname) {
|
||||
return fmt.Errorf("%w: %s", ErrNotFound, fname)
|
||||
}
|
||||
|
||||
if chkGone(fname) {
|
||||
return fmt.Errorf("%w: %s", ErrGone, fname)
|
||||
}
|
||||
|
||||
f, err := os.Open(fname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if !hasPath {
|
||||
pr := readutil.NewPreviewReader(f)
|
||||
|
||||
mime, err := readutil.ReadMIME(pr, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
w.Header().Set("Content-Type", mime)
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
|
||||
_, _ = io.Copy(w, pr.Drain())
|
||||
return nil
|
||||
}
|
||||
|
||||
rdr, err := readutil.Decompress(f)
|
||||
if err != nil && err != readutil.ErrUnsupportedType {
|
||||
return fmt.Errorf("%w: %w", ErrReadingContent, err)
|
||||
}
|
||||
if cl, ok := rdr.(io.ReadCloser); ok {
|
||||
defer cl.Close()
|
||||
}
|
||||
|
||||
tr := tar.NewReader(rdr)
|
||||
if path == "@" {
|
||||
var paths []string
|
||||
for {
|
||||
hdr, err := tr.Next()
|
||||
if err == io.EOF {
|
||||
break // End of archive
|
||||
}
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
paths = append(paths, hdr.Name)
|
||||
}
|
||||
|
||||
httpsrv.WriteObject(w, http.StatusOK, paths)
|
||||
return nil
|
||||
}
|
||||
|
||||
for {
|
||||
hdr, err := tr.Next()
|
||||
if err == io.EOF {
|
||||
break // End of archive
|
||||
}
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
break
|
||||
}
|
||||
if path == "~" && hdr.Name == "index.html" {
|
||||
path = hdr.Name
|
||||
}
|
||||
if path == "~" && hdr.Name == "index.md" {
|
||||
path = hdr.Name
|
||||
}
|
||||
|
||||
if hdr.Name == path {
|
||||
if strings.HasSuffix(hdr.Name, ".md") {
|
||||
md, err := io.ReadAll(tr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %w", ErrReadingContent, err)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
extensions := parser.CommonExtensions | parser.AutoHeadingIDs
|
||||
p := parser.NewWithExtensions(extensions)
|
||||
|
||||
htmlFlags := html.CommonFlags | html.HrefTargetBlank
|
||||
opts := html.RendererOptions{Flags: htmlFlags}
|
||||
renderer := html.NewRenderer(opts)
|
||||
|
||||
b := markdown.ToHTML(md, p, renderer)
|
||||
_, _ = w.Write(b)
|
||||
return nil
|
||||
}
|
||||
|
||||
pr := readutil.NewPreviewReader(tr)
|
||||
|
||||
mime, err := readutil.ReadMIME(pr, hdr.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %w", ErrReadingContent, err)
|
||||
}
|
||||
w.Header().Set("Content-Type", mime)
|
||||
|
||||
if _, err := io.Copy(w, pr.Drain()); err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
http.Error(w, "Not Found in Archive", http.StatusNotFound)
|
||||
return ErrNotFound
|
||||
}
|
||||
func (a *artifact) put(r io.ReadCloser, length int) (string, error) {
|
||||
defer r.Close()
|
||||
|
||||
if length > 0 {
|
||||
if int64(length) > a.maxSize {
|
||||
return "", ErrSizeTooLarge
|
||||
}
|
||||
}
|
||||
|
||||
rdr := io.LimitReader(r, a.maxSize)
|
||||
pr := readutil.NewPreviewReader(rdr)
|
||||
rdr = pr.Drain()
|
||||
|
||||
s256 := sha256.New()
|
||||
tmp, err := os.CreateTemp(a.store, "arch-")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%w: %w", ErrBadInput, err)
|
||||
}
|
||||
|
||||
defer os.Remove(tmp.Name())
|
||||
|
||||
m := io.MultiWriter(s256, tmp)
|
||||
if _, err := io.Copy(m, rdr); err != nil {
|
||||
return "", fmt.Errorf("%w: %w", ErrBadInput, err)
|
||||
}
|
||||
tmp.Close()
|
||||
|
||||
id := base64.RawURLEncoding.EncodeToString(s256.Sum(nil)[12:])
|
||||
fname := filepath.Join(a.store, id)
|
||||
|
||||
// log.Debugs("Artifact: moving file", "src", tmp.Name(), "dst", fname)
|
||||
_ = os.Rename(tmp.Name(), fname)
|
||||
|
||||
return id, nil
|
||||
}
|
||||
func (a *artifact) list(w io.Writer) error {
|
||||
return filepath.Walk(a.store, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err = fmt.Fprintln(w, "FILE: ", info.Name())
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func chkStore(path string) bool {
|
||||
file, err := os.Stat(path)
|
||||
|
||||
if err != nil && os.IsNotExist(err) {
|
||||
err = os.MkdirAll(path, 0744)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
file, err = os.Stat(path)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if !file.IsDir() {
|
||||
return false
|
||||
}
|
||||
|
||||
if unix.Access(path, unix.W_OK&unix.R_OK) != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
func chkFile(path string) bool {
|
||||
file, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if file.IsDir() {
|
||||
return false
|
||||
}
|
||||
|
||||
if unix.Access(path, unix.W_OK&unix.R_OK) != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
func chkGone(path string) bool {
|
||||
file, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
|
||||
if file.Size() == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("not found")
|
||||
ErrGone = errors.New("gone")
|
||||
ErrReadingContent = errors.New("reading content")
|
||||
ErrSizeTooLarge = errors.New("size too large")
|
||||
ErrBadInput = errors.New("bad input")
|
||||
)
|
||||
50
v2/main.go
Normal file
50
v2/main.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
|
||||
"go.sour.is/pkg/lg"
|
||||
"go.sour.is/pkg/service"
|
||||
)
|
||||
|
||||
var apps service.Apps
|
||||
var appName, version = service.AppName()
|
||||
|
||||
func main() {
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
defer cancel() // restore interrupt function
|
||||
}()
|
||||
if err := run(ctx); err != nil {
|
||||
log.Fatal(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
func run(ctx context.Context) error {
|
||||
// TODO: make this configurable.
|
||||
level := slog.LevelInfo
|
||||
if ok, _ := strconv.ParseBool(os.Getenv("LOG_DEBUG")); ok {
|
||||
level = slog.LevelDebug
|
||||
}
|
||||
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: level})))
|
||||
|
||||
svc := &service.Harness{}
|
||||
ctx, stop := lg.Init(ctx, appName)
|
||||
svc.OnStop(stop)
|
||||
svc.Add(lg.NewHTTP(ctx))
|
||||
svc.Setup(ctx, apps.Apps()...)
|
||||
|
||||
// Run application
|
||||
if err := svc.Run(ctx, appName, version); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
188
v2/paste/paste.go
Normal file
188
v2/paste/paste.go
Normal file
@@ -0,0 +1,188 @@
|
||||
package paste
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.sour.is/paste/src/pkg/readutil"
|
||||
"golang.org/x/sys/unix"
|
||||
"sour.is/x/toolbox/log"
|
||||
)
|
||||
|
||||
type Paste struct {
|
||||
store string
|
||||
}
|
||||
|
||||
func New(store string) (p *Paste, err error) {
|
||||
p = &Paste{}
|
||||
p.store = store
|
||||
|
||||
if !chkStore(p.store) {
|
||||
err = fmt.Errorf("paste: store location [%s] does not exist or is not writable", p.store)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func chkStore(path string) bool {
|
||||
file, err := os.Stat(path)
|
||||
|
||||
if err != nil && os.IsNotExist(err) {
|
||||
err = os.MkdirAll(path, 0744)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
file, err = os.Stat(path)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if !file.IsDir() {
|
||||
return false
|
||||
}
|
||||
|
||||
if unix.Access(path, unix.W_OK&unix.R_OK) != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func chkFile(path string) bool {
|
||||
file, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if file.IsDir() {
|
||||
return false
|
||||
}
|
||||
|
||||
if unix.Access(path, unix.W_OK&unix.R_OK) != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func chkGone(path string) bool {
|
||||
file, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
|
||||
if file.Size() == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *Paste) Get(w io.Writer, id string) error {
|
||||
fname := filepath.Join(p.store, id)
|
||||
|
||||
if !chkFile(fname) {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
if chkGone(fname) {
|
||||
return ErrGone
|
||||
}
|
||||
|
||||
f, err := os.Open(fname)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %w", ErrReadingContent, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
pr := readutil.NewPreviewReader(f)
|
||||
|
||||
keep := true
|
||||
scanner := bufio.NewScanner(pr)
|
||||
for scanner.Scan() {
|
||||
txt := scanner.Text()
|
||||
log.Debug(txt)
|
||||
|
||||
// End of header found
|
||||
if txt == "" {
|
||||
break
|
||||
}
|
||||
|
||||
if strings.HasPrefix(txt, "exp:") {
|
||||
now := time.Now().Unix()
|
||||
exp, err := strconv.ParseInt(strings.TrimSpace(strings.TrimPrefix(txt, "exp:")), 10, 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %w", ErrReadingContent, err)
|
||||
}
|
||||
|
||||
log.Debugf("%d > %d", now, exp)
|
||||
if now > exp {
|
||||
deleteFile(fname)
|
||||
return ErrGone
|
||||
}
|
||||
}
|
||||
|
||||
if strings.HasPrefix(txt, "burn:") {
|
||||
burn := strings.TrimSpace(strings.TrimPrefix(txt, "burn:"))
|
||||
|
||||
if burn == "true" {
|
||||
keep = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_, _ = io.Copy(w, pr.Drain())
|
||||
|
||||
if !keep {
|
||||
deleteFile(fname)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Paste) Set(r io.ReadCloser) (string, error) {
|
||||
defer r.Close()
|
||||
|
||||
rdr := io.LimitReader(r, 1048576)
|
||||
|
||||
s256 := sha256.New()
|
||||
tmp, _ := os.CreateTemp(p.store, "arch-")
|
||||
defer os.Remove(tmp.Name())
|
||||
|
||||
m := io.MultiWriter(s256, tmp)
|
||||
if _, err := io.Copy(m, rdr); err != nil {
|
||||
return "", fmt.Errorf("%w: %w", ErrBadInput, err)
|
||||
}
|
||||
tmp.Close()
|
||||
|
||||
id := base64.RawURLEncoding.EncodeToString(s256.Sum(nil)[12:])
|
||||
fname := filepath.Join(p.store, id)
|
||||
|
||||
_ = os.Rename(tmp.Name(), fname)
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func deleteFile(path string) {
|
||||
_ = ioutil.WriteFile(path, nil, 0644)
|
||||
}
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("not found")
|
||||
ErrGone = errors.New("gone")
|
||||
ErrReadingContent = errors.New("reading content")
|
||||
ErrBadInput = errors.New("bad input")
|
||||
)
|
||||
47
v2/svc.http.go
Normal file
47
v2/svc.http.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/cors"
|
||||
|
||||
"go.sour.is/pkg/env"
|
||||
"go.sour.is/pkg/lg"
|
||||
"go.sour.is/pkg/mux"
|
||||
"go.sour.is/pkg/service"
|
||||
"go.sour.is/pkg/slice"
|
||||
)
|
||||
|
||||
var _ = apps.Register(20, func(ctx context.Context, svc *service.Harness) error {
|
||||
s := &http.Server{}
|
||||
svc.Add(s)
|
||||
|
||||
mux := mux.New()
|
||||
s.Handler = cors.AllowAll().Handler(mux)
|
||||
|
||||
s.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println(r.Method, r.URL.Path)
|
||||
mux.ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
s.Addr = env.Default("HTTP_LISTEN", ":8080")
|
||||
if strings.HasPrefix(s.Addr, ":") {
|
||||
s.Addr = "[::]" + s.Addr
|
||||
}
|
||||
svc.OnStart(func(ctx context.Context) error {
|
||||
_, span := lg.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
log.Print("Listen on ", s.Addr)
|
||||
span.AddEvent("begin listen and serve on " + s.Addr)
|
||||
|
||||
mux.Add(slice.FilterType[interface{ RegisterHTTP(*http.ServeMux) }](svc.Services...)...)
|
||||
return s.ListenAndServe()
|
||||
})
|
||||
svc.OnStop(s.Shutdown)
|
||||
|
||||
return nil
|
||||
})
|
||||
Reference in New Issue
Block a user