Skip to content

Commit

Permalink
Build procedure improvement.
Browse files Browse the repository at this point in the history
  • Loading branch information
krashanoff committed Mar 5, 2022
1 parent 7d939e6 commit 73f28b1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 33 deletions.
2 changes: 0 additions & 2 deletions backend/Makefile

This file was deleted.

2 changes: 1 addition & 1 deletion backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ You will need:

```sh
# make sure you're in the backend directory
make
go build --tags=sqlite_foreign_keys
./backend
```

Expand Down
40 changes: 16 additions & 24 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"database/sql"
"flag"
"fmt"
"net/http"
"os"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
Expand All @@ -19,7 +19,7 @@ import (
)

var (
connString string
databaseFile string
secretKey string
port string
maxJobs uint
Expand All @@ -29,9 +29,9 @@ var (

func main() {
// Sensible default values for parameters.
flag.StringVar(&connString, "c", "file:test.db?cache=shared&mode=rwc", "sqlite `connection string`")
flag.StringVar(&port, "p", os.Getenv("PORT"), "`port` to serve the HTTP server on")
flag.StringVar(&secretKey, "k", "", "secret `key` to use in JWT minting")
flag.StringVar(&databaseFile, "c", "gradebetter.db", "sqlite database `filename`")
flag.StringVar(&port, "p", "8080", "`port` to serve the HTTP server on")
flag.StringVar(&secretKey, "k", "gradebetter", "secret `key` to use in JWT minting")
flag.UintVar(&maxJobs, "j", 1, "Maximum number of concurrent test scripts running at a given time")
flag.BoolVar(&initializeTables, "I", false, "Initialize SQLite schema then exit (if no prior database exists)")
flag.BoolVar(&resetTables, "D", false, "Reset SQLite database schema then exit (DROP ALL TABLES)")
Expand All @@ -48,21 +48,24 @@ func main() {
e.Use(middleware.Recover())
e.Use(middleware.CORS())

e.Static("/", "build")
e.File("/", "build/index.html")

// Set up our database.
db, err := sql.Open("sqlite3", connString)
db, err := sql.Open("sqlite3", fmt.Sprintf("file:%s?cache=shared&mode=rwc", databaseFile))
if err != nil {
e.Logger.Error(err)
return
}
defer db.Close()

if resetTables || initializeTables {
e.Logger.Info("Migrating...")
if err := schemas.Migrate(db, resetTables); err != nil {
e.Logger.Error(err)
return
}
e.Logger.Info("Done.")
e.Logger.Info("Initializing tables...")
if err := schemas.Migrate(db, resetTables); err != nil {
e.Logger.Error(err)
return
}
e.Logger.Info("Done.")
if resetTables {
return
}

Expand Down Expand Up @@ -92,11 +95,6 @@ func main() {
}
})

// Smoke test.
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "It works.")
})

// Login or create a user.
e.POST("/login", handler.LoginUser)
e.POST("/user", handler.CreateUser)
Expand Down Expand Up @@ -124,7 +122,6 @@ func main() {
classApi.GET("/:classId/info", handler.GetClass)
classApi.GET("/:classId/:assignmentId", handler.GetAssignment)
classApi.POST("/:classId/assignment", handler.CreateAssignment)
e.POST("/:classId/:assignmentId/script", Unimplemented)
classApi.POST("/:classId/:assignmentId/upload", handler.UploadSubmission)
classApi.POST("/:classId/invite", handler.CreateInvite)
classApi.POST("/join", handler.EnrollStudent)
Expand All @@ -135,8 +132,3 @@ func main() {
// Start serving the backend on port 8080.
e.Logger.Fatal(e.Start(":" + port))
}

// This endpoint hasn't been implemented yet!
func Unimplemented(c echo.Context) error {
return nil
}
6 changes: 3 additions & 3 deletions backend/schemas/1-accounts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


-- All accounts in the system.
CREATE TABLE Accounts (
CREATE TABLE IF NOT EXISTS Accounts (
-- Generated ID for internal use only.
id INTEGER PRIMARY KEY AUTOINCREMENT,

Expand All @@ -26,7 +26,7 @@ CREATE TABLE Accounts (
deleted TIMESTAMP DEFAULT NULL
);

CREATE TABLE Courses (
CREATE TABLE IF NOT EXISTS Courses (
id INTEGER PRIMARY KEY AUTOINCREMENT,

-- Name of the class, presented to users.
Expand Down Expand Up @@ -65,7 +65,7 @@ CREATE TABLE IF NOT EXISTS ClassMembers (


-- Valid invite codes to classes.
CREATE TABLE Invites (
CREATE TABLE IF NOT EXISTS Invites (
-- ID of the invite (code that is used on user side).
id TEXT NOT NULL,

Expand Down
6 changes: 3 additions & 3 deletions backend/schemas/2-assignments.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
-- Schemas for all class assignments and submissions.
--

CREATE TABLE Assignments (
CREATE TABLE IF NOT EXISTS Assignments (
id INTEGER PRIMARY KEY AUTOINCREMENT,

-- Owning class
Expand All @@ -25,7 +25,7 @@ CREATE TABLE Assignments (
FOREIGN KEY (class) REFERENCES Courses (id)
);

CREATE TABLE Submissions (
CREATE TABLE IF NOT EXISTS Submissions (
-- Unique submission ID
id TEXT NOT NULL,

Expand All @@ -47,7 +47,7 @@ CREATE TABLE Submissions (
);

-- Detailed table of results for each test case.
CREATE TABLE Results (
CREATE TABLE IF NOT EXISTS Results (
-- Associated submission.
submission_id TEXT NOT NULL,

Expand Down

0 comments on commit 73f28b1

Please sign in to comment.