Added a couple of Makefiles to simply build and deploy
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
|
||||
.PHONY: all
|
||||
all: setup build
|
||||
|
||||
.PHONY: setup
|
||||
setup:
|
||||
mkdir -p bin/
|
||||
|
||||
.PHONY: build
|
||||
build:
|
||||
go build -o bin/posti-dm .
|
||||
|
||||
.PHONY: run
|
||||
run:
|
||||
go run .
|
||||
|
||||
.PHONY: watch
|
||||
watch:
|
||||
find -name '*.go' | entr -r -s 'go run .'
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
var Mode string
|
||||
|
||||
var Host string
|
||||
var Url string
|
||||
|
||||
var Email string
|
||||
|
||||
func loadEnv(target *string, name, defaultValue string) {
|
||||
value := os.Getenv(name)
|
||||
if len(strings.TrimSpace(value)) == 0 {
|
||||
*target = defaultValue
|
||||
} else {
|
||||
*target = value
|
||||
}
|
||||
log.Printf("%s = %v", name, *target)
|
||||
}
|
||||
|
||||
func Load() {
|
||||
godotenv.Load()
|
||||
|
||||
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
||||
|
||||
loadEnv(&Mode, "MODE", "production")
|
||||
loadEnv(&Host, "HOST", "localhost:4000")
|
||||
loadEnv(&Url, "URL", "http://localhost:3000")
|
||||
loadEnv(&Email, "EMAIL", "mail@example.org")
|
||||
}
|
||||
@@ -5,4 +5,5 @@ go 1.17
|
||||
require (
|
||||
github.com/alecthomas/repr v0.0.0-20220113201626-b1b626ac65ae
|
||||
github.com/go-chi/chi/v5 v5.0.7
|
||||
github.com/joho/godotenv v1.4.0
|
||||
)
|
||||
|
||||
@@ -2,3 +2,5 @@ github.com/alecthomas/repr v0.0.0-20220113201626-b1b626ac65ae h1:zzGwJfFlFGD94Cy
|
||||
github.com/alecthomas/repr v0.0.0-20220113201626-b1b626ac65ae/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
|
||||
github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8=
|
||||
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
||||
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
|
||||
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
|
||||
+24
-18
@@ -4,13 +4,14 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.phc.dm.unipi.it/aziis98/posti-dm/server/config"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
)
|
||||
|
||||
var HOST = ":4000"
|
||||
|
||||
func main() {
|
||||
config.Load()
|
||||
|
||||
r := chi.NewRouter()
|
||||
|
||||
r.Use(middleware.RequestID)
|
||||
@@ -18,25 +19,30 @@ func main() {
|
||||
r.Use(middleware.Logger)
|
||||
r.Use(middleware.Recoverer)
|
||||
|
||||
if config.Mode == "production" {
|
||||
log.Printf(`Adding production routes...`)
|
||||
|
||||
// Assets
|
||||
r.Handle("/assets/*", http.StripPrefix("/assets/", http.FileServer(http.Dir("./assets"))))
|
||||
|
||||
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, "./index.html")
|
||||
})
|
||||
r.Get("/login", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, "./login.html")
|
||||
})
|
||||
|
||||
// FIXME: Route temporanea, basta configurare ViteJS per bene
|
||||
r.Get("/login.html", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
|
||||
})
|
||||
}
|
||||
|
||||
server := NewServer()
|
||||
|
||||
// api := chi.NewRouter()
|
||||
// api.Get("/example", func(w http.ResponseWriter, r *http.Request) {
|
||||
// w.Header().Add("Content-Type", "application/json")
|
||||
// http.SetCookie(w, &http.Cookie{
|
||||
// Name: "test",
|
||||
// Value: "Prova",
|
||||
// Expires: time.Now().Add(120 * time.Second),
|
||||
// })
|
||||
// json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
// "foo": "bar",
|
||||
// })
|
||||
// })
|
||||
|
||||
r.Mount("/api", server.ApiRoute)
|
||||
|
||||
log.Printf(`Starting server on %s...`, HOST)
|
||||
err := http.ListenAndServe(HOST, r)
|
||||
log.Printf(`Starting server on "%s"...`, config.Host)
|
||||
err := http.ListenAndServe(config.Host, r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user