From c8d67edf2cbe1ce48675bd41eb794e6aadbdb6c1 Mon Sep 17 00:00:00 2001 From: codeskyblue Date: Thu, 21 Feb 2019 20:23:29 +0800 Subject: [PATCH] format code and remove useless code --- README.md | 2 +- assets/js/index.js | 25 +++++++--------- httpstaticserver.go | 70 +++++++++++++++++---------------------------- oauth2-proxy.go | 6 ++-- 4 files changed, 40 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index 02b8d84..8c7774c 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ docker run -it --rm -p 8000:8000 -v $PWD:/app/public --name gohttpserver \ $ gohttpserver --auth-type openid --auth-openid https://login.example-hostname.com/openid/ ``` -- Use oauth2 with +- Use oauth2 with (TODO: need more details) ```sh $ gohttpserver --auth-type oauth2-proxy diff --git a/assets/js/index.js b/assets/js/index.js index 76f5046..7dde5da 100644 --- a/assets/js/index.js +++ b/assets/js/index.js @@ -235,7 +235,7 @@ var vm = new Vue({ $.ajax({ url: pathJoin(["/", location.pathname, f.name]), data: { - op: "info", + op: "info", }, method: "GET", success: function (res) { @@ -254,9 +254,6 @@ var vm = new Vue({ } $.ajax({ url: pathJoin(["/", location.pathname, "/", name]), - data: { - op: "mkdir", - }, method: "POST", success: function (res) { console.log(res) @@ -348,10 +345,10 @@ window.onpopstate = function (event) { function loadFileOrDir(reqPath) { let requestUri = reqPath + location.search var retObj = loadFileList(requestUri) - if(retObj !== null) { - retObj.done(function (value) { - window.history.pushState({}, "", requestUri); - }); + if (retObj !== null) { + retObj.done(function () { + window.history.pushState({}, "", requestUri); + }); } } @@ -374,12 +371,12 @@ function loadFileList(pathname) { vm.auth = res.auth; }, error: function (jqXHR, textStatus, errorThrown) { - let errMsg = jqXHR.getResponseHeader("x-auth-authentication-message") - if(errMsg==null){ - errMsg = jqXHR.statusText - } - alert(String(jqXHR.status).concat(":", errMsg)); - console.error(errMsg) + let errMsg = jqXHR.getResponseHeader("x-auth-authentication-message") + if (errMsg == null) { + errMsg = jqXHR.statusText + } + alert(String(jqXHR.status).concat(":", errMsg)); + console.error(errMsg) }, }); diff --git a/httpstaticserver.go b/httpstaticserver.go index 6001a54..20afbd5 100644 --- a/httpstaticserver.go +++ b/httpstaticserver.go @@ -84,20 +84,20 @@ func NewHTTPStaticServer(root string) *HTTPStaticServer { }() m.HandleFunc("/-/status", s.hStatus) //unused - m.HandleFunc("/-/zip/{path:.*}", s.hZip) //unused - m.HandleFunc("/-/unzip/{zip_path:.*}/-/{path:.*}", s.hUnzip) //unused - m.HandleFunc("/-/json/{path:.*}", s.hJSONList) //unused + m.HandleFunc("/-/zip/{path:.*}", s.hZip) + m.HandleFunc("/-/unzip/{zip_path:.*}/-/{path:.*}", s.hUnzip) + m.HandleFunc("/-/json/{path:.*}", s.hJSONList) // routers for Apple *.ipa - m.HandleFunc("/-/ipa/plist/{path:.*}", s.hPlist) //unused - m.HandleFunc("/-/ipa/link/{path:.*}", s.hIpaLink) //unused + m.HandleFunc("/-/ipa/plist/{path:.*}", s.hPlist) + m.HandleFunc("/-/ipa/link/{path:.*}", s.hIpaLink) // TODO: /ipa/info - m.HandleFunc("/-/info/{path:.*}", s.hInfo) //unused - m.HandleFunc("/-/mkdir/{path:.*}", s.hMkdir) //unused + m.HandleFunc("/-/info/{path:.*}", s.hInfo) + m.HandleFunc("/-/mkdir/{path:.*}", s.hMkdir) - m.HandleFunc("/{path:.*}", s.hGet).Methods("GET", "HEAD") - m.HandleFunc("/{path:.*}", s.hPOST).Methods("POST") - m.HandleFunc("/{path:.*}", s.hDELETE).Methods("DELETE") + m.HandleFunc("/{path:.*}", s.hIndex).Methods("GET", "HEAD") + m.HandleFunc("/{path:.*}", s.hUploadOrMkdir).Methods("POST") + m.HandleFunc("/{path:.*}", s.hDelete).Methods("DELETE") return s } @@ -105,29 +105,6 @@ func (s *HTTPStaticServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.m.ServeHTTP(w, r) } -func (s *HTTPStaticServer) hGet(w http.ResponseWriter, r *http.Request) { - if r.FormValue("op") == "info" { - s.hInfo(w, r) - }else { - s.hIndex(w, r) - } - return -} - -func (s *HTTPStaticServer) hPOST(w http.ResponseWriter, r *http.Request) { - if r.FormValue("op") == "mkdir" { - s.hMkdir(w, r) - }else { - s.hUpload(w, r) - } - return -} - -func (s *HTTPStaticServer) hDELETE(w http.ResponseWriter, r *http.Request) { - s.hDelete(w, r) - return -} - func (s *HTTPStaticServer) hIndex(w http.ResponseWriter, r *http.Request) { path := mux.Vars(r)["path"] relPath := filepath.Join(s.Root, path) @@ -136,6 +113,11 @@ func (s *HTTPStaticServer) hIndex(w http.ResponseWriter, r *http.Request) { return } + if r.FormValue("op") == "info" { + s.hInfo(w, r) + return + } + log.Println("GET", path, relPath) if r.FormValue("raw") == "false" || isDir(relPath) { if r.Method == "HEAD" { @@ -188,7 +170,6 @@ func (s *HTTPStaticServer) hDelete(w http.ResponseWriter, req *http.Request) { // only can delete file now path := mux.Vars(req)["path"] auth := s.readAccessConf(path) - log.Printf("%#v", auth) if !auth.canDelete(req) { http.Error(w, "Delete forbidden", http.StatusForbidden) return @@ -201,7 +182,7 @@ func (s *HTTPStaticServer) hDelete(w http.ResponseWriter, req *http.Request) { w.Write([]byte("Success")) } -func (s *HTTPStaticServer) hUpload(w http.ResponseWriter, req *http.Request) { +func (s *HTTPStaticServer) hUploadOrMkdir(w http.ResponseWriter, req *http.Request) { path := mux.Vars(req)["path"] dirpath := filepath.Join(s.Root, path) @@ -220,14 +201,15 @@ func (s *HTTPStaticServer) hUpload(w http.ResponseWriter, req *http.Request) { http.Error(w, "Directory create "+err.Error(), http.StatusInternalServerError) return } - if file == nil { - w.Header().Set("Content-Type", "application/json;charset=utf-8") - json.NewEncoder(w).Encode(map[string]interface{}{ - "success": true, - "destination": dirpath, - }) - return - } + } + + if file == nil { // only mkdir + w.Header().Set("Content-Type", "application/json;charset=utf-8") + json.NewEncoder(w).Encode(map[string]interface{}{ + "success": true, + "destination": dirpath, + }) + return } if err != nil { @@ -337,7 +319,7 @@ func (s *HTTPStaticServer) hInfo(w http.ResponseWriter, r *http.Request) { fji.Type = "apk" fji.Extra = parseApkInfo(relPath) case "": - fji.Type = "Dir" + fji.Type = "dir" default: fji.Type = "text" } diff --git a/oauth2-proxy.go b/oauth2-proxy.go index 6e7faf8..9b9215d 100644 --- a/oauth2-proxy.go +++ b/oauth2-proxy.go @@ -1,15 +1,14 @@ package main import ( - "net/http" "encoding/json" + "net/http" "net/url" ) func handleOauth2() { - http.HandleFunc("/-/user", func(w http.ResponseWriter, r *http.Request) { - fullNameMap, _:= url.ParseQuery(r.Header.Get("X-Auth-Request-Fullname")) + fullNameMap, _ := url.ParseQuery(r.Header.Get("X-Auth-Request-Fullname")) var fullName string for k := range fullNameMap { fullName = k @@ -25,5 +24,4 @@ func handleOauth2() { data, _ := json.Marshal(user) w.Write(data) }) - }