2018-10-18 06:00:18 +02:00
|
|
|
// Copyright Project Harbor Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2018-03-08 06:16:57 +01:00
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-02-11 07:25:37 +01:00
|
|
|
"context"
|
2018-03-08 06:16:57 +01:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2020-02-11 07:25:37 +01:00
|
|
|
commonhttp "github.com/goharbor/harbor/src/common/http"
|
2018-08-23 09:02:20 +02:00
|
|
|
"github.com/goharbor/harbor/src/jobservice/config"
|
|
|
|
"github.com/goharbor/harbor/src/jobservice/logger"
|
2018-03-08 06:16:57 +01:00
|
|
|
)
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// Server serves the http requests.
|
2018-03-08 06:16:57 +01:00
|
|
|
type Server struct {
|
2018-09-05 10:16:31 +02:00
|
|
|
// The real backend http server to serve the requests
|
2018-03-08 06:16:57 +01:00
|
|
|
httpServer *http.Server
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// Define the routes of http service
|
2018-03-08 06:16:57 +01:00
|
|
|
router Router
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// Keep the configurations of server
|
2018-03-08 06:16:57 +01:00
|
|
|
config ServerConfig
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// The context
|
2019-04-18 10:02:49 +02:00
|
|
|
context context.Context
|
2018-03-08 06:16:57 +01:00
|
|
|
}
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// ServerConfig contains the configurations of Server.
|
2018-03-08 06:16:57 +01:00
|
|
|
type ServerConfig struct {
|
2018-09-05 10:16:31 +02:00
|
|
|
// Protocol server listening on: https/http
|
2018-03-08 06:16:57 +01:00
|
|
|
Protocol string
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// Server listening port
|
2018-03-08 06:16:57 +01:00
|
|
|
Port uint
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// Cert file path if using https
|
2018-03-08 06:16:57 +01:00
|
|
|
Cert string
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// Key file path if using https
|
2018-03-08 06:16:57 +01:00
|
|
|
Key string
|
|
|
|
}
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// NewServer is constructor of Server.
|
2019-04-18 10:02:49 +02:00
|
|
|
func NewServer(ctx context.Context, router Router, cfg ServerConfig) *Server {
|
2018-03-15 05:26:01 +01:00
|
|
|
apiServer := &Server{
|
|
|
|
router: router,
|
|
|
|
config: cfg,
|
|
|
|
context: ctx,
|
|
|
|
}
|
|
|
|
|
2018-03-08 06:16:57 +01:00
|
|
|
srv := &http.Server{
|
|
|
|
Addr: fmt.Sprintf(":%d", cfg.Port),
|
|
|
|
Handler: http.HandlerFunc(router.ServeHTTP),
|
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
IdleTimeout: 60 * time.Second,
|
|
|
|
}
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// Initialize TLS/SSL config if protocol is https
|
2018-03-08 06:16:57 +01:00
|
|
|
if cfg.Protocol == config.JobServiceProtocolHTTPS {
|
2020-02-11 07:25:37 +01:00
|
|
|
logger.Infof("https enabled, load trustCAs")
|
|
|
|
srv.TLSConfig = &tls.Config{
|
|
|
|
ClientAuth: tls.RequireAndVerifyClientCert,
|
|
|
|
ClientCAs: commonhttp.GetInternalCA(nil),
|
2018-03-08 06:16:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 05:26:01 +01:00
|
|
|
apiServer.httpServer = srv
|
|
|
|
|
|
|
|
return apiServer
|
2018-03-08 06:16:57 +01:00
|
|
|
}
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// Start the server to serve requests.
|
2019-04-18 10:02:49 +02:00
|
|
|
// Blocking call
|
|
|
|
func (s *Server) Start() error {
|
2019-04-19 07:54:23 +02:00
|
|
|
defer func() {
|
|
|
|
logger.Info("API server is stopped")
|
2018-03-08 06:16:57 +01:00
|
|
|
}()
|
2019-04-19 07:54:23 +02:00
|
|
|
|
2019-04-18 10:02:49 +02:00
|
|
|
if s.config.Protocol == config.JobServiceProtocolHTTPS {
|
|
|
|
return s.httpServer.ListenAndServeTLS(s.config.Cert, s.config.Key)
|
|
|
|
}
|
2019-04-19 08:16:08 +02:00
|
|
|
|
|
|
|
return s.httpServer.ListenAndServe()
|
2018-03-08 06:16:57 +01:00
|
|
|
}
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// Stop server gracefully.
|
2019-04-18 10:02:49 +02:00
|
|
|
func (s *Server) Stop() error {
|
|
|
|
shutDownCtx, cancel := context.WithTimeout(s.context, 15*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
return s.httpServer.Shutdown(shutDownCtx)
|
2018-03-08 06:16:57 +01:00
|
|
|
}
|