From 19a209619318ee901639084d2bdb465ffb1c22f1 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Sun, 12 Sep 2021 00:32:07 +0200 Subject: [PATCH] Add README and WIP server lifecycle --- .gitignore | 1 + README.md | 21 +++++++++++++++++++++ cmd/rmsn/rmsn.go | 36 ++++++++++++++++++++++++++++++++++++ rmsn.yml | 3 +++ server/server.go | 36 ++++++++++++++++++++++++++++++++++-- services/store/store.go | 1 + web/web.go | 22 ++++++++++++++++++++++ 7 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 cmd/rmsn/rmsn.go create mode 100644 rmsn.yml create mode 100644 web/web.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..14761d1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/rmsn.sqlite diff --git a/README.md b/README.md new file mode 100644 index 0000000..81baf4d --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# RMSN + +## Config + +Default configuration can be found in the [rmsn.yml](./rmsn.yml) file +at the root of the repository. + +## Roadmap + +- [ ] Add simple logger everywhere +- [ ] Make server start, teardown and block +- [ ] Bootstrap webapp +- [ ] Embed webapp in server +- [ ] Split server and webapp in different folders +- [ ] Add modd for easy development forkflow +- [ ] Add webapp make targets +- [ ] Add bundle and install make targets +- [ ] Create basic webapp structure and development workflow +- [ ] Think of a better name +- [ ] Add email config and communications +- [ ] Add webpush notifications diff --git a/cmd/rmsn/rmsn.go b/cmd/rmsn/rmsn.go new file mode 100644 index 0000000..5685815 --- /dev/null +++ b/cmd/rmsn/rmsn.go @@ -0,0 +1,36 @@ +package main + +import ( + "flag" + "fmt" + "os" + "os/signal" + + "git.ctrlz.es/mgdelacroix/rmsn/server" +) + +func main() { + configFlag := flag.String("config", "rmsn.yml", "the configuration path") + flag.Parse() + + srv, err := server.NewServerWithConfigPath(*configFlag) + if err != nil { + fmt.Fprintf(os.Stderr, "cannot create server: %s", err) + os.Exit(1) + } + + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + closed := make(chan bool, 1) + + go func() { + fmt.Println("Starting rmsn") + srv.Start() // ToDo: this should be blocking + closed <- true + }() + + s := <-c + fmt.Printf("Got signal %s, exiting...\n", s) + srv.Close() + <-closed +} diff --git a/rmsn.yml b/rmsn.yml new file mode 100644 index 0000000..494d8a9 --- /dev/null +++ b/rmsn.yml @@ -0,0 +1,3 @@ +--- +database_path: rmsn.sqlite +port: 8080 diff --git a/server/server.go b/server/server.go index bd6967e..f874052 100644 --- a/server/server.go +++ b/server/server.go @@ -2,12 +2,18 @@ package server import ( "fmt" + "net/http" "git.ctrlz.es/mgdelacroix/rmsn/model" + "git.ctrlz.es/mgdelacroix/rmsn/services/store" + "git.ctrlz.es/mgdelacroix/rmsn/web" ) type Server struct { Config *model.Config + + Store *store.Store + WebServer *http.Server } func NewServer(config *model.Config) (*Server, error) { @@ -15,15 +21,41 @@ func NewServer(config *model.Config) (*Server, error) { return nil, fmt.Errorf("config is not valid: %w", err) } - srv := &Server{Config: config} + webserver, err := web.NewWebServer(*config.Port) + if err != nil { + return nil, fmt.Errorf("cannot create webserver: %w", err) + } - return srv, nil + store, err := store.NewStore(*config.DatabasePath) + if err != nil { + return nil, fmt.Errorf("cannot create store: %w", err) + } + + return &Server{ + Config: config, + WebServer: webserver, + Store: store, + }, err +} + +func NewServerWithConfigPath(path string) (*Server, error) { + config, err := model.ReadConfig(path) + if err != nil { + return nil, fmt.Errorf("cannot read config from path %q: %w", path, err) + } + + return NewServer(config) } func (s *Server) Start() error { + // ToDo: start webserver here + return nil } func (s *Server) Close() error { + // ToDo: stop webserver + // ToDo: close store + return nil } diff --git a/services/store/store.go b/services/store/store.go index 7dffa67..19ed544 100644 --- a/services/store/store.go +++ b/services/store/store.go @@ -25,6 +25,7 @@ func addPathOptions(path string) string { func NewStore(path string) (*Store, error) { s := &Store{Path: path} + // ToDo: should do this at start time instead of create time? conn, err := sql.Open("sqlite3", addPathOptions(path)) if err != nil { return nil, err diff --git a/web/web.go b/web/web.go new file mode 100644 index 0000000..e5b969d --- /dev/null +++ b/web/web.go @@ -0,0 +1,22 @@ +package web + +import ( + "fmt" + "net/http" +) + +func NewWebServer(port int) (*http.Server, error) { + mux := http.NewServeMux() + + // ToDo: configure routes + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello rmsn!!") + }) + + s := &http.Server{ + Addr: fmt.Sprintf(":%d", port), + Handler: mux, + } + + return s, nil +}