-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (48 loc) · 1.47 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
// Use this intead of custom tsv handler
// https://earthly.dev/blog/golang-csv-files/#:~:text=data%20in%20Golang.-,Using%20Encoding%2Fcsv%20To%20Work%20with%20CSV%20Files,read%20and%20write%20CSV%20data.
import (
"context"
"database/sql"
"fmt"
"log"
"log/slog"
"net/http"
"os"
"github.com/Unaimend/cpdBoy/server"
_ "github.com/mattn/go-sqlite3"
)
func main() {
// Creating log file
logFile, err := os.OpenFile("logs/app.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
defer logFile.Close()
//Init db and slog
myHandler := &server.DataBaseHandler {}
logger := slog.New(slog.NewJSONHandler(logFile, nil))
logger.LogAttrs(context.Background(), slog.LevelInfo, "Starting the server")
slog.SetDefault(logger)
db, err := sql.Open("sqlite3", "./my_database.db")
myHandler.Db = *db
if err != nil {
log.Fatal("Failed to open the database:", err)
}
slog.Info("Opened the database successfully")
defer db.Close()
// Check the connection
if err := db.Ping(); err != nil {
log.Fatal("Failed to connect to the database:", err)
}
// Set up routes
http.HandleFunc("/message", func(w http.ResponseWriter, r *http.Request) {
myHandler.PostMessage(w, r)
})
// Start the server
port := ":3000"
slog.Info(fmt.Sprintf("Server is running on http://localhost%s\n", port))
if err := http.ListenAndServe(port, nil); err != nil {
log.Fatalf("Error starting server: %v", err)
}
}