Started BE structure / docker for FE dev

This commit is contained in:
Leons Aleksandrovs
2025-07-03 17:55:15 +03:00
parent df38c61069
commit 6130fc8abf
15 changed files with 278 additions and 19 deletions
+20
View File
@@ -0,0 +1,20 @@
package config
import "os"
func defaultValue(val string, def string) string {
if val == "" {
return def
}
return val
}
func LoadEnv() map[string]string {
// Create object where to store used variables
env := make(map[string]string, 1)
// Get env variables that will be used while server is running
env["port"] = defaultValue(os.Getenv("PORT"), "8080")
return env
}
+2
View File
@@ -1,3 +1,5 @@
module backend
go 1.24.4
require github.com/go-chi/chi/v5 v5.2.2
+2
View File
@@ -0,0 +1,2 @@
github.com/go-chi/chi/v5 v5.2.2 h1:CMwsvRVTbXVytCk1Wd72Zy1LAsAh9GxMmSNWLHCG618=
github.com/go-chi/chi/v5 v5.2.2/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
+14 -5
View File
@@ -1,11 +1,20 @@
package main
import "time"
import (
"backend/config"
"backend/routes"
"log"
"net/http"
)
func main() {
println("Hello, world!")
// Load env variables
env := config.LoadEnv()
for {
time.Sleep(1 * time.Second)
}
// Setup routes
routes := routes.SetupRoutes()
// Listen on port smth
log.Printf("Starting server on %s PORT\n", env["port"])
log.Fatal(http.ListenAndServe(":"+env["port"], routes))
}
+19
View File
@@ -0,0 +1,19 @@
package routes
import (
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
func SetupRoutes() http.Handler {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!"))
})
return r
}