Last active
May 17, 2020 22:08
-
-
Save LemoNode/2de6dfd299d59880fc42c589b5c3fa72 to your computer and use it in GitHub Desktop.
simple sqlite/web server in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<head> | |
<meta charset="utf-8"> | |
<title>Pågåendelistan</title> | |
<link rel="stylesheet" type="text/css" href="style.css"> | |
</head> | |
<body onload="main()"> | |
<nav> | |
<div id="nav-title"> | |
<h2>Affektiva: Index</h2> | |
</div> | |
</nav> | |
<main> | |
<center><input type="password" id="password" placeholder="Lösenord"></center> | |
<div id="content"></div> | |
</main> | |
<script> | |
const token = "<|>"; | |
const dom = { | |
password: document.querySelector("#password"), | |
}; | |
const socket = new WebSocket("ws://" + document.location.host + "/echo"); | |
socket.onopen = e => { | |
console.log("loaded!"); | |
} | |
socket.onmessage = e => { | |
if (e.data == "error") { | |
dom.password.style.border = "1px solid crimson"; | |
alert("Fel lösenord, försök igen") | |
} else { | |
console.log(JSON.parse(e.data)); | |
dom.password.remove(); | |
} | |
} | |
events = () => { | |
events.password(dom.password); | |
} | |
events.password = element => { | |
element.onkeydown = e => { | |
if (e.key == "Enter") { | |
socket.send("<password>" + token + e.target.value); | |
dom.password.style.border = "1px solid #ccc"; | |
dom.password.value = ""; | |
} | |
} | |
} | |
main = () => { | |
events(); | |
} | |
</script> | |
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"html/template" | |
"log" | |
"os/exec" | |
"net/http" | |
"database/sql" | |
"strings" | |
"github.com/gorilla/websocket" | |
_ "github.com/CovenantSQL/go-sqlite3-encrypt" | |
) | |
// global states | |
const token = "<|>" | |
const port = "localhost:8810" | |
const index = "index.html" | |
var upgrader = websocket.Upgrader{} | |
// sql | |
func sql_open(location string, column []string) (map[string][]string, error) { | |
table := map[string][]string{} | |
db, err := sql.Open("sqlite3", location) | |
if err != nil { | |
return table, err | |
} | |
defer db.Close() | |
selection := strings.Join(column[:], ", ") | |
row, err := db.Query("SELECT " + selection + " FROM main;") | |
if err != nil { | |
return table, err | |
} | |
defer row.Close() | |
ptrs := make([]interface{}, len(column)) | |
copy := make([]interface{}, len(column)) | |
for i := 0; i < len(column); i++ { | |
ptrs[i] = ©[i] | |
} | |
for row.Next() { | |
err := row.Scan(ptrs...) | |
if err != nil { | |
log.Println(err) | |
} | |
for i, val := range copy { | |
if str, ok := val.(string); ok { | |
table[column[i]] = append(table[column[i]], str) | |
} | |
} | |
} | |
return table, nil | |
} | |
// echo | |
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(message) | |
if strings.Contains(data, "<password>") { | |
echo_table(msgtype, data, c) | |
} | |
} | |
} | |
func echo_table(msgtype int, message string, c *websocket.Conn) { | |
columns := []string{"personnummer", "namn", "vgrid_fvk"} | |
password := strings.Split(message, token)[1] | |
location := "file:pågåendelista.sqlite3?_crypto_key=" + password + ";" | |
table, err := sql_open(location, columns) | |
if err != nil { | |
log.Println(err) | |
if err := c.WriteMessage(msgtype, []byte("error")); err != nil { | |
log.Println(err) | |
} | |
} else { | |
if err := c.WriteJSON(table); err != nil { | |
log.Println(err) | |
} | |
} | |
} | |
// entry | |
func main() { | |
dir := "./code" | |
fs := http.FileServer(http.Dir(dir)) | |
http.Handle("/", fs) | |
http.HandleFunc("/echo", echo) | |
http.HandleFunc("/index", func(w http.ResponseWriter, r *http.Request) { | |
t := template.Must(template.ParseGlob(dir + "/" + index)) | |
t.ExecuteTemplate(w, index, nil) | |
}) | |
exec.Command("cmd", "/C", "start chrome http://" + port + "/index").Run() | |
log.Fatal(http.ListenAndServe(port, nil)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:root { | |
--main-width: 650px; | |
} | |
body { | |
font: 14px Consolas; | |
} | |
nav { | |
display: flex; | |
border-bottom: 1px solid #999; | |
vertical-align: middle; | |
} | |
main { | |
margin: auto; | |
width: var(--main-width); | |
padding: 25px 0 25px 0; | |
} | |
input { | |
padding: 5px 5px 5px 5px; | |
border-radius: 3px; | |
border: 1px solid #ccc; | |
outline: none; | |
} | |
input:focus { | |
border: 1px solid #333; | |
} | |
input[type="password"] { | |
font: 16px Consolas; | |
} | |
h2 { | |
font-weight: lighter; | |
} | |
#nav-title { | |
margin: auto; | |
width: var(--main-width); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment