-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
103 lines (86 loc) · 1.99 KB
/
app.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"context"
"fmt"
"log"
"github.com/sanda0/webmatic/webmaticlib"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type Response struct {
Status int `json:"status"`
Data interface{} `json:"data"`
}
// App struct
type App struct {
ctx context.Context
DB *gorm.DB
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
var err error
a.DB, err = gorm.Open(sqlite.Open("data.db"), &gorm.Config{})
if err != nil {
log.Fatalf("failed to connect database: %v", err)
}
a.DB.AutoMigrate(&webmaticlib.Project{})
}
// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name)
}
func (a *App) FileUpload(fileName string) int {
return 100
}
func (a *App) SaveMatic(name string, autor string) Response {
fileName, err := webmaticlib.SaveMatic(a.DB, name, autor)
if err != nil {
log.Println(err.Error())
return Response{
Status: 500,
Data: err.Error(),
}
}
return Response{Status: 200, Data: fileName}
}
func (a *App) GetAllMatics() Response {
matics, err := webmaticlib.GetAllMatics(a.DB)
if err != nil {
log.Println(err.Error())
return Response{
Status: 500,
Data: err.Error(),
}
}
return Response{Status: 200, Data: matics}
}
func (a *App) GetMaticById(id uint) Response {
matic, err := webmaticlib.GetMaticById(a.DB, id)
if err != nil {
return Response{
Status: 500,
Data: err.Error(),
}
}
return Response{Status: 200, Data: matic}
}
func (a *App) SaveXML(id uint, data string) Response {
err := webmaticlib.SaveXML(a.DB, id, data)
if err != nil {
return Response{
Status: 500,
Data: err.Error(),
}
}
return Response{Status: 200, Data: "xml saved"}
}
func (a *App) RunMatic(jsonStr string) {
// fmt.Println(jsonStr)
webmaticlib.RunMatic(jsonStr)
}