...

Source file src/discovery/discovery.go

Documentation: discovery

     1  /*
     2   * P2PFaaS - A framework for FaaS Load Balancing
     3   * Copyright (c) 2019. Gabriele Proietti Mattia <pm.gabriele@outlook.com>
     4   *
     5   * This program is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
    17   */
    18  
    19  // Package main is the entrypoint of the service
    20  package main
    21  
    22  import (
    23  	"discovery/api"
    24  	"discovery/config"
    25  	"discovery/db"
    26  	"discovery/log"
    27  	"discovery/watcher"
    28  	"fmt"
    29  	"github.com/gorilla/mux"
    30  	"net/http"
    31  	"sync"
    32  )
    33  
    34  var wg sync.WaitGroup
    35  
    36  func main() {
    37  	// init modules
    38  	config.Start()
    39  	db.Start()
    40  
    41  	wg.Add(2)
    42  	go server()
    43  	go watchd()
    44  
    45  	log.Log.Infof("Discovery server started successfully")
    46  
    47  	wg.Wait()
    48  }
    49  
    50  func server() {
    51  	// init modules
    52  	// db.AddInitServers(config.GetInitServers())
    53  
    54  	// init api
    55  	router := mux.NewRouter()
    56  	router.HandleFunc("/", api.Hello).Methods("GET")
    57  	router.HandleFunc("/list", api.ServersGetList).Methods("GET")
    58  	router.HandleFunc("/reset", api.ServersReset).Methods("GET")
    59  	// dev apis
    60  	// if config.Configuration.GetRunningEnvironment() == config.RunningEnvironmentDevelopment {
    61  	// TODO secure these apis
    62  	router.HandleFunc("/configuration", api.ConfigurationGet).Methods("GET")
    63  	router.HandleFunc("/configuration", api.ConfigurationSet).Methods("POST")
    64  	// }
    65  
    66  	server := &http.Server{
    67  		Addr:    fmt.Sprintf("%s:%d", config.GetListeningHost(), config.GetListeningPort()),
    68  		Handler: router,
    69  	}
    70  
    71  	log.Log.Infof("Started listening on %s:%d", config.GetListeningHost(), config.GetListeningPort())
    72  	err := server.ListenAndServe()
    73  
    74  	log.Log.Fatalf("Error while starting server: %s", err)
    75  	wg.Done()
    76  }
    77  
    78  func watchd() {
    79  	log.Log.Infof("Watcher started")
    80  	watcher.PollingLooper()
    81  	wg.Done()
    82  }
    83  

View as plain text