little formatting & flag mode at startup

This commit is contained in:
Suraj Patil 2016-05-13 10:32:07 +05:30
parent 9c9c52a025
commit 13a4d4f80e
2 changed files with 51 additions and 17 deletions

View File

@ -13,7 +13,7 @@ import (
"log" "log"
) )
//Stores the main configuration for the application //Configuration Stores the main configuration for the application
type Configuration struct { type Configuration struct {
ServerPort string ServerPort string
} }
@ -23,15 +23,17 @@ var config Configuration
//ReadConfig will read the configuration json file to read the parameters //ReadConfig will read the configuration json file to read the parameters
//which will be passed in the config file //which will be passed in the config file
func ReadConfig(fileName string) Configuration { func ReadConfig(fileName string) (Configuration, error) {
configFile, err := ioutil.ReadFile(fileName) configFile, err := ioutil.ReadFile(fileName)
if err != nil { if err != nil {
log.Fatalf("Unable to read config file '%s'", fileName) log.Print("Unable to read config file, switching to flag mode")
return Configuration{}, err
} }
//log.Print(configFile) //log.Print(configFile)
err = json.Unmarshal(configFile, &config) err = json.Unmarshal(configFile, &config)
if err != nil { if err != nil {
log.Print(err) log.Print("Invalid JSON, expecting port from command line flag")
return Configuration{}, err
} }
return config return config, nil
} }

56
main.go
View File

@ -5,39 +5,71 @@ package main
* License: MIT * License: MIT
**/ **/
import ( import (
"flag"
"log" "log"
"net/http" "net/http"
"strings"
"github.com/thewhitetulip/Tasks/config" "github.com/thewhitetulip/Tasks/config"
"github.com/thewhitetulip/Tasks/views" "github.com/thewhitetulip/Tasks/views"
) )
func main() { func main() {
values := config.ReadConfig("config.json") values, err := config.ReadConfig("config.json")
var port *string
if err != nil {
port = flag.String("port", "", "IP address")
flag.Parse()
//User is expected to give :8080 like input, if they give 8080
//we'll append the required ':'
if !strings.HasPrefix(*port, ":") {
*port = ":" + *port
log.Println("port is " + *port)
}
values.ServerPort = *port
}
views.PopulateTemplates() views.PopulateTemplates()
http.HandleFunc("/", views.RequiresLogin(views.ShowAllTasksFunc))
//Login logout
http.HandleFunc("/login/", views.LoginFunc) http.HandleFunc("/login/", views.LoginFunc)
http.HandleFunc("/logout/", views.RequiresLogin(views.LogoutFunc)) http.HandleFunc("/logout/", views.RequiresLogin(views.LogoutFunc))
http.HandleFunc("/add-category/", views.RequiresLogin(views.AddCategoryFunc)) http.HandleFunc("/add-category/", views.RequiresLogin(views.AddCategoryFunc))
http.HandleFunc("/add-comment/", views.RequiresLogin(views.AddCommentFunc)) http.HandleFunc("/add-comment/", views.RequiresLogin(views.AddCommentFunc))
http.HandleFunc("/add/", views.RequiresLogin(views.AddTaskFunc))
//these handlers are used to delete
http.HandleFunc("/del-comment/", views.RequiresLogin(views.DeleteCommentFunc)) http.HandleFunc("/del-comment/", views.RequiresLogin(views.DeleteCommentFunc))
http.HandleFunc("/del-category/", views.RequiresLogin(views.DeleteCategoryFunc)) http.HandleFunc("/del-category/", views.RequiresLogin(views.DeleteCategoryFunc))
http.HandleFunc("/upd-category/", views.RequiresLogin(views.UpdateCategoryFunc))
http.HandleFunc("/category/", views.RequiresLogin(views.ShowCategoryFunc))
http.HandleFunc("/complete/", views.RequiresLogin(views.CompleteTaskFunc))
http.HandleFunc("/delete/", views.RequiresLogin(views.DeleteTaskFunc)) http.HandleFunc("/delete/", views.RequiresLogin(views.DeleteTaskFunc))
http.HandleFunc("/files/", views.RequiresLogin(views.UploadedFileHandler))
//these handlers update
http.HandleFunc("/upd-category/", views.RequiresLogin(views.UpdateCategoryFunc))
http.HandleFunc("/update/", views.RequiresLogin(views.UpdateTaskFunc))
//these handlers are used for restoring tasks
http.HandleFunc("/incomplete/", views.RequiresLogin(views.RestoreFromCompleteFunc))
http.HandleFunc("/restore/", views.RequiresLogin(views.RestoreTaskFunc))
//these handlers fetch set of tasks
http.HandleFunc("/", views.RequiresLogin(views.ShowAllTasksFunc))
http.HandleFunc("/category/", views.RequiresLogin(views.ShowCategoryFunc))
http.HandleFunc("/deleted/", views.RequiresLogin(views.ShowTrashTaskFunc)) http.HandleFunc("/deleted/", views.RequiresLogin(views.ShowTrashTaskFunc))
http.HandleFunc("/completed/", views.RequiresLogin(views.ShowCompleteTasksFunc))
//these handlers perform action like delete, mark as complete etc
http.HandleFunc("/complete/", views.RequiresLogin(views.CompleteTaskFunc))
http.HandleFunc("/files/", views.RequiresLogin(views.UploadedFileHandler))
http.HandleFunc("/trash/", views.RequiresLogin(views.TrashTaskFunc)) http.HandleFunc("/trash/", views.RequiresLogin(views.TrashTaskFunc))
http.HandleFunc("/edit/", views.RequiresLogin(views.EditTaskFunc)) http.HandleFunc("/edit/", views.RequiresLogin(views.EditTaskFunc))
http.HandleFunc("/completed/", views.RequiresLogin(views.ShowCompleteTasksFunc))
http.HandleFunc("/restore/", views.RequiresLogin(views.RestoreTaskFunc))
http.HandleFunc("/incomplete/", views.RequiresLogin(views.RestoreFromCompleteFunc))
http.HandleFunc("/add/", views.RequiresLogin(views.AddTaskFunc))
http.HandleFunc("/update/", views.RequiresLogin(views.UpdateTaskFunc))
http.HandleFunc("/search/", views.RequiresLogin(views.SearchTaskFunc)) http.HandleFunc("/search/", views.RequiresLogin(views.SearchTaskFunc))
//http.HandleFunc("/static/", ServeStaticFunc)
http.Handle("/static/", http.FileServer(http.Dir("public"))) http.Handle("/static/", http.FileServer(http.Dir("public")))
log.Println("running server on ", values.ServerPort) log.Println("running server on ", values.ServerPort)
log.Fatal(http.ListenAndServe(values.ServerPort, nil)) log.Fatal(http.ListenAndServe(values.ServerPort, nil))
} }