Skip to content

Commit

Permalink
add binding host option
Browse files Browse the repository at this point in the history
  • Loading branch information
sycki committed Jan 22, 2018
1 parent 1d30427 commit be7adc5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
5 changes: 4 additions & 1 deletion server/ctx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import (
)

var (
version = "mknote-v2.2.3"
version = "mknote-v2.2.4"
Config *config
)

type config struct {
HostName string
HomeDir string
LogFile string
LogLevel int
Expand All @@ -43,6 +44,7 @@ func init() {
workDir := filepath.Dir(self)

Config = &config{
"",
workDir,
workDir + "/log/mknote.log",
1,
Expand All @@ -62,6 +64,7 @@ func init() {
}
}

flag.StringVar(&Config.HostName, "hostname", Config.HostName, "binding hostname")
flag.StringVar(&Config.LogFile, "log-file", Config.LogFile, "set log output file")
flag.IntVar(&Config.LogLevel, "log-level", Config.LogLevel, "set log output level, 0...4")
flag.StringVar(&Config.ArticlesDir, "articles-dir", Config.ArticlesDir, "markdown files dir")
Expand Down
12 changes: 7 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package server

import (
"context"
"fmt"
"mknote/server/controller/page"
"mknote/server/controller/rest"
"mknote/server/ctx"
Expand Down Expand Up @@ -75,11 +74,10 @@ func securityRest(h http.HandlerFunc) http.HandlerFunc {

func securityHandler(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
visitEntry := fmt.Sprint(r.Method, r.RemoteAddr, "=>", r.RequestURI, "["+r.UserAgent()+"] ")
ctx.Info(visitEntry)
ctx.Info(r.Method, r.RemoteAddr, "=>", r.RequestURI, "["+r.UserAgent()+"] ")
defer func() {
if err := recover(); err != nil {
ctx.Error(visitEntry, err)
ctx.Error(r.Method, r.RemoteAddr, "=>", r.RequestURI, "["+r.UserAgent()+"]", err)
http.Error(w, "500", http.StatusInternalServerError)
}
}()
Expand All @@ -88,7 +86,11 @@ func securityHandler(h http.HandlerFunc) http.HandlerFunc {
}

func redirect80(w http.ResponseWriter, r *http.Request) {
target := "https://" + r.Host + r.URL.Path
hostname := ctx.Config.HostName
if hostname == "" {
hostname = r.Host
}
target := "https://" + hostname + r.URL.Path
if len(r.URL.RawQuery) > 0 {
target += "?" + r.URL.RawQuery
}
Expand Down

0 comments on commit be7adc5

Please sign in to comment.