96 lines
1.9 KiB
Go
96 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
//"html/template"
|
|
"net/http"
|
|
"flag"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
const version = "1.0.0"
|
|
|
|
type config struct {
|
|
port int
|
|
env string
|
|
}
|
|
|
|
type application struct {
|
|
config config
|
|
logger *log.Logger
|
|
}
|
|
|
|
type Message struct {
|
|
Type string `json:"type"`
|
|
Timestamp string `json:"timestamp"`
|
|
Random float64 `json:"random"`
|
|
}
|
|
|
|
//var page_template = template.Must(template.ParseFiles("../../ui/html/index.html"))
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
CheckOrigin: func(r *http.Request) bool { return true }, // allow all origins
|
|
}
|
|
|
|
func main() {
|
|
|
|
|
|
var cfg config
|
|
|
|
flag.IntVar(&cfg.port, "port", 4000, "API server port")
|
|
flag.StringVar(&cfg.env, "env", "development", "Environment (development|staging|production)")
|
|
//addr := flag.String("addr", ":8443", "HTTP network address")
|
|
flag.Parse()
|
|
|
|
|
|
logger := log.New(os.Stdout, "", log.Ldate | log.Ltime)
|
|
|
|
app := &application{
|
|
config: cfg,
|
|
logger: logger,
|
|
}
|
|
|
|
conn, err := pgx.Connect(context.Background(), "postgres://party:iK2SoVbDhdCki5n3LxGyP6zKpLspt4@losandesgames.com:5432/party")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
defer conn.Close(context.Background())
|
|
|
|
database_init(conn)
|
|
|
|
fileServer := http.FileServer(http.Dir("ui/static"))
|
|
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/", home)
|
|
mux.HandleFunc("/ws", ws)
|
|
mux.Handle("/static/", http.StripPrefix("/static", fileServer))
|
|
mux.HandleFunc("/v1/healthcheck", app.healthcheckHandler)
|
|
|
|
srv := &http.Server{
|
|
Addr: fmt.Sprintf(":%d", cfg.port),
|
|
Handler: mux,
|
|
IdleTimeout: time.Minute,
|
|
ReadTimeout: 10 * time.Second,
|
|
WriteTimeout: 30 * time.Second,
|
|
}
|
|
|
|
logger.Printf("starting %s server on %s", cfg.env, srv.Addr);
|
|
|
|
// Start HTTPS server (requires cert.pem and key.pem in current dir)
|
|
err = srv.ListenAndServe()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|