func init() { logFile, err := os.OpenFile("systic.log", os.O_APPEND|os.O_WRONLY, 0644) if err != nil { log.Printf("Couldn't open systic.log. Logging to stdout.\n") } else { log.SetOutput(logFile) } Config, err := config.LoadConfig(filepath.Join("config", "app.conf")) if err != nil { log.Fatalf("Couldn't read app.conf: %s\n", err) } appName = Config.AppName if len(Config.Secret) < 20 { log.Fatalln("That secret in app.conf is not long enough! Make it hella long!") } Jar = sessions.NewCookieStore([]byte(Config.Secret)) }
func buildProject(args []string) { // If intermediate directory already exists, remove it. // We're going to recreate it anyway. This is just easier. if err := os.RemoveAll(intermediateDir); err != nil { fatalError(err, "Couldn't remove existing intermediate directory!") } // Same for the build directory. if err := os.RemoveAll(buildDir); err != nil { fatalError(err, "Couldn't remove existing build directory!") } // Create the directory structure we need in the intermediate directory. for _, dirPath := range ids { os.MkdirAll(dirPath, 0744) } // Parse app.conf and apply its settings. appConfig, err := config.LoadConfig(join(configDir, appConf)) if err != nil { fatalError(err, "Error reading app.conf:") } // Create initServer.go, which contains the generated code that sets up routes and stuff. serverSrcFile, err := os.Create(join(intermediateDir, serverSrc)) if err != nil { fatalError(err, "Error creating file for generated server init:") } transformer.GenerateServerMain(serverSrcFile, join(configDir, routingConf), appConfig) serverSrcFile.Close() // Get a list of the controllers... controllerFiles, err := ioutil.ReadDir(controllersDir) if err != nil { fatalErrorf(err, "Error reading contents of %v:", controllersDir) } // ...and transform them and create their packages in intermediate. for _, controllerFileInfo := range controllerFiles { controllerFileName := controllerFileInfo.Name() if !strings.HasSuffix(controllerFileName, controllerExtension) { continue } controllerName := controllerFileName[0 : len(controllerFileName)-len(".controller")] controllerFilePath := join(controllersDir, controllerFileName) transformedControllerFileName := fmt.Sprintf("%s.go", controllerName) transformedControllerDirectory := join(intermediateDir, controllersDir, controllerName) transformedControllerFilePath := join(transformedControllerDirectory, transformedControllerFileName) err = os.Mkdir(transformedControllerDirectory, 0744) if err != nil { fatalErrorf(err, "Error creating package directory for %v:", controllerName) } transformedControllerFile, err := os.Create(transformedControllerFilePath) if err != nil { fatalErrorf(err, "Error transforming %v:", controllerFileName) } transformer.TransformController(transformedControllerFile, controllerFilePath) transformedControllerFile.Close() } // Copy over server.go, which contains the main() for the server. // This contains no generated code; it's part of a systic project. // skeleton includes a basic server.go which should be fine for most normal web applications. // Users may want to modify server.go to start other stuff like a websocket listener on another goroutine. err = copyFile(mainSrc, join(intermediateDir, mainSrc)) if err != nil { fatalError(err, "Couldn't copy server.go to intermediate! Is it in your project root?") } // Make the build directory, which is where all the files needed for deployment end up. mkdirOrDie(buildDir, "Error making build directory.") // Copy over the view templates... mkdirOrDie(join(buildDir, viewsDir), "Couldn't create the views directory for deployment.") viewFiles, err := ioutil.ReadDir(viewsDir) if err != nil { fatalErrorf(err, "Error reading contents of %v:", viewsDir) } for _, viewFileInfo := range viewFiles { viewFileName := viewFileInfo.Name() if !strings.HasSuffix(viewFileName, viewExtension) { continue } src := join(viewsDir, viewFileName) dst := join(buildDir, viewsDir, viewFileName) copyFile(src, dst) } // Copy over the config... mkdirOrDie(join(buildDir, configDir), "Couldn't create config directory for deployment.") src := join(configDir, appConf) dst := join(buildDir, configDir, appConf) copyFile(src, dst) appname := appConfig.AppName // F*****g Windows 98... if runtime.GOOS == "windows" { appname = fmt.Sprintf("%v.exe", appname) } // Compile intermediate and create the executable. outputFilePath := join(buildDir, appname) cmdArgs := []string{"build", "-o", outputFilePath, "-v", strings.Join([]string{".", intermediateDir}, string(os.PathSeparator))} log.Printf("%v\n", strings.Join(cmdArgs, " ")) cmd := exec.Command("go", cmdArgs...) output, err := cmd.CombinedOutput() os.Stdout.Write(output) if err != nil { fatalError(err, "Build did not complete successfully.") } fmt.Println("No errors. Build successful?") }
func copyProject(srcPath string, srcInfo os.FileInfo, err error) error { // Get the path of this file relative to the root of the project. relativeSrcPath := strings.TrimLeft(srcPath[len(baseProjectDir):], string(os.PathSeparator)) if len(relativeSrcPath) == 0 { return nil } absDestPath := path.Join(newProjectDir, relativeSrcPath) _, err = os.Stat(absDestPath) // If it's a directory, try to create it. if srcInfo.IsDir() { err := os.Mkdir(absDestPath, 0744) if err != nil { if !os.IsExist(err) { errorf("Failed to create directory: %s\n", err) } } return nil } // Need to do this double negation; we don't get an error if it DOES exist. if !os.IsNotExist(err) { // Skip if it's a README.md that we already copied... if strings.Contains(relativeSrcPath, "README.md") { return nil } fmt.Printf("Overwrite %v? [Y/n] ", absDestPath) var choice string fmt.Scan(&choice) choice = strings.ToLower(choice[0:1]) fmt.Print("\n") if choice == "n" { return nil } } /* Process app.conf. Set appname to the name of the base directory of the new project. Set secret to a randomly generated value. */ if strings.Contains(relativeSrcPath, "app.conf") { appConfig, err := config.LoadConfig(srcPath) if err != nil { errorf("Couldn't read app.conf: %s\n", err) } appConfig.AppName = filepath.Base(newProjectDir) appConfig.Secret = generateSecret() configBytes, err := json.MarshalIndent(appConfig, "", "\t") if err != nil { errorf("Error marshalling app.conf: %s\n", err) } err = ioutil.WriteFile(absDestPath, configBytes, 0644) if err != nil { errorf("Error writing %s: %s\n", absDestPath, err) } return nil } // Anything else is just a plain old copy... srcBytes, err := ioutil.ReadFile(srcPath) if err != nil { errorf("Failed to read file: %s\n", err) } err = ioutil.WriteFile(absDestPath, srcBytes, 0600) if err != nil { errorf("Failed to write file: %s\n", err) } return nil }