-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
84 lines (73 loc) · 2.54 KB
/
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
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
package main
import (
"github.com/labstack/echo/v4"
"github.com/swaggo/echo-swagger"
"github.com/tgunsch/httpod/internal/cookies"
"github.com/tgunsch/httpod/internal/docs"
"github.com/tgunsch/httpod/internal/http"
"github.com/tgunsch/httpod/internal/status"
"github.com/tgunsch/httpod/internal/util"
"os"
"strings"
)
// @title httPod
// @version 0.0.1
// @description A simple HTTP Request & HTTPResponse Service, shamelessly stolen from httpbin.org.
// @tag.name HTTP Methods
// @tag.description Testing different HTTP methods
// @tag.name Status codes
// @tag.description Generates responses with given status code
// @tag.name Cookies
// @tag.description Creates, reads and deletes Cookies
func main() {
const (
SWAGGER_PATH = "/swagger"
API_PATH = "/api"
BASE_PATH_ENV = "BASE_PATH"
PORT = "PORT"
)
server := echo.New()
port := os.Getenv(PORT)
if port == "" {
port = "8080"
}
basePath := os.Getenv(BASE_PATH_ENV)
if basePath != "" {
basePath = "/" + basePath
}
endpoints := server.Group(basePath)
// swagger ui will be available on /basePath/swagger/index.html
// api will be available on /basePath/api
// swagger info will use X-Forwarded headers if available;
// e.g.: X-Forwarded-Host=my.domain.com X-Forwarded-Prefix=myPrefix swagger ui show api on url http://my.domain.com/myPrefix/basePath/api
apiPath := basePath + API_PATH
endpoints.GET(SWAGGER_PATH+"/*", echoSwagger.WrapHandler, swaggerMiddleware(apiPath))
api := endpoints.Group(API_PATH)
api.GET("/get", http.GetHandler)
api.DELETE("/delete", http.DeleteHandler)
api.PATCH("/patch", http.PatchHandler)
api.POST("/post", http.PostHandler)
api.PUT("/put", http.PutHandler)
api.DELETE("/status/:code", status.DeleteHandler)
api.GET("/status/:code", status.GetHandler)
api.PATCH("/status/:code", status.PatchHandler)
api.POST("/status/:code", status.PostHandler)
api.PUT("/status/:code", status.PutHandler)
api.GET("/cookies", cookies.GetHandler)
api.POST("/cookies/:cookieName", cookies.PostHandler)
api.DELETE("/cookies/:cookieName", cookies.DeleteHandler)
server.Logger.Fatal(server.Start(":" + port))
}
func swaggerMiddleware(path string) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if strings.HasSuffix(c.Request().URL.Path, "index.html") {
scheme, host := util.GetSchemeHost(c.Request())
docs.SwaggerInfo.Host = host
docs.SwaggerInfo.Schemes = []string{scheme}
docs.SwaggerInfo.BasePath = util.GetPath(path, c.Request())
}
return next(c)
}
}
}