-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (69 loc) · 1.93 KB
/
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
84
85
86
87
88
89
// Zaradb lite fast document database
package main
import (
"embed"
"fmt"
"io"
"log"
"net/http"
"zaradb/engine"
)
//go:embed static
var content embed.FS
// TODO: Close program gracefully.
func main() {
// should remove from here
db := engine.NewDB("test.db")
db.CreateCollection("test")
defer db.Close()
fmt.Printf("interacte with zaradb through %s:%s\n", Host, Port)
http.Handle("/static/", http.FileServer(http.FS(content)))
http.HandleFunc("/", index)
http.HandleFunc("/index", shell)
http.HandleFunc("/queries", queries)
http.HandleFunc("/shell", shell)
// standard endpoint
http.HandleFunc("/ws", engine.Ws)
// endpoints for speed network
http.HandleFunc("/query", engine.Request)
http.HandleFunc("/result", engine.Response)
// for pages under development
http.HandleFunc("/dev", dev)
log.Println(http.ListenAndServe(":1111", nil))
}
// render static shell.html file
func queries(w http.ResponseWriter, r *http.Request) {
// Read the body of the request
query, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Failed to read request body", http.StatusBadRequest)
return
}
defer r.Body.Close()
result := engine.HandleQueries(string(query))
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(result))
}
// redirect to shell page temporary
func dev(w http.ResponseWriter, r *http.Request) {
f, err := content.ReadFile("static/dev.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprint(w, string(f))
}
// render static shell.html file
func shell(w http.ResponseWriter, r *http.Request) {
f, err := content.ReadFile("static/shell.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprint(w, string(f))
}
// redirect to shell page temporary
func index(w http.ResponseWriter, r *http.Request) {
// TODO create index page
http.Redirect(w, r, "http://localhost:1111/shell", http.StatusSeeOther)
}