123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- /*
- * Rufs - Remote Union File System
- *
- * Copyright 2017 Matthias Ladkau. All rights reserved.
- *
- * This Source Code Form is subject to the terms of the MIT
- * License, If a copy of the MIT License was not distributed with this
- * file, You can obtain one at https://opensource.org/licenses/MIT.
- */
- package main
- import (
- "flag"
- "fmt"
- "os"
- "os/signal"
- "path/filepath"
- "sync"
- "syscall"
- "devt.de/krotik/common/datautil"
- "devt.de/krotik/common/errorutil"
- "devt.de/krotik/common/fileutil"
- "devt.de/krotik/rufs"
- "devt.de/krotik/rufs/config"
- )
- /*
- DefaultServerConfigFile is the default config file when running in server mode
- */
- const DefaultServerConfigFile = "rufs.server.json"
- /*
- serverCli handles the server command line.
- */
- func serverCli() error {
- var err error
- serverConfigFile := flag.String("config", DefaultServerConfigFile, "Server configuration file")
- secretFile, certDir := commonCliOptions()
- showHelp := flag.Bool("help", false, "Show this help message")
- flag.Usage = func() {
- fmt.Println()
- fmt.Println(fmt.Sprintf("Usage of %s server [options]", os.Args[0]))
- fmt.Println()
- flag.PrintDefaults()
- fmt.Println()
- fmt.Println("The server will automatically create a default config file and")
- fmt.Println("default directories if nothing is specified.")
- fmt.Println()
- }
- flag.CommandLine.Parse(os.Args[2:])
- if *showHelp {
- flag.Usage()
- return nil
- }
- // Load secret and ssl certificate
- secret, cert, err := loadSecretAndCert(*secretFile, *certDir)
- if err == nil {
- // Load configuration
- var cfg map[string]interface{}
- defaultConfig := datautil.MergeMaps(config.DefaultBranchExportConfig)
- delete(defaultConfig, config.BranchSecret)
- // Set environment specific values for default config
- if ip, lerr := externalIP(); lerr == nil {
- defaultConfig[config.BranchName] = ip
- defaultConfig[config.RPCHost] = ip
- }
- cfg, err = fileutil.LoadConfig(*serverConfigFile, defaultConfig)
- errorutil.AssertOk(err)
- cfg[config.BranchSecret] = secret
- fmt.Println(fmt.Sprintf("Using config: %s", *serverConfigFile))
- // Ensure the local shared folder actually exists
- if ok, _ := fileutil.PathExists(cfg[config.LocalFolder].(string)); !ok {
- os.MkdirAll(cfg[config.LocalFolder].(string), 0777)
- }
- absLocalFolder, _ := filepath.Abs(cfg[config.LocalFolder].(string))
- fmt.Println(fmt.Sprintf("Exporting folder: %s", absLocalFolder))
- // We got everything together let's start
- var branch *rufs.Branch
- if branch, err = rufs.NewBranch(cfg, cert); err == nil {
- // Attach SIGINT handler - on unix and windows this is send
- // when the user presses ^C (Control-C).
- sigchan := make(chan os.Signal)
- signal.Notify(sigchan, syscall.SIGINT)
- // Create a wait group to wait for the os signal
- wg := sync.WaitGroup{}
- // Kick off a polling thread which waits for the signal
- go func() {
- for true {
- signal := <-sigchan
- if signal == syscall.SIGINT {
- // Shutdown the branch
- branch.Shutdown()
- break
- }
- }
- // Done waiting main thread can exit
- wg.Done()
- }()
- // Suspend main thread until branch is shutdown
- wg.Add(1)
- wg.Wait()
- }
- }
- return err
- }
|