-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
79 lines (64 loc) · 1.82 KB
/
server.go
File metadata and controls
79 lines (64 loc) · 1.82 KB
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
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
"github.com/spf13/viper"
)
func init() {
viper.SetDefault("port", 80)
viper.SetDefault("sslport", 443)
}
func statusAPI(c echo.Context) error {
status, err := getStatus()
if err != nil {
// TODO: log this error
return err
}
return c.JSON(http.StatusOK, status)
}
func main() {
e := echo.New()
e.Use(middleware.Gzip())
e.Use(middleware.Logger())
e.Use(middleware.RequestID())
corsConfig := middleware.CORSConfig{AllowOrigins: []string{"*"}}
e.Use(middleware.CORSWithConfig(corsConfig))
e.GET("/api", statusAPI)
e.Static("/", "dist")
viper.AutomaticEnv()
viper.SetEnvPrefix("isno")
port := viper.GetInt("port")
sslport := viper.GetInt("sslport")
e.Logger.SetLevel(log.INFO)
e.Logger.Infof("*** STARTING PID %v", os.Getpid())
// Start port 443
go func(c *echo.Echo) {
e.Logger.Fatal(e.StartAutoTLS(fmt.Sprintf(":%v", sslport)))
}(e)
// Start port 80
go func() {
if err := e.Start(fmt.Sprintf(":%v", port)); err != nil && err != http.ErrServerClosed {
e.Logger.Fatal(err)
}
}()
// Labstack graceful shutdown code from https://echo.labstack.com/docs/cookbook/graceful-shutdown
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
// Use a buffered channel to avoid missing signals as recommended for signal.Notify
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
quitSignal := <-quit
e.Logger.Warnf("*** STOPPING PID %v with signal %v", os.Getpid(), quitSignal)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err)
}
}