-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_integration.go
60 lines (48 loc) · 1.31 KB
/
main_integration.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
//go:build test
// +build test
package main
import (
"api/internal/database"
"api/internal/server"
"fmt"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
var PORT = "5000"
func main() {
db, err := database.OpenConnection(true)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open connection to the database: %s", err.Error())
os.Exit(1)
}
// Turn on gorm debug mode to print SQL queries to the console in local development.
if strings.ToLower(os.Getenv("ENVIRONMENT")) == "development" {
db = db.Debug()
}
// Initialize the server
serv := server.NewAPIServer(db)
// Determine the port to run the server on. If only the PORT environment
// variable is set, use that as the port. If a port is provided via a
// a command flag, use this value instead
port := os.Getenv("PORT")
if port == "" {
// Use command flag value instead
port = PORT
}
serv.Router.POST("/database/reset", func(ctx *gin.Context) {
resetSession := db.Session(&gorm.Session{AllowGlobalUpdate: true})
oldEnvironment := os.Getenv("ENVIRONMENT")
os.Setenv("ENVIRONMENT", "test")
defer os.Setenv("ENVIRONMENT", oldEnvironment)
err := database.WipeDB(resetSession)
if err != nil {
ctx.AbortWithStatus(http.StatusInternalServerError)
return
}
ctx.Status(http.StatusOK)
})
serv.Run(port)
}