Serve starts a new app web server. NB! The app should be bootstrapped before starting the web server. Example: app.Bootstrap() apis.Serve(app, apis.ServeConfig{ HttpAddr: "127.0.0.1:8080", ShowStartBanner: false, })
(app core.App, config ServeConfig)
| 59 | // ShowStartBanner: false, |
| 60 | // }) |
| 61 | func Serve(app core.App, config ServeConfig) error { |
| 62 | if len(config.AllowedOrigins) == 0 { |
| 63 | config.AllowedOrigins = []string{"*"} |
| 64 | } |
| 65 | |
| 66 | // ensure that the latest migrations are applied before starting the server |
| 67 | err := app.RunAllMigrations() |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | |
| 72 | pbRouter, err := NewRouter(app) |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | |
| 77 | pbRouter.Bind(CORS(CORSConfig{ |
| 78 | AllowOrigins: config.AllowedOrigins, |
| 79 | AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete}, |
| 80 | })) |
| 81 | |
| 82 | // @todo consider moving in base |
| 83 | if ui.DistDirFS != nil { |
| 84 | pbRouter.GET("/_/{path...}", Static(ui.DistDirFS, false)). |
| 85 | BindFunc(func(e *core.RequestEvent) error { |
| 86 | if !e.App.IsDev() && |
| 87 | // exclude root path |
| 88 | e.Request.PathValue(StaticWildcardParam) != "" && |
| 89 | e.Response.Header().Get("Cache-Control") == "" { |
| 90 | e.Response.Header().Set("Cache-Control", "max-age=1209600, stale-while-revalidate=86400") |
| 91 | } |
| 92 | |
| 93 | if e.Response.Header().Get("Content-Security-Policy") == "" { |
| 94 | e.Response.Header().Set("Content-Security-Policy", defaultCSP) |
| 95 | } |
| 96 | |
| 97 | return e.Next() |
| 98 | }). |
| 99 | Bind(Gzip()) |
| 100 | } |
| 101 | |
| 102 | // start http server |
| 103 | // --- |
| 104 | mainAddr := config.HttpAddr |
| 105 | if config.HttpsAddr != "" { |
| 106 | mainAddr = config.HttpsAddr |
| 107 | } |
| 108 | |
| 109 | var wwwRedirects []string |
| 110 | |
| 111 | // extract the host names for the certificate host policy |
| 112 | hostNames := config.CertificateDomains |
| 113 | if len(hostNames) == 0 { |
| 114 | host, _, _ := net.SplitHostPort(mainAddr) |
| 115 | hostNames = append(hostNames, host) |
| 116 | } |
| 117 | for _, host := range hostNames { |
| 118 | if strings.HasPrefix(host, "www.") { |
no test coverage detected
searching dependent graphs…