2017-03-22 05:46:05 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-23 17:32:46 +03:00
|
|
|
"fmt"
|
2018-04-28 04:32:08 +03:00
|
|
|
"net"
|
2017-03-22 06:51:52 +03:00
|
|
|
"os"
|
|
|
|
"time"
|
2017-03-22 05:46:05 +03:00
|
|
|
|
2018-04-28 04:32:08 +03:00
|
|
|
"github.com/jcelliott/lumber"
|
|
|
|
"github.com/schollz/cowyo/server"
|
|
|
|
|
|
|
|
cli "gopkg.in/urfave/cli.v1"
|
2017-03-22 05:46:05 +03:00
|
|
|
)
|
|
|
|
|
2017-03-22 06:51:52 +03:00
|
|
|
var version string
|
|
|
|
var pathToData string
|
2017-03-22 05:46:05 +03:00
|
|
|
|
2017-03-22 06:51:52 +03:00
|
|
|
func main() {
|
|
|
|
app := cli.NewApp()
|
2017-03-23 00:35:02 +03:00
|
|
|
app.Name = "cowyo"
|
|
|
|
app.Usage = "a simple wiki"
|
2017-03-22 06:51:52 +03:00
|
|
|
app.Version = version
|
|
|
|
app.Compiled = time.Now()
|
|
|
|
app.Action = func(c *cli.Context) error {
|
2017-03-22 23:14:47 +03:00
|
|
|
pathToData = c.GlobalString("data")
|
|
|
|
os.MkdirAll(pathToData, 0755)
|
2017-03-28 17:01:03 +03:00
|
|
|
host := c.GlobalString("host")
|
2017-06-23 16:40:35 +03:00
|
|
|
crt_f := c.GlobalString("cert") // crt flag
|
|
|
|
key_f := c.GlobalString("key") // key flag
|
2017-03-28 17:01:03 +03:00
|
|
|
if host == "" {
|
|
|
|
host = GetLocalIP()
|
|
|
|
}
|
2017-06-23 16:40:35 +03:00
|
|
|
TLS := false
|
|
|
|
if crt_f != "" && key_f != "" {
|
|
|
|
TLS = true
|
|
|
|
}
|
|
|
|
if TLS {
|
|
|
|
fmt.Printf("\nRunning cowyo server (version %s) at https://%s:%s\n\n", version, host, c.GlobalString("port"))
|
|
|
|
} else {
|
|
|
|
fmt.Printf("\nRunning cowyo server (version %s) at http://%s:%s\n\n", version, host, c.GlobalString("port"))
|
|
|
|
}
|
2018-01-18 12:50:55 +03:00
|
|
|
|
2018-04-28 04:32:08 +03:00
|
|
|
server.Serve(
|
|
|
|
pathToData,
|
2018-01-18 12:50:55 +03:00
|
|
|
c.GlobalString("host"),
|
|
|
|
c.GlobalString("port"),
|
|
|
|
c.GlobalString("cert"),
|
|
|
|
c.GlobalString("key"),
|
|
|
|
TLS,
|
|
|
|
c.GlobalString("css"),
|
|
|
|
c.GlobalString("default-page"),
|
|
|
|
c.GlobalString("lock"),
|
|
|
|
c.GlobalInt("debounce"),
|
|
|
|
c.GlobalBool("diary"),
|
2018-01-18 12:51:08 +03:00
|
|
|
c.GlobalString("cookie-secret"),
|
2018-01-21 04:09:16 +03:00
|
|
|
c.GlobalString("access-code"),
|
|
|
|
c.GlobalBool("allow-insecure-markup"),
|
2018-04-28 04:32:08 +03:00
|
|
|
c.GlobalBool("allow-file-uploads"),
|
|
|
|
c.GlobalUint("max-upload-mb"),
|
2018-05-02 09:27:35 +03:00
|
|
|
c.GlobalUint("max-document-length"),
|
2018-04-28 04:32:08 +03:00
|
|
|
logger(c.GlobalBool("debug")),
|
2018-01-18 12:50:55 +03:00
|
|
|
)
|
2017-03-22 06:51:52 +03:00
|
|
|
return nil
|
2017-03-22 05:46:05 +03:00
|
|
|
}
|
2017-03-22 06:51:52 +03:00
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "data",
|
|
|
|
Value: "data",
|
|
|
|
Usage: "data folder to use",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "olddata",
|
|
|
|
Value: "",
|
|
|
|
Usage: "data folder for migrating",
|
|
|
|
},
|
2017-03-28 17:01:03 +03:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "host",
|
|
|
|
Value: "",
|
|
|
|
Usage: "host to use",
|
|
|
|
},
|
2017-03-22 17:26:34 +03:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "port,p",
|
|
|
|
Value: "8050",
|
|
|
|
Usage: "port to use",
|
|
|
|
},
|
2017-06-23 16:40:35 +03:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "cert",
|
|
|
|
Value: "",
|
2017-06-23 22:02:56 +03:00
|
|
|
Usage: "absolute path to SSL public sertificate",
|
2017-06-23 16:40:35 +03:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "key",
|
|
|
|
Value: "",
|
2017-06-23 22:02:56 +03:00
|
|
|
Usage: "absolute path to SSL private key",
|
2017-06-23 16:40:35 +03:00
|
|
|
},
|
2017-11-04 13:18:05 +03:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "css",
|
|
|
|
Value: "",
|
|
|
|
Usage: "use a custom CSS file",
|
|
|
|
},
|
2017-11-04 13:21:51 +03:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "default-page",
|
|
|
|
Value: "",
|
2017-11-04 13:41:56 +03:00
|
|
|
Usage: "show default-page/read instead of editing (default: show random editing)",
|
|
|
|
},
|
2018-01-18 12:50:55 +03:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "allow-insecure-markup",
|
|
|
|
Usage: "Skip HTML sanitization",
|
|
|
|
},
|
2017-11-04 13:41:56 +03:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "lock",
|
|
|
|
Value: "",
|
|
|
|
Usage: "password to lock editing all files (default: all pages unlocked)",
|
2017-11-04 13:21:51 +03:00
|
|
|
},
|
2017-11-05 00:30:44 +03:00
|
|
|
cli.IntFlag{
|
|
|
|
Name: "debounce",
|
|
|
|
Value: 500,
|
|
|
|
Usage: "debounce time for saving data, in milliseconds",
|
|
|
|
},
|
2017-03-22 06:51:52 +03:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "debug, d",
|
|
|
|
Usage: "turn on debugging",
|
|
|
|
},
|
2017-11-05 01:09:10 +03:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "diary",
|
|
|
|
Usage: "turn diary mode (doing New will give a timestamped page)",
|
|
|
|
},
|
2018-01-21 04:09:16 +03:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "access-code",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Secret code to login with before accessing any wiki stuff",
|
|
|
|
},
|
2018-01-18 12:51:08 +03:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "cookie-secret",
|
|
|
|
Value: "secret",
|
|
|
|
Usage: "random data to use for cookies; changing it will invalidate all sessions",
|
|
|
|
},
|
2018-02-16 02:50:32 +03:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "allow-file-uploads",
|
|
|
|
Usage: "Enable file uploads",
|
|
|
|
},
|
|
|
|
cli.UintFlag{
|
|
|
|
Name: "max-upload-mb",
|
|
|
|
Value: 2,
|
|
|
|
Usage: "Largest file upload (in mb) allowed",
|
|
|
|
},
|
2018-05-02 09:27:35 +03:00
|
|
|
cli.UintFlag{
|
|
|
|
Name: "max-document-length",
|
|
|
|
Value: 100000000,
|
|
|
|
Usage: "Largest wiki page (in characters) allowed",
|
|
|
|
},
|
2017-03-22 05:46:05 +03:00
|
|
|
}
|
2017-03-22 06:51:52 +03:00
|
|
|
app.Commands = []cli.Command{
|
|
|
|
{
|
|
|
|
Name: "migrate",
|
|
|
|
Aliases: []string{"m"},
|
|
|
|
Usage: "migrate from the old cowyo",
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
pathToData = c.GlobalString("data")
|
|
|
|
pathToOldData := c.GlobalString("olddata")
|
|
|
|
if len(pathToOldData) == 0 {
|
|
|
|
fmt.Printf("You need to specify folder with -olddata")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
os.MkdirAll(pathToData, 0755)
|
|
|
|
if !exists(pathToOldData) {
|
|
|
|
fmt.Printf("Can not find '%s', does it exist?", pathToOldData)
|
|
|
|
return nil
|
|
|
|
}
|
2018-04-28 04:32:08 +03:00
|
|
|
server.Migrate(pathToOldData, pathToData, logger(c.GlobalBool("debug")))
|
2017-03-22 06:51:52 +03:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
2017-03-22 05:46:05 +03:00
|
|
|
}
|
2017-03-22 06:06:35 +03:00
|
|
|
|
2017-03-22 06:51:52 +03:00
|
|
|
app.Run(os.Args)
|
2017-03-22 06:06:35 +03:00
|
|
|
}
|
2018-04-28 04:32:08 +03:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
}
|