From 8944170646fae36f0f1884786faf859c3e5304a1 Mon Sep 17 00:00:00 2001 From: Daniel Heath Date: Sat, 28 Apr 2018 11:32:08 +1000 Subject: [PATCH] Initial split (towards a separately mountable cowyo) --- Makefile | 27 +++++----- main.go => cmd/main.go | 56 ++++++++++++++++----- bindata-debug.go => server/bindata-debug.go | 2 +- bindata.go => server/bindata.go | 2 +- debug.go => server/debug.go | 2 +- handlers.go => server/handlers.go | 18 ++++++- listify.go => server/listify.go | 2 +- migrate.go => server/migrate.go | 7 ++- page.go => server/page.go | 2 +- page_test.go => server/page_test.go | 2 +- utils.go => server/utils.go | 37 +------------- utils_test.go => server/utils_test.go | 2 +- 12 files changed, 87 insertions(+), 72 deletions(-) rename main.go => cmd/main.go (77%) rename bindata-debug.go => server/bindata-debug.go (99%) rename bindata.go => server/bindata.go (99%) rename debug.go => server/debug.go (80%) rename handlers.go => server/handlers.go (98%) rename listify.go => server/listify.go (99%) rename migrate.go => server/migrate.go (77%) rename page.go => server/page.go (99%) rename page_test.go => server/page_test.go (99%) rename utils.go => server/utils.go (88%) rename utils_test.go => server/utils_test.go (97%) diff --git a/Makefile b/Makefile index 72c494d..e7e45da 100644 --- a/Makefile +++ b/Makefile @@ -2,52 +2,51 @@ # make -j4 release VERSION=$(shell git describe) -LDFLAGS=-ldflags "-X main.version=${VERSION}" +LDFLAGS=-ldflags "-X main.version=${VERSION}" -o cowyo github.com/schollz/cowyo/cmd .PHONY: build -build: bindata.go +build: server/bindata.go go build ${LDFLAGS} STATICFILES := $(wildcard static/*) TEMPLATES := $(wildcard templates/*) -bindata.go: $(STATICFILES) $(TEMPLATES) - go-bindata -tags '!debug' static/... templates/... +server/bindata.go: $(STATICFILES) $(TEMPLATES) + go-bindata -tags '!debug' -o server/bindata.go static/... templates/... go fmt -bindata-debug.go: $(STATICFILES) $(TEMPLATES) - go-bindata -tags 'debug' -o bindata-debug.go -debug static/... templates/... +server/bindata-debug.go: $(STATICFILES) $(TEMPLATES) + go-bindata -tags 'debug' -o server/bindata-debug.go -debug static/... templates/... go fmt - .PHONY: devel -devel: bindata-debug.go +devel: server/bindata-debug.go go build -tags debug .PHONY: quick -quick: bindata.go +quick: server/bindata.go go build .PHONY: linuxarm -linuxarm: bindata.go +linuxarm: server/bindata.go env GOOS=linux GOARCH=arm go build ${LDFLAGS} -o dist/cowyo_linux_arm #cd dist && upx --brute cowyo_linux_arm .PHONY: linux32 -linux32: bindata.go +linux32: server/bindata.go env GOOS=linux GOARCH=386 go build ${LDFLAGS} -o dist/cowyo_linux_32bit #cd dist && upx --brute cowyo_linux_32bit .PHONY: linux64 -linux64: bindata.go +linux64: server/bindata.go env GOOS=linux GOARCH=amd64 go build ${LDFLAGS} -o dist/cowyo_linux_amd64 .PHONY: windows -windows: bindata.go +windows: server/bindata.go env GOOS=windows GOARCH=amd64 go build ${LDFLAGS} -o dist/cowyo_windows_amd64.exe #cd dist && upx --brute cowyo_windows_amd64.exe .PHONY: osx -osx: bindata.go +osx: server/bindata.go env GOOS=darwin GOARCH=amd64 go build ${LDFLAGS} -o dist/cowyo_osx_amd64 #cd dist && upx --brute cowyo_osx_amd64 diff --git a/main.go b/cmd/main.go similarity index 77% rename from main.go rename to cmd/main.go index 5cf4831..9dde394 100755 --- a/main.go +++ b/cmd/main.go @@ -2,10 +2,14 @@ package main import ( "fmt" + "net" "os" "time" - "gopkg.in/urfave/cli.v1" + "github.com/jcelliott/lumber" + "github.com/schollz/cowyo/server" + + cli "gopkg.in/urfave/cli.v1" ) var version string @@ -19,9 +23,7 @@ func main() { app.Version = version app.Compiled = time.Now() app.Action = func(c *cli.Context) error { - if !c.GlobalBool("debug") { - turnOffDebugger() - } + pathToData = c.GlobalString("data") os.MkdirAll(pathToData, 0755) host := c.GlobalString("host") @@ -40,10 +42,8 @@ func main() { fmt.Printf("\nRunning cowyo server (version %s) at http://%s:%s\n\n", version, host, c.GlobalString("port")) } - allowFileUploads = c.GlobalBool("allow-file-uploads") - maxUploadMB = c.GlobalUint("max-upload-mb") - - serve( + server.Serve( + pathToData, c.GlobalString("host"), c.GlobalString("port"), c.GlobalString("cert"), @@ -58,6 +58,9 @@ func main() { c.GlobalString("access-code"), c.GlobalBool("allow-insecure-markup"), hotTemplateReloading, + c.GlobalBool("allow-file-uploads"), + c.GlobalUint("max-upload-mb"), + logger(c.GlobalBool("debug")), ) return nil } @@ -150,9 +153,6 @@ func main() { Aliases: []string{"m"}, Usage: "migrate from the old cowyo", Action: func(c *cli.Context) error { - if !c.GlobalBool("debug") { - turnOffDebugger() - } pathToData = c.GlobalString("data") pathToOldData := c.GlobalString("olddata") if len(pathToOldData) == 0 { @@ -164,7 +164,7 @@ func main() { fmt.Printf("Can not find '%s', does it exist?", pathToOldData) return nil } - migrate(pathToOldData, pathToData) + server.Migrate(pathToOldData, pathToData, logger(c.GlobalBool("debug"))) return nil }, }, @@ -173,3 +173,35 @@ func main() { app.Run(os.Args) } + +// GetLocalIP returns the local ip address +func GetLocalIP() string { + addrs, err := net.InterfaceAddrs() + if err != nil { + return "" + } + bestIP := "" + for _, address := range addrs { + // check the address type and if it is not a loopback the display it + if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { + if ipnet.IP.To4() != nil { + return ipnet.IP.String() + } + } + } + return bestIP +} + +// exists returns whether the given file or directory exists or not +func exists(path string) bool { + _, err := os.Stat(path) + return !os.IsNotExist(err) +} + +func logger(debug bool) *lumber.ConsoleLogger { + if !debug { + return lumber.NewConsoleLogger(lumber.WARN) + } + return lumber.NewConsoleLogger(lumber.TRACE) + +} diff --git a/bindata-debug.go b/server/bindata-debug.go similarity index 99% rename from bindata-debug.go rename to server/bindata-debug.go index f21fd75..5288697 100644 --- a/bindata-debug.go +++ b/server/bindata-debug.go @@ -51,7 +51,7 @@ // +build debug -package main +package server import ( "fmt" diff --git a/bindata.go b/server/bindata.go similarity index 99% rename from bindata.go rename to server/bindata.go index c3851b1..b6b50be 100644 --- a/bindata.go +++ b/server/bindata.go @@ -51,7 +51,7 @@ // +build !debug -package main +package server import ( "bytes" diff --git a/debug.go b/server/debug.go similarity index 80% rename from debug.go rename to server/debug.go index 28dead5..900744c 100644 --- a/debug.go +++ b/server/debug.go @@ -1,6 +1,6 @@ // +build debug -package main +package server func init() { hotTemplateReloading = true diff --git a/handlers.go b/server/handlers.go similarity index 98% rename from handlers.go rename to server/handlers.go index e490e56..6cf13fa 100755 --- a/handlers.go +++ b/server/handlers.go @@ -1,4 +1,4 @@ -package main +package server import ( "crypto/sha256" @@ -19,6 +19,7 @@ import ( "github.com/gin-contrib/multitemplate" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" + "github.com/jcelliott/lumber" "github.com/schollz/cowyo/encrypt" ) @@ -31,8 +32,11 @@ var diaryMode bool var allowFileUploads bool var maxUploadMB uint var needSitemapUpdate = true +var pathToData string +var log *lumber.ConsoleLogger -func serve( +func Serve( + filepathToData, host, port, crt_path, @@ -47,7 +51,17 @@ func serve( secretCode string, allowInsecure bool, hotTemplateReloading bool, + fileuploads bool, + maxUploadSize uint, + logger *lumber.ConsoleLogger, ) { + pathToData = filepathToData + allowFileUploads = fileuploads + maxUploadMB = maxUploadSize + log = logger + if log == nil { + log = lumber.NewConsoleLogger(lumber.TRACE) + } if hotTemplateReloading { gin.SetMode(gin.DebugMode) diff --git a/listify.go b/server/listify.go similarity index 99% rename from listify.go rename to server/listify.go index 5fb2dfd..8c76173 100644 --- a/listify.go +++ b/server/listify.go @@ -1,4 +1,4 @@ -package main +package server import ( "html/template" diff --git a/migrate.go b/server/migrate.go similarity index 77% rename from migrate.go rename to server/migrate.go index b91f355..32ef5d0 100755 --- a/migrate.go +++ b/server/migrate.go @@ -1,12 +1,15 @@ -package main +package server import ( "fmt" "io/ioutil" "path" + + "github.com/jcelliott/lumber" ) -func migrate(pathToOldData, pathToData string) error { +func Migrate(pathToOldData, pathToData string, logger *lumber.ConsoleLogger) error { + log = logger files, err := ioutil.ReadDir(pathToOldData) if len(files) == 0 { return err diff --git a/page.go b/server/page.go similarity index 99% rename from page.go rename to server/page.go index 1f265bd..d623915 100755 --- a/page.go +++ b/server/page.go @@ -1,4 +1,4 @@ -package main +package server import ( "encoding/json" diff --git a/page_test.go b/server/page_test.go similarity index 99% rename from page_test.go rename to server/page_test.go index 1816e55..09aa9bb 100755 --- a/page_test.go +++ b/server/page_test.go @@ -1,4 +1,4 @@ -package main +package server import ( "os" diff --git a/utils.go b/server/utils.go similarity index 88% rename from utils.go rename to server/utils.go index 5f50635..d4fd46a 100644 --- a/utils.go +++ b/server/utils.go @@ -1,18 +1,16 @@ -package main +package server import ( "encoding/base32" "encoding/binary" "encoding/hex" "math/rand" - "net" "net/http" "os" "path" "strings" "time" - "github.com/jcelliott/lumber" "github.com/microcosm-cc/bluemonday" "github.com/russross/blackfriday" "github.com/shurcooL/github_flavored_markdown" @@ -24,19 +22,12 @@ var adjectives []string var aboutPageText string var allowInsecureHtml bool -var log *lumber.ConsoleLogger - func init() { rand.Seed(time.Now().Unix()) animalsText, _ := Asset("static/text/animals") animals = strings.Split(string(animalsText), ",") adjectivesText, _ := Asset("static/text/adjectives") adjectives = strings.Split(string(adjectivesText), "\n") - log = lumber.NewConsoleLogger(lumber.TRACE) -} - -func turnOffDebugger() { - log = lumber.NewConsoleLogger(lumber.WARN) } func randomAnimal() string { @@ -146,24 +137,6 @@ func RandStringBytesMaskImprSrc(n int) string { return string(b) } -// GetLocalIP returns the local ip address -func GetLocalIP() string { - addrs, err := net.InterfaceAddrs() - if err != nil { - return "" - } - bestIP := "" - for _, address := range addrs { - // check the address type and if it is not a loopback the display it - if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { - if ipnet.IP.To4() != nil { - return ipnet.IP.String() - } - } - } - return bestIP -} - // HashPassword generates a bcrypt hash of the password using work factor 14. // https://github.com/gtank/cryptopasta/blob/master/hash.go func HashPassword(password string) string { @@ -185,13 +158,7 @@ func CheckPasswordHash(password, hashedString string) error { // exists returns whether the given file or directory exists or not func exists(path string) bool { _, err := os.Stat(path) - if err == nil { - return true - } - if os.IsNotExist(err) { - return false - } - return true + return !os.IsNotExist(err) } func MarkdownToHtml(s string) string { diff --git a/utils_test.go b/server/utils_test.go similarity index 97% rename from utils_test.go rename to server/utils_test.go index 81d79c2..1942891 100755 --- a/utils_test.go +++ b/server/utils_test.go @@ -1,4 +1,4 @@ -package main +package server import ( "testing"