1
0
Fork 0
mnemonics/main.go

116 lines
3.2 KiB
Go
Raw Normal View History

2016-03-07 04:31:13 +09:00
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"net/http"
2016-03-07 04:31:13 +09:00
"runtime"
2016-03-07 19:16:06 +09:00
"time"
2016-03-07 19:16:06 +09:00
"amuz.es/go/mnemonics/route"
"amuz.es/go/mnemonics/util"
"github.com/gin-gonic/gin"
"gopkg.in/tylerb/graceful.v1"
2016-03-07 04:31:13 +09:00
)
var argIp = flag.String("listen_ip", "", "IP to listen on, defaults to all IPs")
var argPort = flag.Int("port", 8080, "port to listen")
var maxProcs = flag.Int("max_procs", 0, "max number of CPUs that can be used simultaneously. Less than 1 for default (number of cores).")
func main() {
2016-03-07 20:13:12 +09:00
flag.Parse()
2016-03-07 04:31:13 +09:00
setMaxProcs()
util.InitLogger()
2016-03-07 19:16:06 +09:00
gin.SetMode(gin.ReleaseMode)
r := gin.New()
// middleware settings
r.Use(util.Ginrus(time.RFC3339, true))
2016-03-07 04:31:13 +09:00
r.Use(gin.Recovery())
// not found handler
2016-03-07 04:31:13 +09:00
r.NoRoute(func(c *gin.Context) {
code := http.StatusNotFound
c.HTML(code, "page_404.html", util.Context{
"statusCode": code,
"statusMessage": http.StatusText(code),
"path": c.Request.URL.Path,
"method": c.Request.Method})
2016-03-07 04:31:13 +09:00
})
2016-03-13 02:14:19 +09:00
// suburl process
path := r.Group("/")
// handlers
2016-03-13 02:14:19 +09:00
path.GET("/api", route.Api)
path.GET("/self", route.Self)
path.GET("/metric", route.Metric)
path.GET("/health", func(c *gin.Context) {
2016-03-07 19:16:06 +09:00
c.String(200, "OK! - mnemonics services are fully operational!")
2016-03-07 04:31:13 +09:00
})
2016-03-13 02:14:19 +09:00
path.GET("/favicon.ico", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/static/favicon/images/favicon.ico")
})
path.GET("/manifest.json", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/static/favicon/manifest.json")
})
path.GET("/browserconfig.xml", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/static/favicon/browserconfig.xml")
})
path.GET("/login", route.Login)
path.GET("/", route.Index)
2016-03-07 04:31:13 +09:00
if gin.IsDebugging() {
2016-03-12 01:17:17 +09:00
// static route
2016-03-13 02:14:19 +09:00
path.StaticFS("/static", util.StaticDebug("asset/static"))
r.HTMLRender = util.TemplateSourceDebug("asset/template")
} else {
2016-03-12 01:17:17 +09:00
// template engine setting
2016-03-13 02:14:19 +09:00
path.StaticFS("/static", util.StaticRelease(""))
r.HTMLRender = util.TemplateSourceRelease("")
}
util.Log().Error("bootstrapped application")
2016-03-08 18:43:20 +09:00
// r.Run() // listen and server on 0.0.0.0:8080
graceful.Run(":8080", 10*time.Second, r)
util.Log().Error("byez")
2016-03-07 04:31:13 +09:00
}
func setMaxProcs() {
// TODO(vmarmol): Consider limiting if we have a CPU mask in effect.
// Allow as many threads as we have cores unless the user specified a value.
var numProcs int
if *maxProcs < 1 {
numProcs = runtime.NumCPU()
} else {
numProcs = *maxProcs
}
runtime.GOMAXPROCS(numProcs)
// Check if the setting was successful.
actualNumProcs := runtime.GOMAXPROCS(0)
if actualNumProcs != numProcs {
util.Log().Warnf("Specified max procs of %v but using %v", numProcs, actualNumProcs)
2016-03-07 04:31:13 +09:00
}
}