From 5f5d0541e17f4748215764a35c0f8b41ef1ffec8 Mon Sep 17 00:00:00 2001 From: xuu Date: Mon, 22 May 2017 11:13:47 -0600 Subject: [PATCH] go fmt --- Makefile | 6 ++- config.go | 14 +++---- main.go | 6 +-- routes/paste.go | 105 ++++++++++++++++++++++++++++-------------------- 4 files changed, 77 insertions(+), 54 deletions(-) diff --git a/Makefile b/Makefile index e12673e..b8e4b47 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,10 @@ all: $(BINARY) clean: rm -f $(BINARY) $(ROUTE_ASSET) $(ROUTE_FILES) +fmt: $(SOURCE) $(SCHEMA_ASSET) $(ROUTE_ASSET) + find . -type f -name "*.go" -printf "%h\n"|sort -u|xargs go fmt + go tool vet -composites=false . + $(BINARY): $(SOURCE) $(ROUTE_ASSET) go build @@ -35,5 +39,5 @@ public/style.css: assets/*.css deploy: cd debian && make && make deploy -.PHONEY: clean deploy +.PHONEY: clean deploy fmt diff --git a/config.go b/config.go index 70eb294..6ec4193 100644 --- a/config.go +++ b/config.go @@ -1,17 +1,17 @@ package main import ( - "github.com/spf13/viper" - "github.com/docopt/docopt.go" - "sour.is/x/log" - "sour.is/x/httpsrv" "bytes" "fmt" + "github.com/docopt/docopt.go" + "github.com/spf13/viper" + "sour.is/x/httpsrv" + "sour.is/x/log" ) var ( APP_VERSION string - APP_BUILD string + APP_BUILD string ) var APP_NAME string = "Paste API" var APP_USAGE string = `Paste API @@ -51,7 +51,7 @@ func init() { } viper.SetConfigName("config") - if args["--config"] != nil{ + if args["--config"] != nil { viper.AddConfigPath(args["--config"].(string)) } @@ -109,4 +109,4 @@ listen = ":9010" [module.paste] random = "4096" store = "data/" -`) \ No newline at end of file +`) diff --git a/main.go b/main.go index fdb948d..8110469 100644 --- a/main.go +++ b/main.go @@ -27,9 +27,9 @@ package main import ( -"sour.is/x/httpsrv" -//_ "sour.is/x/httpsrv/routes" -_ "sour.is/x/paste/routes" + "sour.is/x/httpsrv" + //_ "sour.is/x/httpsrv/routes" + _ "sour.is/x/paste/routes" ) func main() { diff --git a/routes/paste.go b/routes/paste.go index 8260bd2..29e181a 100644 --- a/routes/paste.go +++ b/routes/paste.go @@ -1,20 +1,20 @@ package routes import ( - "net/http" - "sour.is/x/httpsrv" - "encoding/json" - "github.com/gorilla/mux" - "io/ioutil" - "io" - "sour.is/x/log" - "os" - "golang.org/x/sys/unix" - "crypto/rand" - "strconv" "bufio" + "crypto/rand" "crypto/sha256" "encoding/base64" + "encoding/json" + "github.com/gorilla/mux" + "golang.org/x/sys/unix" + "io" + "io/ioutil" + "net/http" + "os" + "sour.is/x/httpsrv" + "sour.is/x/log" + "strconv" "strings" "time" ) @@ -26,23 +26,23 @@ func init() { httpsrv.RegisterModule("paste", setConfig) httpsrv.HttpRegister("paste", httpsrv.HttpRoutes{ - { "getRandom", "GET", "/paste/rng", getRandom, }, - { "getPaste", "GET", "/paste/{id}", getPaste, }, - { "getPaste", "GET", "/paste/get/{id}", getPaste, }, - { "postPaste", "POST", "/paste", postPaste, }, + {"getRandom", "GET", "/paste/rng", getRandom}, + {"getPaste", "GET", "/paste/{id}", getPaste}, + {"getPaste", "GET", "/paste/get/{id}", getPaste}, + {"postPaste", "POST", "/paste", postPaste}, - { "getRandom", "GET", "/api/rng", getRandom, }, - { "getPaste", "GET", "/api/{id}", getPaste, }, - { "getPaste", "GET", "/api/get/{id}", getPaste, }, - { "postPaste", "POST", "/api", postPaste, }, + {"getRandom", "GET", "/api/rng", getRandom}, + {"getPaste", "GET", "/api/{id}", getPaste}, + {"getPaste", "GET", "/api/get/{id}", getPaste}, + {"postPaste", "POST", "/api", postPaste}, }) httpsrv.AssetRegister("paste", httpsrv.AssetRoutes{ - { "Assets", "/", httpsrv.FsHtml5( assetFS() ) }, + {"Assets", "/", httpsrv.FsHtml5(assetFS())}, }) } -func setConfig (config map[string]string) { +func setConfig(config map[string]string) { store = "data/" if config["store"] != "" { @@ -63,28 +63,48 @@ func setConfig (config map[string]string) { func chkStore(path string) bool { file, err := os.Stat(path) - if err == nil { return true } - if os.IsNotExist(err) { return false } - if !file.IsDir() { return false } - if unix.Access(path, unix.W_OK & unix.R_OK) != nil { return false } + if err == nil { + return true + } + if os.IsNotExist(err) { + return false + } + if !file.IsDir() { + return false + } + if unix.Access(path, unix.W_OK&unix.R_OK) != nil { + return false + } return true } func chkFile(path string) bool { file, err := os.Stat(path) - if err == nil { return true } - if os.IsNotExist(err) { return false } - if file.IsDir() { return false } - if unix.Access(path, unix.W_OK & unix.R_OK) != nil { return false } + if err == nil { + return true + } + if os.IsNotExist(err) { + return false + } + if file.IsDir() { + return false + } + if unix.Access(path, unix.W_OK&unix.R_OK) != nil { + return false + } return true } func chkGone(path string) bool { file, err := os.Stat(path) - if err != nil { return true } - if file.Size() == 0 { return true } + if err != nil { + return true + } + if file.Size() == 0 { + return true + } return false } @@ -92,7 +112,7 @@ func getRandom(w http.ResponseWriter, r *http.Request) { s := make([]byte, randBytes) rand.Read(s) - w.Header().Set("content-type","application/octet-stream") + w.Header().Set("content-type", "application/octet-stream") w.WriteHeader(http.StatusOK) w.Write(s) } @@ -102,22 +122,21 @@ func getPaste(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id := vars["id"] - if !chkFile(store + id) { - w.WriteHeader(http.StatusNotFound) - w.Write([]byte("ERR Not Found")) - return + w.WriteHeader(http.StatusNotFound) + w.Write([]byte("ERR Not Found")) + return } if chkGone(store + id) { - w.WriteHeader(http.StatusGone) - w.Write([]byte("ERR Gone")) + w.WriteHeader(http.StatusGone) + w.Write([]byte("ERR Gone")) return } head, err := os.Open(store + id) if err != nil { - log.Fatal(err) + log.Fatal(err) } defer head.Close() @@ -127,7 +146,7 @@ func getPaste(w http.ResponseWriter, r *http.Request) { txt := scanner.Text() log.Debug(txt) - if (txt == "") { + if txt == "" { break } @@ -183,7 +202,7 @@ func postPaste(w http.ResponseWriter, r *http.Request) { s256 := sha256.Sum256(body) id := base64.RawURLEncoding.EncodeToString(s256[12:]) - ioutil.WriteFile(store + id, body, 0644) + ioutil.WriteFile(store+id, body, 0644) w.WriteHeader(http.StatusCreated) w.Write([]byte("OK " + id)) @@ -194,11 +213,11 @@ func postPaste(w http.ResponseWriter, r *http.Request) { func checkErr(err error, w http.ResponseWriter) { if err != nil { w.WriteHeader(http.StatusBadRequest) - json.NewEncoder(w).Encode(err); + json.NewEncoder(w).Encode(err) panic(err) } } func deleteFile(path string) { ioutil.WriteFile(path, []byte(""), 0644) -} \ No newline at end of file +}