-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
91 lines (80 loc) · 1.96 KB
/
main.go
File metadata and controls
91 lines (80 loc) · 1.96 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"fmt"
"io"
"net/http"
"os"
"time"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/", func(c echo.Context) error {
ctx := c.Request().Context()
select {
case <-ctx.Done():
fmt.Println("Request timeout")
return c.JSON(http.StatusRequestTimeout, map[string]string{
"message": "Request timeout",
})
case <-time.After(2 * time.Second):
fmt.Println("After 2 secs")
return c.JSON(http.StatusOK, map[string]string{
"message": "After 2 secs",
})
}
})
e.POST("/echo", func(c echo.Context) error {
file, err := c.FormFile("file")
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{
"message": "Invalid request",
})
}
src, err := file.Open()
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Internal server error",
})
}
defer src.Close()
img, err := os.Create("./" + file.Filename)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Internal server error",
})
}
defer img.Close()
// Write the file
data, err := io.Copy(img, src)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Internal server error",
})
}
fmt.Println(data)
return c.JSON(http.StatusOK, file)
})
e.GET("/wait", func(c echo.Context) error {
ctx := c.Request().Context()
select {
case <-ctx.Done():
fmt.Println("Request timeout")
return c.JSON(http.StatusRequestTimeout, map[string]string{
"message": "Request timeout",
})
case <-time.After(5 * time.Second):
fmt.Println("After 5 secs")
return c.JSON(http.StatusOK, map[string]string{
"message": "After 5 secs",
})
}
})
e.GET("/temp", func(c echo.Context) error {
return c.File("./user.json")
})
e.Logger.Fatal(e.Start(":3001"))
}