-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.go
84 lines (68 loc) · 1.96 KB
/
setup.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package activityserve
import (
"fmt"
"net/http"
"os"
"github.com/gologme/log"
"gopkg.in/ini.v1"
)
var slash = string(os.PathSeparator)
var baseURL = "http://example.com/"
var storage = "storage"
var userAgent = "activityserve"
var printer *log.Logger
const libName = "activityserve"
const version = "0.99"
var client = http.Client{}
// Setup sets our environment up
func Setup(configurationFile string, debug bool) *ini.File {
// read configuration file (config.ini)
if configurationFile == "" {
configurationFile = "config.ini"
}
cfg, err := ini.Load("config.ini")
if err != nil {
fmt.Printf("Fail to read file: %v", err)
os.Exit(1)
}
// Load base url from configuration file
baseURL = cfg.Section("general").Key("baseURL").String()
// check if it ends with a / and append one if not
if baseURL[len(baseURL)-1:] != "/" {
baseURL += "/"
}
// print it for our users
fmt.Println()
fmt.Println("Domain Name:", baseURL)
// Load storage location (only local filesystem supported for now) from config
storage = cfg.Section("general").Key("storage").String()
cwd, err := os.Getwd()
fmt.Println("Storage Location:", cwd+slash+storage)
fmt.Println()
SetupStorage(storage)
// Load user agent
userAgent = cfg.Section("general").Key("userAgent").String()
// I prefer long file so that I can click it in the terminal and open it
// in the editor above
log.SetFlags(log.Llongfile)
log.EnableLevel("warn")
// create a logger with levels but without prefixes for easier to read
// debug output
printer = log.New(os.Stdout, " ", 0)
if debug == true {
fmt.Println()
fmt.Println("debug mode on")
log.EnableLevel("info")
printer.EnableLevel("info")
}
return cfg
}
// SetupStorage creates storage
func SetupStorage(storage string) {
// prepare storage for foreign activities (activities we store that don't
// belong to us)
foreignDir := storage + slash + "foreign"
if _, err := os.Stat(foreignDir); os.IsNotExist(err) {
os.MkdirAll(foreignDir, 0755)
}
}