srv.go 925 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package server
  2. import (
  3. "fmt"
  4. "net/http"
  5. "git.linuxit.ro/turos.robert/mynotes/lib/inout"
  6. )
  7. func NewServer(filename string) *Server {
  8. srv := Server{}
  9. err := inout.FileToObj(filename, &srv)
  10. if err != nil {
  11. srv.Err = err
  12. }
  13. return &srv
  14. }
  15. func (s *Server) Run() {
  16. fmt.Println(s.Port, s.Host)
  17. // adding endpoints to the server
  18. http.HandleFunc("/", Wildcard)
  19. http.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
  20. API(w, r)
  21. })
  22. http.HandleFunc("/notes/", func(w http.ResponseWriter, r *http.Request) {
  23. note := r.URL.Query().Get("note")
  24. Notes(w, r, note)
  25. })
  26. http.HandleFunc("/list_notes/", func(w http.ResponseWriter, r *http.Request) {
  27. ListNotes(w, r)
  28. })
  29. //running server
  30. fmt.Printf("Server running at http://%s:%s/\n", s.Host, s.Port)
  31. err := http.ListenAndServe(fmt.Sprintf("%s:%s", s.Host, s.Port), nil)
  32. if err != nil {
  33. fmt.Printf("Error starting server: %+v\n", err)
  34. }
  35. }