Browse Source

Initial commit

Dmitry Yu Okunev 3 years ago
commit
e61af6c1b3
3 changed files with 65 additions and 0 deletions
  1. 6 0
      .gitignore
  2. 9 0
      Makefile
  3. 50 0
      main.go

+ 6 - 0
.gitignore

@@ -0,0 +1,6 @@
+run.sh
+outer_check_site
+.tmp
+db
+.profile
+.bash_history

+ 9 - 0
Makefile

@@ -0,0 +1,9 @@
+
+all: outer_check_site
+
+outer_check_site:
+	go build
+
+clean:
+	go clean
+

+ 50 - 0
main.go

@@ -0,0 +1,50 @@
+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())
+	}
+}