diff options
author | Emile <git@emile.space> | 2025-02-12 22:05:22 +0100 |
---|---|---|
committer | Emile <git@emile.space> | 2025-02-12 22:05:22 +0100 |
commit | 7152205c80f059d3649e1830fb4dfc46d1fc158f (patch) | |
tree | b0d9a9aea760fabd61ee5cb06814d45cac5bb629 /nix/templates/goapp/backend/main.go | |
parent | 4d08790c43b2d0720ef43b657a651a7c541d30d2 (diff) |
template: the goapp docker package should now (in theory) build
It's quite a weird way to pull out the packge name from the attribute set of defined packages, yet it kind of works. I can't test it, as docker doesn't want to run Mach-O binaries, but kicking this into hydra should result in some nice builds.
Diffstat (limited to 'nix/templates/goapp/backend/main.go')
-rw-r--r-- | nix/templates/goapp/backend/main.go | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/nix/templates/goapp/backend/main.go b/nix/templates/goapp/backend/main.go new file mode 100644 index 0000000..d793869 --- /dev/null +++ b/nix/templates/goapp/backend/main.go @@ -0,0 +1,80 @@ +package main + +import ( + "flag" + "fmt" + "log" + "net/http" + "os" + "time" + + "github.com/gorilla/mux" +) + +var ( + host string + port int + logFilePath string + databasePath string + sessiondbPath string + globalState *State +) + +func initFlags() { + flag.StringVar(&host, "host", "127.0.0.1", "The host to listen on") + flag.StringVar(&host, "h", "127.0.0.1", "The host to listen on (shorthand)") + + flag.IntVar(&port, "port", 8080, "The port to listen on") + flag.IntVar(&port, "p", 8080, "The port to listen on (shorthand)") + + flag.StringVar(&logFilePath, "logfilepath", "./server.log", "The path to the log file") + flag.StringVar(&databasePath, "databasepath", "./main.db", "The path to the main database") + flag.StringVar(&sessiondbPath, "sessiondbpath", "./sessions.db", "The path to the session database") +} + +func indexHandler(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello World from the backend") +} + +func main() { + initFlags() + flag.Parse() + + // log init + log.Println("[i] Setting up logging...") + logFile, err := os.OpenFile(logFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0664) + if err != nil { + log.Fatal("Error opening the server.log file: ", err) + } + logger := loggingMiddleware{logFile} + + // db init + log.Println("[i] Setting up Global State Struct...") + s, err := NewState() + if err != nil { + log.Fatal("Error creating the NewState(): ", err) + } + globalState = s + + // session init + log.Println("[i] Setting up Session Storage...") + store, err := NewSqliteStore(sessiondbPath, "sessions", "/", 3600, []byte(os.Getenv("SESSION_KEY"))) + if err != nil { + panic(err) + } + globalState.sessions = store + + r := mux.NewRouter() + r.Use(logger.Middleware) + r.HandleFunc("/", indexHandler) + + srv := &http.Server{ + Handler: r, + Addr: ":8080", + WriteTimeout: 15 * time.Second, + ReadTimeout: 15 * time.Second, + } + + log.Printf("[i] Running the server on %s", srv.Addr) + log.Fatal(srv.ListenAndServe()) +} |