Files

61 lines
1.7 KiB
Go
Raw Permalink Normal View History

2020-02-14 15:42:48 +01:00
package http
import (
"fmt"
"os"
2022-05-04 14:49:59 +02:00
pkgcrypto "github.com/owncloud/ocis/v2/ocis-pkg/crypto"
"github.com/owncloud/ocis/v2/ocis-pkg/service/http"
"github.com/owncloud/ocis/v2/ocis-pkg/shared"
2022-05-04 14:49:59 +02:00
"github.com/owncloud/ocis/v2/ocis-pkg/version"
2021-10-12 17:29:33 +02:00
"go-micro.dev/v4"
2020-02-14 15:42:48 +01:00
)
// Server initializes the http service and server.
2023-01-13 15:29:14 +01:00
func Server(opts ...Option) (http.Service, error) {
2020-02-14 15:42:48 +01:00
options := newOptions(opts...)
2020-03-13 19:52:46 +01:00
l := options.Logger
httpCfg := options.Config.HTTP
2020-07-15 16:29:59 +02:00
if options.Config.HTTP.TLS {
l.Warn().Msgf("No tls certificate provided, using a generated one")
_, certErr := os.Stat(httpCfg.TLSCert)
_, keyErr := os.Stat(httpCfg.TLSKey)
if os.IsNotExist(certErr) || os.IsNotExist(keyErr) {
// GenCert has side effects as it writes 2 files to the binary running location
2021-06-07 17:11:29 +02:00
if err := pkgcrypto.GenCert(httpCfg.TLSCert, httpCfg.TLSKey, l); err != nil {
l.Fatal().Err(err).Msgf("Could not generate test-certificate")
os.Exit(1)
2020-07-15 16:29:59 +02:00
}
}
}
chain := options.Middlewares.Then(options.Handler)
2023-01-13 15:29:14 +01:00
service, err := http.NewService(
http.Name(options.Config.Service.Name),
http.Version(version.GetString()),
http.TLSConfig(shared.HTTPServiceTLS{
Enabled: options.Config.HTTP.TLS,
Cert: options.Config.HTTP.TLSCert,
Key: options.Config.HTTP.TLSKey,
}),
2023-01-13 15:29:14 +01:00
http.Logger(options.Logger),
http.Address(options.Config.HTTP.Addr),
http.Namespace(options.Config.HTTP.Namespace),
http.Context(options.Context),
http.Flags(options.Flags...),
2020-02-14 15:42:48 +01:00
)
if err != nil {
options.Logger.Error().
Err(err).
Msg("Error initializing http service")
return http.Service{}, fmt.Errorf("could not initialize http service: %w", err)
}
2020-02-14 15:42:48 +01:00
2021-02-26 14:24:25 +01:00
if err := micro.RegisterHandler(service.Server(), chain); err != nil {
2023-01-13 15:29:14 +01:00
return http.Service{}, err
2021-02-26 14:24:25 +01:00
}
2020-02-14 15:42:48 +01:00
return service, nil
}