handler-utils.go (675B)
1 package handlers 2 3 import ( 4 "database/sql" 5 "fmt" 6 "log" 7 "net/http" 8 "os" 9 "time" 10 11 "git.hanabi.in/dev/am-i-alive/config" 12 "git.hanabi.in/dev/am-i-alive/consts" 13 _ "github.com/mattn/go-sqlite3" 14 ) 15 16 func open_db() (db *sql.DB, err error) { 17 db, err = sql.Open(consts.SQLITE3, config.DB) 18 return db, err 19 } 20 21 func logRequest(r *http.Request) { 22 if config.ShouldLog { 23 file, err := os.OpenFile(config.Logfile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600) 24 defer file.Close() 25 if err != nil { 26 log.Fatalf("Error %v", err) 27 } 28 R := fmt.Sprintf("%s %#v -- %#v\n\n", time.Now(), r, r.URL) 29 if _, err = file.Write([]byte(R)); err != nil { 30 log.Fatalf("Error %v", err) 31 } 32 } 33 }