package appserver import ( "errors" "github.com/gorilla/context" "github.com/gorilla/handlers" "github.com/gorilla/mux" "gitlab.com/arkadooti.sarkar/go-boilerplate/core/appcontext" "gitlab.com/arkadooti.sarkar/go-boilerplate/core/log" "go.elastic.co/apm/module/apmgorilla" "net/http" "net/http/pprof" ) var ( AppName string ) func (s *server) Start(ctx appcontext.AppContext) { allowedOrigins := handlers.AllowedOrigins([]string{"*"}) // Allowing all origin as of now allowedHeaders := handlers.AllowedHeaders([]string{ "Accept", "Content-Type", "contentType", "Content-Length", "Accept-Encoding", "Client-Security-Token", "X-CSRF-Token", "X-Auth-Token", "processData", "Authorization", "Access-Control-Request-Headers", "Access-Control-Request-Method", "Connection", "Host", "Origin", "User-Agent", "Referer", "Cache-Control", "X-header", "X-Requested-With", "timezone", "locale", "gzip-compress", "task", "access_token", "application", }) allowedMethods := handlers.AllowedMethods([]string{ "POST", "GET", "DELETE", "PUT", "PATCH", "OPTIONS"}) allowCredential := handlers.AllowCredentials() serverHandler := handlers.CORS( allowedHeaders, allowedMethods, allowedOrigins, allowCredential)( context.ClearHandler( s.newRouter(s.subRoute), ), ) log.GenericInfo(ctx, "Starting Server", log.FieldsMap{ "Port": s.port, "SubRoute": s.subRoute, "App": AppName, }) err := http.ListenAndServe(":"+s.port, serverHandler) if err != nil { log.GenericError(ctx, errors.New("failed to start appserver"), log.FieldsMap{ "Port": s.port, "SubRoute": s.subRoute, "App": AppName, }) return } } // Handles all incoming request who matches registered routes against the request. func (s *server) newRouter(subRoute string) *mux.Router { muxRouter := mux.NewRouter().StrictSlash(true) muxRouter.HandleFunc(subRoute+"/debug/pprof", pprof.Index) muxRouter.HandleFunc(subRoute+"/debug/pprof/cmdline", pprof.Cmdline) muxRouter.HandleFunc(subRoute+"/debug/pprof/profile", pprof.Profile) muxRouter.HandleFunc(subRoute+"/debug/pprof/symbol", pprof.Symbol) muxRouter.HandleFunc(subRoute+"/debug/pprof/trace", pprof.Trace) muxRouter.Handle(subRoute+"/debug/pprof/goroutine", pprof.Handler("goroutine")) muxRouter.Handle(subRoute+"/debug/pprof/heap", pprof.Handler("heap")) muxRouter.Handle(subRoute+"/debug/pprof/thread/create", pprof.Handler("threadcreate")) muxRouter.Handle(subRoute+"/debug/pprof/block", pprof.Handler("block")) muxRouter.Use(SetTraceID, apmgorilla.Middleware()) for _, r := range s.routes { muxRouter.HandleFunc(subRoute+r.Pattern, r.HandlerFunc).Methods(r.Method) } return muxRouter }