Add Inbound and Outbound api for RACS
This commit is contained in:
parent
57d949724c
commit
741adccc6b
105
APIServer.go
105
APIServer.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"APIServer/cnf"
|
"APIServer/cnf"
|
||||||
"APIServer/ews"
|
"APIServer/ews"
|
||||||
|
"APIServer/racs"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -186,6 +187,8 @@ func startSvc() {
|
|||||||
http.HandleFunc("/fileinfo/", handleFileInfo)
|
http.HandleFunc("/fileinfo/", handleFileInfo)
|
||||||
http.HandleFunc("/setuutinfo/", handleSetUutInfo)
|
http.HandleFunc("/setuutinfo/", handleSetUutInfo)
|
||||||
http.HandleFunc("/getuutinfo/", handleGetUutInfo)
|
http.HandleFunc("/getuutinfo/", handleGetUutInfo)
|
||||||
|
http.HandleFunc("/racs/inbound/", handleRacsInbound)
|
||||||
|
http.HandleFunc("/racs/outbound/", handleRacsOutbound)
|
||||||
logger.Printf("[MSG] Starting HTTP Server On Port: %s\r\n", cfg.Settings.ListenPort)
|
logger.Printf("[MSG] Starting HTTP Server On Port: %s\r\n", cfg.Settings.ListenPort)
|
||||||
err := http.ListenAndServe(":"+cfg.Settings.ListenPort, nil)
|
err := http.ListenAndServe(":"+cfg.Settings.ListenPort, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -346,6 +349,19 @@ func handleFileInfo(w http.ResponseWriter, r *http.Request) {
|
|||||||
fmt.Fprintf(w, "%s", string(jsonObj))
|
fmt.Fprintf(w, "%s", string(jsonObj))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func fileMD5(filePath string) string {
|
||||||
|
file, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
hash := md5.New()
|
||||||
|
_, err = io.Copy(hash, file)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return hex.EncodeToString(hash.Sum(nil))
|
||||||
|
}
|
||||||
|
|
||||||
func handleSetUutInfo(w http.ResponseWriter, r *http.Request) {
|
func handleSetUutInfo(w http.ResponseWriter, r *http.Request) {
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
if r.RequestURI == "/favicon.ico" {
|
if r.RequestURI == "/favicon.ico" {
|
||||||
@ -459,6 +475,82 @@ func handleGetUutInfo(w http.ResponseWriter, r *http.Request) {
|
|||||||
fmt.Fprintf(w, "%v", rst["ErrMsg"])
|
fmt.Fprintf(w, "%v", rst["ErrMsg"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleRacsInbound(w http.ResponseWriter, r *http.Request) {
|
||||||
|
defer r.Body.Close()
|
||||||
|
if r.RequestURI == "/favicon.ico" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
addr := strings.Split(r.RemoteAddr, ":")[0]
|
||||||
|
uri := r.RequestURI
|
||||||
|
rst := map[string]string{"Result": "OK", "ErrMsg": ""}
|
||||||
|
|
||||||
|
params, err := parseReqParams(r)
|
||||||
|
if err != nil {
|
||||||
|
logger.Printf("[ERR] %s; %s; %#v; %s\r\n", r.RemoteAddr, r.RequestURI, params, rst["ErrMsg"])
|
||||||
|
rst["Result"] = "NG"
|
||||||
|
rst["ErrMsg"] = "racs.HandleInbound: " + err.Error()
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
fmt.Fprintf(w, "%v", strings.ReplaceAll(fmt.Sprintf("%#v", rst), "map[string]string", ""))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Settings.LogRequest {
|
||||||
|
logger.Printf("[MSG] %s; %s; Request: %#v\r\n", addr, uri, params)
|
||||||
|
}
|
||||||
|
racs := new(racs.RACS)
|
||||||
|
rst = racs.Inbound(cfg, logger, addr, uri, params)
|
||||||
|
if cfg.Settings.LogResponse {
|
||||||
|
logger.Printf("[MSG] %s; %s; Response: %#v\r\n", addr, uri, rst)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rst["Result"] != "OK" {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, "%v", strings.ReplaceAll(fmt.Sprintf("%#v", rst), "map[string]string", ""))
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleRacsOutbound(w http.ResponseWriter, r *http.Request) {
|
||||||
|
defer r.Body.Close()
|
||||||
|
if r.RequestURI == "/favicon.ico" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
addr := strings.Split(r.RemoteAddr, ":")[0]
|
||||||
|
uri := r.RequestURI
|
||||||
|
rst := map[string]string{"Result": "OK", "ErrMsg": ""}
|
||||||
|
|
||||||
|
params, err := parseReqParams(r)
|
||||||
|
if err != nil {
|
||||||
|
logger.Printf("[ERR] %s; %s; %#v; %s\r\n", r.RemoteAddr, r.RequestURI, params, rst["ErrMsg"])
|
||||||
|
rst["Result"] = "NG"
|
||||||
|
rst["ErrMsg"] = "racs.HandleOutbound: " + err.Error()
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
fmt.Fprintf(w, "%v", strings.ReplaceAll(fmt.Sprintf("%#v", rst), "map[string]string", ""))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Settings.LogRequest {
|
||||||
|
logger.Printf("[MSG] %s; %s; Request: %#v\r\n", addr, uri, params)
|
||||||
|
}
|
||||||
|
racs := new(racs.RACS)
|
||||||
|
rst = racs.Outbound(cfg, logger, addr, uri, params)
|
||||||
|
if cfg.Settings.LogResponse {
|
||||||
|
logger.Printf("[MSG] %s; %s; Response: %#v\r\n", addr, uri, rst)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rst["Result"] != "OK" {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, "%v", strings.ReplaceAll(fmt.Sprintf("%#v", rst), "map[string]string", ""))
|
||||||
|
}
|
||||||
|
|
||||||
func parseReqParams(r *http.Request) (map[string]string, error) {
|
func parseReqParams(r *http.Request) (map[string]string, error) {
|
||||||
params := make(map[string]string, 0)
|
params := make(map[string]string, 0)
|
||||||
if err := r.ParseForm(); err != nil {
|
if err := r.ParseForm(); err != nil {
|
||||||
@ -473,19 +565,6 @@ func parseReqParams(r *http.Request) (map[string]string, error) {
|
|||||||
return params, nil
|
return params, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func fileMD5(filePath string) string {
|
|
||||||
file, err := os.Open(filePath)
|
|
||||||
if err != nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
hash := md5.New()
|
|
||||||
_, err = io.Copy(hash, file)
|
|
||||||
if err != nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return hex.EncodeToString(hash.Sum(nil))
|
|
||||||
}
|
|
||||||
|
|
||||||
func chkParams(arg string) bool {
|
func chkParams(arg string) bool {
|
||||||
if strings.Contains(arg, ";") ||
|
if strings.Contains(arg, ";") ||
|
||||||
strings.Contains(arg, "|") ||
|
strings.Contains(arg, "|") ||
|
||||||
|
@ -14,6 +14,7 @@ MySQL:
|
|||||||
User: apisvc
|
User: apisvc
|
||||||
Password: wcqte
|
Password: wcqte
|
||||||
IssuesTable: issues
|
IssuesTable: issues
|
||||||
|
StorageTable: racs
|
||||||
IssuesView: v_issues
|
IssuesView: v_issues
|
||||||
OfflineView: v_offline
|
OfflineView: v_offline
|
||||||
Remark:
|
Remark:
|
||||||
|
17
cnf/cnf.go
17
cnf/cnf.go
@ -16,12 +16,13 @@ type Settings struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type MySQL struct {
|
type MySQL struct {
|
||||||
Server string `yaml:"Server"`
|
Server string `yaml:"Server"`
|
||||||
Port string `yaml:"Port"`
|
Port string `yaml:"Port"`
|
||||||
Database string `yaml:"Database"`
|
Database string `yaml:"Database"`
|
||||||
User string `yaml:"User"`
|
User string `yaml:"User"`
|
||||||
Password string `yaml:"Password"`
|
Password string `yaml:"Password"`
|
||||||
IssuesTable string `yaml:"IssuesTable"`
|
IssuesTable string `yaml:"IssuesTable"`
|
||||||
IssuesView string `yaml:"IssuesView"`
|
StorageTable string `yaml:"StorageTable"`
|
||||||
OfflineView string `yaml:"OfflineView"`
|
IssuesView string `yaml:"IssuesView"`
|
||||||
|
OfflineView string `yaml:"OfflineView"`
|
||||||
}
|
}
|
||||||
|
90
racs/racs.go
Normal file
90
racs/racs.go
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
package racs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"APIServer/cnf"
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RACS struct{}
|
||||||
|
type StorageInfo struct {
|
||||||
|
USN string
|
||||||
|
Storage string
|
||||||
|
UpdateTime string
|
||||||
|
Mark1 string
|
||||||
|
Mark2 string
|
||||||
|
Mark3 string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RACS) Inbound(cfg cnf.Cfg, logger *log.Logger, addr string, uri string, params map[string]string) map[string]string {
|
||||||
|
rst := map[string]string{"Result": "OK", "ErrMsg": ""}
|
||||||
|
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s",
|
||||||
|
cfg.MySQL.User,
|
||||||
|
cfg.MySQL.Password,
|
||||||
|
cfg.MySQL.Server,
|
||||||
|
cfg.MySQL.Port,
|
||||||
|
cfg.MySQL.Database,
|
||||||
|
)
|
||||||
|
dbo, err := sql.Open("mysql", dsn)
|
||||||
|
if err != nil {
|
||||||
|
logger.Printf("[ERR] %s; %s; %#v; %s\r\n", addr, uri, params, err.Error())
|
||||||
|
rst["Result"] = "NG"
|
||||||
|
rst["ErrMsg"] = "racs.Inbound: " + err.Error()
|
||||||
|
return rst
|
||||||
|
}
|
||||||
|
dmlInbound := fmt.Sprintf(`INSERT INTO %s (usn,storage,update_time,mark1,mark2,mark3) VALUES ('%s','%s',%s,'%s','%s','%s');`,
|
||||||
|
cfg.MySQL.StorageTable,
|
||||||
|
params["USN"],
|
||||||
|
"Inbound",
|
||||||
|
"NOW()",
|
||||||
|
params["MARK1"],
|
||||||
|
params["MARK2"],
|
||||||
|
params["MARK3"],
|
||||||
|
)
|
||||||
|
_, err = dbo.Exec(dmlInbound)
|
||||||
|
if err != nil {
|
||||||
|
logger.Printf("[ERR] %s; %s; %#v; %s\r\n", addr, uri, params, err.Error())
|
||||||
|
rst["Result"] = "NG"
|
||||||
|
rst["ErrMsg"] = "racs.Inbound: " + err.Error()
|
||||||
|
return rst
|
||||||
|
}
|
||||||
|
return rst
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RACS) Outbound(cfg cnf.Cfg, logger *log.Logger, addr string, uri string, params map[string]string) map[string]string {
|
||||||
|
rst := map[string]string{"Result": "OK", "ErrMsg": ""}
|
||||||
|
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s",
|
||||||
|
cfg.MySQL.User,
|
||||||
|
cfg.MySQL.Password,
|
||||||
|
cfg.MySQL.Server,
|
||||||
|
cfg.MySQL.Port,
|
||||||
|
cfg.MySQL.Database,
|
||||||
|
)
|
||||||
|
dbo, err := sql.Open("mysql", dsn)
|
||||||
|
if err != nil {
|
||||||
|
logger.Printf("[ERR] %s; %s; %#v; %s\r\n", addr, uri, params, err.Error())
|
||||||
|
rst["Result"] = "NG"
|
||||||
|
rst["ErrMsg"] = "racs.Outbound: " + err.Error()
|
||||||
|
return rst
|
||||||
|
}
|
||||||
|
dmlOutbound := fmt.Sprintf(`INSERT INTO %s (usn,storage,update_time,mark1,mark2,mark3) VALUES ('%s','%s',%s,'%s','%s','%s');`,
|
||||||
|
cfg.MySQL.StorageTable,
|
||||||
|
params["USN"],
|
||||||
|
"Outbound",
|
||||||
|
"NOW()",
|
||||||
|
params["MARK1"],
|
||||||
|
params["MARK2"],
|
||||||
|
params["MARK3"],
|
||||||
|
)
|
||||||
|
_, err = dbo.Exec(dmlOutbound)
|
||||||
|
if err != nil {
|
||||||
|
logger.Printf("[ERR] %s; %s; %#v; %s\r\n", addr, uri, params, err.Error())
|
||||||
|
rst["Result"] = "NG"
|
||||||
|
rst["ErrMsg"] = "racs.Outbound: " + err.Error()
|
||||||
|
return rst
|
||||||
|
}
|
||||||
|
return rst
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user