refactor: move into package
Some checks failed
Go Test / test (push) Failing after 32s
Go Bump / bump (push) Successful in 34s

This commit is contained in:
xuu 2024-01-22 16:05:13 -07:00
parent 79114e8b8e
commit f9c064e948
Signed by: xuu
GPG Key ID: 8B3B0604F164E04F
5 changed files with 136 additions and 102 deletions

View File

@ -23,7 +23,7 @@ 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)
fmt.Fprint(w, name, '@', version)
})
}

View File

@ -2,19 +2,12 @@ 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/v2/assets"
"go.sour.is/paste/v2/paste"
)
@ -28,104 +21,12 @@ var _ = apps.Register(50, func(ctx context.Context, svc *service.Harness) error
return err
}
p, err := paste.New(store)
srv, err := paste.NewService(store, randBytes)
if err != nil {
return err
}
svc.Add(&pasteSRV{
ui: http.FileServer(http.FS(assets.GetUI())),
paste: p,
randBytes: int(randBytes),
})
svc.Add(srv)
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)
}

19
paste/public/index.html Normal file
View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<link rel="manifest" href="/ui/manifest.json">
<link rel="shortcut icon" href="/ui/favicon.ico">
<title>DN42 Paste</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link href="paste.css" rel="stylesheet">
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
</body>
</html>

0
paste/public/paste.css Normal file
View File

114
paste/service.go Normal file
View File

@ -0,0 +1,114 @@
package paste
import (
"crypto/rand"
"errors"
"fmt"
"log"
"net/http"
"strings"
"go.sour.is/paste/v2/assets"
"go.sour.is/pkg/lg"
)
type service struct {
ui http.Handler
paste *Paste
randBytes int
}
func NewService(store string, randBytes int64) (*service, error) {
p, err := New(store)
if err != nil {
return nil, err
}
return &service{
ui: http.FileServer(http.FS(assets.GetUI())),
paste: p,
randBytes: int(randBytes),
}, nil
}
func (a *service) 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 *service) 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 *service) 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 *service) 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, 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, "ERR %s", err)
return
}
}
func (p *service) 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)
}