mirror of
https://github.com/restic/rest-server.git
synced 2025-12-07 09:36:13 -08:00
Move main function into separate package (closes #12)
This commit is contained in:
committed by
Zlatko Čalušić
parent
07b6d5facf
commit
65152c7bf5
90
cmd/rest-server/main.go
Normal file
90
cmd/rest-server/main.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"runtime/pprof"
|
||||
|
||||
restserver "github.com/restic/rest-server"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// cmdRoot is the base command when no other command has been specified.
|
||||
var cmdRoot = &cobra.Command{
|
||||
Use: "rest-server",
|
||||
Short: "Run a REST server for use with restic",
|
||||
SilenceErrors: true,
|
||||
SilenceUsage: true,
|
||||
RunE: runRoot,
|
||||
}
|
||||
|
||||
func init() {
|
||||
flags := cmdRoot.Flags()
|
||||
flags.StringVar(&restserver.Config.CPUProfile, "cpuprofile", restserver.Config.CPUProfile, "write CPU profile to file")
|
||||
flags.BoolVar(&restserver.Config.Debug, "debug", restserver.Config.Debug, "output debug messages")
|
||||
flags.StringVar(&restserver.Config.Listen, "listen", restserver.Config.Listen, "listen address")
|
||||
flags.StringVar(&restserver.Config.Log, "log", restserver.Config.Log, "log HTTP requests in the combined log format")
|
||||
flags.StringVar(&restserver.Config.Path, "path", restserver.Config.Path, "data directory")
|
||||
flags.BoolVar(&restserver.Config.TLS, "tls", restserver.Config.TLS, "turn on TLS support")
|
||||
}
|
||||
|
||||
var version = "manually"
|
||||
|
||||
func runRoot(cmd *cobra.Command, args []string) error {
|
||||
log.SetFlags(0)
|
||||
|
||||
log.Printf("rest-server %s compiled with %v on %v/%v\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
|
||||
log.Printf("Data directory: %s", restserver.Config.Path)
|
||||
|
||||
if restserver.Config.CPUProfile != "" {
|
||||
f, err := os.Create(restserver.Config.CPUProfile)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := pprof.StartCPUProfile(f); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println("CPU profiling enabled")
|
||||
defer pprof.StopCPUProfile()
|
||||
}
|
||||
|
||||
mux := restserver.NewMux()
|
||||
|
||||
var handler http.Handler
|
||||
htpasswdFile, err := restserver.NewHtpasswdFromFile(filepath.Join(restserver.Config.Path, ".htpasswd"))
|
||||
if err != nil {
|
||||
handler = mux
|
||||
log.Println("Authentication disabled")
|
||||
} else {
|
||||
handler = restserver.AuthHandler(htpasswdFile, mux)
|
||||
log.Println("Authentication enabled")
|
||||
}
|
||||
|
||||
if !restserver.Config.TLS {
|
||||
log.Printf("Starting server on %s\n", restserver.Config.Listen)
|
||||
err = http.ListenAndServe(restserver.Config.Listen, handler)
|
||||
} else {
|
||||
privateKey := filepath.Join(restserver.Config.Path, "private_key")
|
||||
publicKey := filepath.Join(restserver.Config.Path, "public_key")
|
||||
log.Println("TLS enabled")
|
||||
log.Printf("Private key: %s", privateKey)
|
||||
log.Printf("Public key: %s", publicKey)
|
||||
log.Printf("Starting server on %s\n", restserver.Config.Listen)
|
||||
err = http.ListenAndServeTLS(restserver.Config.Listen, publicKey, privateKey, handler)
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := cmdRoot.Execute(); err != nil {
|
||||
log.Fatalf("error: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user