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

used flag for command line arguments

Former-commit-id: 2ac565e4d78c22ffafc25532ef0771d4c8fa6da6 [formerly 2b65957a84d49a578c09e005d4100b269b6dc8e5] [formerly 115c739d59b0f5ff34de0f51588e973e94e77689 [formerly db7c0b4e9dcd7135b8911792effaa165e7ad4cff [formerly f0fbd2c30f]]]
Former-commit-id: f28ba24ecfad414f37d054b6099aeb8480776914 [formerly d4e575940c207425c74141f5a78c43f5323d437c]
Former-commit-id: bc1dc9204608c1a4cca74639c46a6edbad8d37c6
Former-commit-id: 322446ba18
This commit is contained in:
Travis Scholl 2016-02-07 10:15:42 -08:00
parent 88d1463cd7
commit 08a253eea5
3 changed files with 42 additions and 16 deletions

8
db.go
View File

@ -4,8 +4,6 @@ import (
"encoding/json"
"fmt"
"log"
"path"
"runtime"
"time"
"github.com/boltdb/bolt"
@ -15,12 +13,10 @@ var db *bolt.DB
var open bool
// Open to create the database and open
func Open() error {
func Open(filename string) error {
var err error
_, filename, _, _ := runtime.Caller(0) // get full path of this file
dbfile := path.Join(path.Dir(filename), "data.db")
config := &bolt.Options{Timeout: 30 * time.Second}
db, err = bolt.Open(dbfile, 0600, config)
db, err = bolt.Open(filename, 0600, config)
if err != nil {
fmt.Println("Opening BoltDB timed out")
log.Fatal(err)

48
main.go
View File

@ -1,26 +1,56 @@
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"runtime"
"github.com/gin-gonic/gin"
)
var ExternalIP string
var AllowedIPs string
// AllowedIPs is a white/black list of
// IP addresses allowed to access cowyo
var AllowedIPs = map[string]bool{
"192.168.1.13": true,
"192.168.1.12": true,
"192.168.1.2": true,
}
func init() {
AllowedIPs = "192.168.1.13,192.168.1.12,192.168.1.2"
// RuntimeArgs contains all runtime
// arguments available
var RuntimeArgs struct {
ExternalIP string
Port string
DatabaseLocation string
ServerCRT string
ServerKey string
}
func main() {
if len(os.Args) == 1 {
_, executableFile, _, _ := runtime.Caller(0) // get full path of this file
databaseFile := path.Join(path.Dir(executableFile), "data.db")
flag.StringVar(&RuntimeArgs.Port, "p", ":12312", "port to bind")
flag.StringVar(&RuntimeArgs.DatabaseLocation, "db", databaseFile, "location of database file")
flag.StringVar(&RuntimeArgs.ServerCRT, "crt", "", "location of ssl crt")
flag.StringVar(&RuntimeArgs.ServerKey, "key", "", "location of ssl key")
flag.CommandLine.Usage = func() {
fmt.Println(`cowyo: a websocket notepad
run this to start the server and then visit localhost at the port you specify
(see parameters).
Example: 'cowyo localhost'
Example: 'cowyo -p :8080 localhost'
Example: 'cowyo -db /var/lib/cowyo/db.bolt localhost'
Options:`)
flag.CommandLine.PrintDefaults()
}
flag.Parse()
RuntimeArgs.ExternalIP = flag.Arg(0)
if RuntimeArgs.ExternalIP == "" {
log.Fatal("You need to specify the external IP address")
}
ExternalIP = os.Args[1]
Open()
Open(RuntimeArgs.DatabaseLocation)
defer Close()
// Default page
@ -34,5 +64,5 @@ func main() {
r.GET("/:title", editNote)
r.GET("/:title/*option", everythingElse)
r.DELETE("/listitem", deleteListItem)
r.Run(":12312")
r.Run(RuntimeArgs.Port)
}

View File

@ -26,7 +26,7 @@ func editNote(c *gin.Context) {
} else {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"Title": title,
"ExternalIP": ExternalIP,
"ExternalIP": RuntimeArgs.ExternalIP,
})
}
}