-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (98 loc) · 2.64 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
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
// Zaradb lite fast document database
package main
import (
"context"
"embed"
"fmt"
"io"
"log"
"net/http"
"os"
"os/signal"
"time"
"zaradb/engine"
)
//go:embed static
var content embed.FS
// TODO: Close program gracefully.
func main() {
db := engine.NewDB("test.db")
if db == nil {
log.Fatal("no db")
return
}
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)
// Create server with a specified address
srv := &http.Server{
Addr: ":1111",
}
// Start the server in a goroutine
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// Graceful shutdown handling
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
fmt.Println()
log.Println("Shutting down zaradb...")
// Create a context with timeout to allow active requests to complete
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
// Gracefully shutdown the server
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("zaradb forced to shutdown:", err)
}
log.Println("zaradb exiting")
}
// 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) {
// TOD create index page
http.Redirect(w, r, "http://localhost:1111/shell", http.StatusSeeOther)
}