Skip to content

Commit

Permalink
Fix #15: Add basic support for expvar
Browse files Browse the repository at this point in the history
  • Loading branch information
andygrunwald committed Dec 23, 2016
1 parent 2b122a8 commit 32a89d2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ $ ./TrendingGithub -help
Usage of ./TrendingGithub:
-debug
Outputs the tweet instead of tweet it (useful for development). Env var: TRENDINGGITHUB_DEBUG
-expvar-port int
Port which will be used for the expvar TCP server. Env var: TRENDINGGITHUB_EXPVAR_PORT (default 8123)
-storage-auth string
Storage Auth (e.g. myPassword or <empty>). Env var: TRENDINGGITHUB_STORAGE_AUTH
-storage-url string
Expand All @@ -53,7 +55,7 @@ Usage of ./TrendingGithub:
-twitter-access-token-secret string
Twitter-API: Access token secret. Env var: TRENDINGGITHUB_TWITTER_ACCESS_TOKEN_SECRET
-twitter-conf-refresh-time duration
Twitter: Time interval to refresh the configuration of twitter (e.g. char length for short url). Default: 24h. Env var: TRENDINGGITHUB_TWITTER_CONF_REFRESH_TIME (default 24h0m0s)
Twitter: Time interval to refresh the configuration of twitter (e.g. char length for short url). Env var: TRENDINGGITHUB_TWITTER_CONF_REFRESH_TIME (default 24h0m0s)
-twitter-consumer-key string
Twitter-API: Consumer key. Env var: TRENDINGGITHUB_TWITTER_CONSUMER_KEY
-twitter-consumer-secret string
Expand Down
25 changes: 25 additions & 0 deletions expvar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
_ "expvar"
"fmt"
"log"
"net"
"net/http"
)

// StartExpvarServer will start a small tcp server for the expvar package.
// This server is only available via localhost on localhost:port/debug/vars
func StartExpvarServer(port int) error {
sock, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port))
if err != nil {
return err
}

go func() {
log.Printf("Expvar initialisation success: HTTP now available at http://localhost:%d/debug/vars", port)
http.Serve(sock, nil)
}()

return nil
}
15 changes: 15 additions & 0 deletions flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ func String(name, env, fallback, help string) *string {
return flag.String(name, value, help)
}

// Int registers a flag and returns the pointer to the resulting int.
// The default value is passed as fallback and env sets the env variable
// that can override the default.
func Int(name, env string, fallback int, help string) *int {
value := fallback
if v := os.Getenv(env); v != "" {
if i, err := strconv.Atoi(v); err == nil {
value = i
}

}

return flag.Int(name, value, help)
}

// Duration registers a flag and returns the pointer to the resulting duration.
// The default value is passed as fallback and env sets the env variable
// that can override the default.
Expand Down
11 changes: 9 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func main() {
storageURL = flags.String("storage-url", "TRENDINGGITHUB_STORAGE_URL", ":6379", "Storage URL (e.g. 1.2.3.4:6379 or :6379). Env var: TRENDINGGITHUB_STORAGE_URL")
storageAuth = flags.String("storage-auth", "TRENDINGGITHUB_STORAGE_AUTH", "", "Storage Auth (e.g. myPassword or <empty>). Env var: TRENDINGGITHUB_STORAGE_AUTH")

expVarPort = flags.Int("expvar-port", "TRENDINGGITHUB_EXPVAR_PORT", 8123, "Port which will be used for the expvar TCP server. Env var: TRENDINGGITHUB_EXPVAR_PORT")
showVersion = flags.Bool("version", "TRENDINGGITHUB_VERSION", false, "Outputs the version number and exit. Env var: TRENDINGGITHUB_VERSION")
debugMode = flags.Bool("debug", "TRENDINGGITHUB_DEBUG", false, "Outputs the tweet instead of tweet it (useful for development). Env var: TRENDINGGITHUB_DEBUG")
)
Expand All @@ -59,10 +60,9 @@ func main() {
log.Fatalf("Twitter Configuration initialisation failed: %s", err)
}
log.Printf("Twitter Configuration initialisation success: ShortUrlLength %d\n", twitterClient.Configuration.ShortUrlLength)
twitterClient.SetupConfigurationRefresh(*configurationRefreshTime)
}

twitterClient.SetupConfigurationRefresh(*configurationRefreshTime)

// Activate our growth hack feature
// Checkout the README for details or read the code (suggested).
if *twitterFollowNewPerson {
Expand All @@ -75,6 +75,13 @@ func main() {
defer storageBackend.Close()
log.Println("Storage backend initialisation success")

// Start the exvar server
err := StartExpvarServer(*expVarPort)
if err != nil {
log.Fatalf("Expvar initialisation failed: %s", err)
}
log.Println("Expvar initialisation started ...")

// Let the party begin
StartTweeting(twitterClient, storageBackend, *tweetTime)
}

0 comments on commit 32a89d2

Please sign in to comment.