-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
53 lines (43 loc) · 1.18 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
package main
import (
"log"
"net/http"
"os"
"user-management/controllers"
"user-management/database"
"user-management/entity"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
)
func main() {
initDB()
log.Println("Starting the HTTP server on port 18080")
router := mux.NewRouter().StrictSlash(true)
initaliseHandlers(router)
log.Fatal(http.ListenAndServe(":18080", router))
}
func initaliseHandlers(router *mux.Router) {
router.HandleFunc("/users/{id}", controllers.UpdateUserByID).Methods("PUT")
router.HandleFunc("/users", controllers.CreateUser).Methods("POST")
router.HandleFunc("/users/{id}", controllers.DeletUserByID).Methods("DELETE")
router.HandleFunc("/users", controllers.GetAllUsers).Methods("GET")
}
func initDB() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
config :=
database.Config{
ServerName: os.Getenv("DB_SERVER"),
User: os.Getenv("DB_USER"),
Password: os.Getenv("DB_PASSWORD"),
DBName: os.Getenv("DB_DBNAME"),
}
connetionString := database.GetConnectionString(config)
err = database.Connect(connetionString)
if err != nil {
panic(err.Error())
}
database.Migrate(&entity.User{})
}