55 lines
1.1 KiB
Go

// Simple server to serve static template files for webapp
// telegram miniapp let the webapp work with bot comunicating info
package main
import (
"context"
"fmt"
"log/slog"
"net/http"
"os"
"git.maximotejeda.com/maximo/telegram-base-bot/config"
)
var (
log *slog.Logger
lv slog.Level
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
e := config.GetEnvironment()
switch e {
case "dev", "development":
lv = slog.LevelDebug
case "prod", "production":
lv = slog.LevelInfo
default:
panic(fmt.Errorf("env variable not recognized"))
}
log = slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
Level: lv,
}))
mux := http.NewServeMux()
mux.Handle("GET /public/", http.StripPrefix("/public/", http.HandlerFunc(serveStaticFiles)))
mux.HandleFunc("GET /{$}", serveStaticFiles)
if err := http.ListenAndServe(":8081", mux); err != nil {
panic(err)
}
fmt.Println("webapp", ctx)
}
// serveStaticFiles
func serveStaticFiles(w http.ResponseWriter, r *http.Request) {
fs := http.Dir("webapp")
h := http.FileServer(fs)
h.ServeHTTP(w, r)
log.Debug("serving staitc file", "route", r.URL.Path)
}