main.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "os/exec"
  7. )
  8. func buildSite() {
  9. fmt.Println("\n---buildSite---\nCompiling website...")
  10. out, err := exec.Command("hugo").Output()
  11. if err != nil {
  12. log.Fatal(err)
  13. }
  14. fmt.Printf("%s\nWebsite compiled!\n---buildSite---\n\n", out)
  15. }
  16. func git(file string) {
  17. //need to accommodate for username, email eventually
  18. cmdName := "git"
  19. gitAdd := []string{"add", file}
  20. outadd, err := exec.Command(cmdName, gitAdd...).Output()
  21. if err != nil {
  22. fmt.Printf("fatal error on git add\n")
  23. fmt.Println(err)
  24. }
  25. fmt.Printf("\n------git------\ngit add "+file+" %s\n", outadd)
  26. gitCommit := []string{"commit", "-m", "updating " + file + " from topiary"}
  27. outci, err := exec.Command(cmdName, gitCommit...).Output()
  28. if err != nil {
  29. fmt.Printf("fatal error on git commit\n")
  30. fmt.Println(err)
  31. }
  32. fmt.Printf("git commit "+file+" %s\n------git------\n", outci)
  33. }
  34. func main() {
  35. initConfig()
  36. buildSite()
  37. initAuth()
  38. // serve site output (`/public`) into `/`
  39. http.Handle("/", http.FileServer(http.Dir("./public")))
  40. // serve the topiary admin
  41. adminroot := getConfig("adminroot")
  42. admindir := getConfig("admindir")
  43. serveAdmin(adminroot, admindir)
  44. // start the server
  45. fmt.Println("Starting server on port 3000...")
  46. fmt.Println("Admin available at:", adminroot)
  47. err := http.ListenAndServe(":3000", nil)
  48. if err != nil {
  49. log.Fatal("ListenAndServe: ", err)
  50. }
  51. }