NewHTTPServer new a HTTP server.
(c *conf.Server, authConf *conf.Auth, downloadSvc *service.DownloadService, providers backend.Providers, logger log.Logger)
| 34 | |
| 35 | // NewHTTPServer new a HTTP server. |
| 36 | func NewHTTPServer(c *conf.Server, authConf *conf.Auth, downloadSvc *service.DownloadService, providers backend.Providers, logger log.Logger) (*http.Server, error) { |
| 37 | var opts = []http.ServerOption{ |
| 38 | http.Middleware( |
| 39 | recovery.Recovery(), |
| 40 | logging.Server(logger), |
| 41 | ), |
| 42 | } |
| 43 | if c.Http.Network != "" { |
| 44 | opts = append(opts, http.Network(c.Http.Network)) |
| 45 | } |
| 46 | if c.Http.Addr != "" { |
| 47 | opts = append(opts, http.Address(c.Http.Addr)) |
| 48 | } |
| 49 | if c.Http.Timeout != nil { |
| 50 | opts = append(opts, http.Timeout(c.Http.Timeout.AsDuration())) |
| 51 | } |
| 52 | |
| 53 | // Load the key on initialization instead of on every request |
| 54 | // TODO: implement jwks endpoint |
| 55 | publicKeyPath := authConf.GetPublicKeyPath() |
| 56 | if publicKeyPath == "" { |
| 57 | // Maintain backwards compatibility |
| 58 | publicKeyPath = authConf.RobotAccountPublicKeyPath |
| 59 | } |
| 60 | |
| 61 | rawKey, err := os.ReadFile(publicKeyPath) |
| 62 | if err != nil { |
| 63 | return nil, fmt.Errorf("failed to load public key: %w", err) |
| 64 | } |
| 65 | |
| 66 | srv := http.NewServer(opts...) |
| 67 | |
| 68 | downloadHandler := middlewares_http.AuthFromQueryParam(loadPublicKey(rawKey), claimsFunc(), casJWT.SigningMethod, downloadSvc) |
| 69 | srv.Handle(service.DownloadPath, CORSMiddleware(c.GetHttp().GetCors().GetAllowOrigins(), downloadHandler)) |
| 70 | api.RegisterStatusServiceHTTPServer(srv, service.NewStatusService(Version, providers)) |
| 71 | return srv, nil |
| 72 | } |
| 73 | |
| 74 | // claimsFunc returns the claims function for the JWT middleware that casts the claims to the correct type |
| 75 | func claimsFunc() middlewares_http.ClaimsFunc { |
no test coverage detected