main.go 851 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. _ "github.com/mattn/go-sqlite3"
  6. "net/http"
  7. "os"
  8. )
  9. var Db *sql.DB
  10. func checkError(err error) {
  11. if err != nil {
  12. fmt.Fprintf(os.Stderr, "Error: %v\n", err.Error())
  13. panic(err)
  14. }
  15. }
  16. func requestHandler(w http.ResponseWriter, r *http.Request) {
  17. defer func() {
  18. if x := recover(); x != nil {
  19. return
  20. }
  21. }()
  22. r.ParseForm()
  23. path := r.URL.Path
  24. stmt, err := Db.Prepare("REPLACE INTO `alive` (`path`, `timestamp`) VALUES (?, CURRENT_TIMESTAMP)")
  25. checkError(err)
  26. _, err = stmt.Exec(path)
  27. checkError(err)
  28. fmt.Fprintf(w, "ok")
  29. }
  30. func main() {
  31. var err error
  32. Db, err = sql.Open("sqlite3", "db")
  33. checkError(err)
  34. http.HandleFunc("/", requestHandler)
  35. err = http.ListenAndServe(":60000", nil)
  36. if err != nil {
  37. fmt.Fprintf(os.Stderr, "http.ListenAndServer() Error: %v\n", err.Error())
  38. }
  39. }