Compare commits
	
		
			No commits in common. "5199576a8713898e86e7ea4b6e6b0a1186b9f587" and "4af9d5d9c3a69029ca5d95ae746988bf45e6cdc8" have entirely different histories.
		
	
	
		
			5199576a87
			...
			4af9d5d9c3
		
	
		
					 2 changed files with 9 additions and 89 deletions
				
			
		| 
						 | 
				
			
			@ -18,49 +18,15 @@ func delayPrint() {
 | 
			
		|||
	fmt.Printf("delay print\n")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Reads files from the migrations directory and runs them
 | 
			
		||||
func runMigrations(db *sqlx.DB) error {
 | 
			
		||||
	// Read the migrations directory
 | 
			
		||||
	files, err := os.ReadDir("migrations")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Run each migration file
 | 
			
		||||
	for _, file := range files {
 | 
			
		||||
		// Skip directories
 | 
			
		||||
		if file.IsDir() {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Read the migration file
 | 
			
		||||
		content, err := os.ReadFile("migrations/" + file.Name())
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Execute the migration SQL
 | 
			
		||||
		_, err = db.Exec(string(content))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Printf("Error executing migration %s: %s\n", file.Name(), err)
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		fmt.Printf("Migration %s executed\n", file.Name())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Println("Migrations complete")
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// This is what a handler looks like
 | 
			
		||||
func handler(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	fmt.Println("Request received")
 | 
			
		||||
	go delayPrint() // This will run in the background
 | 
			
		||||
func getRoot(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	fmt.Printf("got / request\n")
 | 
			
		||||
	go delayPrint()
 | 
			
		||||
	io.WriteString(w, "This is my website!\n")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func dbConnect() *sqlx.DB {
 | 
			
		||||
func main() {
 | 
			
		||||
	println("Starting server...")
 | 
			
		||||
	// Check for the environment variable
 | 
			
		||||
	dbpath := os.Getenv("SQLITE_DB_PATH")
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -75,60 +41,21 @@ func dbConnect() *sqlx.DB {
 | 
			
		|||
	if err != nil {
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
	defer db.Close()
 | 
			
		||||
 | 
			
		||||
	err = db.Ping()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Run the migrations
 | 
			
		||||
	err = runMigrations(db)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return db
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	println("Starting server...")
 | 
			
		||||
 | 
			
		||||
	// Connect to the database
 | 
			
		||||
	db := dbConnect()
 | 
			
		||||
	defer db.Close()
 | 
			
		||||
 | 
			
		||||
	// Tx is a transaction
 | 
			
		||||
	tx := db.MustBegin()
 | 
			
		||||
	tx.MustExec("INSERT INTO users (username, password) VALUES (?, ?)", "John Doe", "a")
 | 
			
		||||
	err := tx.Commit()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// This is how you query the database
 | 
			
		||||
	rows, err := db.Queryx("SELECT * FROM users")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for rows.Next() {
 | 
			
		||||
		var user struct {
 | 
			
		||||
			ID       int
 | 
			
		||||
			Username string
 | 
			
		||||
			Password string
 | 
			
		||||
		}
 | 
			
		||||
		err = rows.StructScan(&user)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			panic(err)
 | 
			
		||||
		}
 | 
			
		||||
		fmt.Printf("User: %d %s %s\n", user.ID, user.Username, user.Password)
 | 
			
		||||
	}
 | 
			
		||||
	// Slide into the database dms with some slick SQL
 | 
			
		||||
	db.MustExec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, email TEXT)")
 | 
			
		||||
 | 
			
		||||
	// Mounting the handler
 | 
			
		||||
	// http.HandleFunc("/", getRoot)
 | 
			
		||||
 | 
			
		||||
	fs := http.FileServer(http.Dir("static"))
 | 
			
		||||
	http.Handle("/", fs)
 | 
			
		||||
	http.HandleFunc("/hello", handler)
 | 
			
		||||
 | 
			
		||||
	// Start the server on port 8080
 | 
			
		||||
	err = http.ListenAndServe(":8080", nil)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +0,0 @@
 | 
			
		|||
PRAGMA foreign_keys = ON;
 | 
			
		||||
 | 
			
		||||
CREATE TABLE IF NOT EXISTS users (
 | 
			
		||||
  id SERIAL PRIMARY KEY,
 | 
			
		||||
  username VARCHAR(255) NOT NULL,
 | 
			
		||||
  password VARCHAR(255) NOT NULL
 | 
			
		||||
);
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue