Skip to content

Commit

Permalink
add log
Browse files Browse the repository at this point in the history
  • Loading branch information
sters committed Oct 26, 2021
1 parent dd051be commit aad82ce
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
2 changes: 2 additions & 0 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type c struct {
RepositoriesDirectory string
KeyDirectoryRelatedFromRepository string
HTTPHeaderKey string
AccessLog bool
logger *zap.Logger
}

Expand All @@ -44,6 +45,7 @@ func Init() {
RepositoriesDirectory: getenv("REPOSITORIES_DIRECTORY", "repositories/"),
KeyDirectoryRelatedFromRepository: getenv("KEY_DIRECTORY_RELATED_FROM_REPOSITORY", "."),
HTTPHeaderKey: getenv("HTTP_HEADER_KEY", "onstaticonstaticonstatic"),
AccessLog: getenvBool("ACCESS_LOG", true),
logger: logger(
zapcore.InfoLevel,
getenv("STDLOG_OUTPUT_PATH", "stdout"), // "/var/log/onstatic/stdout.log"),
Expand Down
23 changes: 19 additions & 4 deletions onstatic/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,25 @@ var fileserver http.Handler
func RegisterHandler(s *http.ServeMux) {
fileserver = http.FileServer(http.Dir(getRepositoriesDir()))

s.HandleFunc("/register", handleRegister)
s.HandleFunc("/pull", handlePull)
s.HandleFunc("/unregister", handleUnregister)
s.HandleFunc("/", handleAll)
for path, handler := range map[string]http.HandlerFunc{
"/register": http.HandlerFunc(handleRegister),
"/pull": http.HandlerFunc(handlePull),
"/unregister": http.HandlerFunc(handleUnregister),
"/": http.HandlerFunc(handleAll),
} {
handle := handler
if conf.Variables.AccessLog {
handle = func(rw http.ResponseWriter, r *http.Request) {
zap.L().Info(
"access log",
zap.String("path", r.URL.Path),
)
handler(rw, r)
}
}

s.HandleFunc(path, handle)
}
}

func handleRegister(res http.ResponseWriter, req *http.Request) {
Expand Down

0 comments on commit aad82ce

Please sign in to comment.