Skip to content

Instantly share code, notes, and snippets.

@LemoNode
Last active March 6, 2020 17:23
Show Gist options
  • Save LemoNode/91521722ec6663019204c8918a79eb6b to your computer and use it in GitHub Desktop.
Save LemoNode/91521722ec6663019204c8918a79eb6b to your computer and use it in GitHub Desktop.
fart knockers
package main
import (
"flag"
"html/template"
"log"
"os/exec"
"net/http"
"strings"
"database/sql"
"github.com/gorilla/websocket"
_ "github.com/mattn/go-sqlite3"
)
type Data struct {
Vgrid []string
Dates []string
Teams []string
}
const index = "index.html"
const port = "localhost:8810"
const db_url = "data.sqlite3"
var upgrader = websocket.Upgrader{}
var users []string
func load_sql() Data {
var data Data
db, err := sql.Open("sqlite3", db_url)
if err != nil {
log.Println(err)
}
defer db.Close()
query := `SELECT vgrid, dates, team FROM my_table;`
rows, err := db.Query(query)
if err != nil {
log.Println(err)
}
defer rows.Close()
var (vgrid, dates, teams string)
for rows.Next() {
err := rows.Scan(&vgrid, &dates, &teams)
if err != nil {
log.Println(err)
}
data.Vgrid = append(data.Vgrid, vgrid)
data.Dates = append(data.Dates, dates)
data.Teams = append(data.Teams, teams)
}
for i := 0; i < len(data.Vgrid); i++ {
users = append(users, data.Vgrid[i])
}
return data
}
func add_sql(result string) string {
db, err := sql.Open("sqlite3", db_url)
if err != nil {
log.Println(err)
}
defer db.Close()
var data []string = strings.Split(result, ":")
query := `INSERT INTO my_table (vgrid, dates, team) VALUES ("` + data[0] + `", '', "` + data[1] + `");`
log.Println(query)
stmt, err := db.Prepare(query)
if err != nil {
log.Println(err)
}
stmt.Exec()
return "added"
}
func save_sql(result string) {
db, err := sql.Open("sqlite3", db_url)
if err != nil {
log.Println(err)
}
defer db.Close()
var ammount []string
if strings.Contains(result, " ") {
ammount = strings.Split(result, " ")
} else {
ammount = append(ammount, result)
}
for i := 0; i < len(ammount); i++ {
data := strings.Split(ammount[i], ":")
query := `UPDATE my_table SET dates="` + data[1] + `" WHERE vgrid="` + data[0] + `"; `
stmt, err := db.Prepare(query)
if err != nil {
log.Println(err)
}
stmt.Exec()
}
}
func echo(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer c.Close()
for {
msgtype, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
var data string = string(message)
var send string = "success"
if strings.Contains(data, "add||") {
var result []string = strings.Split(data, "||")
send = add_sql(result[1])
}
if strings.Contains(data, "save||") {
var result []string = strings.Split(data, "||")
save_sql(result[1])
}
if err = c.WriteMessage(msgtype, []byte(send)); err != nil {
log.Println(err)
break
}
}
}
func main() {
addr := flag.String("addr", port, "http service address")
flag.Parse()
log.SetFlags(0)
http.HandleFunc("/echo", echo)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var result Data = load_sql()
data := map[string][]string{
"vgrid": result.Vgrid,
"dates": result.Dates,
"teams": result.Teams,
}
t := template.Must(template.ParseGlob(index))
t.ExecuteTemplate(w, index, data)
})
exec.Command("cmd", "/C", "start chrome http://localhost:8810/").Run()
log.Fatal(http.ListenAndServe(*addr, nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment