aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar alecdwm 2015-10-07 17:43:04 +1000
committerGravatar alecdwm 2015-10-07 17:46:25 +1000
commitf03aad04b63e5edf61ac75a32464cd0d59cb4b57 (patch)
tree916df9d84ea77b9a7795323ca24eafc3a795adde
parent6e9a8abe81d409f74cb2a8d063497b63a7d4af7d (diff)
Multiple Improvements
+ More clearly defined the split between CLI and LIB + Replaces global variables in gogetme.go with a closure to pass variables to http handler + Updated various Todo comments in the code + Variable renaming: gitHubUser -> githubUser gitHubUserAvatarURL -> githubUserAvatarURL
-rw-r--r--lib/gogetme.go91
-rw-r--r--main.go48
2 files changed, 64 insertions, 75 deletions
diff --git a/lib/gogetme.go b/lib/gogetme.go
index 289111f..cc4553a 100644
--- a/lib/gogetme.go
+++ b/lib/gogetme.go
@@ -17,20 +17,12 @@
package lib
import (
- "bufio"
"encoding/json"
- "flag"
"fmt"
"net/http"
- "os"
- "strings"
"time"
)
-var customUrl string
-var gitHubUser string
-var gitHubUserAvatarURL string
-
type GitHubUserData struct {
Login string `json:"login"`
Id int `json:"id"`
@@ -72,15 +64,16 @@ func getGitHubUserData(username string) *GitHubUserData {
handleError("Fetching GitHub user data", err)
// Decode
- gitHubUserData := new(GitHubUserData)
- err = json.NewDecoder(response.Body).Decode(&gitHubUserData)
+ githubUserData := new(GitHubUserData)
+ err = json.NewDecoder(response.Body).Decode(&githubUserData)
handleError("Parsing response as JSON", err)
// Return
- return gitHubUserData
+ return githubUserData
}
-func handler(w http.ResponseWriter, r *http.Request) {
+func handler(w http.ResponseWriter, r *http.Request,
+ customUrl, githubUser, githubUserAvatarURL string) {
t := time.Now()
fmt.Printf(
"[%s] Handled connection from: %s\n",
@@ -199,79 +192,31 @@ func handler(w http.ResponseWriter, r *http.Request) {
</body>
</html>
`,
- customUrl+"/"+r.URL.Path[1:], gitHubUser+"/"+r.URL.Path[1:], // go-import
+ customUrl+"/"+r.URL.Path[1:], githubUser+"/"+r.URL.Path[1:], // go-import
customUrl+"/"+r.URL.Path[1:], // page title
pageCss, // page custom css
- gitHubUser, gitHubUserAvatarURL, // github avatar img
- gitHubUser, gitHubUser, // github username
+ githubUser, githubUserAvatarURL, // github avatar img
+ githubUser, githubUser, // github username
customUrl+"/"+r.URL.Path[1:], // go get -u [project url]
- gitHubUser+"/"+r.URL.Path[1:], "Visit the project on GitHub")
+ githubUser+"/"+r.URL.Path[1:], "Visit the project on GitHub")
fmt.Fprint(w, pageHtml)
}
-func GoGetMe() {
- // Get ready to GO!
- listenPort := ":9999"
- flag.StringVar(
- &customUrl,
- "customUrl",
- "unset", "URL to host your Go packages at")
-
- flag.StringVar(
- &gitHubUser,
- "gitHubUser",
- "unset",
- "your GitHub username")
-
- flag.StringVar(&listenPort,
- "listenPort",
- "unset",
- "port to listen for connections on")
-
- flag.Parse()
-
- reader := bufio.NewReader(os.Stdin)
- if customUrl == "unset" {
- fmt.Print("Input Custom Go Import URL > ")
-
- input, err := reader.ReadString('\n')
- handleError("Reading input string", err)
-
- customUrl = strings.Trim(input, "\n")
- }
- fmt.Printf("\nCustom Go Import URL set to '%s'\n", customUrl)
-
- if gitHubUser == "unset" {
- fmt.Print("\nInput GitHub Username > ")
-
- input, err := reader.ReadString('\n')
- handleError("Reading input string", err)
-
- gitHubUser = strings.Trim(input, "\n")
- }
- fmt.Printf("\nGitHub Username set to '%s'\n", gitHubUser)
-
- gitHubUserAvatarURL = getGitHubUserData(gitHubUser).AvatarUrl
- fmt.Printf("\nGitHub Avatar URL set to '%s'\n", gitHubUserAvatarURL)
-
- if listenPort == "unset" {
- fmt.Print("\nInput port to listen on > ")
-
- input, err := reader.ReadString('\n')
- handleError("Reading input string", err)
+func Serve(customUrl, githubUser, listenPort string) {
+ // Process inputs
+ githubUserAvatarURL := getGitHubUserData(githubUser).AvatarUrl
+ listenPort = ":" + listenPort
- listenPort = ":" + strings.Trim(input, "\n")
- } else {
- listenPort = ":" + listenPort
- }
- fmt.Printf("\nListen port set to '%s'\n", listenPort)
+ // Prepare handler
+ // Todo: Add separate handler func for / vs /*, / is profile page for user
+ http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+ handler(w, r, customUrl, githubUser, githubUserAvatarURL)
+ })
// Listen and SERVE!
fmt.Print("\nHandling connections:\n")
- http.HandleFunc("/", handler)
- // Todo: ListenAndServeTLS
err := http.ListenAndServe(listenPort, nil)
handleError("Serving HTTP requests", err)
}
diff --git a/main.go b/main.go
index 07282c5..2c8f924 100644
--- a/main.go
+++ b/main.go
@@ -16,8 +16,52 @@
package main
-import gogetme "go.owls.io/gogetme/lib"
+import (
+ "bufio"
+ "flag"
+ "fmt"
+ "os"
+ "strings"
+
+ gogetme "go.owls.io/gogetme/lib"
+)
+
+func checkVarPromptUnset(promptVar *string, promptDesc string) {
+ if *promptVar == "unset" {
+ fmt.Printf("%s > ", promptDesc)
+
+ input, err := bufio.NewReader(os.Stdin).ReadString('\n')
+ if err != nil {
+ fmt.Println(err)
+ panic("Error: Reading input string")
+ }
+
+ *promptVar = strings.Trim(input, "\n")
+ }
+ fmt.Printf("\n%s set to '%s'\n\n", promptDesc, *promptVar)
+}
func main() {
- gogetme.GoGetMe()
+ // Get ready to GO!
+ var customUrl string
+ var githubUser string
+ var listenPort string
+
+ flag.StringVar(&customUrl, "customUrl", "unset",
+ "URL to host your Go packages at")
+
+ flag.StringVar(&githubUser, "githubUser", "unset",
+ "your GitHub username")
+
+ flag.StringVar(&listenPort, "listenPort", "unset",
+ "port to listen for connections on")
+
+ flag.Parse()
+
+ checkVarPromptUnset(&customUrl, "Custom 'go get' url")
+ checkVarPromptUnset(&githubUser, "GitHub username")
+ checkVarPromptUnset(&listenPort, "Port to listen on")
+
+ // GOGOGO!
+ gogetme.Serve(customUrl, githubUser, listenPort)
}