forked from sayedalmahdi/cse-carnival-chapter-2-2023
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
73 lines (57 loc) · 2.05 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
package main
import (
"database/sql"
"fmt"
"net/http"
"handyHire/router"
_ "github.com/go-sql-driver/mysql"
"github.com/rs/cors"
)
func main() {
if CreateDbConnection() {
fmt.Println("Server is running on port 5000...")
} else {
fmt.Println("Server is stopped!")
return
}
r := router.Router()
// Create a CORS handler with desired options.
// it will allow api to be accessed from any origin
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"}, // All origins
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
})
// wrapping router with the CORS handler.
// wrapping is done to allow api to be accessed from any origin
handler := c.Handler(r)
//connect my frontend dir G:\SUST_FTMS_Frontend with my backend dir G:\SUST_FTMS_Backend also handle handler
// http.Handle("/", http.FileServer(http.Dir("G:\\SUST_FTMS_Frontend"))) // registering router with http Handle.
http.Handle("/api/", handler) // registering router with http Handle.
// it will handle all the incoming requests. "/" means all incoming requests.
// second parameter is the router. here it is wrapped with CORS handler.
http.ListenAndServe(":5000", nil) // this will start the server.
// second parameter is the handler. nil means use default handler.
// default handler is router. so it will use router to handle all the incoming requests.
fmt.Println("Server is stopped!...") // shows that server is stopped
}
// connecting to mysql database
func CreateDbConnection() bool {
db, err := sql.Open("mysql", "root:@tcp(localhost:3306)/handyhire")
defer db.Close()
// port 3306 is the default port for mysql in xampp
// here ftms is the database name
if err != nil {
fmt.Println("Error connecting databse!")
return false
// panic(err.Error())
}
// db.SetMaxOpenConns(10)
// db.SetMaxIdleConns(5)
// Ping the database to ensure the connection is valid.
if err := db.Ping(); err != nil {
fmt.Printf("Could not connect to the database: %v", err)
return false
}
fmt.Println("Successfully connected to mysql database.")
return true
}