Skip to content

Commit

Permalink
server main function uses refactored server struct
Browse files Browse the repository at this point in the history
Signed-off-by: Omri Bornstein <omribor@gmail.com>
  • Loading branch information
AppleGamer22 committed Dec 30, 2023
1 parent 5d70f7b commit 74f9009
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 74 deletions.
@@ -1,4 +1,4 @@
package server
package configuration

import (
"context"
Expand All @@ -17,8 +17,6 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
)

var Authenticator authenticator.Authenticator

func (server *RakerServer) InstagramSignUp(writer http.ResponseWriter, request *http.Request) {
if request.Method != http.MethodPost {
http.Redirect(writer, request, "/", http.StatusTemporaryRedirect)
Expand Down Expand Up @@ -140,7 +138,7 @@ func (server *RakerServer) InstagramSignIn(writer http.ResponseWriter, request *
return
}

webToken, expiry, err := Authenticator.Sign(user.ID, user.Username)
webToken, expiry, err := server.Authenticator.Sign(user.ID, user.Username)
if err != nil {
http.Error(writer, "sign-in failed", http.StatusUnauthorized)
log.Error(err, "ID", user.ID.Hex())
Expand Down
@@ -1,4 +1,4 @@
package server
package configuration

import (
"context"
Expand Down
@@ -1,4 +1,4 @@
package server
package configuration

import (
"fmt"
Expand Down Expand Up @@ -67,7 +67,7 @@ func NewRakerServer() (*RakerServer, error) {
log.Fatal(err)
}
rakerServer.DBClient = dbClient
// remember to defer client.Close()
// remember to defer client.Disconnet
rakerServer.Histories = database.Collection("histories")
rakerServer.Users = database.Collection("users")

Expand Down
2 changes: 1 addition & 1 deletion server/highlight.go → server/configuration/highlight.go
@@ -1,4 +1,4 @@
package server
package configuration

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion server/history.go → server/configuration/history.go
@@ -1,4 +1,4 @@
package server
package configuration

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion server/info.go → server/configuration/info.go
@@ -1,4 +1,4 @@
package server
package configuration

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion server/instagram.go → server/configuration/instagram.go
@@ -1,4 +1,4 @@
package server
package configuration

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion server/log.go → server/configuration/log.go
@@ -1,4 +1,4 @@
package server
package configuration

import (
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion server/storage.go → server/configuration/storage.go
@@ -1,4 +1,4 @@
package server
package configuration

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion server/story.go → server/configuration/story.go
@@ -1,4 +1,4 @@
package server
package configuration

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion server/templates.go → server/configuration/templates.go
@@ -1,4 +1,4 @@
package server
package configuration

import (
"html/template"
Expand Down
2 changes: 1 addition & 1 deletion server/tiktok.go → server/configuration/tiktok.go
@@ -1,4 +1,4 @@
package server
package configuration

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion server/vsco.go → server/configuration/vsco.go
@@ -1,4 +1,4 @@
package server
package configuration

import (
"context"
Expand Down
68 changes: 11 additions & 57 deletions server/main.go
@@ -1,4 +1,4 @@
package server
package main

import (
"context"
Expand All @@ -10,78 +10,32 @@ import (
"runtime"
"syscall"

// "github.com/AppleGamer22/raker/server/handlers"
"github.com/AppleGamer22/raker/server/configuration"
"github.com/AppleGamer22/raker/shared"
"github.com/charmbracelet/log"
"github.com/spf13/viper"
)

func main() {
if err1 := viper.ReadInConfig(); err1 != nil {
if _, err := os.Stat("/.dockerenv"); err != nil {
log.Error(err1)
}
}

if err := viper.Unmarshal(&configuration); err != nil {
log.Fatal(err)
}

if configuration.Secret == "" && !viper.IsSet("secret") {
log.Fatal("A JWT secret must be set via a config file or an environment variable")
}
// handlers.Authenticator = authenticator.New(configuration.Secret)
// Authenticator = authenticator.New(configuration.Secret)
// client, err := db.Connect(configuration.URI, configuration.Database)
rakerServer, err := configuration.NewRakerServer()
if err != nil {
log.Fatal(err)
}
defer client.Disconnect(context.Background())
defer rakerServer.DBClient.Disconnect(context.Background())

log.Infof("raker %s %s (%s/%s)", shared.Version, shared.Hash, runtime.GOOS, runtime.GOARCH)
log.Infof("Storage path: %s", configuration.Storage)
if configuration.Directories {
log.Infof("Storage path: %s", rakerServer.Storage)
if rakerServer.Directories {
log.Info("allowing directory listing")
}
log.Infof("MongoDB database URI: %s", configuration.URI)
log.Infof("MongoDB database: %s", configuration.Database)
log.Infof("Server is listening at http://localhost:%d", configuration.Port)

// mux := http.NewServeMux()

// mux.HandleFunc("/api/auth/sign_up/instagram", handlers.InstagramSignUp)
// mux.HandleFunc("/api/auth/sign_in/instagram", handlers.InstagramSignIn)
// mux.HandleFunc("/api/auth/update/instagram", handlers.InstagramUpdateCredentials)
// mux.HandleFunc("/api/auth/sign_out/instagram", handlers.InstagramSignOut)
// mux.HandleFunc("/api/categories", handlers.Categories)
// mux.HandleFunc("/api/history", handlers.History)
// mux.HandleFunc("/api/info", handlers.Information)
// mux.Handle("/api/storage/", http.StripPrefix("/api/storage", handlers.NewStorageHandler(configuration.Storage, configuration.Directories)))
// mux.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets"))))
// mux.Handle("/favicon.ico", http.RedirectHandler("/assets/icons/favicon.ico", http.StatusPermanentRedirect))
// mux.Handle("/robots.txt", http.RedirectHandler("/assets/robots.txt", http.StatusPermanentRedirect))

// mux.HandleFunc("/", handlers.AuthenticationPage)
// mux.HandleFunc("/history", handlers.HistoryPage)
// mux.HandleFunc("/instagram", handlers.InstagramPage)
// mux.HandleFunc("/highlight", handlers.HighlightPage)
// mux.HandleFunc("/story", handlers.StoryPage)
// mux.HandleFunc("/tiktok", handlers.TikTokPage)
// mux.HandleFunc("/vsco", handlers.VSCOPage)

server := http.Server{
Addr: fmt.Sprintf(":%d", configuration.Port),
// Handler: handlers.NewLoggerMiddleware(mux),
ErrorLog: log.Default().StandardLog(log.StandardLogOptions{
ForceLevel: log.ErrorLevel,
}),
}
log.Infof("MongoDB database URI: %s", rakerServer.URI)
log.Infof("MongoDB database: %s", rakerServer.Database)
log.Infof("Server is listening at http://localhost:%d", rakerServer.Port)

signals := make(chan os.Signal, 2)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)

go func() {
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
if err := rakerServer.HTTPServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Error(err)
signals <- os.Interrupt
}
Expand All @@ -90,7 +44,7 @@ func main() {
<-signals
fmt.Print("\r")
log.Warn("shutting down server...")
if err := server.Shutdown(context.Background()); err != nil {
if err := rakerServer.HTTPServer.Shutdown(context.Background()); err != nil {
log.Warn(err)
}
}

0 comments on commit 74f9009

Please sign in to comment.