From e53b843a93922f21289b9fc4958f1dfc47a49bdf Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Thu, 19 Dec 2019 21:21:56 -0500 Subject: some functions were missing comments. added those in where needed. --- svc/conf.go | 2 ++ svc/handlers.go | 7 +++++++ svc/http.go | 3 +++ svc/init.go | 4 ++++ svc/leveldb.go | 6 ++++++ svc/query.go | 6 ++++++ svc/sqlite.go | 6 ++++++ 7 files changed, 34 insertions(+) diff --git a/svc/conf.go b/svc/conf.go index b5b65ad..f3797c6 100644 --- a/svc/conf.go +++ b/svc/conf.go @@ -213,6 +213,8 @@ func bindConfig() { announceConfig() } +// Echoes chosen configuration options on startup and after +// a change in getwtxt.yml is detected. func announceConfig() { confObj.Mu.RLock() defer confObj.Mu.RUnlock() diff --git a/svc/handlers.go b/svc/handlers.go index cd0d920..ee58622 100644 --- a/svc/handlers.go +++ b/svc/handlers.go @@ -31,6 +31,8 @@ import ( "github.com/gorilla/mux" ) +// Takes the modtime of one of the static files, derives +// an FNV hash from it, then truncates it. func getEtagFromTime(modtime time.Time) string { shabytes, err := modtime.MarshalText() errLog("", err) @@ -38,11 +40,15 @@ func getEtagFromTime(modtime time.Time) string { return fmt.Sprintf("%x", bytes[:16]) } +// Takes the body of a response to a request, derives an +// FNV hash from it, then truncates it. Used for non-static +// responses. func getEtag(data []byte) string { bytes := fnv.New32().Sum(data) return fmt.Sprintf("%x", bytes[:16]) } +// Serves index.html and the stylesheet to go with it. func servStatic(w http.ResponseWriter, isCSS bool) error { pingAssets() staticCache.mu.RLock() @@ -89,6 +95,7 @@ func apiFormatHandler(w http.ResponseWriter, r *http.Request) { staticHandler(w, r) } +// Serves all tweets without pagination. func apiAllTweetsHandler(w http.ResponseWriter, r *http.Request) { out, err := twtxtCache.QueryAllStatuses() if err != nil { diff --git a/svc/http.go b/svc/http.go index 0c1d7e8..6350e9e 100644 --- a/svc/http.go +++ b/svc/http.go @@ -78,12 +78,15 @@ func ipMiddleware(hop http.Handler) http.Handler { }) } +// Appends a 200 OK to the request log func log200(r *http.Request) { useragent := r.Header["User-Agent"] uip := getIPFromCtx(r.Context()) reqLog.Printf("*** %v :: 200 :: %v %v :: %v\n", uip, r.Method, r.URL, useragent) } +// Appends a request of a given status code to the request +// log. Intended for errors. func errHTTP(w http.ResponseWriter, r *http.Request, err error, code int) { useragent := r.Header["User-Agent"] uip := getIPFromCtx(r.Context()) diff --git a/svc/init.go b/svc/init.go index b5f4061..00e107b 100644 --- a/svc/init.go +++ b/svc/init.go @@ -72,12 +72,15 @@ var remoteRegistries = &RemoteRegistries{ // the parsed landing page and the stylesheet. var staticCache = &staticAssets{} +// Logs an error that should cause a catastrophic +// failure of getwtxt func errFatal(context string, err error) { if err != nil { log.Fatalf(context+"%v\n", err.Error()) } } +// Logs non-fatal errors. func errLog(context string, err error) { if err != nil { log.Printf(context+"%v\n", err.Error()) @@ -101,6 +104,7 @@ func initSvc() { watchForInterrupt() } +// Responds to some command-line flags func checkFlags() { pflag.Parse() if *flagVersion { diff --git a/svc/leveldb.go b/svc/leveldb.go index 31f0953..16751ce 100644 --- a/svc/leveldb.go +++ b/svc/leveldb.go @@ -28,10 +28,13 @@ import ( "github.com/syndtr/goleveldb/leveldb" ) +// Wrapper type for the LevelDB connection type dbLevel struct { db *leveldb.DB } +// Called intermittently to commit registry data to +// a LevelDB database. func (lvl *dbLevel) push() error { twtxtCache.Mu.RLock() defer twtxtCache.Mu.RUnlock() @@ -57,6 +60,9 @@ func (lvl *dbLevel) push() error { return lvl.db.Write(dbBasket, nil) } +// Called on startup to retrieve previously-committed data +// from a LevelDB database. Stores the retrieved data in +// memory. func (lvl *dbLevel) pull() { iter := lvl.db.NewIterator(nil, nil) twtxtCache.Mu.Lock() diff --git a/svc/query.go b/svc/query.go index 24541fc..7047df3 100644 --- a/svc/query.go +++ b/svc/query.go @@ -32,6 +32,8 @@ import ( "github.com/gorilla/mux" ) +// Wrapper to check if an error is non-nil, then +// log the error if applicable. func apiErrCheck(err error, r *http.Request) { if err != nil { uip := getIPFromCtx(r.Context()) @@ -39,6 +41,7 @@ func apiErrCheck(err error, r *http.Request) { } } +// Deduplicates a slice of strings func dedupe(list []string) []string { out := []string{} seen := make(map[string]bool) @@ -134,6 +137,8 @@ func apiEndpointQuery(w http.ResponseWriter, r *http.Request) error { return err } +// For composite queries, join the various slices of strings +// into a single slice of strings, then deduplicates them. func joinQueryOuts(data ...[]string) []string { single := []string{} for _, e := range data { @@ -142,6 +147,7 @@ func joinQueryOuts(data ...[]string) []string { return dedupe(single) } +// Performs a composite query against the statuses. func compositeStatusQuery(query string, r *http.Request) []string { var wg sync.WaitGroup var out, out2, out3 []string diff --git a/svc/sqlite.go b/svc/sqlite.go index 572651f..58a804d 100644 --- a/svc/sqlite.go +++ b/svc/sqlite.go @@ -28,12 +28,16 @@ import ( _ "github.com/mattn/go-sqlite3" // for the sqlite3 driver ) +// Wrapper containing a SQLite database connection, +// along with two prepared statements for pushing +// and pulling via said connection. type dbSqlite struct { db *sql.DB pullStmt *sql.Stmt pushStmt *sql.Stmt } +// Initializes a SQLite database. func initSqlite() *dbSqlite { confObj.Mu.RLock() dbpath := confObj.DBPath @@ -60,6 +64,7 @@ func initSqlite() *dbSqlite { } } +// Commits data from memory to a SQLite database intermittently. func (lite *dbSqlite) push() error { if err := lite.db.Ping(); err != nil { lite = initSqlite() @@ -104,6 +109,7 @@ func (lite *dbSqlite) push() error { return nil } +// Retrieves stored data from a SQLite database on startup. func (lite *dbSqlite) pull() { errLog("Error pinging sqlite DB: ", lite.db.Ping()) rows, err := lite.pullStmt.Query() -- cgit 1.4.1-2-gfad0