-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
142 lines (120 loc) · 4.11 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// This is the server that accepts and stores data from clients. It is basically an https server.
package main
import (
"net/http"
"github.com/gorilla/mux"
"fmt"
"os"
"path/filepath"
"sync"
"github.com/bankole7782/flaarum/flaarum_shared"
"github.com/bankole7782/zazabul"
"encoding/json"
"github.com/pkg/errors"
)
var projsMutex *sync.RWMutex // for projects and tables (table data uses different mutexes) creation, editing, deletion
var tablesMutexes map[string]*sync.RWMutex
var STOP_WORDS []string
func init() {
dataPath, err := GetDataPath()
if err != nil {
panic(err)
}
// create default project
err = os.MkdirAll(filepath.Join(dataPath, "first_proj"), 0777)
if err != nil {
panic(err)
}
// create mutexes
projsMutex = &sync.RWMutex{}
tablesMutexes = make(map[string]*sync.RWMutex)
confPath, err := flaarum_shared.GetConfigPath()
if err != nil {
panic(err)
}
if ! doesPathExists(confPath) {
conf, err := zazabul.ParseConfig(flaarum_shared.RootConfigTemplate)
if err != nil {
panic(err)
}
conf.Write(confPath)
}
// load stop words once
stopWordsJsonPath := flaarum_shared.G("english-stopwords.json")
jsonBytes, err := os.ReadFile(stopWordsJsonPath)
if err != nil {
panic(err)
}
stopWordsList := make([]string, 0)
err = json.Unmarshal(jsonBytes, &stopWordsList)
if err != nil {
panic(err)
}
STOP_WORDS = stopWordsList
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/is-flaarum", func (w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "yeah-flaarum")
})
// projects
r.HandleFunc("/create-project/{proj}", createProject)
r.HandleFunc("/delete-project/{proj}", deleteProject)
r.HandleFunc("/list-projects", listProjects)
r.HandleFunc("/rename-project/{proj}/{nproj}", renameProject)
// tables
r.HandleFunc("/create-table/{proj}", createTable)
r.HandleFunc("/update-table-structure/{proj}", updateTableStructure)
r.HandleFunc("/get-current-version-num/{proj}/{tbl}", getCurrentVersionNumHTTP)
r.HandleFunc("/get-table-structure/{proj}/{tbl}/{vnum}", getTableStructureHTTP)
r.HandleFunc("/empty-table/{proj}/{tbl}", emptyTable)
r.HandleFunc("/list-tables/{proj}", listTables)
r.HandleFunc("/rename-table/{proj}/{tbl}/{ntbl}", renameTable)
r.HandleFunc("/delete-table/{proj}/{tbl}", deleteTable)
// rows
r.HandleFunc("/insert-row/{proj}/{tbl}", insertRow)
r.HandleFunc("/search-table/{proj}", searchTable)
r.HandleFunc("/delete-rows/{proj}", deleteRows)
r.HandleFunc("/delete-fields/{proj}", deleteFields)
r.HandleFunc("/update-rows/{proj}", updateRows)
r.HandleFunc("/count-rows/{proj}", countRows)
r.HandleFunc("/sum-rows/{proj}", sumRows)
r.HandleFunc("/all-rows-count/{proj}/{tbl}", allRowsCount)
// stats
r.HandleFunc("/get-and-delete-stats", getAndDeleteStats)
r.Use(keyEnforcementMiddleware)
fmt.Printf("Serving on port: %d\n",flaarum_shared.PORT)
err := http.ListenAndServeTLS(fmt.Sprintf(":%d", flaarum_shared.PORT), flaarum_shared.G("https-server.crt"),
flaarum_shared.G("https-server.key"), r)
if err != nil {
panic(err)
}
}
func keyEnforcementMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/get-and-delete-stats" {
next.ServeHTTP(w, r)
} else {
inProd := flaarum_shared.GetSetting("in_production")
if inProd == "" {
panic(errors.New("Have you installed and launched flaarum.store"))
} else if inProd == "true" {
keyStr := r.FormValue("key-str")
keyPath := flaarum_shared.GetKeyStrPath()
raw, err := os.ReadFile(keyPath)
if err != nil {
http.Error(w, "Improperly Configured Server", http.StatusInternalServerError)
}
if keyStr == string(raw) {
// Call the next handler, which can be another middleware in the chain, or the final handler.
next.ServeHTTP(w, r)
} else {
http.Error(w, "Forbidden", http.StatusForbidden)
}
} else {
// Call the next handler, which can be another middleware in the chain, or the final handler.
next.ServeHTTP(w, r)
}
}
})
}