-
Notifications
You must be signed in to change notification settings - Fork 173
/
server.go
152 lines (126 loc) · 3.58 KB
/
server.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package actions
import (
"context"
"fmt"
"net/http"
"sort"
"strings"
"sync"
"github.com/lovoo/goka"
"github.com/lovoo/goka/web/templates"
"github.com/gorilla/mux"
)
type Actor interface {
RunAction(ctx context.Context, value string) error
Description() string
}
// Server is a provides HTTP routes for querying the group table.
type Server struct {
log goka.Logger
m sync.RWMutex
basePath string
loader templates.Loader
actions map[string]*action
}
// NewServer creates a server with the given options.
func NewServer(basePath string, router *mux.Router, opts ...Option) *Server {
srv := &Server{
log: goka.DefaultLogger(),
basePath: basePath,
loader: &templates.EmbedLoader{},
actions: make(map[string]*action),
}
for _, opt := range opts {
opt(srv)
}
sub := router.PathPrefix(basePath).Subrouter()
sub.HandleFunc("/", srv.index)
sub.HandleFunc("", srv.index)
sub.HandleFunc("/start/{action:.*}", srv.startAction).Methods("POST")
sub.HandleFunc("/stop/{action:.*}", srv.stopAction).Methods("POST")
return srv
}
func (s *Server) startAction(w http.ResponseWriter, r *http.Request) {
actionName := mux.Vars(r)["action"]
action := s.actions[actionName]
switch {
case action == nil:
s.redirect(w, r, fmt.Sprintf("Action '%s' not found", actionName))
case action.IsRunning():
s.redirect(w, r, "action already running.")
default:
action.Start(r.FormValue("value"))
s.redirect(w, r, "")
}
}
func (s *Server) stopAction(w http.ResponseWriter, r *http.Request) {
actionName := mux.Vars(r)["action"]
action := s.actions[actionName]
switch {
case action == nil:
s.redirect(w, r, fmt.Sprintf("Action '%s' not found", actionName))
case !action.IsRunning():
s.redirect(w, r, "action is not running.")
default:
action.Stop()
s.redirect(w, r, "")
}
}
func (s *Server) redirect(w http.ResponseWriter, r *http.Request, errMessage string) {
var path = s.basePath
if errMessage != "" {
path += "?error=" + errMessage
}
http.Redirect(w, r, path, http.StatusFound)
}
func (s *Server) BasePath() string {
return s.basePath
}
func (s *Server) sortedActions() []*action {
s.m.RLock()
defer s.m.RUnlock()
var actions []*action
for _, action := range s.actions {
actions = append(actions, action)
}
sort.Slice(actions, func(i, j int) bool {
return strings.Compare(actions[i].name, actions[j].name) < 0
})
return actions
}
// AttachSource attaches a new source to the query server.
func (s *Server) AttachAction(name string, actor Actor) error {
s.m.Lock()
defer s.m.Unlock()
if _, exists := s.actions[name]; exists {
return fmt.Errorf("source with name '%s' is already attached", name)
}
s.actions[name] = &action{
name: name,
actor: actor,
}
return nil
}
func (s *Server) AttachFuncAction(name string, description string, actor func(ctx context.Context, value string) error) error {
return s.AttachAction(name, FuncActor(description, actor))
}
func (s *Server) executeTemplate(w http.ResponseWriter, params map[string]interface{}) {
tmpl, err := templates.LoadTemplates(append(templates.BaseTemplates, "web/templates/actions/index.go.html")...)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
params["menu_title"] = "menu title"
params["base_path"] = s.basePath
if err := tmpl.Execute(w, params); err != nil {
s.log.Printf("error executing query template: %v", err)
}
}
func (s *Server) index(w http.ResponseWriter, r *http.Request) {
params := map[string]interface{}{
"page_title": "Actions",
"actions": s.sortedActions(),
"error": r.URL.Query()["error"],
}
s.executeTemplate(w, params)
}