happyDomain/main.go

113 lines
2.9 KiB
Go
Raw Normal View History

2023-12-24 10:18:08 +00:00
// This file is part of the happyDomain (R) project.
// Copyright (c) 2020-2024 happyDomain
// Authors: Pierre-Olivier Mercier, et al.
2020-05-04 14:58:02 +00:00
//
2023-12-24 10:18:08 +00:00
// This program is offered under a commercial and under the AGPL license.
// For commercial licensing, contact us at <contact@happydomain.org>.
2020-05-04 14:58:02 +00:00
//
2023-12-24 10:18:08 +00:00
// For AGPL licensing:
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
2020-05-04 14:58:02 +00:00
//
2023-12-24 10:18:08 +00:00
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
2020-05-04 14:58:02 +00:00
//
2023-12-24 10:18:08 +00:00
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-05-04 14:58:02 +00:00
2019-07-16 09:07:03 +00:00
package main
import (
2020-06-08 19:07:53 +00:00
"fmt"
2019-07-16 09:07:03 +00:00
"log"
"math/rand"
2020-06-08 19:07:53 +00:00
"os"
"os/signal"
"syscall"
"time"
2019-07-16 09:07:03 +00:00
2023-05-18 17:34:54 +00:00
"github.com/fatih/color"
2023-09-07 09:37:18 +00:00
"git.happydns.org/happyDomain/api"
"git.happydns.org/happyDomain/config"
"git.happydns.org/happyDomain/internal/app"
"git.happydns.org/happyDomain/storage"
2020-04-22 11:12:27 +00:00
2023-09-07 09:37:18 +00:00
_ "git.happydns.org/happyDomain/services/providers/google"
2020-10-10 16:44:17 +00:00
2023-09-07 09:37:18 +00:00
_ "git.happydns.org/happyDomain/storage/leveldb"
2019-07-16 09:07:03 +00:00
)
var (
Version = "custom-build"
)
2019-07-16 09:07:03 +00:00
func main() {
var err error
2019-07-16 09:07:03 +00:00
2023-08-05 16:15:52 +00:00
api.HDVersion = api.Version{
Version: Version,
}
2021-12-31 14:52:15 +00:00
log.Println("This is happyDomain", Version)
rand.Seed(time.Now().UTC().UnixNano())
2023-05-18 17:34:54 +00:00
// Disabled colors in dnscontrol corrections
color.NoColor = true
// Load and parse options
var opts *config.Options
if opts, err = config.ConsolidateConfig(); err != nil {
log.Fatal(err)
2019-07-16 09:07:03 +00:00
}
// Initialize storage
if s, ok := storage.StorageEngines[opts.StorageEngine]; !ok {
2023-02-23 01:21:12 +00:00
log.Fatal(fmt.Sprintf("Nonexistent storage engine: %q, please select one of: %v", opts.StorageEngine, storage.GetStorageEngines()))
} else {
log.Println("Opening database...")
if store, err := s(); err != nil {
2023-02-23 01:21:12 +00:00
log.Fatal("Could not open the database: ", err)
} else {
defer store.Close()
storage.MainStore = store
}
}
if opts.NoAuth {
2023-02-23 01:21:12 +00:00
log.Println("WARNING: NoAuth option must be used for testing or private use behind another restriction/authentication method.")
}
2023-02-23 01:21:12 +00:00
log.Println("Performing database migrations...")
if err = storage.MainStore.DoMigration(); err != nil {
2023-02-23 01:21:12 +00:00
log.Fatal("Could not migrate database: ", err)
2019-07-16 09:07:03 +00:00
}
2020-06-08 19:07:53 +00:00
// Prepare graceful shutdown
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
2021-05-05 01:48:16 +00:00
var adminSrv *app.Admin
2020-06-08 19:07:53 +00:00
if opts.AdminBind != "" {
2021-05-05 01:48:16 +00:00
adminSrv := app.NewAdmin(opts)
go adminSrv.Start()
2020-06-08 19:07:53 +00:00
}
2021-05-04 14:48:16 +00:00
a := app.NewApp(opts)
go a.Start()
2020-06-08 19:07:53 +00:00
// Wait shutdown signal
<-interrupt
2021-05-04 14:48:16 +00:00
log.Println("Stopping the service...")
a.Stop()
if adminSrv != nil {
2021-05-05 01:48:16 +00:00
adminSrv.Stop()
2021-05-04 14:48:16 +00:00
}
log.Println("Stopped")
2019-07-16 09:07:03 +00:00
}