From 245076d017f26e12c162c08933fa3078cf19f63b Mon Sep 17 00:00:00 2001 From: Jordan Date: Sat, 18 Dec 2021 16:45:53 -0700 Subject: web, db: add processed URL index, housekeeping --- keep.go | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) (limited to 'keep.go') diff --git a/keep.go b/keep.go index 2a93f2c..8457ff2 100644 --- a/keep.go +++ b/keep.go @@ -25,6 +25,8 @@ type Config struct { Token string `json:"token"` Verbose bool `json:"verbose"` Ignore []string `json:"ignore"` + Host string `json:"host"` + Port string `json:"port"` } type Message struct { @@ -34,6 +36,10 @@ type Message struct { Channel string } +type SqliteDB struct { + db *sql.DB +} + var ( messageChan chan *Message config Config @@ -63,12 +69,19 @@ func main() { } // Create and initialize URL cache database - db := initDB(path.Join(keepDir, "keep.db")) + sqlSqliteDB := initDB(path.Join(keepDir, "keep.db")) + db := &SqliteDB{db: sqlSqliteDB} // Channel for passing URLs to the archive goroutine for archival messageChan = make(chan *Message, 25) go archiver(db) + // Start HTTP server + http.HandleFunc("/", db.IndexHandler) + log.Printf("Listening on %v port %v (http://%v:%v/)\n", config.Host, + config.Port, config.Host, config.Port) + go http.ListenAndServe(fmt.Sprintf("%s:%s", config.Host, config.Port), nil) + // Create a new Discord session using provided credentials dg, err := discordgo.New(config.Token) if err != nil { @@ -87,11 +100,11 @@ func main() { dg.Identify.Intents = discordgo.IntentsGuildMessages // Open a websocket connection to Discord and begin listening - err = dg.Open() - if err != nil { - fmt.Println("error opening connection,", err) - return - } + //err = dg.Open() + //if err != nil { + // fmt.Println("error opening connection,", err) + // return + //} // Wait here until CTRL-C or other term signal is received sc := make(chan os.Signal, 1) @@ -104,7 +117,7 @@ func main() { // archiver is intended to be run in its own goroutine, receiving URLs from main // over a shared channel for processing -func archiver(db *sql.DB) { +func archiver(db *SqliteDB) { // Each iteration removes and processes one url from the channel for { @@ -113,7 +126,7 @@ func archiver(db *sql.DB) { message := <-messageChan // Skip if we have URL in database - cached, _ := isCached(db, message.URL) + cached, _ := db.IsCached(message.URL) if cached { continue } @@ -121,14 +134,14 @@ func archiver(db *sql.DB) { // Skip if the Internet Archive already has a copy available archived, status_code := isArchived(message.URL) if archived && status_code == http.StatusOK { - addArchived(db, message, status_code) + db.AddArchived(message, status_code) log.Printf("SKIP %d %s", status_code, message.URL) continue } // Archive, URL is not present in cache or IA status_code = archive(message.URL) - addArchived(db, message, status_code) + db.AddArchived(message, status_code) log.Printf("SAVE %d %s", status_code, message.URL) // Limit requests to Wayback API to 5-second intervals -- cgit v1.2.3-54-g00ecf