Update directory structure to serve html via golang server
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
const (
|
||||
PORT = "8080"
|
||||
DB_FILE = "./datefinder.sqlite3"
|
||||
)
|
||||
|
||||
var (
|
||||
db *sql.DB
|
||||
)
|
||||
|
||||
type ChoicesPost struct {
|
||||
PollId int `json:"pollId"`
|
||||
Username string `json:"username"`
|
||||
Choices [][]bool `json:"choices"`
|
||||
}
|
||||
|
||||
func initDatabase() {
|
||||
var err error
|
||||
db, err = sql.Open("sqlite3", DB_FILE)
|
||||
if err != nil {
|
||||
log.Fatalf("[!] Could not open database file %s, error: %s\n", DB_FILE, err)
|
||||
}
|
||||
|
||||
const createPollsQuery = `
|
||||
CREATE TABLE IF NOT EXISTS polls (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL
|
||||
);
|
||||
`
|
||||
|
||||
_, err = db.Exec(createPollsQuery)
|
||||
if err != nil {
|
||||
log.Fatalf("[!] Could not create database table polls, error: %s\n", err)
|
||||
}
|
||||
|
||||
const createChoicesQuery = `
|
||||
CREATE TABLE IF NOT EXISTS choices (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
pollid INTEGER NOT NULL,
|
||||
username TEXT NOT NULL,
|
||||
choices TEXT NOT NULL,
|
||||
FOREIGN KEY(pollid) REFERENCES polls(id)
|
||||
);
|
||||
`
|
||||
|
||||
_, err = db.Exec(createChoicesQuery)
|
||||
if err != nil {
|
||||
log.Fatalf("[!] Could not create database table choices, error: %s\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func handleGetPing(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("[*] Received request %v %v\n", r.Method, r.URL)
|
||||
fmt.Fprintf(w, "PONG\n")
|
||||
}
|
||||
|
||||
func handlePostSubmit(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("[*] Received request %v %v\n", r.Method, r.URL)
|
||||
log.Printf("[*] %v\n", r.Body)
|
||||
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Origin")
|
||||
|
||||
var choicesPost ChoicesPost
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&choicesPost)
|
||||
if err != nil {
|
||||
log.Printf("[!] Error decoding request Body %v, error: %s\n", r.Body, err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
|
||||
choicesJson, err := json.Marshal(choicesPost.Choices)
|
||||
if err != nil {
|
||||
log.Printf("[!] Could not marshal choices, error: %s\n", err)
|
||||
}
|
||||
|
||||
const query = `
|
||||
INSERT INTO choices (pollid, username, choices)
|
||||
VALUES (?, ?, ?);
|
||||
`
|
||||
|
||||
_, err = db.Exec(query, choicesPost.PollId, choicesPost.Username, string(choicesJson))
|
||||
if err != nil {
|
||||
log.Printf("[!] Could not insert choices into database, error: %s\n", err)
|
||||
}
|
||||
|
||||
log.Printf("[*] Persisted choices for %s to database\n", "DUMMY USERNAME")
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func handleOptionsSubmit(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Origin")
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func main() {
|
||||
initDatabase()
|
||||
defer db.Close()
|
||||
|
||||
http.HandleFunc(
|
||||
"GET /api/ping",
|
||||
handleGetPing,
|
||||
)
|
||||
|
||||
http.HandleFunc(
|
||||
"POST /api/submit",
|
||||
handlePostSubmit,
|
||||
)
|
||||
|
||||
http.HandleFunc(
|
||||
"OPTIONS /api/submit",
|
||||
handleOptionsSubmit,
|
||||
)
|
||||
|
||||
log.Printf("[*] Datefinder server listening on :%s\n", PORT)
|
||||
if err := http.ListenAndServe(":8080", nil); err != nil {
|
||||
log.Fatalf("[!] Could not start server on %s, error: %s\n", PORT, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user