Captain Hook - Web hook server
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.

171 lines
3.7 KiB

  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. // "sort"
  9. "strings"
  10. _ "github.com/go-sql-driver/mysql"
  11. )
  12. var db *sql.DB
  13. //Show the index
  14. func index(w http.ResponseWriter, r *http.Request) {
  15. log.Println(r.URL.String())
  16. if r.URL.String() == "/" {
  17. http.ServeFile(w, r, "../html/index.html")
  18. } else {
  19. http.ServeFile(w, r, "../html/404.html")
  20. }
  21. }
  22. //Run a particular hook (will probably come up with a better solution)
  23. func runHook(w http.ResponseWriter, r *http.Request, hook Hook) {
  24. fmt.Fprintln(w, hook.Name)
  25. }
  26. //Handles all of the hooks
  27. func hookHandler(w http.ResponseWriter, r *http.Request) {
  28. log.Println(r.URL.String())
  29. name := r.URL.String()[len("/hook/"):]
  30. row := db.QueryRow("SELECT EXISTS(SELECT id FROM HOOKS WHERE HOOKS.id = ?)", name)
  31. var res int
  32. row.Scan(&res)
  33. if res == 0 {
  34. http.ServeFile(w, r, "../html/404.html")
  35. return
  36. }
  37. hook := Hook{
  38. Name: name,
  39. params: nil,
  40. actions: nil,
  41. }
  42. runHook(w, r, hook)
  43. }
  44. //Shows all of the hooks
  45. func showHooks(w http.ResponseWriter, r *http.Request) {
  46. log.Println(r.URL.String())
  47. if r.URL.String() != "/hooks/" {
  48. http.ServeFile(w, r, "../html/404.html")
  49. return
  50. }
  51. rows, err := db.Query("SELECT ID FROM HOOKS")
  52. if err != nil {
  53. log.Fatal(err)
  54. }
  55. defer rows.Close()
  56. hookNames := ""
  57. var name string
  58. for rows.Next() {
  59. err = rows.Scan(&name)
  60. if err != nil {
  61. log.Fatal(err)
  62. }
  63. hookNames += name + "\n"
  64. }
  65. fmt.Fprint(w, hookNames)
  66. }
  67. //Creates a new hook
  68. func createHook(w http.ResponseWriter, r *http.Request) {
  69. log.Println(r.URL.String())
  70. //Doesn't actually work yet
  71. if r.URL.String() == "/hook/create/" {
  72. http.ServeFile(w, r, "../html/create.html")
  73. } else {
  74. //Create a hook
  75. name := r.URL.String()[len("/hook/create/"):]
  76. statement, err := db.Prepare("INSERT IGNORE INTO HOOKS(id) VALUES(?)")
  77. if err != nil {
  78. log.Fatal(err)
  79. fmt.Fprintln(w, err)
  80. return
  81. }
  82. defer statement.Close()
  83. statement.Exec(name)
  84. fmt.Fprintln(w, name)
  85. }
  86. }
  87. //Deletes all hooks
  88. func deleteHook(w http.ResponseWriter, r *http.Request) {
  89. log.Println(r.URL.String())
  90. name := r.URL.String()[len("/hook/delete/"):]
  91. statement, err := db.Prepare("DELETE FROM HOOKS where ID=(?)")
  92. if err != nil {
  93. log.Println(err)
  94. fmt.Fprintln(w, err)
  95. return
  96. }
  97. defer statement.Close()
  98. res, err := statement.Exec(name)
  99. if err != nil {
  100. log.Println(err)
  101. fmt.Fprintln(w, err)
  102. return
  103. }
  104. n, err := res.RowsAffected()
  105. if err != nil {
  106. log.Println(err)
  107. fmt.Fprintln(w, err)
  108. return
  109. }
  110. if n == 0 {
  111. http.ServeFile(w, r, "../html/404.html")
  112. return
  113. }
  114. fmt.Fprintln(w, name)
  115. // log.Println(cap(Hooks))
  116. // //Clear hooks (this is perfectly safe to do)
  117. // Hooks = nil
  118. }
  119. func main() {
  120. //Get user credentials
  121. password, err := ioutil.ReadFile(".password")
  122. if err != nil {
  123. log.Fatal(err)
  124. }
  125. connectString := strings.TrimSpace(string(password))
  126. //Open the db and initialize the table if it doesn't exist
  127. db, err = sql.Open("mysql", connectString + "@tcp(localhost:3306)/captain")
  128. if err != nil {
  129. log.Fatal(err)
  130. }
  131. defer db.Close()
  132. var statement *sql.Stmt
  133. //// DEBUG: Making sure table is good
  134. // statement, err = db.Prepare("DROP TABLE IF EXISTS HOOKS")
  135. // if err != nil {
  136. // log.Fatal(err)
  137. // }
  138. // statement.Exec()
  139. statement, err = db.Prepare(`CREATE TABLE IF NOT EXISTS HOOKS(
  140. ID varchar(64) PRIMARY KEY
  141. )`)
  142. if err != nil {
  143. log.Fatal(err)
  144. }
  145. statement.Exec()
  146. //Handle hooks
  147. http.HandleFunc("/hook/delete/", deleteHook)
  148. http.HandleFunc("/hook/create/", createHook)
  149. http.HandleFunc("/hook/", hookHandler)
  150. http.HandleFunc("/hooks/", showHooks)
  151. http.HandleFunc("/", index)
  152. //Start the server
  153. log.Println("Starting server")
  154. log.Fatal(http.ListenAndServe("127.0.0.1:8000", nil))
  155. }