-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
67 lines (56 loc) · 1.85 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"flag"
"log"
"github.com/vharitonsky/iniflags"
"github.com/xtrafrancyz/golish/backend"
)
var Config struct {
host string
port int
slugLength int
adminPath string
defaultRedirect string
backend string
// mysql backend
mysqlHost string
mysqlDatabase string
mysqlUser string
mysqlPassword string
// file backend
filePath string
slugCharacters string
}
func main() {
flag.StringVar(&Config.host, "host", "0.0.0.0", "Server host")
flag.IntVar(&Config.port, "port", 34532, "Server port")
flag.IntVar(&Config.slugLength, "slug-length", 5, "Length of generated url")
flag.StringVar(&Config.adminPath, "admin-path", "admin", "Admin path")
flag.StringVar(&Config.defaultRedirect, "default-redirect", "", "Default redirect path")
flag.StringVar(&Config.backend, "backend", "file", "Backend name (mysql or file)")
flag.StringVar(&Config.mysqlHost, "mysql-host", "127.0.0.1:3306", "MySQL host")
flag.StringVar(&Config.mysqlDatabase, "mysql-database", "golish", "MySQL database")
flag.StringVar(&Config.mysqlUser, "mysql-user", "golish", "MySQL user")
flag.StringVar(&Config.mysqlPassword, "mysql-password", "golish", "MySQL user")
flag.StringVar(&Config.filePath, "file-path", "db.json", "Database file (for file backend)")
Config.slugCharacters = "a-zA-Z0-9_-"
iniflags.Parse()
var err error = nil
var storage backend.Backend = nil
if Config.backend == "mysql" {
storage, err = backend.NewMysql(Config.slugLength, Config.mysqlHost, Config.mysqlUser, Config.mysqlPassword, Config.mysqlDatabase)
} else if Config.backend == "file" {
storage, err = backend.NewFile(Config.slugLength, Config.filePath)
} else {
log.Fatalf("backend must be 'mysql' or 'file'")
return
}
if err != nil {
log.Fatalf("backend creation: %s", err)
return
}
web := &web{
backend: storage,
}
web.run()
}