-
Notifications
You must be signed in to change notification settings - Fork 1
/
http-main.go
83 lines (70 loc) · 1.71 KB
/
http-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
// Copyright 2021 Safecast. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
)
// Handle http requests to the root url
func httpMainHandler(rsp http.ResponseWriter, req *http.Request) {
// Process the request URI, looking for things that will indicate "dev"
method := req.Method
if method == "" {
method = "GET"
}
// Get the target
target, _, err := httpArgs(req, "")
if err != nil {
return
}
if strings.HasSuffix(target, "/") {
target = strings.TrimSuffix(target, "/")
}
// Process the stream
if method == "GET" && target == "" {
streamLaunch(rsp, req)
return
}
// If the resource has a slash in it, it's in a collection
if strings.Contains(target, "/") {
s := strings.Split(target, "/")
collection := s[0]
filename := s[1]
contents, err := uploadRead(collection, filename)
if err != nil {
fmt.Printf("uploadRead: %s\n", err)
} else {
rsp.Write(contents)
}
return
}
// Attempt to load the resource
contents, _ := resourceRead(target)
rsp.Write(contents)
// Done
return
}
// Get a resource path
func resourcePath(filename string) (path string) {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return ""
}
path = dir + filePathResources + filename
return
}
// Get a resource URL
func resourceURL(filename string) (url string) {
url = "http://" + thisServerAddressIPv4 + thisServerPort + "/" + filename
return
}
// Get a resource's contents
func resourceRead(filename string) (contents []byte, err error) {
contents, err = ioutil.ReadFile(resourcePath(filename))
return
}