-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
67 lines (53 loc) · 1.48 KB
/
main.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
package main
import (
"os"
"syscall"
"github.com/spf13/viper"
"pack.ag/tftp"
"github.com/sirupsen/logrus"
"github.com/jmoiron/sqlx"
_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
_ "github.com/lib/pq"
)
var log = logrus.New()
func init() {
viper.SetDefault("listen", ":69")
viper.SetDefault("root_dir", "/tftpboot")
viper.SetDefault("shared_secret", "key")
viper.SetDefault("db_driver", "postgres")
viper.SetDefault("db_dsn", "")
viper.SetDefault("upgrade_server", "0.0.0.0")
viper.SetDefault("log_format", "text")
viper.SetDefault("rewrites", map[string]string{} )
viper.SetConfigName("docsistftp")
viper.SetEnvPrefix("DOCSISTFTP")
viper.AddConfigPath("/etc")
viper.AddConfigPath(".")
viper.ReadInConfig()
viper.AutomaticEnv()
if viper.GetString("log_format") == "json" {
log.Formatter = &logrus.JSONFormatter{}
} else {
log.Formatter = &logrus.TextFormatter{}
}
}
func main() {
log.Out = os.Stdout
db, err := sqlx.Open(viper.GetString("db_driver"), viper.GetString("db_dsn"))
if err != nil {
log.Fatal(err)
}
defer db.Close()
if err := syscall.Chroot(viper.GetString("root_dir")); err != nil {
log.Fatal(err)
}
os.Chdir("/")
server, err := tftp.NewServer(viper.GetString("listen"))
if err != nil {
log.Fatal(err)
}
handler := NewServerHandler(db)
server.ReadHandler(handler)
log.Fatal(server.ListenAndServe())
}