-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathosx-builder.go
More file actions
47 lines (39 loc) · 1.24 KB
/
osx-builder.go
File metadata and controls
47 lines (39 loc) · 1.24 KB
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"log"
"net/http"
"strings"
"github.com/c4milo/osx-builder/config"
"github.com/c4milo/osx-builder/vms"
)
// Version string is injected when building the binary from Makefile.
var Version string
func main() {
// Keeps a registry of path function handlers.
registry := map[string]map[string]func(http.ResponseWriter, *http.Request){
"/vms": vms.Handlers,
}
// Main entry point to handle requests. Based on a URL path, this piece of code
// iterates the registry and invokes the path's function handler if there is
// match.
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
for p, handlers := range registry {
if strings.HasPrefix(req.URL.Path, p) {
if handlerFn, ok := handlers[req.Method]; ok {
handlerFn(w, req)
return
}
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("Method Not Allowed"))
return
}
}
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Not Found"))
})
address := ":" + config.Port
log.Fatal(http.ListenAndServe(address, nil))
}