package server import ( "encoding/json" "fmt" "git.sa.vin/any-remark/backend/datastore" "git.sa.vin/any-remark/backend/types" "github.com/dyatlov/go-opengraph/opengraph" "net/http" ) type Server interface { OpenGraphHandler(w http.ResponseWriter, r *http.Request) ShortLinkHandler(w http.ResponseWriter, r *http.Request) ShortenLinkHandler(w http.ResponseWriter, r *http.Request) AddComment(w http.ResponseWriter, r *http.Request) ListComments(w http.ResponseWriter, r *http.Request) } type server struct { mapper datastore.Mapper } func NewServer(mapper datastore.Mapper) Server { // Empty function for NewServer return &server{ mapper: mapper, } } func (s *server) OpenGraphHandler(w http.ResponseWriter, r *http.Request) { // get the url from the query string queryParams := r.URL.Query() url := queryParams.Get("url") og := opengraph.NewOpenGraph() if url == "" { http.Error(w, "url parameter is required", http.StatusBadRequest) return } // check if our db has the url already // get the opengraph data from the db ogString, err := s.mapper.GetOpenGraphData(url) if err != nil || ogString == "" { // if it doesn't, fetch the opengraph data from the url // TODO: fetch the url require that it responds with html resp, err := http.Get(url) // TODO: use an external proxy to fetch the url if err != nil { http.Error(w, fmt.Sprintf("error fetching url: %s", err), http.StatusInternalServerError) return } defer resp.Body.Close() err = og.ProcessHTML(resp.Body) if err != nil { // TODO: decide what to do if the given site does not respond with html or doesn't exist etc. http.Error(w, fmt.Sprintf("error processing html: %s", err), http.StatusInternalServerError) return } // save the opengraph data to the db err = s.mapper.SaveOpenGraphData(url, og.String()) if err != nil { http.Error(w, fmt.Sprintf("error saving opengraph data: %s", err), http.StatusInternalServerError) return } // return the opengraph data w.Header().Set("Content-Type", "application/json") _, err = w.Write([]byte(og.String())) if err != nil { http.Error(w, fmt.Sprintf("error writing opengraph data: %s", err), http.StatusInternalServerError) return } return } // return the opengraph data w.Header().Set("Content-Type", "application/json") _, err = w.Write([]byte(ogString)) if err != nil { http.Error(w, fmt.Sprintf("error writing opengraph data: %s", err), http.StatusInternalServerError) return } } func (s *server) ShortLinkHandler(w http.ResponseWriter, r *http.Request) { // Empty handler for /x/{id} } func (s *server) ShortenLinkHandler(w http.ResponseWriter, r *http.Request) { // Empty handler for /api/v1/shorten } func (s *server) AddComment(w http.ResponseWriter, r *http.Request) { var req types.AddCommentRequest // unmarshal the request body into the req struct err := json.NewDecoder(r.Body).Decode(&req) if err != nil { http.Error(w, fmt.Sprintf("error decoding request body: %s", err), http.StatusBadRequest) return } err = s.mapper.AddComment(req) if err != nil { http.Error(w, fmt.Sprintf("error adding comment: %s", err), http.StatusInternalServerError) return } w.WriteHeader(http.StatusCreated) } func (s *server) ListComments(w http.ResponseWriter, r *http.Request) { comments, err := s.mapper.GetComments(r.URL.Query().Get("url")) if err != nil { http.Error(w, fmt.Sprintf("error getting comments: %s", err), http.StatusInternalServerError) return } // TODO: add pagination // return the comments w.Header().Set("Content-Type", "application/json") err = json.NewEncoder(w).Encode(comments) if err != nil { http.Error(w, fmt.Sprintf("error encoding comments: %s", err), http.StatusInternalServerError) return } } // CorsMiddleware is a middleware that allows CORS func CorsMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // allow all origins w.Header().Set("Access-Control-Allow-Origin", "*") // allow all headers w.Header().Set("Access-Control-Allow-Headers", "*") // allow all methods w.Header().Set("Access-Control-Allow-Methods", "*") // allow credentials w.Header().Set("Access-Control-Allow-Credentials", "true") // handle pre-flight requests if r.Method == http.MethodOptions { w.WriteHeader(http.StatusOK) return } // call the next handler next.ServeHTTP(w, r) }) }