diff options
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | database.go (renamed from utils/database.go) | 2 | ||||
-rw-r--r-- | errors.go (renamed from utils/errors.go) | 2 | ||||
-rw-r--r-- | handler.go (renamed from web/handler.go) | 54 | ||||
-rw-r--r-- | main.go | 9 | ||||
-rw-r--r-- | router.go (renamed from web/router.go) | 6 | ||||
-rw-r--r-- | runtime_config.go (renamed from utils/runtime_config.go) | 2 | ||||
-rw-r--r-- | storage.go (renamed from utils/storage.go) | 2 | ||||
-rw-r--r-- | templates.go (renamed from utils/templates.go) | 2 |
9 files changed, 38 insertions, 45 deletions
@@ -6,7 +6,7 @@ PREFIX="/usr" all: mkdir -p build - go build -o build/ceres main.go + go build -o build/ceres *.go clean: rm -rf build @@ -26,4 +26,4 @@ install: all install -Dm 644 sql/0002_migration.sql $(DESTDIR)$(PREFIX)/share/ceres/migrations/0002_migration.sql debug: - go run main.go -c config/debug.json + go run *.go -c config/debug.json diff --git a/utils/database.go b/database.go index f48af35..20ec5a6 100644 --- a/utils/database.go +++ b/database.go @@ -1,5 +1,5 @@ -package utils +package main import ( "log" diff --git a/utils/errors.go b/errors.go index 757d2a1..193cc11 100644 --- a/utils/errors.go +++ b/errors.go @@ -1,5 +1,5 @@ -package utils +package main import ( "fmt" diff --git a/web/handler.go b/handler.go index 07d04fa..f7e14d3 100644 --- a/web/handler.go +++ b/handler.go @@ -1,5 +1,5 @@ -package web +package main import ( "bytes" @@ -10,8 +10,6 @@ import ( "net/http" "path/filepath" - "xengineering.eu/ceres/utils" - "github.com/yuin/goldmark" ) @@ -28,7 +26,7 @@ func static(filename string, staticRoot string) func(http.ResponseWriter, *http. } } -func index(db *utils.Database, templateRoot string) func(http.ResponseWriter, *http.Request) { +func index(db *Database, templateRoot string) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { @@ -37,7 +35,7 @@ func index(db *utils.Database, templateRoot string) func(http.ResponseWriter, *h log.Printf("Query: %s", cmd) rows, err := db.Backend.Query(cmd) if err != nil { - utils.Err(w, 1) + Err(w, 1) return } defer rows.Close() @@ -54,7 +52,7 @@ func index(db *utils.Database, templateRoot string) func(http.ResponseWriter, *h var element Element err := rows.Scan(&element.Id, &element.Title) if err != nil { - utils.Err(w, 2) + Err(w, 2) return } else { elements = append(elements, element) @@ -63,18 +61,18 @@ func index(db *utils.Database, templateRoot string) func(http.ResponseWriter, *h // render and return template path := filepath.Join(templateRoot, "index.html") - utils.ServeTemplate(w, "index", path, elements) + ServeTemplate(w, "index", path, elements) } } -func recipe(db *utils.Database, templateRoot string) func(http.ResponseWriter, *http.Request) { +func recipe(db *Database, templateRoot string) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { // get id from URL parameters ids := r.URL.Query()["id"] if len(ids) != 1 { - utils.Err(w, 3, len(ids)) + Err(w, 3, len(ids)) return } idStr := ids[0] @@ -82,7 +80,7 @@ func recipe(db *utils.Database, templateRoot string) func(http.ResponseWriter, * // validate id idRegex := regexp.MustCompile(VALID_ID_REGEX) if !(idRegex.MatchString(idStr)) { - utils.Err(w, 4, idStr, VALID_ID_REGEX) + Err(w, 4, idStr, VALID_ID_REGEX) return } @@ -93,7 +91,7 @@ func recipe(db *utils.Database, templateRoot string) func(http.ResponseWriter, * log.Printf("Query: %s", cmd) rows, err := db.Backend.Query(cmd) if err != nil { - utils.Err(w, 5, err) + Err(w, 5, err) return } defer rows.Close() @@ -114,7 +112,7 @@ func recipe(db *utils.Database, templateRoot string) func(http.ResponseWriter, * element.Id = idStr err := rows.Scan(&element.Title, &element.UpstreamUrl, &element.DescriptionMarkdown) if err != nil { - utils.Err(w, 2) + Err(w, 2) return } else { elements = append(elements, element) @@ -123,7 +121,7 @@ func recipe(db *utils.Database, templateRoot string) func(http.ResponseWriter, * // check result if len(elements) != 1 { - utils.Err(w, 6, len(elements)) + Err(w, 6, len(elements)) return } @@ -134,7 +132,7 @@ func recipe(db *utils.Database, templateRoot string) func(http.ResponseWriter, * // render and return template path := filepath.Join(templateRoot, "recipe.html") - utils.ServeTemplate(w, "recipe", path, elements[0]) + ServeTemplate(w, "recipe", path, elements[0]) } if r.Method == "POST" { @@ -148,14 +146,14 @@ func recipe(db *utils.Database, templateRoot string) func(http.ResponseWriter, * } } -func recipe_edit(db *utils.Database, templateRoot string) func(http.ResponseWriter, *http.Request) { +func recipe_edit(db *Database, templateRoot string) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { // get id from URL parameters ids := r.URL.Query()["id"] if len(ids) != 1 { - utils.Err(w, 3, len(ids)) + Err(w, 3, len(ids)) return } idStr := ids[0] @@ -163,7 +161,7 @@ func recipe_edit(db *utils.Database, templateRoot string) func(http.ResponseWrit // validate id idRegex := regexp.MustCompile(VALID_ID_REGEX) if !(idRegex.MatchString(idStr)) { - utils.Err(w, 4, idStr, VALID_ID_REGEX) + Err(w, 4, idStr, VALID_ID_REGEX) return } @@ -174,7 +172,7 @@ func recipe_edit(db *utils.Database, templateRoot string) func(http.ResponseWrit log.Printf("Query: %s", cmd) rows, err := db.Backend.Query(cmd) if err != nil { - utils.Err(w, 5, err) + Err(w, 5, err) return } defer rows.Close() @@ -195,7 +193,7 @@ func recipe_edit(db *utils.Database, templateRoot string) func(http.ResponseWrit element.Id = idStr err := rows.Scan(&element.Title, &element.UpstreamUrl, &element.DescriptionMarkdown) if err != nil { - utils.Err(w, 2) + Err(w, 2) return } else { elements = append(elements, element) @@ -204,7 +202,7 @@ func recipe_edit(db *utils.Database, templateRoot string) func(http.ResponseWrit // check result if len(elements) != 1 { - utils.Err(w, 6, len(elements)) + Err(w, 6, len(elements)) return } @@ -215,7 +213,7 @@ func recipe_edit(db *utils.Database, templateRoot string) func(http.ResponseWrit // render and return template path := filepath.Join(templateRoot, "recipe_edit.html") - utils.ServeTemplate(w, "recipe", path, elements[0]) + ServeTemplate(w, "recipe", path, elements[0]) } if r.Method == "POST" { @@ -229,7 +227,7 @@ func recipe_edit(db *utils.Database, templateRoot string) func(http.ResponseWrit } } -func updateRecipe(db *utils.Database, body string, idStr string) { +func updateRecipe(db *Database, body string, idStr string) { // execute SQL UPDATE _,_ = db.Backend.Exec(` @@ -253,7 +251,7 @@ func image(storageRoot string) func(http.ResponseWriter, *http.Request) { // get ID ids := r.URL.Query()["id"] if len(ids) != 1 { - utils.Err(w, 3, len(ids)) + Err(w, 3, len(ids)) return } idStr := ids[0] @@ -261,17 +259,17 @@ func image(storageRoot string) func(http.ResponseWriter, *http.Request) { // validate ID idRegex := regexp.MustCompile(VALID_ID_REGEX) if !idRegex.MatchString(idStr) { - utils.Err(w, 4, idStr, VALID_ID_REGEX) + Err(w, 4, idStr, VALID_ID_REGEX) return } // serve image path := fmt.Sprintf("recipes/image/%s.jpg", idStr) - utils.ServeStorage(w, r, storageRoot, path) + ServeStorage(w, r, storageRoot, path) } } -func add_recipes(db *utils.Database, storageRoot string, staticRoot string) func(http.ResponseWriter, *http.Request) { +func add_recipes(db *Database, storageRoot string, staticRoot string) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { @@ -291,12 +289,12 @@ func add_recipes(db *utils.Database, storageRoot string, staticRoot string) func log.Println(cmd) res,err := db.Backend.Exec(cmd) if err != nil { - utils.Err(w, 9, err) + Err(w, 9, err) return } id,err := res.LastInsertId() if err != nil { - utils.Err(w, 11, err) + Err(w, 11, err) return } else { log.Println("Added custom recipe.") @@ -3,22 +3,19 @@ package main import ( "log" - - "xengineering.eu/ceres/utils" - "xengineering.eu/ceres/web" ) func main() { // read all sources of runtime configuration (e.g. CLI flags and config file) - config := utils.GetRuntimeConfig() + config := GetRuntimeConfig() // print start message log.Printf("Starting ceres with config file '%s'\n", config.Path) // initialize database - db := utils.InitDatabase(config.Database) + db := InitDatabase(config.Database) // start web server - web.RunServer(config.Http, &db) + RunServer(config.Http, &db) } diff --git a/web/router.go b/router.go index ec67399..6985780 100644 --- a/web/router.go +++ b/router.go @@ -1,14 +1,12 @@ -package web +package main import ( "log" "net/http" - - "xengineering.eu/ceres/utils" ) -func RunServer(config utils.HttpConfig, db *utils.Database) { +func RunServer(config HttpConfig, db *Database) { http.HandleFunc("/", index(db, config.Templates)) diff --git a/utils/runtime_config.go b/runtime_config.go index fb5bd72..9302e11 100644 --- a/utils/runtime_config.go +++ b/runtime_config.go @@ -1,5 +1,5 @@ -package utils +package main import ( "fmt" diff --git a/utils/storage.go b/storage.go index ee5b7bf..02bc94d 100644 --- a/utils/storage.go +++ b/storage.go @@ -1,5 +1,5 @@ -package utils +package main import ( "log" diff --git a/utils/templates.go b/templates.go index 00a8eb2..84e449a 100644 --- a/utils/templates.go +++ b/templates.go @@ -1,5 +1,5 @@ -package utils +package main import ( "log" |