github.com/bitcubate/cryptojournal@v1.2.5-0.20171102134152-f578b3d788ab/server.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 7 "github.com/fragmenta/server" 8 "github.com/fragmenta/server/config" 9 10 "github.com/bitcubate/cryptojournal/src/app" 11 ) 12 13 // Main entrypoint for the server which performs bootstrap, setup 14 // then runs the server. Most setup is delegated to the src/app pkg. 15 func main() { 16 17 // Bootstrap if required (no config file found). 18 if app.RequiresBootStrap() { 19 err := app.Bootstrap() 20 if err != nil { 21 fmt.Printf("Error bootstrapping server %s\n", err) 22 return 23 } 24 } 25 26 // Setup our server 27 server, err := SetupServer() 28 if err != nil { 29 fmt.Printf("server: error setting up %s\n", err) 30 return 31 } 32 33 // Inform user of server setup 34 server.Logf("#info Starting server in %s mode on port %d", server.Mode(), server.Port()) 35 36 // In production, server 37 if server.Production() { 38 39 // Redirect all :80 traffic to our canonical url on :443 40 server.StartRedirectAll(80, server.Config("root_url")) 41 42 // If in production, serve over tls with autocerts from let's encrypt 43 err = server.StartTLSAutocert(server.Config("autocert_email"), server.Config("autocert_domains")) 44 if err != nil { 45 server.Fatalf("Error starting server %s", err) 46 } 47 48 } else { 49 // In development just serve with http on local port 50 err = server.Start() 51 if err != nil { 52 server.Fatalf("Error starting server %s", err) 53 } 54 } 55 56 } 57 58 // SetupServer creates a new server, and delegates setup to the app pkg. 59 func SetupServer() (*server.Server, error) { 60 61 // Setup server 62 s, err := server.New() 63 if err != nil { 64 return nil, err 65 } 66 67 // Load the appropriate config 68 c := config.New() 69 err = c.Load("secrets/fragmenta.json") 70 if err != nil { 71 return nil, err 72 } 73 config.Current = c 74 75 // Check environment variable to see if we are in production mode 76 if os.Getenv("FRAG_ENV") == "production" { 77 config.Current.Mode = config.ModeProduction 78 } 79 80 // Call the app to perform additional setup 81 app.Setup() 82 83 return s, nil 84 }