goctl added
This commit is contained in:
10
tools/goctl/update/config/config.go
Normal file
10
tools/goctl/update/config/config.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package config
|
||||
|
||||
import "zero/core/logx"
|
||||
|
||||
type Config struct {
|
||||
logx.LogConf
|
||||
ListenOn string
|
||||
FileDir string
|
||||
FilePath string
|
||||
}
|
||||
6
tools/goctl/update/etc/update-api.json
Normal file
6
tools/goctl/update/etc/update-api.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"Name": "update-api",
|
||||
"ListenOn": "localhost:7777",
|
||||
"FileDir": ".",
|
||||
"FilePath": "/"
|
||||
}
|
||||
60
tools/goctl/update/update.go
Normal file
60
tools/goctl/update/update.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"path"
|
||||
|
||||
"zero/core/conf"
|
||||
"zero/core/hash"
|
||||
"zero/core/lang"
|
||||
"zero/core/logx"
|
||||
"zero/tools/goctl/update/config"
|
||||
"zero/tools/goctl/util"
|
||||
)
|
||||
|
||||
const (
|
||||
contentMd5Header = "Content-Md5"
|
||||
filename = "goctl"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/update-api.json", "the config file")
|
||||
|
||||
func forChksumHandler(file string, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if !util.FileExists(file) {
|
||||
logx.Errorf("file %q not exist", file)
|
||||
http.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
|
||||
content, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
chksum := hash.Md5Hex(content)
|
||||
if chksum == r.Header.Get(contentMd5Header) {
|
||||
w.WriteHeader(http.StatusNotModified)
|
||||
return
|
||||
} else {
|
||||
w.Header().Set(contentMd5Header, chksum)
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
|
||||
fs := http.FileServer(http.Dir(c.FileDir))
|
||||
http.Handle(c.FilePath, http.StripPrefix(c.FilePath, forChksumHandler(path.Join(c.FileDir, filename), fs)))
|
||||
lang.Must(http.ListenAndServe(c.ListenOn, nil))
|
||||
}
|
||||
Reference in New Issue
Block a user