/
echo.go
74 lines (64 loc) · 1.73 KB
/
echo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package serve
import (
"context"
"fmt"
"net"
"net/http"
"strconv"
"github.com/cured-plumbum/i3qws/pkg/i3qws"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/sirupsen/logrus"
)
type (
ops struct {
i3qws *i3qws.I3qws
}
)
// EchoServe creates and starts echo server on Unix socket
func EchoServe(ctx context.Context, stopCh chan bool, i3qws *i3qws.I3qws, socket string) (*echo.Echo, error) {
var lc net.ListenConfig
l, err := lc.Listen(ctx, "unix", socket)
if err != nil {
return nil, fmt.Errorf("is another instance server running? — %w", err)
}
e := echo.New()
// e.Logger = logrus.StandardLogger()
e.Pre(middleware.RemoveTrailingSlash())
e.Listener = l
o := &ops{i3qws: i3qws}
e.Any("/focus/:num", o.focus)
e.Any("/list", o.list)
e.Any("/stop", o.stop(stopCh))
go func() {
if e2 := e.Start(""); e != nil {
logrus.WithError(e2).Warn("Echo exited")
}
}()
return e, nil
}
func (o *ops) focus(c echo.Context) error {
snum := c.Param("num")
num, err := strconv.Atoi(snum)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to convert '%s' to a number", snum)).SetInternal(err)
}
n, err := o.i3qws.Focus(num)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to focus %d window", num)).SetInternal(err)
}
if n == nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("There is no such window number %d", num))
}
return c.JSON(http.StatusOK, n)
}
func (o *ops) list(c echo.Context) error {
nn := o.i3qws.DumpList()
return c.JSON(http.StatusOK, nn)
}
func (o *ops) stop(stopCh chan bool) echo.HandlerFunc {
return func(c echo.Context) error {
close(stopCh)
return c.String(http.StatusOK, "done")
}
}