This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (96 loc) · 2.31 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
murmur "github.com/mono0x/murmur/lib"
"github.com/urfave/cli"
"golang.org/x/sync/errgroup"
)
func run() error {
app := cli.NewApp()
app.Name = "murmur"
app.Commands = []cli.Command{
{
Name: "update",
Flags: []cli.Flag{
cli.StringFlag{Name: "config", Value: "config.yaml", Usage: "config file"},
},
Action: func(c *cli.Context) error {
configFile := c.String("config")
f, err := os.Open(configFile)
if err != nil {
return fmt.Errorf("%w", err)
}
defer f.Close()
config, err := murmur.LoadConfig(f)
if err != nil {
return err
}
return murmur.Execute(config)
},
},
{
Name: "serve",
Flags: []cli.Flag{
cli.StringFlag{Name: "listen"},
cli.IntFlag{Name: "port", Value: 8080, EnvVar: "PORT", Usage: "listen port"},
},
Action: func(c *cli.Context) error {
r := chi.NewRouter()
r.Use(middleware.Recoverer)
r.Post("/jobs/exec", func(w http.ResponseWriter, r *http.Request) {
config, err := murmur.LoadConfig(r.Body)
if err != nil {
log.Printf("%+v\n", err)
w.WriteHeader(http.StatusBadRequest)
return
}
if err := murmur.Execute(config); err != nil {
log.Printf("%+v\n", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
})
r.Post("/jobs/exec_bulk", func(w http.ResponseWriter, r *http.Request) {
configs, err := murmur.LoadBulkConfig(r.Body)
if err != nil {
log.Printf("%+v\n", err)
w.WriteHeader(http.StatusBadRequest)
return
}
eg := errgroup.Group{}
for _, config := range configs {
config := config
eg.Go(func() error {
return murmur.Execute(config)
})
}
if err := eg.Wait(); err != nil {
log.Printf("%+v\n", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
})
listen := c.String("listen")
if listen == "" {
listen = ":" + strconv.Itoa(c.Int("port"))
}
return http.ListenAndServe(listen, r)
},
},
}
return app.Run(os.Args)
}
func main() {
log.SetFlags(log.Lshortfile)
if err := run(); err != nil {
log.Fatalf("%v\n", err)
}
}