forked from wuhan-support/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
208 lines (186 loc) · 6.36 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"log"
"net/http"
"os"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/jinzhu/configor"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/labstack/echo"
shimo_openapi "github.com/wuhan-support/shimo-openapi"
"gopkg.in/go-playground/validator.v9"
)
var (
config Config
Log *log.Logger
db *gorm.DB
)
func (cv *CustomValidator) Validate(i interface{}) error {
return cv.validator.Struct(i)
}
//func SimulateDelay(next echo.HandlerFunc) echo.HandlerFunc {
// return func(c echo.Context) error {
// time.Sleep(time.Millisecond * 500)
// return next(c)
// }
//}
func init() {
err := configor.Load(&config, "config.yml")
if err != nil {
Log.Fatalf("failed to initialize config file: %v", err)
}
//db, err = gorm.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local", config.DBInfo.User, config.DBInfo.Pwd, config.DBInfo.Addr, config.DBInfo.DBName))
//db, err = gorm.Open("sqlite3", "sqlite.db")
//if err != nil {
// fmt.Println(fmt.Sprintf("%s:%s@%s/%s?charset=utf8&parseTime=True&loc=Local", config.DBInfo.User, config.DBInfo.Pwd, config.DBInfo.Addr, config.DBInfo.DBName))
// panic("failed to connect mysql:" + err.Error())
//}
//
//if !db.HasTable(Submission{}) {
// d := db.CreateTable(Submission{})
// if d.Error != nil {
// panic("create table failed:" + d.Error.Error())
// }
//}
//_, err = os.Stat(config.UploadPath)
//if os.IsNotExist(err) {
// err = os.MkdirAll(config.UploadPath, os.ModePerm)
// if err != nil {
// panic("failed create upload path:" + err.Error())
// }
//}
}
func main() {
logFile, err := os.OpenFile("runtime.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
if err != nil {
panic(err)
}
Log = log.New(logFile, "[http] ", log.LstdFlags)
e := echo.New()
e.Debug = false
e.Validator = &CustomValidator{validator: validator.New()}
//e.Use(SimulateDelay)
shimoC := shimo_openapi.NewClient(config.Shimoauth.ClientId, config.Shimoauth.ClientSecret, config.Shimoauth.Username, config.Shimoauth.Password, config.Shimoauth.Scope)
Log.Println(config, shimoC)
api := e.Group("/api")
// 返回住宿信息列表
api.GET("/accommodations", func(c echo.Context) error {
fileId := "6c6GKvX83hRCVdG8"
opt := shimo_openapi.Opts{"工作表1", 278, "R", "(", time.Minute * 5}
message, err := shimoC.GetFileWithOpts(fileId, opt)
if err != nil {
Log.Printf("failed to get document: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get document")
}
return c.JSONBlob(http.StatusOK, message)
})
// 返回心理咨询机构列表
api.GET("/platforms/psychological", func(c echo.Context) error {
fileId := "Dpy6Q668cj3Xx8Rq"
opt := shimo_openapi.Opts{"工作表1", 17, "O", "\n", time.Minute * 5}
message, err := shimoC.GetFileWithOpts(fileId, opt)
if err != nil {
Log.Printf("failed to get document: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get document")
}
return c.JSONBlob(http.StatusOK, message)
})
// 返回线上医疗平台列表
api.GET("/platforms/medical", func(c echo.Context) error {
fileId := "kDQJ6vWgWWwq8r8H"
opt := shimo_openapi.Opts{"工作表1", 23, "O", " (", time.Minute * 5}
message, err := shimoC.GetFileWithOpts(fileId, opt)
if err != nil {
Log.Printf("failed to get document: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get document")
}
return c.JSONBlob(http.StatusOK, message)
})
// 返回医院需求列表
api.GET("/hospital/supplies", func(c echo.Context) error {
fileId := "zN32MwmPjmCLF0Av"
opt := shimo_openapi.Opts{"已合成", 160, "AP", " ", time.Minute * 5}
message, err := shimoC.GetFileWithOpts(fileId, opt)
if err != nil {
Log.Printf("failed to get document: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get document")
}
return c.JSONBlob(http.StatusOK, message)
})
//api.GET("/hospital/supplies/submissions", func(c echo.Context) error {
// var request GetSubmissionsRequest
// var submissions []Submission
//
// if c.Bind(&request) != nil && c.Validate(&request) != nil {
// fmt.Println(c.Validate(&request))
// return echo.NewHTTPError(http.StatusBadRequest, "bad request")
// }
//
// paginator := pagination.Paging(&pagination.Param{
// DB: db,
// Page: request.Page,
// Limit: request.Limit,
// OrderBy: []string{"id desc"},
// }, &submissions)
//
// return c.JSON(http.StatusOK, paginator)
//})
//
//api.POST("/hospital/supplies/submissions", func(c echo.Context) error {
// var request Submission
// if c.Bind(&request) != nil && c.Validate(&request) != nil {
// // fmt.Println(c.Bind(request))
// fmt.Println(c.Validate(&request))
// return echo.NewHTTPError(http.StatusBadRequest, "bad reqeust")
// }
//
// d := db.Create(&request)
// if d.Error != nil {
// Log.Printf("create collect_form failed:%v", d.Error)
// return echo.NewHTTPError(http.StatusInternalServerError)
// }
// return c.NoContent(http.StatusNoContent)
//})
api.POST("/report", func(c echo.Context) error {
var request ReportRequest
if c.Bind(&request) != nil && c.Validate(&request) != nil {
return echo.NewHTTPError(http.StatusBadRequest, "bad request")
}
Log.Printf("[report] new report record: %v", spew.Sdump(request))
return c.NoContent(http.StatusNoContent)
})
//api.POST("/upload", func(c echo.Context) error {
// fn, err := c.FormFile("file")
// if err != nil {
// return echo.NewHTTPError(http.StatusBadRequest, "file name not found")
// }
// fs, err := fn.Open()
// if err != nil {
// Log.Printf("open upload file %s failed:%v", fn.Filename, err)
// return echo.NewHTTPError(http.StatusInternalServerError)
// }
//
// suf := ""
// index := strings.LastIndex(fn.Filename, ".")
// if index != -1 {
// suf = fn.Filename[index:]
// }
//
// fname := tool.GetGUID() + suf
// fd, err := os.Create(fmt.Sprintf("%s/%s", config.UploadPath, fname))
// if err != nil {
// Log.Printf("create file %s failed:%v", fname, err)
// return echo.NewHTTPError(http.StatusInternalServerError)
// }
// if _, err = io.Copy(fd, fs); err != nil {
// Log.Printf("copy file failed:%v", err)
// return echo.NewHTTPError(http.StatusInternalServerError)
// }
//
// return c.JSON(http.StatusOK, map[string]string{"fcode": fname})
//})
Log.Fatal(e.Start(config.Server.Address))
}