package main import ( "context" "errors" "log" "net/http" "strings" "github.com/rs/cors" "go.sour.is/pkg/env" "go.sour.is/pkg/lg" "go.sour.is/pkg/mux" "go.sour.is/pkg/service" "go.sour.is/pkg/slice" ) var _ = apps.Register(20, func(ctx context.Context, svc *service.Harness) error { s := &http.Server{} svc.Add(s) mux := mux.New() s.Handler = cors.AllowAll().Handler(mux) // print access requests s.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Println(r.Method, r.URL.Path) mux.ServeHTTP(w, r) }) s.Addr = env.Default("EV_HTTP", ":4088") if strings.HasPrefix(s.Addr, ":") { s.Addr = "[::]" + s.Addr } svc.OnStart(func(ctx context.Context) error { _, span := lg.Span(ctx) defer span.End() log.Print("Listen on ", s.Addr) span.AddEvent("begin listen and serve on " + s.Addr) mux.Add(slice.FilterType[interface{ RegisterHTTP(*http.ServeMux) }](svc.Services...)...) err := s.ListenAndServe() if errors.Is(err, http.ErrServerClosed) { return nil } return err }) svc.OnStop(s.Shutdown) return nil })