This commit is contained in:
xuu 2017-05-22 11:13:47 -06:00
parent db6d5afb09
commit 5f5d0541e1
4 changed files with 77 additions and 54 deletions

View File

@ -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

View File

@ -1,12 +1,12 @@
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 (

View File

@ -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,15 +26,15 @@ 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{
@ -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
}
@ -102,7 +122,6 @@ 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"))
@ -127,7 +146,7 @@ func getPaste(w http.ResponseWriter, r *http.Request) {
txt := scanner.Text()
log.Debug(txt)
if (txt == "") {
if txt == "" {
break
}
@ -194,7 +213,7 @@ 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)
}
}