-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
61 lines (50 loc) · 1.31 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
package main
import (
"net/http"
"strconv"
"time"
"log/slog"
"github.com/gorilla/mux"
"github.com/sbshah97/surrealdb-go-starter-project/pkg/database"
"github.com/sbshah97/surrealdb-go-starter-project/pkg/handlers"
)
const (
wsString = "ws://db:8000/rpc"
tableName = "user"
namespace = "test"
dbName = "test"
port = 8080
username = "root"
password = "root"
)
func main() {
db, err := database.NewDatabase(wsString, username, password, namespace, dbName)
if err != nil {
slog.Error("failed to create shortener repository", "error", err)
return
}
slog.Info("Connected to database")
// defer database.DB.Close()
// Close connections to the database at program shutdown
defer func() {
slog.Info("Closing database")
db.Close()
}()
handler := handlers.NewHandler(db)
r := mux.NewRouter()
r.HandleFunc("/users/{id}", handler.FetchUsers).Methods("GET")
r.HandleFunc("/users", handler.CreateUsers).Methods("POST")
http.Handle("/", r)
slog.Info("Listening on http://localhost:" + strconv.Itoa(port))
server := &http.Server{
Addr: ":" + strconv.Itoa(port),
Handler: r,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 15 * time.Second,
}
err = server.ListenAndServe()
if err != nil {
slog.Error("failed to listen", "error", err)
}
}