Go server draft
This commit is contained in:
parent
51faa74cd8
commit
29d5885b67
7 changed files with 148 additions and 0 deletions
50
backend/Makefile
Normal file
50
backend/Makefile
Normal file
|
@ -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
|
32
backend/cmd/main.go
Normal file
32
backend/cmd/main.go
Normal file
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
8
backend/go.mod
Normal file
8
backend/go.mod
Normal file
|
@ -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
|
||||||
|
)
|
9
backend/go.sum
Normal file
9
backend/go.sum
Normal file
|
@ -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=
|
32
backend/internal/database/db.go
Normal file
32
backend/internal/database/db.go
Normal file
|
@ -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
|
||||||
|
}
|
10
backend/internal/database/db_test.go
Normal file
10
backend/internal/database/db_test.go
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDbConnect(t *testing.T) {
|
||||||
|
db := DbConnect()
|
||||||
|
_ = db
|
||||||
|
}
|
7
backend/migrations/0010.sql
Normal file
7
backend/migrations/0010.sql
Normal file
|
@ -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
|
||||||
|
);
|
Loading…
Reference in a new issue