-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
188 lines (167 loc) · 5.54 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"fmt"
"github.com/EricBurnett/WebCmd/modules"
"github.com/EricBurnett/WebCmd/resources"
"github.com/EricBurnett/WebCmd/staticcontent"
"html/template"
"log"
"net/http"
"strings"
)
// A WebCmd webserver.
type WebCmdServer struct {
http.Server
errorTemplate *template.Template
modules map[string]modules.Module
staticContentServer *staticcontent.Server
}
// Returns a WebCmd server, fully initialized but not started. If any
// unhandleable errors are encountered during initialization, panics.
func CreateServer(host string) *WebCmdServer {
var err error
var errorTemplate = template.New("Error template")
errorTemplate, err = errorTemplate.Parse(ERROR_TEMPLATE_STR)
if err != nil {
log.Fatal("Error creating error template: ", err)
}
server := WebCmdServer{
Server: http.Server{
Addr: host,
},
errorTemplate: errorTemplate,
modules: make(map[string]modules.Module),
}
server.staticContentServer = staticcontent.NewServer("/static_root", server.Server)
if err = staticcontent.AddCsvPaths(server.staticContentServer); err != nil {
log.Println("Error installing paths from csv:", err)
}
allModules := modules.InstalledModules(server.staticContentServer)
for _, module := range allModules {
for _, command := range module.Commands() {
if _, has := server.modules[command]; has {
log.Println("Handler already present for", command,
"; not installing", module.Name())
continue
}
path := fmt.Sprintf("/%v", command)
http.Handle(path, http.HandlerFunc(server.BareModuleHandler(command, module)))
log.Println("Installing", module.Name(), "at path", path)
server.modules[command] = module
}
}
http.Handle("/", http.HandlerFunc(server.RootHandler()))
return &server
}
type page struct {
Title string // Page title
Message string // Text message to be printed at the top
Body template.HTML // HTML body to be injected into the middle
Path string // Path the main form should redirect to (skip first /)
QueryString string // The query string to add to the form
Command string // The command to delegate to for form executions
}
var BARE_MODULE_FILE = "templates/bare_module.html.template"
// Returns a handler method for running a module outside of the command
// interface.
func (server WebCmdServer) BareModuleHandler(command string, m modules.Module) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
logRequest(req)
template_content, err := resources.Load(BARE_MODULE_FILE)
if err != nil {
server.PrintError(w, err)
return
}
var bareModuleTemplate = template.New("Bare module template")
bareModuleTemplate, err = bareModuleTemplate.Parse(string(template_content))
if err != nil {
server.PrintError(w, err)
return
}
body, err := m.RunEvent(req)
if err != nil {
server.PrintError(w, err)
return
}
query := req.FormValue("q")
p := page{
Title: m.Name(), Body: body, Path: command,
Command: command, QueryString: query}
bareModuleTemplate.Execute(w, &p)
}
}
var ROOT_TEMPLATE_FILE = "templates/root.html.template"
// Returns the base handler method for running a page. Based on the query
// or posted data, may run modules within the command interface, and delegate
// to them for producing the page content.
func (server WebCmdServer) RootHandler() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
logRequest(req)
source := req.FormValue("source")
query := strings.TrimSpace(req.FormValue("q"))
var message string
var command string
var body template.HTML
var err error
if source == "" || (source == "query" && (query == "" || strings.ToLower(query) == "help")) {
body, err = ModuleList(server.modules)
} else if source == "query" {
queryPieces := strings.SplitN(query, " ", 2)
if len(queryPieces) == 1 {
queryPieces = append(queryPieces, "")
}
if module, has := server.modules[queryPieces[0]]; has {
command = queryPieces[0]
body, err = module.RunCommand(queryPieces[0], queryPieces[1])
} else {
message = "Module not found for query. Try again?"
}
} else {
if module, has := server.modules[source]; has {
command = source
body, err = module.RunEvent(req)
} else {
message = "Requested module not found. Try a query instead!"
}
}
if err != nil {
log.Println(err)
message = err.Error()
}
template_content, err := resources.Load(ROOT_TEMPLATE_FILE)
if err != nil {
server.PrintError(w, err)
return
}
var rootTemplate = template.New("Root template")
rootTemplate, err = rootTemplate.Parse(string(template_content))
if err != nil {
server.PrintError(w, err)
return
}
p := page{
Title: "root", QueryString: query, Message: message, Body: body,
Command: command}
rootTemplate.Execute(w, &p)
}
}
type pageError struct {
Error string
}
var ERROR_TEMPLATE_STR = "<html><head><title>WebCmd - Error</title></head>" +
"<body>{{printf \"%s\" .Error |html}}</body></html>"
// Prints an error page detailing the specific error.
func (server WebCmdServer) PrintError(w http.ResponseWriter, e error) {
log.Println(e)
server.errorTemplate.Execute(w, &pageError{Error: e.Error()})
}
// Write the information from a request to the output file.
func logRequest(req *http.Request) {
var form = ""
if req.Method == "POST" {
req.ParseForm()
form = " Form: " + fmt.Sprintf("%v", req.Form)
}
log.Printf("Request: %v %v %v %v%v %v", req.Method, req.Host, req.URL,
req.Proto, form, "From "+req.RemoteAddr)
}