This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
halochat-server.go
71 lines (56 loc) · 1.84 KB
/
halochat-server.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
package main
import (
"database/sql"
"encoding/json"
"halochat/halochat-server/database/common"
"halochat/halochat-server/database/event"
"halochat/halochat-server/database/follow"
"halochat/halochat-server/database/seen"
"halochat/halochat-server/database/tag"
"halochat/halochat-server/database/user"
"log"
"mime"
"net/http"
_ "github.com/mattn/go-sqlite3"
)
const fileName = "sqlite.db"
func databases(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*") // for CORS
w.WriteHeader(http.StatusOK)
test := []string{}
test = append(test, "Hello")
test = append(test, "World")
json.NewEncoder(w).Encode(test)
}
var myDb *sql.DB
// Todo
// Crud follow endpoints/
// Crud Block endpoints.
// Add events to database and return events from this database.
func main() {
/*
err := os.Remove(fileName)
if err != nil {
log.Fatal(err)
}
*/
db, err := sql.Open("sqlite3", fileName)
if err != nil {
log.Fatal(err)
}
defer db.Close()
common.CreateTables(db, event.CreatQuery+follow.CreateQuery+seen.CreatQuery+tag.CreatQuery+user.CreatQuery)
follow.InitController(db)
// Windows may be missing this
mime.AddExtensionType(".js", "application/javascript")
log.Println("Starting webserver and listen on http://localhost:8080")
http.Handle("/test", http.HandlerFunc(databases))
http.Handle("/api/follow", http.HandlerFunc(follow.Index)) // Get all
http.Handle("/api/follow/create", http.HandlerFunc(follow.Create))
http.Handle("/api/follow/get", http.HandlerFunc(follow.Get)) // Get by pubkey
http.Handle("/api/follow/delete", http.HandlerFunc(follow.Delete)) // Delete by pubkey
http.Handle("/api/event/create", http.HandlerFunc(event.Create))
http.Handle("/", http.FileServer(http.Dir("dist")))
log.Fatal(http.ListenAndServe(":8080", nil))
}