diff options
Diffstat (limited to 'pkg/api/api.go')
-rw-r--r-- | pkg/api/api.go | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/pkg/api/api.go b/pkg/api/api.go new file mode 100644 index 0000000..6bbdd30 --- /dev/null +++ b/pkg/api/api.go @@ -0,0 +1,59 @@ +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.getBlacklistTxtHandler) + mux.HandleFunc(`GET /u/e/{ids...}`, a.getEchosHandler) + mux.HandleFunc(`GET /u/m/{ids...}`, a.getBundleHandler) + mux.HandleFunc(`GET /u/point/{pauth}/{tmsg}`, a.getPointHandler) + 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) + + 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 +} |