-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
101 lines (75 loc) · 2 KB
/
server.go
File metadata and controls
101 lines (75 loc) · 2 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
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
package gover
import (
"encoding/json"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"path"
"strings"
)
type RequestHandler func(w http.ResponseWriter, r *http.Request, md map[string]interface{})
// Creates an endpoint instance
func Endpoint(path string) endpoint {
return endpoint{path: path}
}
/*
Dynamicly parses the request body, if it is in json.
Tip: To convert an interface{} to a diffrent type, instead of using type(val) use val.(type)
For example:
var x interface{} = 5
y := x.(int)
*/
func DynamicJSONBodyParser(body io.ReadCloser) (map[string]interface{}, error) {
var out map[string]interface{}
data, err := io.ReadAll(body)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &out)
if err != nil {
return nil, err
}
return out, nil
}
// Starts the server on a port
func Listen(port int) {
fmt.Printf("Server listening on port %v\n", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf("127.0.0.1:%v", port), nil))
}
// Hosts a normal file bin
func HostFolder(path string, folder string) {
http.Handle(path, http.FileServer(http.Dir(folder)))
}
// Hosts a SPA application with a built in router on history mode
func HostSPA(folder string) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
url := r.URL.Path
if url == "/" || strings.Contains(url, ".") {
http.FileServer(http.Dir(folder)).ServeHTTP(w, r)
return
}
http.ServeFile(w, r, path.Join(folder, "/index.html"))
})
}
/*
Gets the file from the request.
The fieldname argument is the name of the input field in your HTML.
For Example: Your HTML input is this:
<input type="file" name="myFile">
In this case you will need to pass to the function "myFile".
*/
func GetFile(fieldname string, r *http.Request) ([]byte, *multipart.FileHeader, error) {
r.ParseMultipartForm(10 << 20)
file, handler, err := r.FormFile(fieldname)
if err != nil {
return nil, nil, err
}
defer file.Close()
data, err := io.ReadAll(file)
if err != nil {
return nil, nil, err
}
return data, handler, nil
}