123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package main
- import (
- "database/sql"
- "fmt"
- _ "github.com/mattn/go-sqlite3"
- "net/http"
- "os"
- )
- var Db *sql.DB
- func checkError(err error) {
- if err != nil {
- fmt.Fprintf(os.Stderr, "Error: %v\n", err.Error())
- panic(err)
- }
- }
- func requestHandler(w http.ResponseWriter, r *http.Request) {
- defer func() {
- if x := recover(); x != nil {
- return
- }
- }()
- r.ParseForm()
- path := r.URL.Path
- stmt, err := Db.Prepare("REPLACE INTO `alive` (`path`, `timestamp`) VALUES (?, CURRENT_TIMESTAMP)")
- checkError(err)
- _, err = stmt.Exec(path)
- checkError(err)
- fmt.Fprintf(w, "ok")
- }
- func main() {
- var err error
- Db, err = sql.Open("sqlite3", "db")
- checkError(err)
- http.HandleFunc("/", requestHandler)
- err = http.ListenAndServe(":60000", nil)
- if err != nil {
- fmt.Fprintf(os.Stderr, "http.ListenAndServer() Error: %v\n", err.Error())
- }
- }
|