-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
49 lines (37 loc) · 931 Bytes
/
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
package main
import (
"log"
"net/http"
"os"
"github.com/labstack/echo/v4"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"github.com/shellbear/dokku-go-example/models"
)
const defaultPort = "8000"
func main() {
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
postgresURL := os.Getenv("DATABASE_URL")
if postgresURL == "" {
log.Fatalln("missing DATABASE_URL ENV variable.")
}
// Connect to database.
db, err := gorm.Open(postgres.Open(postgresURL), &gorm.Config{})
if err != nil {
log.Fatalln("failed to connect to database:", err)
}
// Create Todo model migrations.
db.Create(&models.Todo{})
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "It works!")
})
e.GET("/todos", GetAllTodos(db))
e.POST("/todos", CreateNewTodo(db))
e.GET("/todos/:id", GetOneTodo(db))
e.PUT("/todos/:id", UpdateOneTodo(db))
e.Logger.Fatal(e.Start(":"+port))
}