netmaker/controllers/controller.go

77 lines
2.1 KiB
Go
Raw Normal View History

2021-03-26 00:17:52 +08:00
package controller
import (
2021-07-22 06:55:19 +08:00
"context"
2021-12-07 04:31:08 +08:00
"fmt"
2021-07-22 06:55:19 +08:00
"net/http"
"os"
"os/signal"
2022-12-13 13:38:53 +08:00
"strings"
2021-07-22 06:55:19 +08:00
"sync"
"syscall"
2021-12-07 04:31:08 +08:00
"time"
2021-10-09 03:07:12 +08:00
2021-07-22 06:55:19 +08:00
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
2021-12-07 04:31:08 +08:00
"github.com/gravitl/netmaker/logger"
2021-07-22 06:55:19 +08:00
"github.com/gravitl/netmaker/servercfg"
2021-03-26 00:17:52 +08:00
)
2022-01-26 00:00:56 +08:00
// HttpHandlers - handler functions for REST interactions
2021-12-09 05:52:32 +08:00
var HttpHandlers = []interface{}{
nodeHandlers,
userHandlers,
networkHandlers,
dnsHandlers,
fileHandlers,
serverHandlers,
extClientHandlers,
ipHandlers,
2022-09-14 03:25:56 +08:00
loggerHandlers,
hostHandlers,
2023-02-16 05:32:16 +08:00
enrollmentKeyHandlers,
2021-12-09 05:52:32 +08:00
}
2021-10-09 03:07:12 +08:00
// HandleRESTRequests - handles the rest requests
func HandleRESTRequests(wg *sync.WaitGroup) {
2021-07-22 06:55:19 +08:00
defer wg.Done()
2021-03-26 00:17:52 +08:00
2021-07-22 06:55:19 +08:00
r := mux.NewRouter()
2021-03-26 00:17:52 +08:00
2021-07-22 06:55:19 +08:00
// Currently allowed dev origin is all. Should change in prod
// should consider analyzing the allowed methods further
headersOk := handlers.AllowedHeaders([]string{"Access-Control-Allow-Origin", "X-Requested-With", "Content-Type", "authorization"})
2022-12-13 13:38:53 +08:00
originsOk := handlers.AllowedOrigins(strings.Split(servercfg.GetAllowedOrigin(), ","))
methodsOk := handlers.AllowedMethods([]string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete})
2021-03-26 00:17:52 +08:00
2021-12-09 05:52:32 +08:00
for _, handler := range HttpHandlers {
handler.(func(*mux.Router))(r)
}
2021-10-09 03:07:12 +08:00
2021-07-22 06:55:19 +08:00
port := servercfg.GetAPIPort()
2021-03-26 00:17:52 +08:00
2021-07-22 06:55:19 +08:00
srv := &http.Server{Addr: ":" + port, Handler: handlers.CORS(originsOk, headersOk, methodsOk)(r)}
go func() {
2021-03-26 00:17:52 +08:00
err := srv.ListenAndServe()
if err != nil {
2021-12-07 04:31:08 +08:00
logger.Log(0, err.Error())
2021-03-26 00:17:52 +08:00
}
2021-07-22 06:55:19 +08:00
}()
2021-12-07 04:31:08 +08:00
logger.Log(0, "REST Server successfully started on port ", port, " (REST)")
2021-03-26 00:17:52 +08:00
// Relay os.Interrupt to our channel (os.Interrupt = CTRL+C)
// Ignore other incoming signals
ctx, stop := signal.NotifyContext(context.TODO(), syscall.SIGTERM, os.Interrupt)
defer stop()
2021-07-22 06:55:19 +08:00
// Block main routine until a signal is received
// As long as user doesn't press CTRL+C a message is not passed and our main routine keeps running
<-ctx.Done()
2021-07-22 06:55:19 +08:00
// After receiving CTRL+C Properly stop the server
2021-12-07 04:31:08 +08:00
logger.Log(0, "Stopping the REST server...")
logger.Log(0, "REST Server closed.")
logger.DumpFile(fmt.Sprintf("data/netmaker.log.%s", time.Now().Format(logger.TimeFormatDay)))
srv.Shutdown(context.TODO())
2021-03-26 00:17:52 +08:00
}