-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (58 loc) · 1.83 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
package main
import (
"context"
"crypto/subtle"
"fmt"
"os"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/joho/godotenv"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
type Customer struct {
ID int `json:"id"`
UUID string `json:"uuid"`
FirstName string `json:"firstname"`
LastName string `json:"lastname"`
Address string `json:"address"`
PhoneNumber string `json:"phoneNumber"`
Email string `json:"email"`
Employer string `json:"employer"`
AnnualIncome float32 `json:"annualIncome"`
RequestedCreditAmount float32 `json:"requestedCreditAmount"`
AdditionalInformation string `json:"additionalInformation"`
}
var DATABASE_URL string
var conn *pgxpool.Pool
func main() {
godotenv.Load()
DATABASE_URL = os.Getenv("DATABASE_URL")
user := os.Getenv("username")
pass := os.Getenv("password")
conn = createDatabaseConnection(DATABASE_URL)
defer conn.Close()
e := echo.New()
e.Use(middleware.CORS())
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: `[${time_rfc3339} ${status} ${method} ${host}${path} ${latency_human}]` + "\n",
}))
e.Use(middleware.BasicAuth(func(username, password string, ctx echo.Context) (bool, error) {
if subtle.ConstantTimeCompare([]byte(username), []byte(user)) == 1 &&
subtle.ConstantTimeCompare([]byte(password), []byte(pass)) == 1 {
return true, nil
}
return false, nil
}))
e.POST("/newcredit", addCustomer)
e.POST("/uploadPDF", uploadPDF)
initTempDir()
e.Logger.Fatal(e.Start(":8000"))
}
func createDatabaseConnection(url string) *pgxpool.Pool {
conn, err := pgxpool.New(context.Background(), url)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
return conn
}