package main import ( "fmt" "io" "net/http" "os" "time" "github.com/jmoiron/sqlx" _ "github.com/mattn/go-sqlite3" ) // This is a function that takes a long time to run // Useful for demonstrating concurrency (goroutines) func delayPrint() { time.Sleep(5 * time.Second) fmt.Printf("delay print\n") } // This is what a handler looks like func getRoot(w http.ResponseWriter, r *http.Request) { fmt.Printf("got / request\n") go delayPrint() io.WriteString(w, "This is my website!\n") } func main() { println("Starting server...") // Check for the environment variable dbpath := os.Getenv("SQLITE_DB_PATH") // Default to something reasonable if dbpath == "" { dbpath = "./db.sqlite3" } // Open the database // db, err := sqlx.Connect("sqlite3", ":memory:") db, err := sqlx.Connect("sqlite3", dbpath) if err != nil { panic(err) } defer db.Close() err = db.Ping() if err != nil { panic(err) } // 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) // Start the server on port 8080 err = http.ListenAndServe(":8080", nil) if err != nil { panic(err) } }