Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

enhancement: docker healthcheck (#371)

Guilherme Oenning0c455e31695c
diff --git a/Dockerfile b/Dockerfile
index 913f1e280..f8724a5fb 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -13,4 +13,6 @@ COPY fider /app
EXPOSE 3000
+HEALTHCHECK --timeout=5s CMD ./fider ping
+
CMD [ "./fider" ]
\ No newline at end of file
diff --git a/app/cmd/ping.go b/app/cmd/ping.go
new file mode 100644
index 000000000..b2fe37b67
--- /dev/null
+++ b/app/cmd/ping.go
@@ -0,0 +1,35 @@
+package cmd
+
+import (
+ "crypto/tls"
+ "fmt"
+ "net/http"
+
+ "github.com/getfider/fider/app/pkg/env"
+)
+
+//RunPing checks if Fider Server is running and is healthy
+//Returns an exitcode, 0 for OK and 1 for ERROR
+func RunPing() int {
+ protocol := "http://"
+ if env.IsDefined("SSL_CERT") || env.IsDefined("SSL_AUTO") {
+ http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{
+ InsecureSkipVerify: true,
+ }
+ protocol = "https://"
+ }
+
+ resp, err := http.Get(protocol + "localhost:3000/-/health")
+ if err != nil {
+ fmt.Printf("Request failed with: %s\n", err)
+ return 1
+ }
+
+ defer resp.Body.Close()
+ if resp.StatusCode >= 400 {
+ fmt.Printf("Request failed with status code: %d\n", resp.StatusCode)
+ return 1
+ }
+
+ return 0
+}
diff --git a/routes.go b/app/cmd/routes.go
similarity index 95%
rename from routes.go
rename to app/cmd/routes.go
index bdfc1b339..ecaf8f7e0 100644
--- a/routes.go
+++ b/app/cmd/routes.go
@@ -1,4 +1,4 @@
-package main
+package cmd
import (
"time"
@@ -10,10 +10,7 @@ import (
"github.com/getfider/fider/app/pkg/web"
)
-// GetMainEngine returns main HTTP engine
-func GetMainEngine(settings *models.SystemSettings) *web.Engine {
- r := web.New(settings)
-
+func routes(r *web.Engine) *web.Engine {
r.Worker().Use(middlewares.WorkerSetup(r.Worker().Logger()))
r.Use(middlewares.Secure())
@@ -30,7 +27,7 @@ func GetMainEngine(settings *models.SystemSettings) *web.Engine {
noTenant := r.Group()
{
- noTenant.Post("/csp-report", handlers.CSPReport())
+ noTenant.Get("/-/health", handlers.Health())
noTenant.Post("/api/tenants", handlers.CreateTenant())
noTenant.Get("/api/tenants/:subdomain/availability", handlers.CheckAvailability())
@@ -79,7 +76,6 @@ func GetMainEngine(settings *models.SystemSettings) *web.Engine {
public.Get("/ideas/:number", handlers.IdeaDetails())
public.Get("/ideas/:number/*all", handlers.IdeaDetails())
public.Get("/signout", handlers.SignOut())
- public.Get("/api/status", handlers.Status(settings))
}
private := page.Group()
@@ -129,6 +125,5 @@ func GetMainEngine(settings *models.SystemSettings) *web.Engine {
private.Post("/api/admin/users/:user_id/role", handlers.ChangeUserRole())
}
}
-
return r
}
diff --git a/routes_test.go b/app/cmd/routes_test.go
similarity index 63%
rename from routes_test.go
rename to app/cmd/routes_test.go
index cc4ece5df..fad3f6b23 100644
--- a/routes_test.go
+++ b/app/cmd/routes_test.go
@@ -1,14 +1,15 @@
-package main
+package cmd
import (
"testing"
+ "github.com/getfider/fider/app/pkg/web"
. "github.com/onsi/gomega"
)
func TestGetMainEngine(t *testing.T) {
RegisterTestingT(t)
- r := GetMainEngine(nil)
+ r := routes(web.New(nil))
Expect(r).ToNot(BeNil())
}
diff --git a/app/cmd/server.go b/app/cmd/server.go
new file mode 100644
index 000000000..bff83c1d6
--- /dev/null
+++ b/app/cmd/server.go
@@ -0,0 +1,22 @@
+package cmd
+
+import (
+ "fmt"
+
+ "github.com/getfider/fider/app/models"
+ "github.com/getfider/fider/app/pkg/env"
+ "github.com/getfider/fider/app/pkg/web"
+)
+
+//RunServer starts the Fider Server
+//Returns an exitcode, 0 for OK and 1 for ERROR
+func RunServer(settings *models.SystemSettings) int {
+
+ fmt.Printf("Application is starting...\n")
+ fmt.Printf("GO_ENV: %s\n", env.Current())
+
+ e := routes(web.New(settings))
+ e.Start(":" + env.GetEnvOrDefault("PORT", "3000"))
+
+ return 0
+}
diff --git a/app/handlers/common.go b/app/handlers/common.go
index 627d92799..b71048f74 100644
--- a/app/handlers/common.go
+++ b/app/handlers/common.go
@@ -1,8 +1,6 @@
package handlers
import (
- "io/ioutil"
- "runtime"
"time"
"github.com/getfider/fider/app"
@@ -11,23 +9,10 @@ import (
"github.com/getfider/fider/app/pkg/web"
)
-//Status returns some useful information
-func Status(settings *models.SystemSettings) web.HandlerFunc {
+//Health always returns OK
+func Health() web.HandlerFunc {
return func(c web.Context) error {
- memStats := &runtime.MemStats{}
- runtime.ReadMemStats(memStats)
-
- return c.Ok(web.Map{
- "build": settings.BuildTime,
- "version": settings.Version,
- "env": settings.Environment,
- "compiler": settings.Compiler,
- "now": time.Now().Format("2006.01.02.150405"),
- "goroutines": runtime.NumGoroutine(),
- "workerQueue": c.Engine().Worker().Length(),
- "heapInMB": memStats.HeapAlloc / 1048576,
- "stackInMB": memStats.StackInuse / 1048576,
- })
+ return c.Ok(web.Map{})
}
}
@@ -38,18 +23,6 @@ func Page() web.HandlerFunc {
}
}
-//CSPReport logs every CSP policy issue reported by clients
-func CSPReport() web.HandlerFunc {
- return func(c web.Context) error {
- bytes, err := ioutil.ReadAll(c.Request.Body)
- if err != nil {
- return c.Failure(err)
- }
- c.Logger().Warnf("CSP Policy Failure: %s", string(bytes))
- return c.Ok(web.Map{})
- }
-}
-
func validateKey(kind models.EmailVerificationKind, c web.Context) (*models.EmailVerification, error) {
key := c.QueryParam("k")
diff --git a/app/handlers/common_test.go b/app/handlers/common_test.go
deleted file mode 100644
index 37a267c45..000000000
--- a/app/handlers/common_test.go
+++ /dev/null
@@ -1,37 +0,0 @@
-package handlers_test
-
-import (
- "net/http"
- "testing"
-
- "github.com/getfider/fider/app/handlers"
- "github.com/getfider/fider/app/models"
- "github.com/getfider/fider/app/pkg/mock"
- . "github.com/onsi/gomega"
-)
-
-func TestStatusHandler(t *testing.T) {
- RegisterTestingT(t)
-
- settings := &models.SystemSettings{
- BuildTime: "today",
- }
-
- server, _ := mock.NewServer()
- status, query := server.ExecuteAsJSON(handlers.Status(settings))
-
- Expect(query.String("build")).To(Equal("today"))
- Expect(status).To(Equal(http.StatusOK))
-}
-
-func TestCSPReportHandler(t *testing.T) {
- RegisterTestingT(t)
-
- server, _ := mock.NewServer()
- status, _ := server.ExecutePost(
- handlers.CSPReport(),
- "This is the message body.",
- )
-
- Expect(status).To(Equal(http.StatusOK))
-}
diff --git a/app/pkg/web/engine.go b/app/pkg/web/engine.go
index 97e2f7d5d..2498396a9 100644
--- a/app/pkg/web/engine.go
+++ b/app/pkg/web/engine.go
@@ -215,7 +215,7 @@ func (g *Group) handler(handler HandlerFunc) httprouter.Handle {
// Static return files from given folder
func (g *Group) Static(prefix, root string) {
- fi, err := os.Stat(root)
+ fi, err := os.Stat(env.Path(root))
if err != nil {
panic(fmt.Sprintf("Path '%s' not found", root))
}
diff --git a/app/pkg/web/ssl.go b/app/pkg/web/ssl.go
index f579eef7e..ca4eb7241 100644
--- a/app/pkg/web/ssl.go
+++ b/app/pkg/web/ssl.go
@@ -4,6 +4,7 @@ import (
"crypto/tls"
"crypto/x509"
"net/http"
+ "strings"
"github.com/getfider/fider/app/pkg/errors"
"golang.org/x/crypto/acme/autocert"
@@ -46,7 +47,9 @@ func NewCertificateManager(certFile, keyFile, cacheDir string) (*CertificateMana
//Otherwise fallsback to a automatically generated certificate by Let's Encrypt
func (m *CertificateManager) GetCertificate(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
if m.leaf != nil {
- if hello.ServerName == "" || m.leaf.VerifyHostname(hello.ServerName) == nil {
+ //skip autoSSL is ServerName is empty or does't contain a dot
+ skipAutoCert := hello.ServerName == "" || !strings.Contains(strings.Trim(hello.ServerName, "."), ".")
+ if skipAutoCert || m.leaf.VerifyHostname(hello.ServerName) == nil {
return &m.cert, nil
}
}
diff --git a/main.go b/main.go
index 319d903a0..c4c9b1f19 100644
--- a/main.go
+++ b/main.go
@@ -1,10 +1,10 @@
package main
import (
+ "os"
"runtime"
- "fmt"
-
+ "github.com/getfider/fider/app/cmd"
"github.com/getfider/fider/app/models"
"github.com/getfider/fider/app/pkg/env"
_ "github.com/lib/pq"
@@ -25,9 +25,6 @@ var buildnumber = "local"
var version = "0.13.0-" + buildnumber
func main() {
- fmt.Printf("Application is starting...\n")
- fmt.Printf("GO_ENV: %s\n", env.Current())
-
settings := &models.SystemSettings{
BuildTime: buildtime,
Version: version,
@@ -38,6 +35,10 @@ func main() {
Domain: env.MultiTenantDomain(),
}
- e := GetMainEngine(settings)
- e.Start(":" + env.GetEnvOrDefault("PORT", "3000"))
+ args := os.Args[1:]
+ if len(args) > 0 && args[0] == "ping" {
+ os.Exit(cmd.RunPing())
+ } else {
+ os.Exit(cmd.RunServer(settings))
+ }
}