github.com/nizigama/simple-note@v0.0.0-20210501235539-2cc762e06fe8/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  	"os"
     8  
     9  	"github.com/nizigama/simple-note/models"
    10  	boltDB "github.com/nizigama/simple-note/services/database"
    11  	auth "github.com/nizigama/simple-note/services/middleware"
    12  )
    13  
    14  // TODO: work on login feature
    15  var (
    16  	logger  *log.Logger
    17  	logFile *os.File
    18  )
    19  
    20  func init() {
    21  
    22  	logFile, err := os.OpenFile("app-log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
    23  
    24  	if err != nil {
    25  		fmt.Println("Failed to create the log file")
    26  		fmt.Println(err)
    27  		os.Exit(1)
    28  	}
    29  
    30  	logger = log.New(logFile, "simple-note-app:", log.Ldate)
    31  
    32  	boltDB.InitDBConnection(logger, models.UserTableName, models.NoteTableName)
    33  }
    34  
    35  func main() {
    36  	defer boltDB.CloseDBConnection()
    37  	defer logFile.Close()
    38  	defer handlePanic()
    39  
    40  	http.HandleFunc("/", index)
    41  	http.Handle("/login", auth.Authorize(login))
    42  	http.Handle("/register", auth.Authorize(register))
    43  	http.Handle("/profile", auth.Authorize(profile))
    44  	http.Handle("/profile-picture", auth.Authorize(profilePicture))
    45  	http.HandleFunc("/get-picture", getPicture)
    46  	http.Handle("/dashboard", auth.Authorize(dashboard))
    47  	http.Handle("/new-note", auth.Authorize(newNote))
    48  	http.Handle("/update-note", auth.Authorize(updateNote))
    49  	http.Handle("/delete-note", auth.Authorize(deleteNote))
    50  	http.Handle("/logout", auth.Authorize(logout))
    51  	http.Handle("/delete-account", auth.Authorize(deleteAccount))
    52  
    53  	err := http.ListenAndServe(":3000", nil)
    54  
    55  	if err != nil {
    56  		logger.Fatalln("Error starting the server, here is the error:", err)
    57  	}
    58  }
    59  
    60  func handlePanic() {
    61  	if message := recover(); message != nil {
    62  		logger.Fatalln("Panic occurred:", message)
    63  	}
    64  }