-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
59 lines (49 loc) · 1.32 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
package main
import (
"flag"
"fmt"
"go/build"
"log"
"path/filepath"
"github.com/mzki/feserver/server"
)
const pkgPath = "github.com/mzki/feserver"
const defaultConfFile = "config.toml"
var confPath string
func init() {
flag.StringVar(&confPath, "config", "", "path for the server config file")
flag.Parse()
}
func main() {
conf, err := loadConfig(confPath)
if err != nil {
log.Println(err)
log.Println("\nCan not find any config path.\nuse builtin config insteadly")
conf = &server.DefaultConfig
}
// launch server process.
if err := server.ListenAndServe(conf); err != nil {
log.Fatalf("FATAL: %v", err)
}
}
func loadConfig(confPath string) (*server.Config, error) {
if confPath == "" {
// get the directory of the feserver repository under GOPATH.
// referenced from https://golang.org/x/tools/cmd/present/local.go.
p, err := build.Default.Import(pkgPath, "", build.FindOnly)
if err != nil {
return nil, fmt.Errorf(
"Couldn't find default config path: %v\n"+confPathMessage,
err,
pkgPath,
)
}
confPath = filepath.Join(p.Dir, defaultConfFile)
}
return server.LoadConfigFile(confPath)
}
const confPathMessage = `
By default, feserver locates the server config file by
looking for a %q package in your Go workspaces (GOPATH).
You may use the -config flag to specify an alternate location.
`