Compare commits
5 Commits
55c540e9c2
...
4c9e6fbe35
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4c9e6fbe35 | ||
|
|
ac642d0bdf | ||
|
|
a59857b549 | ||
|
|
efb75f74cb | ||
|
|
6f8e2e5c53 |
32
src/cmd/server/handler/handler.go
Normal file
32
src/cmd/server/handler/handler.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Handler struct {
|
||||||
|
ctx context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(ctx context.Context) Handler {
|
||||||
|
return Handler{ctx: ctx}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if err := h.serveHTTP(w, r); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h Handler) serveHTTP(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
if strings.HasPrefix(r.URL.Path, "/v1/vpntor") {
|
||||||
|
return h.vpntor(r.Context(), r.Body)
|
||||||
|
} else if strings.HasPrefix(r.URL.Path, "/experimental/ui") {
|
||||||
|
return h.ui(w, r)
|
||||||
|
} else {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
21
src/cmd/server/handler/handler_test.go
Normal file
21
src/cmd/server/handler/handler_test.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package handler_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"show-rss/src/cmd/server/handler"
|
||||||
|
"show-rss/src/db"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHandler(t *testing.T) {
|
||||||
|
h := handler.New(db.Test(t, context.Background()))
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
r := httptest.NewRequest(http.MethodGet, "/not-found", nil)
|
||||||
|
h.ServeHTTP(w, r)
|
||||||
|
if w.Code != http.StatusNotFound {
|
||||||
|
t.Errorf("%+v", w)
|
||||||
|
}
|
||||||
|
}
|
||||||
8
src/cmd/server/handler/testdata/index.tmpl
vendored
Normal file
8
src/cmd/server/handler/testdata/index.tmpl
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h2>Hello templated world b</h2>
|
||||||
|
{{ range feeds }}
|
||||||
|
feed = {{ . }}
|
||||||
|
{{ end }}
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
49
src/cmd/server/handler/ui.go
Normal file
49
src/cmd/server/handler/ui.go
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"show-rss/src/feeds"
|
||||||
|
)
|
||||||
|
|
||||||
|
var dir = func() string {
|
||||||
|
if v := os.Getenv("UI_D"); v != "" {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return "./src/cmd/server/handler/testdata"
|
||||||
|
}()
|
||||||
|
|
||||||
|
func (h Handler) ui(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
fs := http.FileServer(http.Dir(dir))
|
||||||
|
if path.Base(r.URL.Path) == "ui" {
|
||||||
|
return h.uiIndex(w, r)
|
||||||
|
}
|
||||||
|
http.StripPrefix("/experimental/ui", fs).ServeHTTP(w, r)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h Handler) uiIndex(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
ctx := r.Context()
|
||||||
|
|
||||||
|
b, _ := os.ReadFile(path.Join(dir, "index.tmpl"))
|
||||||
|
|
||||||
|
tmpl := template.New(r.URL.Path).Funcs(template.FuncMap{
|
||||||
|
"feeds": func() ([]feeds.Feed, error) {
|
||||||
|
all := []feeds.Feed{}
|
||||||
|
err := feeds.ForEach(ctx, func(f feeds.Feed) error {
|
||||||
|
all = append(all, f)
|
||||||
|
return ctx.Err()
|
||||||
|
})
|
||||||
|
return all, err
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
tmpl, err := tmpl.Parse(string(b))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return tmpl.Execute(w, nil)
|
||||||
|
}
|
||||||
65
src/cmd/server/handler/vpntor.go
Normal file
65
src/cmd/server/handler/vpntor.go
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h Handler) vpntor(ctx context.Context, r io.Reader) error {
|
||||||
|
var form struct {
|
||||||
|
Magnet string
|
||||||
|
Dir string
|
||||||
|
URL string
|
||||||
|
}
|
||||||
|
if err := json.NewDecoder(r).Decode(&form); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if form.Magnet == "" || form.Dir == "" || form.URL == "" {
|
||||||
|
return fmt.Errorf("did not specify all of .Magnet, .Dir, .URL")
|
||||||
|
}
|
||||||
|
|
||||||
|
c := http.Client{
|
||||||
|
Timeout: time.Minute,
|
||||||
|
Transport: &http.Transport{DisableKeepAlives: true},
|
||||||
|
}
|
||||||
|
sresp, err := c.Get(form.URL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer sresp.Body.Close()
|
||||||
|
defer io.Copy(io.Discard, sresp.Body)
|
||||||
|
|
||||||
|
b, _ := json.Marshal(map[string]any{
|
||||||
|
"method": "torrent-add",
|
||||||
|
"arguments": map[string]string{
|
||||||
|
"filename": form.Magnet,
|
||||||
|
"download-dir": form.Dir,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
req, err := http.NewRequest(http.MethodPost, form.URL, bytes.NewReader(b))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req = req.WithContext(ctx)
|
||||||
|
req.Header.Add("X-Transmission-Session-Id", sresp.Header.Get("X-Transmission-Session-Id"))
|
||||||
|
|
||||||
|
resp, err := c.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
defer io.Copy(io.Discard, resp.Body)
|
||||||
|
|
||||||
|
if b, _ := ioutil.ReadAll(resp.Body); resp.StatusCode > 220 || !bytes.Contains(b, []byte(`success`)) {
|
||||||
|
return fmt.Errorf("(%d) %s", resp.StatusCode, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
66
src/cmd/server/handler/vpntor_test.go
Normal file
66
src/cmd/server/handler/vpntor_test.go
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
package handler_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"show-rss/src/cmd/server/handler"
|
||||||
|
"show-rss/src/db"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestVpntor(t *testing.T) {
|
||||||
|
h := handler.New(db.Test(t, context.Background()))
|
||||||
|
|
||||||
|
t.Run("no body", func(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
r := httptest.NewRequest(http.MethodGet, "/v1/vpntor", nil)
|
||||||
|
h.ServeHTTP(w, r)
|
||||||
|
if w.Code != http.StatusInternalServerError {
|
||||||
|
t.Errorf("%+v", w)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("bad body", func(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
r := httptest.NewRequest(http.MethodPost, "/v1/vpntor", strings.NewReader(`{}`))
|
||||||
|
h.ServeHTTP(w, r)
|
||||||
|
t.Logf("%s", w.Body.Bytes())
|
||||||
|
if w.Code != http.StatusInternalServerError {
|
||||||
|
t.Errorf("%+v", w.Code)
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(w.Body.Bytes()), `.Magnet, .Dir, .URL`) {
|
||||||
|
t.Errorf("%+v", string(w.Body.Bytes()))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("doit", func(t *testing.T) {
|
||||||
|
calls := 0
|
||||||
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
b, _ := io.ReadAll(r.Body)
|
||||||
|
t.Logf("%s | %s", r.Header.Get("X-Transmission-Session-Id"), b)
|
||||||
|
calls += 1
|
||||||
|
w.Header().Set("X-Transmission-Session-Id", "session")
|
||||||
|
w.Write([]byte(`success`))
|
||||||
|
}))
|
||||||
|
defer s.Close()
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
r := httptest.NewRequest(http.MethodPost, "/v1/vpntor", strings.NewReader(fmt.Sprintf(`{
|
||||||
|
"Magnet": %q,
|
||||||
|
"Dir": %q,
|
||||||
|
"URL": %q
|
||||||
|
}`, "magnet", "dir", s.URL)))
|
||||||
|
h.ServeHTTP(w, r)
|
||||||
|
if w.Code != http.StatusOK {
|
||||||
|
t.Errorf("%+v", w.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
if calls != 2 {
|
||||||
|
t.Errorf("expected 2 but got %d calls", calls)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -2,9 +2,47 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"show-rss/src/cmd/server/handler"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Main(ctx context.Context) error {
|
func Main(ctx context.Context) error {
|
||||||
return io.EOF
|
port, _ := strconv.Atoi(os.Getenv("PORT"))
|
||||||
|
if port == 0 {
|
||||||
|
port = 10000
|
||||||
|
}
|
||||||
|
|
||||||
|
return Run(ctx, fmt.Sprintf(":%d", port))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Run(ctx context.Context, listen string) error {
|
||||||
|
ctx, can := context.WithCancel(ctx)
|
||||||
|
defer can()
|
||||||
|
|
||||||
|
s := http.Server{
|
||||||
|
Addr: listen,
|
||||||
|
Handler: handler.New(ctx),
|
||||||
|
BaseContext: func(net.Listener) context.Context { return ctx },
|
||||||
|
}
|
||||||
|
defer s.Close()
|
||||||
|
|
||||||
|
errs := make(chan error)
|
||||||
|
go func() {
|
||||||
|
defer close(errs)
|
||||||
|
select {
|
||||||
|
case errs <- s.ListenAndServe():
|
||||||
|
case <-ctx.Done():
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
case err := <-errs:
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return s.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
50
src/cmd/server/main_test.go
Normal file
50
src/cmd/server/main_test.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package server_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
|
"show-rss/src/cmd/server"
|
||||||
|
"show-rss/src/db"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestServerStarts(t *testing.T) {
|
||||||
|
With(t, func(url string) {
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func With(t *testing.T, cb func(url string)) {
|
||||||
|
ctx, can := context.WithTimeout(context.Background(), 15*time.Second)
|
||||||
|
defer can()
|
||||||
|
|
||||||
|
listen := func() string {
|
||||||
|
s := httptest.NewServer(http.HandlerFunc(http.NotFound))
|
||||||
|
s.Close()
|
||||||
|
u, _ := url.Parse(s.URL)
|
||||||
|
return u.Host
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
if err := server.Run(db.Test(t, ctx), listen); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
url := "http://" + listen
|
||||||
|
for {
|
||||||
|
if resp, err := http.Get(url); err == nil {
|
||||||
|
resp.Body.Close()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-time.After(100 * time.Millisecond):
|
||||||
|
case <-ctx.Done():
|
||||||
|
t.Fatal(ctx.Err())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cb(url)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user