diff options
Diffstat (limited to 'pkg/api')
-rw-r--r-- | pkg/api/api.go | 63 | ||||
-rw-r--r-- | pkg/api/echo.go | 57 | ||||
-rw-r--r-- | pkg/api/file.go | 51 | ||||
-rw-r--r-- | pkg/api/list.go | 29 | ||||
-rw-r--r-- | pkg/api/message.go | 68 | ||||
-rw-r--r-- | pkg/api/misc.go | 15 |
6 files changed, 0 insertions, 283 deletions
diff --git a/pkg/api/api.go b/pkg/api/api.go deleted file mode 100644 index fd55f1d..0000000 --- a/pkg/api/api.go +++ /dev/null @@ -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 -} diff --git a/pkg/api/echo.go b/pkg/api/echo.go deleted file mode 100644 index 8f7a852..0000000 --- a/pkg/api/echo.go +++ /dev/null @@ -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) - } -} diff --git a/pkg/api/file.go b/pkg/api/file.go deleted file mode 100644 index e2fa8d9..0000000 --- a/pkg/api/file.go +++ /dev/null @@ -1,51 +0,0 @@ -package api - -import ( - "fmt" - "net/http" -) - -func (a *API) getFilelistHandler(w http.ResponseWriter, r *http.Request) { - pauth := r.PathValue("pauth") - if err := r.ParseForm(); err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - - return - } - form := r.PostForm - if form.Has("pauth") { - pauth = form.Get("pauth") - } - - files, err := a.idec.FilesList(pauth) - if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - - return - } - - for _, file := range files { - fmt.Fprintf(w, "%s:%d:%s\n", file.FullName, file.Size, file.Name) - } -} - -func (a *API) getFileHandler(w http.ResponseWriter, r *http.Request) { - filename := r.PathValue("filename") - pauth := "" - if err := r.ParseForm(); err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - - return - } - form := r.PostForm - if form.Has("pauth") { - pauth = form.Get("pauth") - } - if form.Has("filename") { - filename = form.Get("filename") - } - - if err := a.idec.GetFile(pauth, filename, w); err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - } -} diff --git a/pkg/api/list.go b/pkg/api/list.go deleted file mode 100644 index 80a34ea..0000000 --- a/pkg/api/list.go +++ /dev/null @@ -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")) -} diff --git a/pkg/api/message.go b/pkg/api/message.go deleted file mode 100644 index 81e863f..0000000 --- a/pkg/api/message.go +++ /dev/null @@ -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 -} diff --git a/pkg/api/misc.go b/pkg/api/misc.go deleted file mode 100644 index 85f7a88..0000000 --- a/pkg/api/misc.go +++ /dev/null @@ -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) - } -} |