This repository has been archived by the owner on Sep 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
905 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
*.yaml | ||
*.db | ||
*.db | ||
to-go |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package cliopts | ||
|
||
import ( | ||
"github.com/leo-alvarenga/to-go/ng" | ||
"github.com/leo-alvarenga/to-go/ng/ngops" | ||
) | ||
|
||
func DashboardOption() bool { | ||
ngops.Dashboard(ng.Config) | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
package ngops | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"runtime" | ||
|
||
"github.com/leo-alvarenga/to-go/ng" | ||
"github.com/leo-alvarenga/to-go/shared/cfg" | ||
"github.com/leo-alvarenga/to-go/shared/task" | ||
) | ||
|
||
func openBrowser(port string) error { | ||
var cmd string | ||
var args []string | ||
|
||
switch runtime.GOOS { | ||
case "windows": | ||
cmd = "cmd" | ||
args = []string{"/c", "start"} | ||
case "darwin": | ||
cmd = "open" | ||
default: | ||
cmd = "xdg-open" | ||
} | ||
args = append(args, "localhost:"+port) | ||
return exec.Command(cmd, args...).Start() | ||
} | ||
|
||
func Dashboard(config *cfg.ConfigValue) { | ||
ex, _ := os.Executable() | ||
statics := filepath.Join(filepath.Dir(ex), "static/") | ||
|
||
// static files | ||
http.Handle("/", http.FileServer(http.Dir(statics))) | ||
|
||
// ng calls | ||
http.HandleFunc("/api/get", getTasksHandler) | ||
http.HandleFunc("/api/add", addTaskHandler) | ||
http.HandleFunc("/api/remove", removeTaskHandler) | ||
http.HandleFunc("/api/edit", editTaskHandler) | ||
http.HandleFunc("/api/update", updateTaskHandler) | ||
|
||
if config.OpenBrowser { | ||
go openBrowser(config.DashboardPort) | ||
} | ||
|
||
log.Println("Serving dashboard on port " + config.DashboardPort) | ||
http.ListenAndServe(":"+config.DashboardPort, nil) | ||
} | ||
|
||
func getTasksHandler(w http.ResponseWriter, req *http.Request) { | ||
res, err := json.Marshal(map[string][]task.Task{"content": ng.TaskList.GetAllTasks()}) | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
|
||
if err != nil { | ||
w.WriteHeader(http.StatusNotFound) | ||
w.Write([]byte(`{"content": []}`)) | ||
} else { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write(res) | ||
} | ||
} | ||
|
||
func addTaskHandler(w http.ResponseWriter, req *http.Request) { | ||
|
||
if req.Method != http.MethodPost { | ||
w.WriteHeader(http.StatusNotImplemented) | ||
return | ||
} | ||
|
||
defer req.Body.Close() | ||
|
||
t := new(task.Task) | ||
|
||
err := json.NewDecoder(req.Body).Decode(t) | ||
if err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte(`{"status":"` + err.Error() + `"}`)) | ||
return | ||
} | ||
|
||
err = Add(*t) | ||
if err != nil { | ||
w.WriteHeader(http.StatusForbidden) | ||
w.Write([]byte(`{"status":"Couldn't save changes because there's another task with this title. ` + err.Error() + `"}`)) | ||
return | ||
} | ||
|
||
killData() | ||
w.WriteHeader(http.StatusCreated) | ||
} | ||
|
||
func removeTaskHandler(w http.ResponseWriter, req *http.Request) { | ||
|
||
if req.Method != http.MethodDelete { | ||
w.WriteHeader(http.StatusForbidden) | ||
return | ||
} | ||
|
||
title := req.URL.Query().Get("title") | ||
|
||
if title == "" { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte(`{"status":"Invalid title"}`)) | ||
return | ||
} | ||
|
||
err := Remove(title) | ||
if err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte(`{"status":"` + err.Error() + `"}`)) | ||
return | ||
} | ||
|
||
killData() | ||
w.WriteHeader(http.StatusOK) | ||
} | ||
|
||
func editTaskHandler(w http.ResponseWriter, req *http.Request) { | ||
if req.Method != http.MethodPatch { | ||
w.WriteHeader(http.StatusForbidden) | ||
return | ||
} | ||
|
||
defer req.Body.Close() | ||
|
||
t := new([]task.Task) | ||
|
||
err := json.NewDecoder(req.Body).Decode(t) | ||
if err != nil || len(*t) <= 0 { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte(`{"status":"` + err.Error() + `"}`)) | ||
return | ||
} | ||
|
||
err = Edit((*t)[1], (*t)[0]) | ||
if err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte(`{"status":"` + err.Error() + `"}`)) | ||
return | ||
} | ||
|
||
killData() | ||
w.WriteHeader(http.StatusOK) | ||
} | ||
|
||
func updateTaskHandler(w http.ResponseWriter, req *http.Request) { | ||
|
||
if req.Method != http.MethodPatch { | ||
w.WriteHeader(http.StatusForbidden) | ||
return | ||
} | ||
|
||
defer req.Body.Close() | ||
|
||
t := new(task.Task) | ||
|
||
err := json.NewDecoder(req.Body).Decode(t) | ||
if err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte(`{"status":"` + err.Error() + `"}`)) | ||
return | ||
} | ||
|
||
err = Update(*t) | ||
if err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte(`{"status":"` + err.Error() + `"}`)) | ||
return | ||
} | ||
|
||
killData() | ||
w.WriteHeader(http.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ngops | ||
|
||
import "github.com/leo-alvarenga/to-go/ng" | ||
|
||
func killData() { | ||
ng.Startup() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.