MCPcopy
hub / github.com/knadh/listmonk / initHTTPServer

Function initHTTPServer

cmd/init.go:921–987  ·  view source on GitHub ↗

initHTTPServer sets up and runs the app's main HTTP server and blocks forever.

(cfg *Config, urlCfg *UrlConfig, i *i18n.I18n, fs stuffbin.FileSystem, app *App)

Source from the content-addressed store, hash-verified

919
920// initHTTPServer sets up and runs the app's main HTTP server and blocks forever.
921func initHTTPServer(cfg *Config, urlCfg *UrlConfig, i *i18n.I18n, fs stuffbin.FileSystem, app *App) *echo.Echo {
922 // Initialize the HTTP server.
923 var srv = echo.New()
924 srv.HideBanner = true
925
926 // Register app (*App) to be injected into all HTTP handlers.
927 srv.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
928 return func(c echo.Context) error {
929 c.Set("app", app)
930 return next(c)
931 }
932 })
933
934 tpl, err := stuffbin.ParseTemplatesGlob(initTplFuncs(i, urlCfg), fs, "/public/templates/*.html")
935 if err != nil {
936 lo.Fatalf("error parsing public templates: %v", err)
937 }
938 srv.Renderer = &tplRenderer{
939 templates: tpl,
940 SiteName: cfg.SiteName,
941 RootURL: urlCfg.RootURL,
942 LogoURL: urlCfg.LogoURL,
943 FaviconURL: urlCfg.FaviconURL,
944 AssetVersion: cfg.AssetVersion,
945 EnablePublicSubPage: cfg.EnablePublicSubPage,
946 EnablePublicArchive: cfg.EnablePublicArchive,
947 IndividualTracking: cfg.Privacy.IndividualTracking,
948 }
949
950 // Initialize the static file server.
951 fSrv := fs.FileServer()
952
953 // Public (subscriber) facing static files.
954 srv.GET("/public/static/*", echo.WrapHandler(fSrv))
955
956 // Admin (frontend) facing static files.
957 srv.GET("/admin/static/*", echo.WrapHandler(fSrv))
958
959 // Public (subscriber) facing media upload files.
960 var (
961 uploadProvider = ko.String("upload.provider")
962 uploadFsURI = ko.String("upload.filesystem.upload_uri")
963 publicURL = ko.String("upload.s3.public_url")
964 )
965 switch {
966 case uploadProvider == "filesystem" && uploadFsURI != "":
967 srv.Static(uploadFsURI, ko.String("upload.filesystem.upload_path"))
968 case uploadProvider == "s3" && strings.HasPrefix(publicURL, "/"):
969 srv.GET(path.Join(publicURL, "/:filepath"), app.ServeS3Media)
970 }
971
972 // Register all HTTP handlers.
973 initHTTPHandlers(srv, app)
974
975 // Start the server.
976 go func() {
977 if err := srv.Start(ko.String("app.address")); err != nil {
978 if errors.Is(err, http.ErrServerClosed) {

Callers 1

mainFunction · 0.85

Calls 3

initTplFuncsFunction · 0.85
initHTTPHandlersFunction · 0.85
StartMethod · 0.80

Tested by

no test coverage detected