From 29d5885b672a6b75d281a002a41ca5eb04122991 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Mon, 12 Feb 2024 12:40:49 +0100 Subject: [PATCH] Go server draft --- backend/Makefile | 50 ++++++++++++++++++++++++++++ backend/cmd/main.go | 32 ++++++++++++++++++ backend/go.mod | 8 +++++ backend/go.sum | 9 +++++ backend/internal/database/db.go | 32 ++++++++++++++++++ backend/internal/database/db_test.go | 10 ++++++ backend/migrations/0010.sql | 7 ++++ 7 files changed, 148 insertions(+) create mode 100644 backend/Makefile create mode 100644 backend/cmd/main.go create mode 100644 backend/go.mod create mode 100644 backend/go.sum create mode 100644 backend/internal/database/db.go create mode 100644 backend/internal/database/db_test.go create mode 100644 backend/migrations/0010.sql diff --git a/backend/Makefile b/backend/Makefile new file mode 100644 index 0000000..ed14841 --- /dev/null +++ b/backend/Makefile @@ -0,0 +1,50 @@ +# Go parameters +GOCMD = go +GOBUILD = $(GOCMD) build +GOCLEAN = $(GOCMD) clean +GOTEST = $(GOCMD) test +GOGET = $(GOCMD) get + +# SQLite database filename +DB_FILE = db.sqlite3 + +# Directory containing migration SQL scripts +MIGRATIONS_DIR = migrations + +# Build target +build: + $(GOBUILD) -o bin/server cmd/*.go + +# Run target +run: build + ./bin/server + +# Clean target +clean: + $(GOCLEAN) + rm -f bin/server + rm -f db.sqlite3 + +# Test target +test: + $(GOTEST) ./... + +# Get dependencies target +deps: + $(GOGET) -v ./... + +# Update dependencies target +update: + $(GOGET) -u -v ./... + $(GOCMD) mod tidy + +# Migration target +migrate: + @echo "Migrating database $(DB_FILE) using SQL scripts in $(MIGRATIONS_DIR)" + @for file in $(wildcard $(MIGRATIONS_DIR)/*.sql); do \ + echo "Applying migration: $$file"; \ + sqlite3 $(DB_FILE) < $$file; \ + done + +# Default target +default: build \ No newline at end of file diff --git a/backend/cmd/main.go b/backend/cmd/main.go new file mode 100644 index 0000000..98fcc96 --- /dev/null +++ b/backend/cmd/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "io" + "net/http" + "tmp/internal/database" + + _ "github.com/mattn/go-sqlite3" +) + +// This is what a handler looks like +func handler(w http.ResponseWriter, r *http.Request) { + fmt.Println("Request received") + io.WriteString(w, "This is my website!\n") +} + +func main() { + println("Starting server...") + database.DbConnect() + + // Mounting the handler + fs := http.FileServer(http.Dir("static")) + http.Handle("/", fs) + http.HandleFunc("/hello", handler) + + // Start the server on port 8080 + err := http.ListenAndServe(":8080", nil) + if err != nil { + panic(err) + } +} diff --git a/backend/go.mod b/backend/go.mod new file mode 100644 index 0000000..005dcc6 --- /dev/null +++ b/backend/go.mod @@ -0,0 +1,8 @@ +module tmp + +go 1.21.1 + +require ( + github.com/jmoiron/sqlx v1.3.5 + github.com/mattn/go-sqlite3 v1.14.22 +) diff --git a/backend/go.sum b/backend/go.sum new file mode 100644 index 0000000..ee85dd9 --- /dev/null +++ b/backend/go.sum @@ -0,0 +1,9 @@ +github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= +github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ= +github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= +github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= diff --git a/backend/internal/database/db.go b/backend/internal/database/db.go new file mode 100644 index 0000000..d99efde --- /dev/null +++ b/backend/internal/database/db.go @@ -0,0 +1,32 @@ +package database + +import ( + "os" + + "github.com/jmoiron/sqlx" + _ "github.com/mattn/go-sqlite3" +) + +func DbConnect() *sqlx.DB { + // Check for the environment variable + dbpath := os.Getenv("SQLITE_DB_PATH") + + // Default to something reasonable + if dbpath == "" { + dbpath = "./db.sqlite3" + } + + // Open the database + // db, err := sqlx.Connect("sqlite3", ":memory:") + db, err := sqlx.Connect("sqlite3", dbpath) + if err != nil { + panic(err) + } + + err = db.Ping() + if err != nil { + panic(err) + } + + return db +} diff --git a/backend/internal/database/db_test.go b/backend/internal/database/db_test.go new file mode 100644 index 0000000..d7b26c9 --- /dev/null +++ b/backend/internal/database/db_test.go @@ -0,0 +1,10 @@ +package database + +import ( + "testing" +) + +func TestDbConnect(t *testing.T) { + db := DbConnect() + _ = db +} diff --git a/backend/migrations/0010.sql b/backend/migrations/0010.sql new file mode 100644 index 0000000..11be51c --- /dev/null +++ b/backend/migrations/0010.sql @@ -0,0 +1,7 @@ +PRAGMA foreign_keys = ON; + +CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY, + username VARCHAR(255) NOT NULL, + password VARCHAR(255) NOT NULL +);