This repository has been archived by the owner on Mar 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
102 lines (92 loc) · 2.99 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"flag"
"log"
"net"
"net/http"
"os"
"os/signal"
"time"
"github.com/matscus/Hamster/MicroServices/service/handlers"
"github.com/matscus/Hamster/Package/Middleware/middleware"
"context"
"github.com/gorilla/mux"
)
var (
pemPath string
keyPath string
proto string
listenport string
host string
wait time.Duration
writeTimeout time.Duration
readTimeout time.Duration
idleTimeout time.Duration
)
//Token - struct for auth token
type Token struct {
Token string `json:"token"`
}
func init() {
go handlers.InitGetResponseAllData()
handlers.CheckService()
}
func main() {
flag.StringVar(&pemPath, "pem", os.Getenv("SERVERREM"), "path to pem file")
flag.StringVar(&keyPath, "key", os.Getenv("SERVERKEY"), "path to key file")
flag.StringVar(&listenport, "port", "10001", "port to Listen")
flag.StringVar(&proto, "proto", "https", "http or https")
flag.DurationVar(&wait, "graceful-timeout", time.Second*15, "the duration for which the server gracefully")
flag.DurationVar(&readTimeout, "read-timeout", time.Second*15, "read server timeout")
flag.DurationVar(&writeTimeout, "write-timeout", time.Second*15, "write server timeout")
flag.DurationVar(&idleTimeout, "idle-timeout", time.Second*60, "idle server timeout")
flag.Parse()
r := mux.NewRouter()
addrs, err := net.InterfaceAddrs()
if err != nil {
log.Println("Get interface adres error: ", err.Error())
os.Exit(1)
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
host = ipnet.IP.String()
}
}
}
srv := &http.Server{
Addr: host + ":" + listenport,
WriteTimeout: writeTimeout,
ReadTimeout: readTimeout,
IdleTimeout: idleTimeout,
Handler: r,
}
r.HandleFunc("/api/v1/service/start", middleware.Middleware(handlers.StartSevice)).Methods(http.MethodPost, http.MethodOptions)
r.HandleFunc("/api/v1/service/stop", middleware.Middleware(handlers.StopService)).Methods(http.MethodPost, http.MethodOptions)
r.HandleFunc("/api/v1/service/administration", middleware.Middleware(handlers.Administration)).Methods(http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodOptions)
r.HandleFunc("/api/v1/service/getallservice", middleware.Middleware(handlers.GetAllServices)).Methods(http.MethodGet, http.MethodOptions).Queries("project", "{project}")
http.Handle("/api/v1/", r)
//r.Use(mux.CORSMethodMiddleware(r))
go func() {
switch proto {
case "https":
log.Printf("Server is run, proto: https, address: %s ", srv.Addr)
if err := srv.ListenAndServeTLS(pemPath, keyPath); err != nil {
log.Println(err)
}
case "http":
log.Printf("Server is run, proto: http, address: %s ", srv.Addr)
if err := srv.ListenAndServe(); err != nil {
log.Println(err)
}
}
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
ctx, cancel := context.WithTimeout(context.Background(), wait)
defer cancel()
srv.Shutdown(ctx)
log.Println("server shutting down")
os.Exit(0)
}