Add README and WIP server lifecycle

This commit is contained in:
Miguel de la Cruz 2021-09-12 00:32:07 +02:00
parent 96bd6a8602
commit 19a2096193
7 changed files with 118 additions and 2 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/rmsn.sqlite

21
README.md Normal file
View file

@ -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

36
cmd/rmsn/rmsn.go Normal file
View file

@ -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
}

3
rmsn.yml Normal file
View file

@ -0,0 +1,3 @@
---
database_path: rmsn.sqlite
port: 8080

View file

@ -2,12 +2,18 @@ package server
import ( import (
"fmt" "fmt"
"net/http"
"git.ctrlz.es/mgdelacroix/rmsn/model" "git.ctrlz.es/mgdelacroix/rmsn/model"
"git.ctrlz.es/mgdelacroix/rmsn/services/store"
"git.ctrlz.es/mgdelacroix/rmsn/web"
) )
type Server struct { type Server struct {
Config *model.Config Config *model.Config
Store *store.Store
WebServer *http.Server
} }
func NewServer(config *model.Config) (*Server, error) { 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) 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 { func (s *Server) Start() error {
// ToDo: start webserver here
return nil return nil
} }
func (s *Server) Close() error { func (s *Server) Close() error {
// ToDo: stop webserver
// ToDo: close store
return nil return nil
} }

View file

@ -25,6 +25,7 @@ func addPathOptions(path string) string {
func NewStore(path string) (*Store, error) { func NewStore(path string) (*Store, error) {
s := &Store{Path: path} s := &Store{Path: path}
// ToDo: should do this at start time instead of create time?
conn, err := sql.Open("sqlite3", addPathOptions(path)) conn, err := sql.Open("sqlite3", addPathOptions(path))
if err != nil { if err != nil {
return nil, err return nil, err

22
web/web.go Normal file
View file

@ -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
}