-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (45 loc) · 1.07 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
package main
import (
"./pkg/config"
"./pkg/logs"
"fmt"
"github.com/gorilla/handlers"
"log"
"net/http"
"time"
)
const port = "8297"
func main() {
db, err:= config.DbConnect()
HandleError(err)
fmt.Println("Connected to Database")
config.Database = db
//Cross-Origin Scripting
headers := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type"})
origins := handlers.AllowedOrigins([]string{"*"})
methods := handlers.AllowedMethods([]string{
http.MethodGet,
http.MethodPost,
http.MethodPost,
http.MethodDelete,
http.MethodOptions,
})
//Start API
fmt.Println("Starting API on port", port)
fmt.Println("API running on port", port)
router:= NewRouter()
server:= &http.Server{
Addr: fmt.Sprintf("127.0.0.1:%v", port),
Handler: handlers.CORS(origins, headers, methods)(router),
WriteTimeout: 1 * time.Second,
ReadTimeout: 1 * time.Second,
}
log.Fatal(server.ListenAndServe())
}
func HandleError(err error) {
if err != nil {
logs.LogError(err)
fmt.Println("Unable to setup resources")
log.Panic(err)
}
}