-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (61 loc) · 1.68 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
85
86
package main
// Hannes de Waal 2019
import (
"fmt"
"hannesdw/sap/saprest/sap"
"log"
"net/http"
"encoding/json"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
var rfc *sap.Rfc
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
sap, err := sap.New()
if err != nil {
log.Fatal(err)
}
server := echo.New()
server.Use(
middleware.Recover(), // Recover from all panics to always have your server up
middleware.Logger(), // Log everything to stdout
middleware.RequestID(), // Generate a request id on the HTTP response headers for identification
)
server.Debug = true
//server.Debug = false
server.HideBanner = true
server.HTTPErrorHandler = func(err error, c echo.Context) {
// pass on error if required
log.Println(c.Path(), c.QueryParams(), err.Error())
server.DefaultHTTPErrorHandler(err, c)
//return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("%s-%s-%s", c.Path(), c.QueryParams(), err.Error()))
}
server.GET("/health", healthHandler)
server.POST("/rfc/:function", sapRfcHandler)
log.Fatal(server.Start(":8088"))
}
//healthHandler
func healthHandler(c echo.Context) error {
//"STFC_STRUCTURE"
err := rfc.Conn.Ping()
if err != nil {
return echo.NewHTTPError(401, err)
}
return c.JSON(http.StatusOK, "")
}
//sapRfcHandler
func sapRfcHandler(c echo.Context) error {
function := c.Param("function")
params := make(map[string]interface{})
log.Print(function)
err := json.NewDecoder(c.Request().Body).Decode(¶ms)
if err != nil {
return echo.NewHTTPError(401, err)
}
r, err := rfc.Call(function, params)
if err != nil {
return echo.NewHTTPError(401, err)
}
return c.JSON(http.StatusOK, r)
}