Skip to content

Commit

Permalink
styling changes
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverLok committed Aug 1, 2024
1 parent 2863d6e commit aed17d1
Show file tree
Hide file tree
Showing 8 changed files with 928 additions and 928 deletions.
162 changes: 81 additions & 81 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,91 +1,91 @@
package main

import (
"os"
"os/signal"
"syscall"

"github.com/alecthomas/kong"

"github.com/edulinq/autograder/internal/api"
"github.com/edulinq/autograder/internal/common"
"github.com/edulinq/autograder/internal/config"
"github.com/edulinq/autograder/internal/db"
"github.com/edulinq/autograder/internal/log"
"github.com/edulinq/autograder/internal/model"
pcourses "github.com/edulinq/autograder/internal/procedures/courses"
"github.com/edulinq/autograder/internal/util"
"os"
"os/signal"
"syscall"

"github.com/alecthomas/kong"

"github.com/edulinq/autograder/internal/api"
"github.com/edulinq/autograder/internal/common"
"github.com/edulinq/autograder/internal/config"
"github.com/edulinq/autograder/internal/db"
"github.com/edulinq/autograder/internal/log"
"github.com/edulinq/autograder/internal/model"
pcourses "github.com/edulinq/autograder/internal/procedures/courses"
"github.com/edulinq/autograder/internal/util"
)

var args struct {
config.ConfigArgs
config.ConfigArgs
}

func main() {
kong.Parse(&args)
err := config.HandleConfigArgs(args.ConfigArgs)
if err != nil {
log.Fatal("Could not load config options.", err)
}

err = common.CreatePIDFile()
if err != nil {
log.Fatal("Could not create PID file.", err)
}

defer func() {
err := common.RemovePIDFile()
if err != nil {
log.Fatal("Could not remove PID file.", err)
}
}()

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
err := common.RemovePIDFile()
if err != nil {
log.Error("Could not remove PID file.", err)
}
os.Exit(1)
}()

log.Info("Autograder Version", log.NewAttr("version", util.GetAutograderFullVersion()))

workingDir, err := os.Getwd()
if err != nil {
log.Fatal("Could not get working directory.", err)
}

db.MustOpen()
defer db.MustClose()

log.Info("Running server with working directory.", log.NewAttr("dir", workingDir))

_, err = db.AddCourses()
if err != nil {
log.Fatal("Could not load courses.", err)
}

courses := db.MustGetCourses()
log.Info("Loaded course(s).", log.NewAttr("count", len(courses)))

// Startup courses (in the background).
for _, course := range courses {
log.Info("Loaded course.", course)
go func(course *model.Course) {
pcourses.UpdateCourse(course, true)
}(course)
}

// Cleanup any temp dirs.
defer util.RemoveRecordedTempDirs()

err = api.StartServer()
if err != nil {
log.Fatal("Server was stopped.", err)
}

log.Info("Server closed.")
kong.Parse(&args)
err := config.HandleConfigArgs(args.ConfigArgs)
if err != nil {
log.Fatal("Could not load config options.", err)
}

err = common.CreatePIDFile()
if err != nil {
log.Fatal("Could not create PID file.", err)
}

defer func() {
err := common.RemovePIDFile()
if err != nil {
log.Fatal("Could not remove PID file.", err)
}
}()

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
err := common.RemovePIDFile()
if err != nil {
log.Error("Could not remove PID file.", err)
}
os.Exit(1)
}()

log.Info("Autograder Version", log.NewAttr("version", util.GetAutograderFullVersion()))

workingDir, err := os.Getwd()
if err != nil {
log.Fatal("Could not get working directory.", err)
}

db.MustOpen()
defer db.MustClose()

log.Info("Running server with working directory.", log.NewAttr("dir", workingDir))

_, err = db.AddCourses()
if err != nil {
log.Fatal("Could not load courses.", err)
}

courses := db.MustGetCourses()
log.Info("Loaded course(s).", log.NewAttr("count", len(courses)))

// Startup courses (in the background).
for _, course := range courses {
log.Info("Loaded course.", course)
go func(course *model.Course) {
pcourses.UpdateCourse(course, true)
}(course)
}

// Cleanup any temp dirs.
defer util.RemoveRecordedTempDirs()

err = api.StartServer()
if err != nil {
log.Fatal("Server was stopped.", err)
}

log.Info("Server closed.")
}
46 changes: 23 additions & 23 deletions internal/api/core/auth.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
package core

import (
"github.com/edulinq/autograder/internal/config"
"github.com/edulinq/autograder/internal/db"
"github.com/edulinq/autograder/internal/log"
"github.com/edulinq/autograder/internal/model"
"github.com/edulinq/autograder/internal/config"
"github.com/edulinq/autograder/internal/db"
"github.com/edulinq/autograder/internal/log"
"github.com/edulinq/autograder/internal/model"
)

// Return a user only in the case that the authentication is successful.
// If any error is retuturned, then the request should end and the response sent based on the error.
// This assumes basic validation has already been done on the request.
func (this *APIRequestUserContext) Auth() (*model.ServerUser, *APIError) {
user, err := db.GetServerUser(this.UserEmail, true)
if err != nil {
return nil, NewAuthBadRequestError("-012", this, "Cannot Get User").Err(err)
}
user, err := db.GetServerUser(this.UserEmail, true)
if err != nil {
return nil, NewAuthBadRequestError("-012", this, "Cannot Get User").Err(err)
}

if user == nil {
return nil, NewAuthBadRequestError("-013", this, "Unknown User")
}
if user == nil {
return nil, NewAuthBadRequestError("-013", this, "Unknown User")
}

if config.NO_AUTH.Get() {
log.Debug("Authentication Disabled.", log.NewUserAttr(this.UserEmail))
return user, nil
}
if config.NO_AUTH.Get() {
log.Debug("Authentication Disabled.", log.NewUserAttr(this.UserEmail))
return user, nil
}

auth, err := user.Auth(this.UserPass)
if err != nil {
return nil, NewBareInternalError("-037", this.Endpoint, "User auth failed.").Err(err)
}
auth, err := user.Auth(this.UserPass)
if err != nil {
return nil, NewBareInternalError("-037", this.Endpoint, "User auth failed.").Err(err)
}

if !auth {
return nil, NewAuthBadRequestError("-014", this, "Bad Password")
}
if !auth {
return nil, NewAuthBadRequestError("-014", this, "Bad Password")
}

return user, nil
return user, nil
}
Loading

0 comments on commit aed17d1

Please sign in to comment.