83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"html/template"
|
|
"io/fs"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"git.maximotejeda.com/maximo/us-dop-api/internal/adapters/dolar"
|
|
"git.maximotejeda.com/maximo/us-dop-api/config"
|
|
"git.maximotejeda.com/maximo/us-dop-api/internal/handlers"
|
|
"git.maximotejeda.com/maximo/us-dop-api/internal/middlewares"
|
|
"git.maximotejeda.com/maximo/us-dop-api/static"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
)
|
|
|
|
var (
|
|
hostSTR = os.Getenv("HOST)")
|
|
portSTR = os.Getenv("PORT")
|
|
templ template.Template
|
|
)
|
|
|
|
func main() {
|
|
|
|
// Log creation
|
|
log := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{}))
|
|
|
|
var opts []grpc.DialOption
|
|
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
dolarConn, err := grpc.NewClient(config.GetDollarServiceURL(), opts...)
|
|
if err != nil {
|
|
log.Error("creating gerpc conn", "error", err)
|
|
panic(err)
|
|
}
|
|
dol, err := dolar.NewAdapter(dolarConn)
|
|
if err != nil {
|
|
log.Error("creating service adapter", "error", err)
|
|
panic(err)
|
|
}
|
|
|
|
// templates
|
|
tmplDirStr := "assets/templates/"
|
|
ftmpl := os.DirFS(tmplDirStr)
|
|
toParseFiles := []string{}
|
|
|
|
// wlaks the Fs looking for templates
|
|
err = fs.WalkDir(ftmpl, ".", func(p string, d fs.DirEntry, err error) error {
|
|
log.Info("parsing", "file", p)
|
|
if filepath.Ext(p) == ".tmpl" {
|
|
toParseFiles = append(toParseFiles, tmplDirStr+p)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// init database
|
|
|
|
// handler containing all the necessary files
|
|
root := handlers.NewRoot( dol, log, toParseFiles)
|
|
|
|
s := middlewares.NewLogger(http.HandlerFunc(static.ServeFiles), log)
|
|
r := middlewares.NewLogger(root, log)
|
|
|
|
mux := http.NewServeMux()
|
|
server := http.Server{
|
|
Addr: hostSTR + ":" + portSTR,
|
|
Handler: mux,
|
|
}
|
|
mux.Handle("/", r)
|
|
|
|
mux.Handle("GET /static/", s)
|
|
if err := server.ListenAndServe(); err != nil {
|
|
log.Error("while serving", "error", err)
|
|
}
|
|
|
|
}
|