Files
ocis/proxy/pkg/server/http/server.go
T

60 lines
1.7 KiB
Go
Raw Normal View History

2020-02-14 15:42:48 +01:00
package http
import (
"crypto/tls"
"os"
2021-06-07 17:11:29 +02:00
pkgcrypto "github.com/owncloud/ocis/ocis-pkg/crypto"
2020-09-18 15:18:41 +02:00
svc "github.com/owncloud/ocis/ocis-pkg/service/http"
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.
func Server(opts ...Option) (svc.Service, error) {
options := newOptions(opts...)
2020-03-13 19:52:46 +01:00
l := options.Logger
httpCfg := options.Config.HTTP
var cer tls.Certificate
2020-07-15 16:29:59 +02:00
var tlsConfig *tls.Config
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
}
}
2020-03-13 19:52:46 +01:00
2020-07-15 16:29:59 +02:00
cer, certErr = tls.LoadX509KeyPair(httpCfg.TLSCert, httpCfg.TLSKey)
if certErr != nil {
options.Logger.Fatal().Err(certErr).Msg("Could not setup TLS")
os.Exit(1)
}
2020-03-13 19:52:46 +01:00
2021-02-25 10:48:40 +01:00
tlsConfig = &tls.Config{MinVersion: tls.VersionTLS12, Certificates: []tls.Certificate{cer}}
}
chain := options.Middlewares.Then(options.Handler)
2020-02-14 15:42:48 +01:00
service := svc.NewService(
svc.Name(options.Config.Service.Name),
2020-03-13 19:52:46 +01:00
svc.TLSConfig(tlsConfig),
2020-02-14 15:42:48 +01:00
svc.Logger(options.Logger),
svc.Namespace(options.Config.Service.Namespace),
svc.Version(options.Config.Service.Version),
2020-02-14 15:42:48 +01:00
svc.Address(options.Config.HTTP.Addr),
svc.Context(options.Context),
svc.Flags(options.Flags...),
)
2021-02-26 14:24:25 +01:00
if err := micro.RegisterHandler(service.Server(), chain); err != nil {
return svc.Service{}, err
}
2020-02-14 15:42:48 +01:00
return service, nil
}