Skip to content

Commit 100cd18

Browse files
committed
Add router log config option
1 parent 03c2468 commit 100cd18

File tree

8 files changed

+32
-9
lines changed

8 files changed

+32
-9
lines changed

conf/app.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ LICENSES = Apache v2 License|GPL v2|MIT License|Affero GPL|Artistic License 2.0|
1515
[server]
1616
PROTOCOL = http
1717
DOMAIN = localhost
18-
ROOT_URL = %(PROTOCOL)s://%(DOMAIN)s:3000/
18+
ROOT_URL = %(PROTOCOL)s://%(DOMAIN)s:%(HTTP_PORT)s/
19+
HTTP_ADDR =
20+
HTTP_PORT = 3000
1921
; Disable CDN even in "prod" mode
2022
OFFLINE_MODE = false
23+
ROUTER_LOG = true
2124
; Generate steps:
2225
; $ cd path/to/gogs/custom/https
2326
; $ go run $GOROOT/src/pkg/crypto/tls/generate_cert.go -ca=true -duration=8760h0m0s -host=myhost.example.com

models/models.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ func NewTestEngine(x *xorm.Engine) (err error) {
5858
case "postgres":
5959
var host, port = "127.0.0.1", "5432"
6060
fields := strings.Split(DbCfg.Host, ":")
61-
if len(fields) > 0 {
61+
if len(fields) > 0 && len(strings.TrimSpace(fields[0])) > 0 {
6262
host = fields[0]
6363
}
64-
if len(fields) > 1 {
64+
if len(fields) > 1 && len(strings.TrimSpace(fields[1])) > 0 {
6565
port = fields[1]
6666
}
6767
cnnstr := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s",
@@ -91,10 +91,10 @@ func SetEngine() (err error) {
9191
case "postgres":
9292
var host, port = "127.0.0.1", "5432"
9393
fields := strings.Split(DbCfg.Host, ":")
94-
if len(fields) > 0 {
94+
if len(fields) > 0 && len(strings.TrimSpace(fields[0])) > 0 {
9595
host = fields[0]
9696
}
97-
if len(fields) > 1 {
97+
if len(fields) > 1 && len(strings.TrimSpace(fields[1])) > 0 {
9898
port = fields[1]
9999
}
100100
orm, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s",

modules/base/conf.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ var (
5050
AppLogo string
5151
AppUrl string
5252
OfflineMode bool
53+
RouterLog bool
5354
ProdMode bool
5455
Domain string
5556
SecretKey string
@@ -327,6 +328,7 @@ func NewConfigContext() {
327328
AppUrl = Cfg.MustValue("server", "ROOT_URL")
328329
Domain = Cfg.MustValue("server", "DOMAIN")
329330
OfflineMode = Cfg.MustBool("server", "OFFLINE_MODE", false)
331+
RouterLog = Cfg.MustBool("server", "ROUTER_LOG", true)
330332
SecretKey = Cfg.MustValue("security", "SECRET_KEY")
331333

332334
InstallLock = Cfg.MustBool("security", "INSTALL_LOCK", false)

modules/middleware/context.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,19 @@ func (ctx *Context) RenderWithErr(msg, tpl string, form auth.Form) {
106106

107107
// Handle handles and logs error by given status.
108108
func (ctx *Context) Handle(status int, title string, err error) {
109-
log.Error("%s: %v", title, err)
110-
if martini.Dev != martini.Prod {
111-
ctx.Data["ErrorMsg"] = err
109+
if err != nil {
110+
log.Error("%s: %v", title, err)
111+
if martini.Dev != martini.Prod {
112+
ctx.Data["ErrorMsg"] = err
113+
}
112114
}
113115

116+
switch status {
117+
case 404:
118+
ctx.Data["Title"] = "Page Not Found"
119+
case 500:
120+
ctx.Data["Title"] = "Internal Server Error"
121+
}
114122
ctx.HTML(status, fmt.Sprintf("status/%d", status))
115123
}
116124

modules/middleware/logger.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"time"
1313

1414
"github.com/go-martini/martini"
15+
16+
"github.com/gogits/gogs/modules/base"
1517
)
1618

1719
var isWindows bool
@@ -22,6 +24,10 @@ func init() {
2224

2325
func Logger() martini.Handler {
2426
return func(res http.ResponseWriter, req *http.Request, ctx martini.Context, log *log.Logger) {
27+
if !base.RouterLog {
28+
return
29+
}
30+
2531
start := time.Now()
2632
log.Printf("Started %s %s", req.Method, req.URL.Path)
2733

routers/admin/admin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ func Config(ctx *middleware.Context) {
140140
ctx.Data["AppUrl"] = base.AppUrl
141141
ctx.Data["Domain"] = base.Domain
142142
ctx.Data["OfflineMode"] = base.OfflineMode
143+
ctx.Data["RouterLog"] = base.RouterLog
143144
ctx.Data["RunUser"] = base.RunUser
144145
ctx.Data["RunMode"] = strings.Title(martini.Env)
145146
ctx.Data["RepoRootPath"] = base.RepoRootPath

templates/admin/config.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
<dd>{{.Domain}}</dd>
2121
<dt>Offline Mode</dt>
2222
<dd><i class="fa fa{{if .OfflineMode}}-check{{end}}-square-o"></i></dd>
23+
<dt>Router Log</dt>
24+
<dd><i class="fa fa{{if .RouterLog}}-check{{end}}-square-o"></i></dd>
2325
<hr/>
2426
<dt>Run User</dt>
2527
<dd>{{.RunUser}}</dd>

web.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func newMartini() *martini.ClassicMartini {
4242
m := martini.New()
4343
m.Use(middleware.Logger())
4444
m.Use(martini.Recovery())
45-
m.Use(martini.Static("public"))
45+
m.Use(martini.Static("public", martini.StaticOptions{SkipLogging: !base.RouterLog}))
4646
m.MapTo(r, (*martini.Routes)(nil))
4747
m.Action(r.Handle)
4848
return &martini.ClassicMartini{m, r}
@@ -208,4 +208,5 @@ func runWeb(*cli.Context) {
208208
qlog.Error(err.Error())
209209
}
210210
}
211+
qlog.Fatalf("Invalid protocol: %s", protocol)
211212
}

0 commit comments

Comments
 (0)