| 1 |
// Copyright 2022-2024 The Archivista Contributors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// A note: this follows a pattern followed by network service mesh. |
| 16 |
// The pattern was copied from the Network Service Mesh Project |
| 17 |
// and modified for use here. The original code was published under the |
| 18 |
// Apache License V2. |
| 19 |
|
| 20 |
package main |
| 21 |
|
| 22 |
import ( |
| 23 |
"context" |
| 24 |
"net" |
| 25 |
"net/http" |
| 26 |
"os" |
| 27 |
"os/signal" |
| 28 |
"strings" |
| 29 |
"syscall" |
| 30 |
"time" |
| 31 |
|
| 32 |
nested "github.com/antonfisher/nested-logrus-formatter" |
| 33 |
"github.com/gorilla/handlers" |
| 34 |
"github.com/in-toto/archivista/pkg/server" |
| 35 |
"github.com/sirupsen/logrus" |
| 36 |
) |
| 37 |
|
| 38 |
func init() { |
| 39 |
logrus.SetFormatter(&nested.Formatter{}) |
| 40 |
} |
| 41 |
|
| 42 |
func main() { |
| 43 |
ctx, cancel := signal.NotifyContext( |
| 44 |
context.Background(), |
| 45 |
os.Interrupt, |
| 46 |
syscall.SIGHUP, |
| 47 |
syscall.SIGQUIT, |
| 48 |
syscall.SIGTERM, |
| 49 |
) |
| 50 |
defer cancel() |
| 51 |
|
| 52 |
startTime := time.Now() |
| 53 |
|
| 54 |
archivistaService := &server.ArchivistaService{Ctx: ctx, Cfg: nil} |
| 55 |
|
| 56 |
server, err := archivistaService.Setup() |
| 57 |
if err != nil { |
| 58 |
logrus.Fatalf("unable to setup archivista service: %+v", err) |
| 59 |
} |
| 60 |
// ******************************************************************************** |
| 61 |
logrus.Infof("executing phase: create and register http service (time since start: %s)", time.Since(startTime)) |
| 62 |
// ******************************************************************************** |
| 63 |
now := time.Now() |
| 64 |
|
| 65 |
listenAddress := archivistaService.Cfg.ListenOn |
| 66 |
listenAddress = strings.ToLower(strings.TrimSpace(listenAddress)) |
| 67 |
proto := "" |
| 68 |
if strings.HasPrefix(listenAddress, "tcp://") { |
| 69 |
proto = "tcp" |
| 70 |
listenAddress = listenAddress[len("tcp://"):] |
| 71 |
} else if strings.HasPrefix(listenAddress, "unix://") { |
| 72 |
proto = "unix" |
| 73 |
listenAddress = listenAddress[len("unix://"):] |
| 74 |
} |
| 75 |
|
| 76 |
listener, err := net.Listen(proto, listenAddress) |
| 77 |
if err != nil { |
| 78 |
logrus.Fatalf("unable to start http listener: %+v", err) |
| 79 |
} |
| 80 |
srv := &http.Server{ |
| 81 |
Handler: handlers.CORS( |
| 82 |
handlers.AllowedOrigins(archivistaService.Cfg.CORSAllowOrigins), |
| 83 |
handlers.AllowedMethods([]string{"GET", "POST", "OPTIONS"}), |
| 84 |
handlers.AllowedHeaders([]string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"}), |
| 85 |
)(server.Router()), |
| 86 |
ReadTimeout: time.Duration(archivistaService.Cfg.ReadTimeout) * time.Second, |
| 87 |
WriteTimeout: time.Duration(archivistaService.Cfg.WriteTimeout) * time.Second, |
| 88 |
} |
| 89 |
|
| 90 |
go func() { |
| 91 |
if archivistaService.Cfg.EnableTLS { |
| 92 |
if err := srv.ServeTLS(listener, archivistaService.Cfg.TLSCert, archivistaService.Cfg.TLSKey); err != nil { |
| 93 |
logrus.Fatalf("unable to start http server: %+v", err) |
| 94 |
} |
| 95 |
} else { |
| 96 |
if err := srv.Serve(listener); err != nil { |
| 97 |
logrus.Fatalf("unable to start http server: %+v", err) |
| 98 |
} |
| 99 |
} |
| 100 |
}() |
| 101 |
|
| 102 |
logrus.WithField("duration", time.Since(now)).Infof("completed phase: create and register http service") |
| 103 |
logrus.Infof("startup complete (time since start: %s)", time.Since(startTime)) |
| 104 |
|
| 105 |
<-ctx.Done() |
| 106 |
<-archivistaService.GetFileStoreCh() |
| 107 |
<-archivistaService.GetSQLStoreCh() |
| 108 |
|
| 109 |
logrus.Infof("exiting, uptime: %v", time.Since(startTime)) |
| 110 |
} |