server.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Author: Matthew Shiel
  2. package main
  3. import (
  4. "embed"
  5. "fmt"
  6. "io/fs"
  7. "net/http"
  8. "git.riomhaire.com/gremlin/elizaservice/eliza"
  9. "github.com/jaffee/commandeer"
  10. )
  11. //go:embed web/*
  12. var templates embed.FS
  13. type Main struct {
  14. BotName string `help:"What This Bot is Called."`
  15. Port int `help:"What port bot is listening too"`
  16. }
  17. func NewMain() *Main {
  18. return &Main{
  19. BotName: "Eliza",
  20. Port: 8070,
  21. }
  22. }
  23. func inputhandler(w http.ResponseWriter, r *http.Request) {
  24. // Extract question from GET request
  25. question := r.URL.Query().Get("value")
  26. // Return Eliza's response's so long as the user doesn't give a quit statement
  27. answer := eliza.ReplyTo(question)
  28. // Return Eliza's answer
  29. fmt.Fprintf(w, "%s", answer)
  30. }
  31. func (m *Main) Run() error {
  32. fmt.Println(`
  33. ####### # ### ####### #
  34. # # # # # #
  35. # # # # # #
  36. ##### # # # # #
  37. # # # # #######
  38. # # # # # #
  39. ####### ####### ### ####### # #
  40. `)
  41. fmt.Println()
  42. // create file server handler - but need to strip off prefix
  43. fsys, err := fs.Sub(templates, "web")
  44. if err != nil {
  45. panic(err)
  46. }
  47. webroot := http.FileServer(http.FS(fsys))
  48. http.Handle("/", webroot)
  49. // Handles the user input and return of Eliza's answers
  50. http.HandleFunc("/user-input", inputhandler)
  51. fmt.Printf(" [*] %s ... Awaiting Conversations on port %d\n", m.BotName, m.Port)
  52. http.ListenAndServe(fmt.Sprintf(":%d", m.Port), nil)
  53. return nil
  54. }
  55. func main() {
  56. err := commandeer.Run(NewMain())
  57. if err != nil {
  58. fmt.Println(err)
  59. }
  60. }