diff options
-rw-r--r-- | cache.go | 44 | ||||
-rw-r--r-- | handlers.go | 3 | ||||
-rw-r--r-- | http.go | 4 | ||||
-rw-r--r-- | init.go | 18 | ||||
-rw-r--r-- | main.go | 6 |
5 files changed, 6 insertions, 69 deletions
diff --git a/cache.go b/cache.go index 14fb0b2..4cb1855 100644 --- a/cache.go +++ b/cache.go @@ -13,20 +13,16 @@ import ( "github.com/syndtr/goleveldb/leveldb" ) -// Checks whether it's time to refresh -// the cache. func checkCacheTime() bool { return time.Since(confObj.LastCache) > confObj.CacheInterval } -// Checks whether it's time to push -// the cache to the database func checkDBtime() bool { return time.Since(confObj.LastPush) > confObj.DBInterval } -// Launched by init as a goroutine to constantly watch -// for the update interval to pass. +// Launched by init as a coroutine to watch +// for the update intervals to pass. func cacheAndPush() { for { if checkCacheTime() { @@ -40,11 +36,8 @@ func cacheAndPush() { } } -// Refreshes the cache. func refreshCache() { - // Iterate over the registry and - // update each individual user. for k := range twtxtCache.Users { err := twtxtCache.UpdateUser(k) if err != nil { @@ -53,9 +46,6 @@ func refreshCache() { } } - // Re-scrape all the remote registries - // to see if they have any new users - // to add locally. remoteRegistries.Mu.RLock() for _, v := range remoteRegistries.List { err := twtxtCache.CrawlRemoteRegistry(v) @@ -72,16 +62,9 @@ func refreshCache() { // Pushes the registry's cache data to a local // database for safe keeping. func pushDatabase() error { - // Acquire the database from the aether. - // goleveldb is concurrency-safe, so we - // can immediately push it back into the - // channel for other functions to use. db := <-dbChan dbChan <- db - // Create a batch write job so it can - // be done at one time rather than - // per entry. twtxtCache.Mu.RLock() var dbBasket = &leveldb.Batch{} for k, v := range twtxtCache.Users { @@ -96,21 +79,16 @@ func pushDatabase() error { } twtxtCache.Mu.RUnlock() - // Save our list of remote registries to scrape. remoteRegistries.Mu.RLock() for k, v := range remoteRegistries.List { dbBasket.Put([]byte("remote*"+string(k)), []byte(v)) } remoteRegistries.Mu.RUnlock() - // Execute the batch job. if err := db.Write(dbBasket, nil); err != nil { return err } - // Update the last push time for - // our timer/watch function to - // reference. confObj.Mu.Lock() confObj.LastPush = time.Now() confObj.Mu.Unlock() @@ -118,19 +96,12 @@ func pushDatabase() error { return nil } -// Pulls registry data from the DB on startup. -// Iterates over the database one entry at a time. func pullDatabase() { - // Acquire the database from the aether. - // goleveldb is concurrency-safe, so we - // can immediately push it back into the - // channel for other functions to use. db := <-dbChan dbChan <- db iter := db.NewIterator(nil, nil) - // Read the database entry-by-entry for iter.Next() { key := string(iter.Key()) val := string(iter.Value()) @@ -140,9 +111,6 @@ func pullDatabase() { field := split[1] if urls != "remote" { - // Start with an empty Data struct. If - // there's already one in the cache, pull - // it and use it instead. data := registry.NewUser() twtxtCache.Mu.RLock() if _, ok := twtxtCache.Users[urls]; ok { @@ -160,9 +128,6 @@ func pullDatabase() { case "Date": data.Date = val case "Status": - // If we're looking at a Status entry in the DB, - // parse the time then add it to the TimeMap under - // data.Status thetime, err := time.Parse(time.RFC3339, split[2]) if err != nil { log.Printf("%v\n", err) @@ -170,16 +135,11 @@ func pullDatabase() { data.Status[thetime] = val } - // Push the data struct (back) into - // the cache. twtxtCache.Mu.Lock() twtxtCache.Users[urls] = data twtxtCache.Mu.Unlock() } else { - // If we've come across an entry for - // a remote twtxt registry to scrape, - // add it to our list. remoteRegistries.Mu.Lock() remoteRegistries.List = append(remoteRegistries.List, val) remoteRegistries.Mu.Unlock() diff --git a/handlers.go b/handlers.go index b25901d..9f1dd8c 100644 --- a/handlers.go +++ b/handlers.go @@ -51,7 +51,6 @@ func apiEndpointHandler(w http.ResponseWriter, r *http.Request) { return } - // if there's a query, execute it if r.FormValue("q") != "" || r.FormValue("url") != "" { err := apiEndpointQuery(w, r) if err != nil { @@ -168,7 +167,7 @@ func apiTagsHandler(w http.ResponseWriter, r *http.Request) { } // Serving the stylesheet virtually because -// files aren't served directly. +// files aren't served directly in getwtxt. func cssHandler(w http.ResponseWriter, r *http.Request) { // Sending the sha256 sum of the modtime in hexadecimal for the ETag header diff --git a/http.go b/http.go index de9eb3b..aaa3a2e 100644 --- a/http.go +++ b/http.go @@ -52,21 +52,18 @@ func ipMiddleware(hop http.Handler) http.Handler { }) } -// log output for 200s func log200(r *http.Request) { uip := getIPFromCtx(r.Context()) log.Printf("*** %v :: 200 :: %v %v\n", uip, r.Method, r.URL) } -// log output for 400s func log400(w http.ResponseWriter, r *http.Request, err string) { uip := getIPFromCtx(r.Context()) log.Printf("*** %v :: 400 :: %v %v :: %v\n", uip, r.Method, r.URL, err) http.Error(w, "400 Bad Request: "+err, http.StatusBadRequest) } -// log output for 404s func log404(w http.ResponseWriter, r *http.Request, err error) { uip := getIPFromCtx(r.Context()) @@ -74,7 +71,6 @@ func log404(w http.ResponseWriter, r *http.Request, err error) { http.Error(w, err.Error(), http.StatusNotFound) } -// log output for 500s func log500(w http.ResponseWriter, r *http.Request, err error) { uip := getIPFromCtx(r.Context()) diff --git a/init.go b/init.go index 3f3440c..a7fd4af 100644 --- a/init.go +++ b/init.go @@ -17,7 +17,6 @@ import ( const getwtxt = "0.1" -// command line flags var ( flagVersion *bool = pflag.BoolP("version", "v", false, "Display version information, then exit.") flagHelp *bool = pflag.BoolP("help", "h", false, "Display the help screen") @@ -25,7 +24,6 @@ var ( flagConfType *string = pflag.StringP("type", "t", "yml", "The filetype of the configuration file.") ) -// config object var confObj = &Configuration{} // signals to close the log file @@ -35,16 +33,12 @@ var closeLog = make(chan bool, 1) // initialization var dbChan = make(chan *leveldb.DB, 1) -// templates var tmpls *template.Template -// registry index var twtxtCache = registry.NewIndex() -// remote registry listing var remoteRegistries = &RemoteRegistries{} -// static assets cache var staticCache = &struct { index []byte indexMod time.Time @@ -94,8 +88,6 @@ func initConfig() { log.Printf("%v\n", err) log.Printf("Using defaults ...\n") } else { - // separate thread to watch for config file changes. - // will log event then run rebindConfig() viper.WatchConfig() viper.OnConfigChange(func(e fsnotify.Event) { log.Printf("Config file change detected. Reloading...\n") @@ -153,7 +145,6 @@ func initConfig() { func initLogging() { - // only open a log file if it's necessary confObj.Mu.RLock() if confObj.StdoutLogging { @@ -183,19 +174,18 @@ func initLogging() { log.SetOutput(logfile) } + confObj.Mu.RUnlock() } func rebindConfig() { - // signal to close the log file then wait confObj.Mu.RLock() if !confObj.StdoutLogging { closeLog <- true } confObj.Mu.RUnlock() - // reassign values to the config object confObj.Mu.Lock() confObj.LogFile = viper.GetString("LogFile") @@ -212,11 +202,9 @@ func rebindConfig() { confObj.Mu.Unlock() - // reinitialize logging initLogging() } -// Parse the HTML templates func initTemplates() *template.Template { return template.Must(template.ParseFiles("assets/tmpl/index.html")) } @@ -230,8 +218,6 @@ func initDatabase() { log.Fatalf("%v\n", err) } - // Send the database reference into - // the aether. dbChan <- db pullDatabase() @@ -250,7 +236,6 @@ func watchForInterrupt() { log.Printf("\n\nCaught %v. Cleaning up ...\n", sigint) confObj.Mu.RLock() - // Close the database cleanly log.Printf("Closing database connection to %v...\n", confObj.DBPath) db := <-dbChan if err := db.Close(); err != nil { @@ -258,7 +243,6 @@ func watchForInterrupt() { } if !confObj.StdoutLogging { - // signal to close the log file closeLog <- true } diff --git a/main.go b/main.go index 9e76c65..e9047cc 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,6 @@ func main() { index := mux.NewRouter().StrictSlash(true) api := index.PathPrefix("/api").Subrouter() - // Begin the path -> handler mapping index.Path("/"). Methods("GET"). HandlerFunc(indexHandler) @@ -77,16 +76,15 @@ func main() { Methods("GET"). HandlerFunc(apiTagsBaseHandler) - // Requests tweets with a specific tag + // Requests statuses with a specific tag api.Path("/{format:(?:plain)}/tags/{tags:[a-zA-Z0-9_-]+}"). Methods("GET"). HandlerFunc(apiTagsHandler) - // format the port for the http.Server object confObj.Mu.RLock() portnum := fmt.Sprintf(":%v", confObj.Port) confObj.Mu.RUnlock() - // defines options for the http server. + // handlers.CompressHandler gzips all responses. // Write/Read timeouts are self explanatory. server := &http.Server{ |