feat: log and basic logic wait for req ksat

main
ZGGSONG 2 years ago
parent ddb05003df
commit 67add2b5f4

@ -1,8 +1,8 @@
package config
import (
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"log"
"os"
"path"
"path/filepath"
@ -32,11 +32,13 @@ func InitialConfig() Config {
}
type Config struct {
Port string
Port string
Quality string
}
func getConfig() Config {
var config Config
config.Port = viper.GetString("server.port")
config.Quality = viper.GetString("img.quality")
return config
}

@ -1,2 +1,4 @@
server:
port: 7201
img:
quality: 20

@ -4,5 +4,5 @@ import "receive-files/config"
var (
GLO_CONFIG config.Config
GLO_RECV_CHAN chan string
GLO_RECV_CHAN chan []string
)

@ -4,7 +4,9 @@ go 1.18
require (
github.com/fsnotify/fsnotify v1.5.4
github.com/sirupsen/logrus v1.9.0
github.com/spf13/viper v1.13.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)
require (
@ -18,7 +20,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect

@ -36,6 +36,7 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
@ -146,6 +147,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/afero v1.8.2 h1:xehSyVa0YnHWsJ49JFljMpg1HX19V6NDZ1fkm1Xznbo=
github.com/spf13/afero v1.8.2/go.mod h1:CtAatgMJh6bJEIs48Ay/FOnkljP3WeGUG0MC1RfAqwo=
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
@ -302,8 +305,8 @@ golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@ -461,6 +464,8 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

@ -1,7 +1,8 @@
package main
import (
"log"
log "github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
"net/http"
"os"
"receive-files/config"
@ -9,8 +10,27 @@ import (
"receive-files/serve"
)
// TODO: 程序退出 关闭日志
func init() {
// 初始化日志
logger := &lumberjack.Logger{
//Filename: "./Log/Receive_File_Log" + time.Now().Format("20060102_150405") + ".txt",
Filename: "./Log/Receive_File_Log.txt",
MaxSize: 10, // 日志文件大小,单位是 MB
MaxBackups: 3, // 最大过期日志保留个数
MaxAge: 28, // 保留过期文件最大时间,单位 天
Compress: true, // 是否压缩日志默认是不压缩。这里设置为true压缩日志
LocalTime: true, // 是否使用本地时间默认是使用UTC时间
}
log.SetOutput(logger) // logrus 设置日志的输出方式
log.SetFormatter(&log.TextFormatter{
TimestampFormat: "2006-01-02 15:04:05",
})
// 初始化配置文件
global.GLO_CONFIG = config.InitialConfig()
// 检查输出目录
_, err := os.Stat("upload")
if err != nil {
err = os.Mkdir("upload", 0755)
@ -23,17 +43,36 @@ func init() {
}
func main() {
// 监听目录下文件
//go util.ListeningDirectory("upload")
global.GLO_RECV_CHAN = make(chan []string)
mux := http.NewServeMux()
mux.HandleFunc("/", serve.HandlerRoot)
mux.HandleFunc("/upload", serve.HandlerSingleFile)
// 监听目录下文件
//go util.ListeningDirectory("upload")
global.GLO_RECV_CHAN = make(chan string)
// 监听每日申报
go chanHandler()
log.Printf("[INFO] Service Listener Port At %v...\n", global.GLO_CONFIG.Port)
log.Printf("[INFO] Service Listener Port At %v...", global.GLO_CONFIG.Port)
err := http.ListenAndServe(":"+global.GLO_CONFIG.Port, mux)
if err != nil {
log.Fatalln(err)
log.Fatalf("[FATAL] Start Server Error %s", err)
}
}
//
// chanHandler
// @Description: 信号处理
//
func chanHandler() {
for {
select {
case result := <-global.GLO_RECV_CHAN:
for _, v := range result {
serve.CompressImageResource(v)
}
serve.DeclarationService(result)
}
}
}

@ -1,10 +1,20 @@
package serve
import (
"bytes"
"fmt"
log "github.com/sirupsen/logrus"
"image"
_ "image/gif"
"image/jpeg"
_ "image/jpeg"
_ "image/png"
"io"
"log"
"net/http"
"os"
"receive-files/global"
"strconv"
"time"
)
//
@ -36,19 +46,18 @@ Please using POST method to upload files...
https://github.com/zggsong`))
return
}
fn := r.FormValue("name")
// 根据字段名获取表单文件
formFile, _, err := r.FormFile("file")
if err != nil {
log.Printf("[ERROR] (HandlerSingleFile) Get form file failed: %s\n", err)
log.Printf("[ERROR] (HandlerSingleFile) Get form file failed: %s", err)
return
}
defer formFile.Close()
// 创建保存文件
destFile, err := os.Create("./upload/" + fn)
if err != nil {
log.Printf("[ERROR] (HandlerSingleFile) Create Save failed: %s\n", err)
log.Printf("[ERROR] (HandlerSingleFile) Create Save failed: %s", err)
return
}
defer destFile.Close()
@ -56,14 +65,74 @@ https://github.com/zggsong`))
// 读取表单文件,写入保存文件
_, err = io.Copy(destFile, formFile)
if err != nil {
log.Printf("[ERROR] (HandlerSingleFile) Write file failed: %s\n", err)
log.Printf("[ERROR] (HandlerSingleFile) Write file failed: %s", err)
return
}
log.Printf("[INFO] Have Receive File: %v\n", fn) //输出上传的文件名
log.Printf("[INFO] Have Receive File: %v", fn) //输出上传的文件名
str, _ := os.Getwd()
dic := fmt.Sprintf("%v/upload/", str)
if fn == "xcm.jpeg" {
global.GLO_RECV_CHAN <- []string{dic + "jkm.jpeg", dic + "xcm.jpeg"}
}
w.Write([]byte("Successful...")) //这个写入到w的是输出到客户端的
}
//
// DeclarationService
// @Description: 申报服务
// @param files
//
func DeclarationService(files []string) {
time.Sleep(3 * time.Second)
for _, file := range files {
if err := os.Remove(file); err != nil {
log.Printf("[ERROR] 删除失败, %v", err)
continue
}
log.Printf("[INFO] delete file %v", file)
}
}
//
// CompressImageResource
// @Description: 压缩JPEG文件
// @param imagePath
//
func CompressImageResource(imagePath string) {
file, err := os.Open(imagePath)
if err != nil {
log.Printf("[ERROR] (compressImageResource) Open File failed err: %v", err)
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
log.Printf("[ERROR] (compressImageResource) Decode error: %v", err)
}
buf := bytes.Buffer{}
quality, err := strconv.Atoi(global.GLO_CONFIG.Quality)
if err != nil {
log.Printf("[ERROR] (compressImageResource) Quality convert from config error: %v", err)
}
err = jpeg.Encode(&buf, img, &jpeg.Options{Quality: quality})
if err != nil {
log.Printf("[ERROR] (compressImageResource) Encode error: %v", err)
}
//保存到新文件中
newFile, err := os.Create(imagePath)
if err != nil {
log.Printf("[ERROR] (compressImageResource) Create Compress File failed err: %v", err)
}
defer newFile.Close()
_, err = newFile.Write(buf.Bytes())
if err != nil {
log.Printf("[ERROR] (compressImageResource) Write Compress File failed err: %v", err)
} else {
log.Printf("[INFO] (compressImageResource) Compress %v success", imagePath)
}
}
//
// HandlerMultiFiles
// @Description: 多文件上传处理
@ -94,7 +163,7 @@ func HandlerMultiFiles(w http.ResponseWriter, r *http.Request) {
log.Fatal(err)
}
io.Copy(destFile, srcFile)
log.Printf("接收文件: %v\n", files[i].Filename) //输出上传的文件名
log.Printf("接收文件: %v", files[i].Filename) //输出上传的文件名
}
w.Write([]byte("successfully")) //这个写入到w的是输出到客户端的
}

@ -2,7 +2,7 @@ package util
import (
"github.com/fsnotify/fsnotify"
"log"
log "github.com/sirupsen/logrus"
)
//

Loading…
Cancel
Save