mirror of
https://github.com/zoriya/vex.git
synced 2026-08-16 02:44:55 +00:00
Add database connection
This commit is contained in:
+30
-2
@@ -1,23 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
type Handler struct{}
|
||||
type Handler struct {
|
||||
database *sql.DB
|
||||
}
|
||||
|
||||
func (h *Handler) GetEntries(c echo.Context) error {
|
||||
ret := make([]interface{}, 0)
|
||||
return c.JSON(200, ret)
|
||||
}
|
||||
|
||||
func (h *Handler) AddFeed(c echo.Context) error {
|
||||
ret := make([]interface{}, 0)
|
||||
return c.JSON(201, ret)
|
||||
}
|
||||
|
||||
func main() {
|
||||
h := Handler{}
|
||||
con := fmt.Sprintf(
|
||||
"postgresql://%v:%v@%v:%v/%v?sslmode=disable",
|
||||
os.Getenv("POSTGRES_USER"),
|
||||
os.Getenv("POSTGRES_PASSWORD"),
|
||||
os.Getenv("POSTGRES_SERVER"),
|
||||
os.Getenv("POSTGRES_PORT"),
|
||||
os.Getenv("POSTGRES_DB"),
|
||||
)
|
||||
db, err := sql.Open("postgres", con)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
h := Handler{
|
||||
database: db,
|
||||
}
|
||||
|
||||
e := echo.New()
|
||||
e.Use(middleware.Logger())
|
||||
e.GET("/entries", h.GetEntries)
|
||||
e.POST("/feed", h.AddFeed)
|
||||
|
||||
e.Start(":1597")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user