A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

184 lines
4.4 KiB

  1. /*
  2. * Copyright © 2018-2021 Musing Studio LLC.
  3. *
  4. * This file is part of WriteFreely.
  5. *
  6. * WriteFreely is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. package main
  11. import (
  12. "fmt"
  13. "os"
  14. "strings"
  15. "github.com/gorilla/mux"
  16. "github.com/urfave/cli/v2"
  17. "github.com/writeas/web-core/log"
  18. "github.com/writefreely/writefreely"
  19. )
  20. func main() {
  21. cli.VersionPrinter = func(c *cli.Context) {
  22. fmt.Printf("%s\n", c.App.Version)
  23. }
  24. app := &cli.App{
  25. Name: "WriteFreely",
  26. Usage: "A beautifully pared-down blogging platform",
  27. Version: writefreely.FormatVersion(),
  28. Action: legacyActions, // legacy due to use of flags for switching actions
  29. Flags: []cli.Flag{
  30. &cli.BoolFlag{
  31. Name: "create-config",
  32. Value: false,
  33. Usage: "Generate a basic configuration",
  34. Hidden: true,
  35. },
  36. &cli.BoolFlag{
  37. Name: "config",
  38. Value: false,
  39. Usage: "Interactive configuration process",
  40. Hidden: true,
  41. },
  42. &cli.StringFlag{
  43. Name: "sections",
  44. Value: "server db app",
  45. Usage: "Which sections of the configuration to go through (requires --config)\n" +
  46. "valid values are any combination of 'server', 'db' and 'app' \n" +
  47. "example: writefreely --config --sections \"db app\"",
  48. Hidden: true,
  49. },
  50. &cli.BoolFlag{
  51. Name: "gen-keys",
  52. Value: false,
  53. Usage: "Generate encryption and authentication keys",
  54. Hidden: true,
  55. },
  56. &cli.BoolFlag{
  57. Name: "init-db",
  58. Value: false,
  59. Usage: "Initialize app database",
  60. Hidden: true,
  61. },
  62. &cli.BoolFlag{
  63. Name: "migrate",
  64. Value: false,
  65. Usage: "Migrate the database",
  66. Hidden: true,
  67. },
  68. &cli.StringFlag{
  69. Name: "create-admin",
  70. Usage: "Create an admin with the given username:password",
  71. Hidden: true,
  72. },
  73. &cli.StringFlag{
  74. Name: "create-user",
  75. Usage: "Create a regular user with the given username:password",
  76. Hidden: true,
  77. },
  78. &cli.StringFlag{
  79. Name: "delete-user",
  80. Usage: "Delete a user with the given username",
  81. Hidden: true,
  82. },
  83. &cli.StringFlag{
  84. Name: "reset-pass",
  85. Usage: "Reset the given user's password",
  86. Hidden: true,
  87. },
  88. }, // legacy flags (set to hidden to eventually switch to bash-complete compatible format)
  89. }
  90. defaultFlags := []cli.Flag{
  91. &cli.StringFlag{
  92. Name: "c",
  93. Value: "config.ini",
  94. Usage: "Load configuration from `FILE`",
  95. },
  96. &cli.BoolFlag{
  97. Name: "debug",
  98. Value: false,
  99. Usage: "Enables debug logging",
  100. },
  101. }
  102. app.Flags = append(app.Flags, defaultFlags...)
  103. app.Commands = []*cli.Command{
  104. &cmdUser,
  105. &cmdDB,
  106. &cmdConfig,
  107. &cmdKeys,
  108. &cmdServe,
  109. }
  110. err := app.Run(os.Args)
  111. if err != nil {
  112. log.Error(err.Error())
  113. os.Exit(1)
  114. }
  115. }
  116. func legacyActions(c *cli.Context) error {
  117. app := writefreely.NewApp(c.String("c"))
  118. switch true {
  119. case c.IsSet("create-config"):
  120. return writefreely.CreateConfig(app)
  121. case c.IsSet("config"):
  122. writefreely.DoConfig(app, c.String("sections"))
  123. return nil
  124. case c.IsSet("gen-keys"):
  125. return writefreely.GenerateKeyFiles(app)
  126. case c.IsSet("init-db"):
  127. return writefreely.CreateSchema(app)
  128. case c.IsSet("migrate"):
  129. return writefreely.Migrate(app)
  130. case c.IsSet("create-admin"):
  131. username, password, err := parseCredentials(c.String("create-admin"))
  132. if err != nil {
  133. return err
  134. }
  135. return writefreely.CreateUser(app, username, password, true)
  136. case c.IsSet("create-user"):
  137. username, password, err := parseCredentials(c.String("create-user"))
  138. if err != nil {
  139. return err
  140. }
  141. return writefreely.CreateUser(app, username, password, false)
  142. case c.IsSet("delete-user"):
  143. return writefreely.DoDeleteAccount(app, c.String("delete-user"))
  144. case c.IsSet("reset-pass"):
  145. return writefreely.ResetPassword(app, c.String("reset-pass"))
  146. }
  147. // Initialize the application
  148. var err error
  149. log.Info("Starting %s...", writefreely.FormatVersion())
  150. app, err = writefreely.Initialize(app, c.Bool("debug"))
  151. if err != nil {
  152. return err
  153. }
  154. // Set app routes
  155. r := mux.NewRouter()
  156. writefreely.InitRoutes(app, r)
  157. app.InitStaticRoutes(r)
  158. // Serve the application
  159. writefreely.Serve(app, r)
  160. return nil
  161. }
  162. func parseCredentials(credentialString string) (string, string, error) {
  163. creds := strings.Split(credentialString, ":")
  164. if len(creds) != 2 {
  165. return "", "", fmt.Errorf("invalid format for passed credentials, must be username:password")
  166. }
  167. return creds[0], creds[1], nil
  168. }