Skip to content

Commit

Permalink
upload download server
Browse files Browse the repository at this point in the history
  • Loading branch information
phachon committed Jan 22, 2018
1 parent b513221 commit bb9aa03
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
10 changes: 8 additions & 2 deletions config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[listen]
# 监听的 server
server="127.0.0.1:8087"
# 监听上传 server
upload="127.0.0.1:8087"
# 监听下载 server
download="127.0.0.1:8088"

[upload]
# 表单提交字段
Expand All @@ -18,6 +20,10 @@ dirname_len = 4
# 缩略图尺寸 width_height
thumbnails = ["200_200", "300_300", "200_400"]

[download]
# 下载的地址 协议://域名:端口
uri = "http://test.com:8088"

# appname 用于授权,可多个
[appname]
[appname.test]
Expand Down
3 changes: 2 additions & 1 deletion example/php/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
// 设置 header 头
$headers = ['Appname: test', 'Token: '.$token,];
// 文件绝对路径
$file = __DIR__.'/image/test.jpg';
$file = realpath(__DIR__.'/../image/test.jpg');

echo $file;
// 发送数据
$data = array(
'upload' => new CURLFile(realpath($file))
Expand Down
7 changes: 5 additions & 2 deletions http_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func (handle *HttpHandle) ImageUpload(w http.ResponseWriter, req *http.Request,
dirNameLen := conf.GetInt("upload.dirname_len")
maxSize := conf.GetInt("upload.max_size")
thumbnails := conf.GetStringSlice("upload.thumbnails")
server := conf.GetString("listen.server")
imageUrl := "http://"+server+"/image/"
downloadUri := conf.GetString("download.uri")
imageUrl := downloadUri+"/image/"

req.ParseMultipartForm(4*1024)
file, fileHeader, err := req.FormFile(formField)
Expand Down Expand Up @@ -197,6 +197,9 @@ func (handle *HttpHandle) ImageUpload(w http.ResponseWriter, req *http.Request,
data["image_"+thumbnail] = imageUrl+ randString + "_" + thumbnail + ext
}

appname := req.Header.Get("Appname")
log.Println("app ["+appname+"] upload image "+randString +ext+" success")

handle.jsonSuccess(w, "", data)
}

Expand Down
34 changes: 29 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"log"
"github.com/julienschmidt/httprouter"
"os"
)

// go 实现的图片上传,存储,缩放,下载服务
Expand All @@ -14,18 +15,41 @@ func init() {

func main() {

server := conf.GetString("listen.server")
go downloadServer()
uploadServer()
}

// 上传 server
func uploadServer() {

uploadServer := conf.GetString("listen.upload")
router := httprouter.New()
httpHandle := NewHttpHandle()
router.GET("/", httpHandle.Index)
router.POST("/image/upload", httpHandle.ImageUpload)
router.GET("/image/:name", httpHandle.ImageFind)
//跨域
router.OPTIONS("/image/upload", httpHandle.CrossDomain)

log.Println("start server: " + server)
err := http.ListenAndServe(server, router)
log.Println("upload server start listen: " + uploadServer)
err := http.ListenAndServe(uploadServer, router)
if err != nil {
log.Println("upload server listen faild: " +err.Error())
os.Exit(0)
}
}

// 下载 server
func downloadServer() {

downloadServer := conf.GetString("listen.download")
router := httprouter.New()
httpHandle := NewHttpHandle()
router.GET("/image/:name", httpHandle.ImageFind)

log.Println("download server start listen: " + downloadServer)
err := http.ListenAndServe(downloadServer, router)
if err != nil {
log.Println(err.Error())
log.Println("download server listen faild:" + err.Error())
os.Exit(0)
}
}

0 comments on commit bb9aa03

Please sign in to comment.