-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (82 loc) · 2.66 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
83
84
85
86
87
88
89
90
91
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
rest "github.com/scblur869/neo4j-api/rest"
handler "github.com/scblur869/neo4j-api/services"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func init() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
key := handler.SetEncryptionKeyEnv()
handler.CreateKeyFile("key.txt", key)
os.Setenv("AESKEY", key)
fmt.Println("key :", os.Getenv("AESKEY"))
}
func main() {
//lets create some tables if they DONT EXIST!!!
// allowedHost := os.Getenv("ALLOWED")
appAddr := "0.0.0.0:" + os.Getenv("PORT")
// setting up the handler for reciever functions
neo := new(handler.NeoHandler)
neo.Ctx = context.Background()
neo.Config = handler.ParseConfiguration()
driver, err := neo.Config.NewDriver()
if err != nil {
log.Fatal(err)
}
neo.Driver = driver
// gin.SetMode(gin.ReleaseMode)
// routes
r := gin.Default()
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost"},
AllowMethods: []string{"POST", "GET", "OPTIONS"},
AllowHeaders: []string{"Access-Control-Allow-Headers", "Access-Control-Allow-Origin", "Origin", "Accept", "X-Requested-With", "Content-Type", "Authorization", "Access-Control-Request-Method", "Access-Control-Request-Headers"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
r.GET("/api/v1/health", rest.HealthCheck) //tested
r.GET("/api/v1/getAll", neo.GetAllNodesAndRelationships) // tested
r.GET("/api/v1/getAllNodes", neo.GetAllNodes) // tested
r.POST("/api/db/purge", neo.PurgeDbData) // tested
r.POST("/api/v1/addNode", neo.CreateNode) // tested
r.POST("/api/v1/addRelation", neo.CreatRelationship) // tested
r.POST("/api/v1/deleteNode", neo.DeleteNode) // tested
r.POST("/api/v1/deleteRelation", neo.DeleteRelation) // tested
r.NoRoute(func(c *gin.Context) {
c.JSON(404, gin.H{"code": "PAGE_NOT_FOUND", "message": "Not Found"})
})
//server config
srv := &http.Server{
Addr: appAddr,
Handler: r,
}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()
//Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Println("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
log.Println("Server exiting")
}