-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
41 lines (31 loc) · 1.43 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
package main
import (
"fmt"
"ftms/controller"
"ftms/router"
"net/http"
"github.com/rs/cors"
)
func main() {
controller.CreateDbConnection() // creating database connection
fmt.Println("Server is running at port 5050...") // shows that server is running
r := router.Router() // create router. it will be used to register routes.
// 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(":5050", 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
}