44 "database/sql"
55 "fmt"
66 "github.com/labstack/echo/v4"
7+ "github.com/labstack/echo/v4/middleware"
78 "net/http"
89 "os"
910
@@ -20,29 +21,53 @@ const (
2021
2122func main () {
2223 e := echo .New ()
24+ e .Use (middleware .Logger ())
25+ e .Use (middleware .Recover ())
26+ e .Use (middleware .CORSWithConfig (middleware.CORSConfig {
27+ AllowOrigins : []string {"http://localhost:3003" },
28+ AllowMethods : []string {"GET" , "POST" , "PUT" , "DELETE" , "OPTIONS" },
29+ }))
2330
24- psqlInfo := fmt .Sprintf ("host=%s port=%d user=%s " +
31+ e .OPTIONS ("/*" , func (c echo.Context ) error {
32+ return c .NoContent (http .StatusOK )
33+ })
34+
35+ e .GET ("/" , func (c echo.Context ) error {
36+ psqlInfo := fmt .Sprintf ("host=%s port=%d user=%s " +
2537 "password=%s dbname=%s sslmode=disable" ,
2638 host , port , user , password , dbname )
2739
28- db , err := sql .Open ("postgres" , psqlInfo )
29- if err != nil {
30- panic (err )
31- }
40+ db , err := sql .Open ("postgres" , psqlInfo )
3241
33- defer func () {
34- if cerr := db .Close (); cerr != nil {
35- fmt .Printf ("error closing DB: %v\n " , cerr )
42+ if err != nil {
43+ panic (err )
3644 }
37- }()
3845
39- e .GET ("/" , func (c echo.Context ) error {
40- return c .String (http .StatusOK , "ok" )
46+ rows , err := db .Query ("SELECT id, name FROM users" )
47+
48+ if err != nil {
49+ panic (err )
50+ }
51+
52+ defer func () {
53+ if cerr := rows .Close (); cerr != nil {
54+ fmt .Printf ("error closing DB: %v\n " , cerr )
55+ }
56+ }()
57+
58+ defer func () {
59+ if cerr := db .Close (); cerr != nil {
60+ fmt .Printf ("error closing DB: %v\n " , cerr )
61+ }
62+ }()
63+
64+ return c .JSON (http .StatusOK , rows )
4165 })
4266
4367 e .GET ("/manifest" , func (c echo.Context ) error {
4468 return c .String (http .StatusOK , "ok" )
4569 })
4670
71+
4772 e .Logger .Fatal (e .Start (":" + os .Getenv ("HTTP_PORT" )))
4873}
0 commit comments