Начальный веб клиент

This commit is contained in:
Александр Кирюхин 2024-10-29 01:21:05 +03:00
parent b26bd10926
commit fd4e0c3112
Signed by: neonxp
SSH key fingerprint: SHA256:SVt7TjxbVc87m1QYaQziOJ0N3OCFURv2g76gD/UTTXI
36 changed files with 1983 additions and 271 deletions

4
.gitignore vendored
View file

@ -1 +1,3 @@
*.db *.db
node_modules
dist

View file

@ -1,8 +1,11 @@
package api package api
import ( import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"gitrepo.ru/neonxp/idecnode/pkg/api" "gitrepo.ru/neonxp/idecnode/pkg/apiv1"
"gitrepo.ru/neonxp/idecnode/pkg/apiv2"
"gitrepo.ru/neonxp/idecnode/pkg/config" "gitrepo.ru/neonxp/idecnode/pkg/config"
"gitrepo.ru/neonxp/idecnode/pkg/idec" "gitrepo.ru/neonxp/idecnode/pkg/idec"
) )
@ -22,8 +25,19 @@ var APICommand *cli.Command = &cli.Command{
return err return err
} }
defer idecApi.Close() defer idecApi.Close()
e := echo.New()
return api.New(idecApi, cfg).Run(c.Context) e.Use(
middleware.Recover(),
middleware.Logger(),
)
apiv1.New(idecApi, cfg).Register(e)
apiv2.New(idecApi, cfg).Register(e)
e.Static("/", "./web/dist")
return e.Start(cfg.Listen)
}, },
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{

68
cmd/frontend/frontend.go Normal file
View file

@ -0,0 +1,68 @@
package frontend
import (
"errors"
"log"
"github.com/evanw/esbuild/pkg/api"
"github.com/urfave/cli/v2"
)
var FrontendCommand *cli.Command = &cli.Command{
Name: "frontend",
Description: "Build/watch frontend",
Action: func(c *cli.Context) error {
dev := c.Bool("dev")
watch := c.Bool("watch")
buildOpts := api.BuildOptions{
EntryPoints: []string{
"./web/src/index.js",
"./web/src/index.css",
"./web/src/index.html",
},
Outdir: "./web/dist",
Bundle: true,
Write: true,
MinifyWhitespace: !dev,
MinifyIdentifiers: !dev,
MinifySyntax: !dev,
JSX: api.JSXAutomatic,
Loader: map[string]api.Loader{
".js": api.LoaderJSX,
".css": api.LoaderCSS,
".html": api.LoaderCopy,
},
}
if watch {
ctx, err := api.Context(buildOpts)
if err != nil {
return err
}
if err := ctx.Watch(api.WatchOptions{}); err != nil {
return err
}
log.Println("watching frontend")
<-c.Done()
} else {
if br := api.Build(buildOpts); br.Errors != nil {
for _, e := range br.Errors {
log.Println(e.Location, e.Detail, e.Text)
}
return errors.New("build failed")
}
}
return nil
},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "dev",
Value: false,
},
&cli.BoolFlag{
Name: "watch",
Value: false,
},
},
}

10
go.mod
View file

@ -2,23 +2,27 @@ module gitrepo.ru/neonxp/idecnode
go 1.23.1 go 1.23.1
require github.com/urfave/cli/v2 v2.27.5 require (
github.com/urfave/cli/v2 v2.27.5
golang.org/x/crypto v0.22.0
)
require ( require (
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/labstack/gommon v0.4.2 // indirect github.com/labstack/gommon v0.4.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.24.0 // indirect golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
) )
require ( require (
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/go-http-utils/logger v0.0.0-20161128092850-f3a42dcdeae6 github.com/evanw/esbuild v0.24.0
github.com/google/uuid v1.6.0 github.com/google/uuid v1.6.0
github.com/labstack/echo/v4 v4.12.0 github.com/labstack/echo/v4 v4.12.0
github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect

18
go.sum
View file

@ -1,7 +1,11 @@
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc= github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/go-http-utils/logger v0.0.0-20161128092850-f3a42dcdeae6 h1:R/ypabUA7vskKTRSlgP6rMUHTU6PBRgIcHVSU9qQ6qM= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/go-http-utils/logger v0.0.0-20161128092850-f3a42dcdeae6/go.mod h1:CpBLxS3WrxouNECP/Y1A3i6qDnUYs8BvcXjgOW4Vqcw= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/evanw/esbuild v0.24.0 h1:GZ78naTLp7FKr+K7eNuM/SLs5maeiHYRPsTg6kmdsSE=
github.com/evanw/esbuild v0.24.0/go.mod h1:D2vIQZqV/vIf/VRHtViaUtViZmG7o+kKmlBfVQuRi48=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0= github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0=
@ -13,8 +17,12 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w= github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ= github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
@ -29,12 +37,18 @@ golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View file

@ -7,6 +7,7 @@ import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"gitrepo.ru/neonxp/idecnode/cmd/api" "gitrepo.ru/neonxp/idecnode/cmd/api"
"gitrepo.ru/neonxp/idecnode/cmd/fetcher" "gitrepo.ru/neonxp/idecnode/cmd/fetcher"
"gitrepo.ru/neonxp/idecnode/cmd/frontend"
"gitrepo.ru/neonxp/idecnode/cmd/point" "gitrepo.ru/neonxp/idecnode/cmd/point"
) )
@ -18,6 +19,7 @@ func main() {
api.APICommand, api.APICommand,
fetcher.FetcherCommand, fetcher.FetcherCommand,
point.PointCommand, point.PointCommand,
frontend.FrontendCommand,
}, },
} }

View file

@ -1,63 +0,0 @@
package api
import (
"context"
"log"
"net/http"
"os"
"github.com/go-http-utils/logger"
"gitrepo.ru/neonxp/idecnode/pkg/config"
"gitrepo.ru/neonxp/idecnode/pkg/idec"
)
type API struct {
config *config.Config
idec *idec.IDEC
}
func New(i *idec.IDEC, cfg *config.Config) *API {
return &API{
config: cfg,
idec: i,
}
}
func (a *API) Run(ctx context.Context) error {
errorLog := log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
mux := http.NewServeMux()
mux.HandleFunc(`GET /list.txt`, a.getListHandler)
mux.HandleFunc(`GET /blacklist.txt`, a.getBlacklistHandler)
mux.HandleFunc(`GET /u/e/{ids...}`, a.getEchosHandler)
mux.HandleFunc(`GET /u/m/{ids...}`, a.getBundleHandler)
mux.HandleFunc(`GET /u/point/{pauth}/{tmsg}`, a.postPointHandler)
mux.HandleFunc(`POST /u/point`, a.postPointHandler)
mux.HandleFunc(`GET /m/{msgID}`, a.getMessageHandler)
mux.HandleFunc(`GET /e/{id}`, a.getEchoHandler)
mux.HandleFunc(`GET /x/features`, a.getFeaturesHandler)
mux.HandleFunc(`GET /x/c/{ids...}`, a.getEchosInfo)
mux.HandleFunc(`POST /x/filelist`, a.getFilelistHandler)
mux.HandleFunc(`GET /x/filelist/{pauth}`, a.getFilelistHandler)
mux.HandleFunc(`POST /x/file`, a.getFileHandler)
mux.HandleFunc(`GET /x/file/{filename}`, a.getFileHandler)
srv := http.Server{
Addr: a.config.Listen,
Handler: logger.Handler(mux, os.Stdout, logger.Type(a.config.LoggerType)),
ErrorLog: errorLog,
}
go func() {
<-ctx.Done()
srv.Close()
}()
log.Println("started IDEC node at", a.config.Listen)
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
return err
}
return nil
}

View file

@ -1,57 +0,0 @@
package api
import (
"fmt"
"net/http"
"strings"
)
func (a *API) getEchoHandler(w http.ResponseWriter, r *http.Request) {
echoID := r.PathValue("id")
echos, err := a.idec.GetEchosByIDs([]string{echoID}, 0, 0)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if len(echos) == 0 {
return
}
fmt.Fprint(w, strings.Join(echos[echoID].Messages, "\n"))
}
func (a *API) getEchosHandler(w http.ResponseWriter, r *http.Request) {
ids := strings.Split(r.PathValue("ids"), "/")
last := ids[len(ids)-1]
offset, limit := 0, 0
if _, err := fmt.Sscanf(last, "%d:%d", &offset, &limit); err == nil {
ids = ids[:len(ids)-1]
}
echos, err := a.idec.GetEchosByIDs(ids, offset, limit)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
for _, echoID := range ids {
e := echos[echoID]
fmt.Fprintln(w, e.Name)
if len(e.Messages) > 0 {
fmt.Fprintln(w, strings.Join(e.Messages, "\n"))
}
}
}
func (a *API) getEchosInfo(w http.ResponseWriter, r *http.Request) {
ids := strings.Split(r.PathValue("ids"), "/")
echos, err := a.idec.GetEchosByIDs(ids, 0, 0)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
for _, e := range echos {
fmt.Fprintf(w, "%s:%d\n", e.Name, e.Count)
}
}

View file

@ -1,29 +0,0 @@
package api
import (
"fmt"
"net/http"
"strings"
)
func (a *API) getListHandler(w http.ResponseWriter, r *http.Request) {
echos, err := a.idec.GetEchos()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
for _, e := range echos {
fmt.Fprintf(w, "%s:%d:%s\n", e.Name, e.Count, e.Description)
}
}
func (a *API) getBlacklistHandler(w http.ResponseWriter, r *http.Request) {
list, err := a.idec.GetBlacklist()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprint(w, strings.Join(list, "\n"))
}

View file

@ -1,68 +0,0 @@
package api
import (
"encoding/base64"
"fmt"
"log"
"net/http"
"strings"
)
func (a *API) getBundleHandler(w http.ResponseWriter, r *http.Request) {
ids := strings.Split(r.PathValue("ids"), "/")
for _, messageID := range ids {
msg, err := a.idec.GetMessage(messageID)
if err != nil {
log.Println("cant read file for message", messageID, err)
continue
}
b64msg := base64.StdEncoding.EncodeToString([]byte(msg.Bundle()))
fmt.Fprintf(w, "%s:%s\n", messageID, b64msg)
}
}
func (a *API) getMessageHandler(w http.ResponseWriter, r *http.Request) {
msgID := r.PathValue("msgID")
msg, err := a.idec.GetMessage(msgID)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
_, err = fmt.Fprintln(w, msg.Bundle())
}
func (a *API) postPointHandler(w http.ResponseWriter, r *http.Request) {
msg, pauth := r.PathValue("tmsg"), r.PathValue("pauth")
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
form := r.PostForm
if form.Has("tmsg") {
msg = form.Get("tmsg")
}
if form.Has("pauth") {
pauth = form.Get("pauth")
}
a.savePointMessage(w, msg, pauth)
}
func (a *API) savePointMessage(w http.ResponseWriter, rawMessage, auth string) error {
point, err := a.idec.GetPointByAuth(auth)
if err != nil {
fmt.Fprintln(w, "error: no auth - wrong authstring")
return err
}
if err := a.idec.SavePointMessage(point.Username, rawMessage); err != nil {
return err
}
fmt.Fprintln(w, "msg ok")
return nil
}

View file

@ -1,15 +0,0 @@
package api
import (
"fmt"
"net/http"
"strings"
"gitrepo.ru/neonxp/idecnode/pkg/idec"
)
func (a *API) getFeaturesHandler(w http.ResponseWriter, r *http.Request) {
if _, err := fmt.Fprint(w, strings.Join(idec.Features, "\n")); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

44
pkg/apiv1/api.go Normal file
View file

@ -0,0 +1,44 @@
package apiv1
import (
"github.com/labstack/echo/v4"
"gitrepo.ru/neonxp/idecnode/pkg/config"
"gitrepo.ru/neonxp/idecnode/pkg/idec"
)
type API struct {
config *config.Config
idec *idec.IDEC
}
func New(i *idec.IDEC, cfg *config.Config) *API {
return &API{
config: cfg,
idec: i,
}
}
func (a *API) Register(e *echo.Echo) {
e.GET(`/list.txt`, a.getListHandler)
e.GET(`/blacklist.txt`, a.getBlacklistHandler)
func(g *echo.Group) {
g.GET(`/e/*`, a.getEchosHandler)
g.GET(`/m/*`, a.getBundleHandler)
g.GET(`/point/:pauth/:tmsg`, a.postPointHandler)
g.POST(`/point`, a.postPointHandler)
}(e.Group("/u"))
e.GET(`/e/:id`, a.getEchoHandler)
e.GET(`/m/:msgID`, a.getMessageHandler)
func(g *echo.Group) {
e.GET(`/features`, a.getFeaturesHandler)
e.GET(`/c/*`, a.getEchosInfo)
// e.POST(`/filelist`, a.getFilelistHandler)
// e.GET(`/filelist/:pauth`, a.getFilelistHandler)
// e.POST(`/file`, a.getFileHandler)
// e.GET(`/file/:filename`, a.getFileHandler)
}(e.Group("/x"))
}

61
pkg/apiv1/echo.go Normal file
View file

@ -0,0 +1,61 @@
package apiv1
import (
"fmt"
"net/http"
"strings"
"github.com/labstack/echo/v4"
)
func (a *API) getEchoHandler(c echo.Context) error {
echoID := c.Param("id")
echos, err := a.idec.GetEchosByIDs([]string{echoID}, 0, 0)
if err != nil {
return echo.ErrBadGateway
}
if len(echos) == 0 {
return nil
}
return c.String(http.StatusOK, strings.Join(echos[echoID].Messages, "\n"))
}
func (a *API) getEchosHandler(c echo.Context) error {
ids := strings.Split(c.Param("*"), "/")
last := ids[len(ids)-1]
offset, limit := 0, 0
if _, err := fmt.Sscanf(last, "%d:%d", &offset, &limit); err == nil {
ids = ids[:len(ids)-1]
}
echos, err := a.idec.GetEchosByIDs(ids, offset, limit)
if err != nil {
return echo.ErrBadRequest
}
for _, echoID := range ids {
e := echos[echoID]
fmt.Fprintln(c.Response(), e.Name)
if len(e.Messages) > 0 {
fmt.Fprintln(c.Response(), strings.Join(e.Messages, "\n"))
}
}
return nil
}
func (a *API) getEchosInfo(c echo.Context) error {
ids := strings.Split(c.Param("*"), "/")
echos, err := a.idec.GetEchosByIDs(ids, 0, 0)
if err != nil {
return echo.ErrBadRequest
}
for _, e := range echos {
fmt.Fprintf(c.Response(), "%s:%d\n", e.Name, e.Count)
}
return nil
}

View file

@ -1,4 +1,4 @@
package api package apiv1
import ( import (
"fmt" "fmt"

32
pkg/apiv1/list.go Normal file
View file

@ -0,0 +1,32 @@
package apiv1
import (
"fmt"
"strings"
"github.com/labstack/echo/v4"
)
func (a *API) getListHandler(c echo.Context) error {
echos, err := a.idec.GetEchos()
if err != nil {
return echo.ErrInternalServerError
}
for _, e := range echos {
fmt.Fprintf(c.Response(), "%s:%d:%s\n", e.Name, e.Count, e.Description)
}
return nil
}
func (a *API) getBlacklistHandler(c echo.Context) error {
list, err := a.idec.GetBlacklist()
if err != nil {
return echo.ErrInternalServerError
}
fmt.Fprint(c.Response(), strings.Join(list, "\n"))
return nil
}

63
pkg/apiv1/message.go Normal file
View file

@ -0,0 +1,63 @@
package apiv1
import (
"encoding/base64"
"fmt"
"log"
"net/http"
"strings"
"github.com/labstack/echo/v4"
)
func (a *API) getBundleHandler(c echo.Context) error {
ids := strings.Split(c.Param("*"), "/")
for _, messageID := range ids {
msg, err := a.idec.GetMessage(messageID)
if err != nil {
log.Println("cant read file for message", messageID, err)
continue
}
b64msg := base64.StdEncoding.EncodeToString([]byte(msg.Bundle()))
fmt.Fprintf(c.Response(), "%s:%s\n", messageID, b64msg)
}
return nil
}
func (a *API) getMessageHandler(c echo.Context) error {
msgID := c.Param("msgID")
msg, err := a.idec.GetMessage(msgID)
if err != nil {
return echo.ErrBadRequest
}
return c.String(http.StatusOK, msg.Bundle())
}
func (a *API) postPointHandler(c echo.Context) error {
form := new(postForm)
if err := c.Bind(form); err != nil {
return err
}
point, err := a.idec.GetPointByAuth(form.PAuth)
if err != nil {
return c.String(http.StatusForbidden, "error: no auth - wrong authstring")
}
if err := a.idec.SavePointMessage(point.Username, form.TMsg); err != nil {
return err
}
return c.String(http.StatusOK, "msg ok")
}
type postForm struct {
TMsg string `form:"tmsg"`
PAuth string `form:"pauth"`
}

13
pkg/apiv1/misc.go Normal file
View file

@ -0,0 +1,13 @@
package apiv1
import (
"net/http"
"strings"
"github.com/labstack/echo/v4"
"gitrepo.ru/neonxp/idecnode/pkg/idec"
)
func (a *API) getFeaturesHandler(c echo.Context) error {
return c.String(http.StatusOK, strings.Join(idec.Features, "\n"))
}

27
pkg/apiv2/api.go Normal file
View file

@ -0,0 +1,27 @@
package apiv2
import (
"github.com/labstack/echo/v4"
"gitrepo.ru/neonxp/idecnode/pkg/config"
"gitrepo.ru/neonxp/idecnode/pkg/idec"
)
type API struct {
config *config.Config
idec *idec.IDEC
}
func New(i *idec.IDEC, cfg *config.Config) *API {
return &API{
config: cfg,
idec: i,
}
}
func (a *API) Register(e *echo.Echo) {
func(g *echo.Group) {
g.GET("/list", a.getListHandler)
g.GET("/e", a.getEchoHandler)
g.GET("/m", a.getMessagesHandler)
}(e.Group("/api"))
}

36
pkg/apiv2/echo.go Normal file
View file

@ -0,0 +1,36 @@
package apiv2
import (
"net/http"
"github.com/labstack/echo/v4"
)
func (a *API) getListHandler(c echo.Context) error {
echos, err := a.idec.GetEchos()
if err != nil {
return err
}
return c.JSON(http.StatusOK, echos)
}
func (a *API) getEchoHandler(c echo.Context) error {
q := new(getEchosRequest)
if err := c.Bind(q); err != nil {
return err
}
echos, err := a.idec.GetEchosByIDs(q.EchoIDs, q.Offset, q.Limit)
if err != nil {
return err
}
return c.JSON(http.StatusOK, echos)
}
type getEchosRequest struct {
EchoIDs []string `query:"e"`
Offset int `query:"offset"`
Limit int `query:"limit"`
}

28
pkg/apiv2/message.go Normal file
View file

@ -0,0 +1,28 @@
package apiv2
import (
"net/http"
"github.com/labstack/echo/v4"
)
func (a *API) getMessagesHandler(c echo.Context) error {
q := new(getMessagesRequest)
if err := c.Bind(q); err != nil {
return err
}
msgs, err := a.idec.GetMessagesByEcho(q.Echo, q.Message, q.Offset, q.Limit)
if err != nil {
return err
}
return c.JSON(http.StatusOK, msgs)
}
type getMessagesRequest struct {
Echo string `query:"e"`
Message string `query:"m"`
Offset int `query:"offset"`
Limit int `query:"limit"`
}

View file

@ -67,7 +67,7 @@ func (f *Fetcher) downloadMessages(node config.Node, messagesToDownloads []strin
func (f *Fetcher) getMissedEchoMessages(node config.Node, echoID string) ([]string, error) { func (f *Fetcher) getMissedEchoMessages(node config.Node, echoID string) ([]string, error) {
missed := []string{} missed := []string{}
messages, err := f.idec.GetMessagesByEcho(echoID, 0, 0) messages, _, err := f.idec.GetMessageIDsByEcho(echoID, 0, 0)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -18,7 +18,7 @@ func (i *IDEC) GetEchosByIDs(echoIDs []string, offset, limit int) (map[string]mo
continue continue
} }
messages, err := i.GetMessagesByEcho(echoID, offset, limit) messages, count, err := i.GetMessageIDsByEcho(echoID, offset, limit)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -27,7 +27,7 @@ func (i *IDEC) GetEchosByIDs(echoIDs []string, offset, limit int) (map[string]mo
Name: echoID, Name: echoID,
Description: echoCfg.Description, Description: echoCfg.Description,
Messages: messages, Messages: messages,
Count: len(messages), Count: count,
} }
} }

View file

@ -3,16 +3,17 @@ package idec
import ( import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"log"
"strings" "strings"
"gitrepo.ru/neonxp/idecnode/pkg/model" "gitrepo.ru/neonxp/idecnode/pkg/model"
"go.etcd.io/bbolt" "go.etcd.io/bbolt"
) )
func (i *IDEC) GetMessagesByEcho(echoID string, offset, limit int) ([]string, error) { func (i *IDEC) GetMessageIDsByEcho(echoID string, offset, limit int) ([]string, int, error) {
messages := make([]string, 0) messages := make([]string, 0, limit)
count := 0
return messages, i.db.View(func(tx *bbolt.Tx) error { return messages, count, i.db.View(func(tx *bbolt.Tx) error {
if _, ok := i.config.Echos[echoID]; !ok { if _, ok := i.config.Echos[echoID]; !ok {
return nil return nil
} }
@ -22,14 +23,15 @@ func (i *IDEC) GetMessagesByEcho(echoID string, offset, limit int) ([]string, er
return nil return nil
} }
count = bEcho.Stats().KeyN
cur := bEcho.Cursor() cur := bEcho.Cursor()
cur.First() cur.First()
all := bEcho.Stats().KeyN
if limit == 0 { if limit == 0 {
limit = all limit = count
} }
if offset < 0 { if offset < 0 {
offset = max(0, all+offset-1) offset = max(0, count+offset-1)
} }
for i := 0; i < offset; i++ { for i := 0; i < offset; i++ {
// skip offset entries // skip offset entries
@ -47,6 +49,57 @@ func (i *IDEC) GetMessagesByEcho(echoID string, offset, limit int) ([]string, er
}) })
} }
func (i *IDEC) GetMessagesByEcho(echoID string, parent string, offset, limit int) ([]*model.Message, error) {
messages := make([]*model.Message, 0, limit)
return messages, i.db.View(func(tx *bbolt.Tx) error {
if _, ok := i.config.Echos[echoID]; !ok {
return nil
}
bEcho := tx.Bucket([]byte(echoID))
if bEcho == nil {
return nil
}
bMessages := tx.Bucket([]byte(msgBucket))
if bMessages == nil {
return nil
}
count := bEcho.Stats().KeyN
cur := bEcho.Cursor()
cur.First()
if limit == 0 {
limit = count
}
if offset < 0 {
offset = max(0, count+offset-1)
}
for i := 0; i < offset; i++ {
// skip offset entries
cur.Next()
}
for i := 0; len(messages) < limit; i++ {
_, v := cur.Next()
if v == nil {
break
}
msgText := bMessages.Get(v)
msg, err := model.MessageFromBundle(string(v), string(msgText))
if err != nil {
return err
}
if msg.RepTo == parent {
messages = append(messages, msg)
}
}
return nil
})
}
func (i *IDEC) GetMessage(messageID string) (*model.Message, error) { func (i *IDEC) GetMessage(messageID string) (*model.Message, error) {
var msg *model.Message var msg *model.Message
return msg, i.db.View(func(tx *bbolt.Tx) error { return msg, i.db.View(func(tx *bbolt.Tx) error {
@ -62,6 +115,28 @@ func (i *IDEC) GetMessage(messageID string) (*model.Message, error) {
}) })
} }
func (i *IDEC) GetMessages(messageIDs []string) ([]*model.Message, error) {
msgs := make([]*model.Message, 0, len(messageIDs))
return msgs, i.db.View(func(tx *bbolt.Tx) error {
bucket := tx.Bucket([]byte(msgBucket))
if bucket == nil {
return ErrMessageNotFound
}
for _, messageID := range messageIDs {
b := bucket.Get([]byte(messageID))
msg, err := model.MessageFromBundle(messageID, string(b))
if err != nil {
log.Println(err)
continue
}
msgs = append(msgs, msg)
}
return nil
})
}
func (i *IDEC) SavePointMessage(point string, rawMessage string) error { func (i *IDEC) SavePointMessage(point string, rawMessage string) error {
rawMessage = strings.NewReplacer("-", "+", "_", "/").Replace(rawMessage) rawMessage = strings.NewReplacer("-", "+", "_", "/").Replace(rawMessage)

View file

@ -6,10 +6,10 @@ import (
) )
type Echo struct { type Echo struct {
Name string Name string `json:"name"`
Description string Description string `json:"description"`
Count int Count int `json:"count"`
Messages []string Messages []string `json:"messages,omitempty"`
} }
func (e *Echo) Format() string { func (e *Echo) Format() string {

View file

@ -1,7 +1,7 @@
package model package model
type File struct { type File struct {
Name string Name string `json:"name"`
Size int64 Size int64 `json:"size"`
FullName string FullName string `json:"full_name"`
} }

View file

@ -8,15 +8,15 @@ import (
) )
type Message struct { type Message struct {
ID string ID string `json:"id"`
RepTo string RepTo string `json:"rep_to"`
EchoArea string EchoArea string `json:"echo_area"`
Date time.Time Date time.Time `json:"date"`
From string From string `json:"from"`
Addr string Addr string `json:"addr"`
MsgTo string MsgTo string `json:"msg_to"`
Subject string Subject string `json:"subject"`
Message string Message string `json:"message"`
} }
func (m *Message) Bundle() string { func (m *Message) Bundle() string {

View file

@ -7,8 +7,8 @@ func init() {
} }
type Point struct { type Point struct {
Username string Username string `json:"username"`
Email string Email string `json:"email"`
Password []byte Password []byte `json:"-"`
AuthString string AuthString string `json:"auth_string"`
} }

1302
web/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

18
web/package.json Normal file
View file

@ -0,0 +1,18 @@
{
"name": "idec",
"version": "1.0.0",
"main": "dist/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"@picocss/pico": "^2.0.6",
"react": "^18.3.1",
"react-markdown": "^9.0.1",
"react-router-dom": "^6.27.0",
"remark-breaks": "^4.0.0"
}
}

View file

@ -0,0 +1,20 @@
import React from "react";
import Markdown from "react-markdown";
import remarkBreaks from "remark-breaks";
const Message = ({message}) => {
return (
<article name={message.id}>
<header className="msg-header">
<span>{message.subject}</span>
<span>{(new Date(message.date)).toLocaleDateString()}</span>
</header>
<Markdown remarkPlugins={[remarkBreaks]}>{message.message}</Markdown>
<pre>
{JSON.stringify(message, null, 4)}
</pre>
</article>
);
}
export default Message;

6
web/src/index.css Normal file
View file

@ -0,0 +1,6 @@
@import url("@picocss/pico");
.msg-header {
display: flex;
justify-content: space-between;
}

13
web/src/index.html Normal file
View file

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>IDEC Client</title>
<link rel="stylesheet" href="./index.css" />
</head>
<body>
<div id="app">Для этого приложения необходим JavaScript</div>
</body>
<script src="./index.js"></script>
</html>

49
web/src/index.js Normal file
View file

@ -0,0 +1,49 @@
import React from "react";
import { createRoot } from "react-dom/client";
import { createHashRouter, RouterProvider } from "react-router-dom";
import Root from "./root";
import List from "./pages/list";
import Echo from "./pages/echo";
const router = createHashRouter([
{
path: "/",
element: <Root />,
// loader: rootLoader,
children: [
{
path: "",
element: <List />,
loader: () => {
return fetch("/api/list").then((x) => x.json());
},
},
{
path: "e/:echoID",
element: <Echo />,
loader: async ({ params }) => {
const echoData = await fetch(
`/api/e?e=${params.echoID}&limit=10`
).then((x) => x.json());
let echo = [];
if (echoData[params.echoID]) {
echo = echoData[params.echoID];
}
const messages = await fetch(
`/api/m?e=${params.echoID}`
).then((x) => x.json());
return { echo, messages: messages.reverse() };
},
},
],
},
]);
const root = createRoot(document.getElementById("app"));
root.render(
<RouterProvider
router={router}
fallbackElement={<article aria-busy="true">Загрузка</article>}
/>
);

22
web/src/pages/echo.js Normal file
View file

@ -0,0 +1,22 @@
import React from "react";
import { useLoaderData, useParams } from "react-router";
import Message from "../components/message";
const Echo = () => {
const {echo, messages} = useLoaderData();
const params = useParams();
if (!echo) {
return (<article aria-busy="true">Загрузка списка сообщений</article>)
}
return (
<>
<h1>{echo.name}</h1>
<p>Сообщений: {echo.count}</p>
{messages.map((message) => (
<Message key={message.id} message={message} />
))}
</>
);
};
export default Echo;

21
web/src/pages/list.js Normal file
View file

@ -0,0 +1,21 @@
import React from "react";
import { useLoaderData } from "react-router";
import { Link } from "react-router-dom";
const List = () => {
const list = useLoaderData();
return list
.sort((e1, e2) => e2.count - e1.count)
.map((e) => (
<article key={e.name}>
<header>
<Link to={`/e/${e.name}`}>{e.name}</Link>
&nbsp; [{e.count}]
</header>
{e.description}
</article>
));
};
export default List;

10
web/src/root.js Normal file
View file

@ -0,0 +1,10 @@
import React from "react";
import { Outlet } from "react-router";
const Root = () => (
<main className="container-fluid">
<Outlet />
</main>
)
export default Root;