2019-09-04 12:33:32 +03:00
|
|
|
package activityserve
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-09-10 11:21:39 +03:00
|
|
|
"os"
|
2019-09-04 12:33:32 +03:00
|
|
|
|
|
|
|
"github.com/gologme/log"
|
|
|
|
"gopkg.in/ini.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
var slash = string(os.PathSeparator)
|
|
|
|
var baseURL = "http://example.com/"
|
|
|
|
var storage = "storage"
|
|
|
|
var userAgent = "activityserve"
|
2019-09-10 11:21:39 +03:00
|
|
|
var printer *log.Logger
|
2019-09-04 12:33:32 +03:00
|
|
|
|
|
|
|
const libName = "activityserve"
|
|
|
|
const version = "0.99"
|
|
|
|
|
|
|
|
var client = http.Client{}
|
|
|
|
|
|
|
|
// Setup sets our environment up
|
2019-09-20 16:21:21 +03:00
|
|
|
func Setup(configurationFile string, debug bool) *ini.File {
|
2019-09-04 12:33:32 +03:00
|
|
|
// 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.SetFlags(log.LstdFlags | log.Lshortfile)
|
|
|
|
log.EnableLevel("warn")
|
|
|
|
// create a logger with levels but without prefixes for easier to read
|
|
|
|
// debug output
|
2019-09-10 11:21:39 +03:00
|
|
|
printer = log.New(os.Stdout, " ", 0)
|
2019-09-04 12:33:32 +03:00
|
|
|
|
|
|
|
if debug == true {
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("debug mode on")
|
|
|
|
log.EnableLevel("info")
|
|
|
|
printer.EnableLevel("info")
|
|
|
|
}
|
2019-09-20 16:21:21 +03:00
|
|
|
|
|
|
|
return cfg
|
2019-09-04 12:33:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|