Feat(Initial): Initial Go codebase
All checks were successful
Webhook-Everything/Webhook-Everything/pipeline/head This commit looks good

This commit is contained in:
2022-05-29 00:06:52 +08:00
parent f31bc0cd52
commit 53829a2788
27 changed files with 1489 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
package main
import (
"net/http"
"github.com/go-chi/render"
)
type HealthStatus struct {
Status string `json:"status"`
}
func (*HealthStatus) Render(w http.ResponseWriter, r *http.Request) error {
// Pre-processing before a response is marshalled and sent across the wire
return nil
}
// Health Check``
// @Summary Responds to health check
// @Description Description
// @Tags Base
// @Accept json
// @Produce json
// @Success 200 {object} string
// @Failure 404 {object} string
// @Router / [get]
func healthHandler(w http.ResponseWriter, r *http.Request) {
var okRender HealthStatus
okRender.Status = "ok"
render.Render(w, r, &okRender)
}

View File

@@ -0,0 +1,70 @@
package main
import (
"log"
"net/http"
"os"
_ "git.samuelpua.com/telboon/webhook-everything/backend/docs"
"git.samuelpua.com/telboon/webhook-everything/backend/internal/common"
"git.samuelpua.com/telboon/webhook-everything/backend/internal/telegrampackage"
"git.samuelpua.com/telboon/webhook-everything/backend/internal/webhookeverything"
"github.com/go-chi/chi"
"github.com/joho/godotenv"
httpSwagger "github.com/swaggo/http-swagger"
)
// Root Handler - Test
// @Summary This is test
// @Description Description
// @Tags Base
// @Accept json
// @Produce json
// @Success 200 {object} string
// @Failure 404 {object} string
// @Router / [get]
func rootHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world"))
}
// @title Eyes of Twitterverse API
// @version 1.0
// @description API for frontend - built on Go-chi
// @BasePath /
// @contact.name Samuel Pua
// @contact.url https://git.samuelpua.com/telboon
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
environment := os.Getenv("ENVIRONMENT")
db := common.InitDB()
// CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
db.Exec("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";")
db.AutoMigrate(&webhookeverything.WebhookRoute{})
db.AutoMigrate(&telegrampackage.ChatIDMap{})
// Setup telegram
telegramAPIKey := os.Getenv("TELEGRAM_API_KEY")
telegramEnv := telegrampackage.NewEnv(db, telegramAPIKey, nil)
// Get Host URL
hostURL := os.Getenv("HOST_URL")
r := chi.NewRouter()
if environment == "dev" {
r.Mount("/swagger", httpSwagger.WrapHandler)
}
r.Get("/", rootHandler)
r.Get("/health", healthHandler)
r.Mount("/webhook", webhookeverything.WebhookEverythingRoutes(db, telegramEnv, hostURL))
server_str := ":8000"
log.Printf("Starting server at %s\n", server_str)
http.ListenAndServe(server_str, r)
}