go fmt
This commit is contained in:
parent
db6d5afb09
commit
5f5d0541e1
6
Makefile
6
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
|
||||
|
||||
|
|
14
config.go
14
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/"
|
||||
`)
|
||||
`)
|
||||
|
|
6
main.go
6
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() {
|
||||
|
|
105
routes/paste.go
105
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user