Repository navigation
Expand file tree
/
Copy pathsettings.go
More file actions
60 lines (53 loc) · 1.42 KB
/
Copy pathsettings.go
File metadata and controls
60 lines (53 loc) · 1.42 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
package api
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/hasura/graphql-engine/cli/v2/migrate"
)
type SettingRequest struct {
Name string `json:"name"`
Value string `json:"value"`
}
func SettingsAPI(c *gin.Context) {
// Get migrate instance
migratePtr, ok := c.Get("migrate")
if !ok {
return
}
t := migratePtr.(*migrate.Migrate)
// Switch on request method
switch c.Request.Method {
case "GET":
name := "migration_mode"
setting, err := t.GetSetting(name)
if err != nil {
if strings.HasPrefix(err.Error(), DataAPIError) {
c.JSON(500, &Response{Code: "data_api_error", Message: err.Error()})
return
}
c.JSON(500, &Response{Code: "internal_error", Message: err.Error()})
return
}
c.JSON(200, &gin.H{name: setting})
case "PUT":
var request SettingRequest
// Bind Request body to Request struct
if c.BindJSON(&request) != nil {
c.JSON(500, &Response{Code: "internal_error", Message: "Something went wrong"})
return
}
err := t.UpdateSetting(request.Name, request.Value)
if err != nil {
if strings.HasPrefix(err.Error(), DataAPIError) {
c.JSON(500, &Response{Code: "data_api_error", Message: err.Error()})
return
}
c.JSON(500, &Response{Code: "internal_error", Message: err.Error()})
return
}
c.JSON(200, &Response{Message: "Successfuly set"})
default:
c.JSON(http.StatusMethodNotAllowed, &gin.H{"message": "Method not allowed"})
}
}