2024-10-20 03:39:07 +03:00
|
|
|
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)
|
2024-10-20 20:38:08 +03:00
|
|
|
mux.HandleFunc(`GET /blacklist.txt`, a.getBlacklistHandler)
|
2024-10-20 03:39:07 +03:00
|
|
|
mux.HandleFunc(`GET /u/e/{ids...}`, a.getEchosHandler)
|
|
|
|
mux.HandleFunc(`GET /u/m/{ids...}`, a.getBundleHandler)
|
2024-10-20 20:38:08 +03:00
|
|
|
mux.HandleFunc(`GET /u/point/{pauth}/{tmsg}`, a.postPointHandler)
|
2024-10-20 03:39:07 +03:00
|
|
|
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)
|
2024-10-20 20:38:08 +03:00
|
|
|
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)
|
2024-10-20 03:39:07 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|