resource-scraper/internal/http/http.go

55 lines
1.1 KiB
Go

package http
import (
"log"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/cors"
"git.amok.space/yevhen/resource-scraper/helper/web"
)
/*type Server struct {
router *chi.Mux
}*/
/*type Routes struct {
Api *chi.Mux
}*/
/*type Handler func(w http.ResponseWriter, r *http.Request) error
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := h(w, r); err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
_, err := w.Write([]byte("bad"))
if err != nil {
log.Fatalf("Error starting HTTP server: %v", err)
}
}
}*/
func Run() {
port := "19576"
r := chi.NewRouter()
r.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"https://*", "http://*"},
AllowedMethods: []string{"GET", "POST"},
AllowedHeaders: []string{"*"},
ExposedHeaders: []string{"Link"},
AllowCredentials: false,
MaxAge: 300,
}))
r.Get("/", web.FallbackHandler)
log.Printf("Server starting on port %v", port)
err := http.ListenAndServe("localhost:"+port, r)
if err != nil {
log.Fatalf("Error starting HTTP server: %v", err)
}
}