-
Notifications
You must be signed in to change notification settings - Fork 1
/
echo.go
54 lines (46 loc) · 1.15 KB
/
echo.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
package main
import (
"fmt"
"log"
"os"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/jjmrocha/load-testing-playground/echo/app"
"github.com/labstack/echo/v4"
)
func main() {
// Connect to db
db := connectDB()
defer db.Close()
// Migrate tables
migrate(db)
// create API handler
h := &app.Handler{DB: db}
// Create routes
e := createServer(h)
e.Logger.Fatal(e.Start(":8080"))
}
func connectDB() *gorm.DB {
host := os.Getenv("DB_SERVER_NAME")
port := os.Getenv("DB_SERVER_PORT")
user := os.Getenv("DB_USER")
dbName := os.Getenv("DB_NAME")
password := os.Getenv("DB_PASSWORD")
connectStr := fmt.Sprintf("sslmode=disable host=%s port=%s dbname=%s user=%s password=%s",
host, port, dbName, user, password)
db, err := gorm.Open("postgres", connectStr)
if err != nil {
log.Fatalf("Failed to connect database: '%v'", err)
}
return db
}
func migrate(db *gorm.DB) {
db.AutoMigrate(&app.Application{})
}
func createServer(h *app.Handler) *echo.Echo {
e := echo.New()
e.GET("/apps/:app_id", h.GetApplication)
e.PUT("/apps/:app_id", h.StoreApplication)
e.DELETE("/apps/:app_id", h.DeleteApplication)
return e
}