Initial split (towards a separately mountable cowyo)

This commit is contained in:
Daniel Heath 2018-04-28 11:32:08 +10:00
parent f21c89f7bd
commit 8944170646
12 changed files with 87 additions and 72 deletions

View File

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

View File

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

View File

@ -51,7 +51,7 @@
// +build debug
package main
package server
import (
"fmt"

View File

@ -51,7 +51,7 @@
// +build !debug
package main
package server
import (
"bytes"

View File

@ -1,6 +1,6 @@
// +build debug
package main
package server
func init() {
hotTemplateReloading = true

View File

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

View File

@ -1,4 +1,4 @@
package main
package server
import (
"html/template"

View File

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

View File

@ -1,4 +1,4 @@
package main
package server
import (
"encoding/json"

View File

@ -1,4 +1,4 @@
package main
package server
import (
"os"

View File

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

View File

@ -1,4 +1,4 @@
package main
package server
import (
"testing"