-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (69 loc) · 1.73 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
package main
import (
"RestAPI_KODE/api"
"RestAPI_KODE/config"
"RestAPI_KODE/database"
"RestAPI_KODE/lib"
"log"
"net/http"
"os"
"os/signal"
"syscall"
)
func main() {
server := &http.Server{
Addr: ":8080",
Handler: nil,
}
log.Println("Started app")
cfg, err := config.LoadConfig("config.yaml")
if err != nil {
lib.Fatalf("Failed to load config: %s", err)
}
database.DB, err = database.NewConnection(cfg.Database)
if err != nil {
lib.Fatalf("Failed to initialized db: %s", err)
}
if database.DB == nil {
log.Fatal("Database not initialized!")
}
lib.Infof("Migrations ok")
http.HandleFunc("/auth", authenticateHandler)
http.HandleFunc("/users", usersHandler)
http.HandleFunc("/notes", notesHandler)
server = &http.Server{Addr: ":8080", Handler: nil}
go func() {
if err := server.ListenAndServe(); err != http.ErrServerClosed {
lib.Fatalf("Error starting the server: %s", err)
}
}()
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT)
<-signals
if err := server.Shutdown(nil); err != nil {
lib.Fatalf("Error shutting down the server: %s", err)
}
}
func authenticateHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
api.AuthUser(w, r)
} else {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
func usersHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
api.CreateUser(w, r)
} else {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
func notesHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
api.AddNote(w, r)
} else if r.Method == "GET" {
api.GetNotes(w, r)
} else {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}