-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
64 lines (51 loc) · 1.46 KB
/
server.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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"time"
"github.com/PrachpaveenY/assessment/database"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func main() {
fmt.Println("Welcome to Server")
database.InitDB()
e := echo.New()
e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
if username == "postgres" || password == "2565" {
return true, nil
}
return false, nil
}))
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.POST("/expenses", database.CreateExpensesAllHandler)
e.GET("/expenses", database.GetExpensesHandler)
e.GET("/expenses/:id", database.GetExpensesIDHandler)
e.PUT("/expenses/:id", database.UpdateAllExpensesHandler)
e.PATCH("/expenses/:id", database.UpdateExpensesHandler)
e.DELETE("/expenses/:id", database.DeleteExpensesHandler)
// serverPort := os.Getenv("PORT")
// e.Logger.Fatal(e.Start(serverPort))
log.Fatal(e.Start(":2565"))
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt)
<-shutdown
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
defer cancel()
if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err)
}
}
// ====================
// use Thunder Client (PostgresSQL15)
// PORT = 2565
// localhost:2565/expenses
// localhost:2565/expenses/:id
// ====================
// fmt.Println("Please use server.go for main file")
// fmt.Println("start at port:", os.Getenv("PORT"))
//