Started BE structure / docker for FE dev
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
module backend
|
||||
|
||||
go 1.24.4
|
||||
|
||||
require github.com/go-chi/chi/v5 v5.2.2
|
||||
|
||||
@@ -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
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user