srv.go 659 B

12345678910111213141516171819202122232425262728293031323334
  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("/", func(w http.ResponseWriter, r *http.Request) {
  19. Wildcard(w, r, s)
  20. })
  21. //running server
  22. fmt.Printf("Server running at http://%s:%s/\n", s.Host, s.Port)
  23. err := http.ListenAndServe(fmt.Sprintf("%s:%s", s.Host, s.Port), nil)
  24. if err != nil {
  25. fmt.Printf("Error starting server: %+v\n", err)
  26. }
  27. }