1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

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 # make -j4 release
VERSION=$(shell git describe) 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 .PHONY: build
build: bindata.go build: server/bindata.go
go build ${LDFLAGS} go build ${LDFLAGS}
STATICFILES := $(wildcard static/*) STATICFILES := $(wildcard static/*)
TEMPLATES := $(wildcard templates/*) TEMPLATES := $(wildcard templates/*)
bindata.go: $(STATICFILES) $(TEMPLATES) server/bindata.go: $(STATICFILES) $(TEMPLATES)
go-bindata -tags '!debug' static/... templates/... go-bindata -tags '!debug' -o server/bindata.go static/... templates/...
go fmt go fmt
bindata-debug.go: $(STATICFILES) $(TEMPLATES) server/bindata-debug.go: $(STATICFILES) $(TEMPLATES)
go-bindata -tags 'debug' -o bindata-debug.go -debug static/... templates/... go-bindata -tags 'debug' -o server/bindata-debug.go -debug static/... templates/...
go fmt go fmt
.PHONY: devel .PHONY: devel
devel: bindata-debug.go devel: server/bindata-debug.go
go build -tags debug go build -tags debug
.PHONY: quick .PHONY: quick
quick: bindata.go quick: server/bindata.go
go build go build
.PHONY: linuxarm .PHONY: linuxarm
linuxarm: bindata.go linuxarm: server/bindata.go
env GOOS=linux GOARCH=arm go build ${LDFLAGS} -o dist/cowyo_linux_arm env GOOS=linux GOARCH=arm go build ${LDFLAGS} -o dist/cowyo_linux_arm
#cd dist && upx --brute cowyo_linux_arm #cd dist && upx --brute cowyo_linux_arm
.PHONY: linux32 .PHONY: linux32
linux32: bindata.go linux32: server/bindata.go
env GOOS=linux GOARCH=386 go build ${LDFLAGS} -o dist/cowyo_linux_32bit env GOOS=linux GOARCH=386 go build ${LDFLAGS} -o dist/cowyo_linux_32bit
#cd dist && upx --brute cowyo_linux_32bit #cd dist && upx --brute cowyo_linux_32bit
.PHONY: linux64 .PHONY: linux64
linux64: bindata.go linux64: server/bindata.go
env GOOS=linux GOARCH=amd64 go build ${LDFLAGS} -o dist/cowyo_linux_amd64 env GOOS=linux GOARCH=amd64 go build ${LDFLAGS} -o dist/cowyo_linux_amd64
.PHONY: windows .PHONY: windows
windows: bindata.go windows: server/bindata.go
env GOOS=windows GOARCH=amd64 go build ${LDFLAGS} -o dist/cowyo_windows_amd64.exe env GOOS=windows GOARCH=amd64 go build ${LDFLAGS} -o dist/cowyo_windows_amd64.exe
#cd dist && upx --brute cowyo_windows_amd64.exe #cd dist && upx --brute cowyo_windows_amd64.exe
.PHONY: osx .PHONY: osx
osx: bindata.go osx: server/bindata.go
env GOOS=darwin GOARCH=amd64 go build ${LDFLAGS} -o dist/cowyo_osx_amd64 env GOOS=darwin GOARCH=amd64 go build ${LDFLAGS} -o dist/cowyo_osx_amd64
#cd dist && upx --brute cowyo_osx_amd64 #cd dist && upx --brute cowyo_osx_amd64

View File

@ -2,10 +2,14 @@ package main
import ( import (
"fmt" "fmt"
"net"
"os" "os"
"time" "time"
"gopkg.in/urfave/cli.v1" "github.com/jcelliott/lumber"
"github.com/schollz/cowyo/server"
cli "gopkg.in/urfave/cli.v1"
) )
var version string var version string
@ -19,9 +23,7 @@ func main() {
app.Version = version app.Version = version
app.Compiled = time.Now() app.Compiled = time.Now()
app.Action = func(c *cli.Context) error { app.Action = func(c *cli.Context) error {
if !c.GlobalBool("debug") {
turnOffDebugger()
}
pathToData = c.GlobalString("data") pathToData = c.GlobalString("data")
os.MkdirAll(pathToData, 0755) os.MkdirAll(pathToData, 0755)
host := c.GlobalString("host") 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")) fmt.Printf("\nRunning cowyo server (version %s) at http://%s:%s\n\n", version, host, c.GlobalString("port"))
} }
allowFileUploads = c.GlobalBool("allow-file-uploads") server.Serve(
maxUploadMB = c.GlobalUint("max-upload-mb") pathToData,
serve(
c.GlobalString("host"), c.GlobalString("host"),
c.GlobalString("port"), c.GlobalString("port"),
c.GlobalString("cert"), c.GlobalString("cert"),
@ -58,6 +58,9 @@ func main() {
c.GlobalString("access-code"), c.GlobalString("access-code"),
c.GlobalBool("allow-insecure-markup"), c.GlobalBool("allow-insecure-markup"),
hotTemplateReloading, hotTemplateReloading,
c.GlobalBool("allow-file-uploads"),
c.GlobalUint("max-upload-mb"),
logger(c.GlobalBool("debug")),
) )
return nil return nil
} }
@ -150,9 +153,6 @@ func main() {
Aliases: []string{"m"}, Aliases: []string{"m"},
Usage: "migrate from the old cowyo", Usage: "migrate from the old cowyo",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
if !c.GlobalBool("debug") {
turnOffDebugger()
}
pathToData = c.GlobalString("data") pathToData = c.GlobalString("data")
pathToOldData := c.GlobalString("olddata") pathToOldData := c.GlobalString("olddata")
if len(pathToOldData) == 0 { if len(pathToOldData) == 0 {
@ -164,7 +164,7 @@ func main() {
fmt.Printf("Can not find '%s', does it exist?", pathToOldData) fmt.Printf("Can not find '%s', does it exist?", pathToOldData)
return nil return nil
} }
migrate(pathToOldData, pathToData) server.Migrate(pathToOldData, pathToData, logger(c.GlobalBool("debug")))
return nil return nil
}, },
}, },
@ -173,3 +173,35 @@ func main() {
app.Run(os.Args) 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 // +build debug
package main package server
import ( import (
"fmt" "fmt"

View File

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

View File

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

View File

@ -1,4 +1,4 @@
package main package server
import ( import (
"crypto/sha256" "crypto/sha256"
@ -19,6 +19,7 @@ import (
"github.com/gin-contrib/multitemplate" "github.com/gin-contrib/multitemplate"
"github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/jcelliott/lumber"
"github.com/schollz/cowyo/encrypt" "github.com/schollz/cowyo/encrypt"
) )
@ -31,8 +32,11 @@ var diaryMode bool
var allowFileUploads bool var allowFileUploads bool
var maxUploadMB uint var maxUploadMB uint
var needSitemapUpdate = true var needSitemapUpdate = true
var pathToData string
var log *lumber.ConsoleLogger
func serve( func Serve(
filepathToData,
host, host,
port, port,
crt_path, crt_path,
@ -47,7 +51,17 @@ func serve(
secretCode string, secretCode string,
allowInsecure bool, allowInsecure bool,
hotTemplateReloading 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 { if hotTemplateReloading {
gin.SetMode(gin.DebugMode) gin.SetMode(gin.DebugMode)

View File

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

View File

@ -1,12 +1,15 @@
package main package server
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"path" "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) files, err := ioutil.ReadDir(pathToOldData)
if len(files) == 0 { if len(files) == 0 {
return err return err

View File

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

View File

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

View File

@ -1,18 +1,16 @@
package main package server
import ( import (
"encoding/base32" "encoding/base32"
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
"math/rand" "math/rand"
"net"
"net/http" "net/http"
"os" "os"
"path" "path"
"strings" "strings"
"time" "time"
"github.com/jcelliott/lumber"
"github.com/microcosm-cc/bluemonday" "github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday" "github.com/russross/blackfriday"
"github.com/shurcooL/github_flavored_markdown" "github.com/shurcooL/github_flavored_markdown"
@ -24,19 +22,12 @@ var adjectives []string
var aboutPageText string var aboutPageText string
var allowInsecureHtml bool var allowInsecureHtml bool
var log *lumber.ConsoleLogger
func init() { func init() {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
animalsText, _ := Asset("static/text/animals") animalsText, _ := Asset("static/text/animals")
animals = strings.Split(string(animalsText), ",") animals = strings.Split(string(animalsText), ",")
adjectivesText, _ := Asset("static/text/adjectives") adjectivesText, _ := Asset("static/text/adjectives")
adjectives = strings.Split(string(adjectivesText), "\n") adjectives = strings.Split(string(adjectivesText), "\n")
log = lumber.NewConsoleLogger(lumber.TRACE)
}
func turnOffDebugger() {
log = lumber.NewConsoleLogger(lumber.WARN)
} }
func randomAnimal() string { func randomAnimal() string {
@ -146,24 +137,6 @@ func RandStringBytesMaskImprSrc(n int) string {
return string(b) 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. // HashPassword generates a bcrypt hash of the password using work factor 14.
// https://github.com/gtank/cryptopasta/blob/master/hash.go // https://github.com/gtank/cryptopasta/blob/master/hash.go
func HashPassword(password string) string { 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 // exists returns whether the given file or directory exists or not
func exists(path string) bool { func exists(path string) bool {
_, err := os.Stat(path) _, err := os.Stat(path)
if err == nil { return !os.IsNotExist(err)
return true
}
if os.IsNotExist(err) {
return false
}
return true
} }
func MarkdownToHtml(s string) string { func MarkdownToHtml(s string) string {

View File

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