Chromagies/src/api/api.go

176 lines
5.3 KiB
Go

package api
import (
"chromagies/src/database"
"chromagies/src/profiler"
"fmt"
"net/http"
"runtime"
"time"
"github.com/dustin/go-humanize"
"github.com/julienschmidt/httprouter"
)
const (
profiler_API_PROCESS_REQUEST string = "API Process Request"
)
var apiRootPath string = "/api/v1"
func Init(router *httprouter.Router) {
profiler.Register(profiler_API_PROCESS_REQUEST, 10000, "µs")
router.GET("/", apiHandler(apiRoot))
router.GET(apiRootPath+"/auth", apiHandler(apiAuth))
router.GET(apiRootPath+"/auth/login", apiHandler(apiAuthLogin))
router.GET(apiRootPath+"/auth/logout", apiHandler(apiAuthLogout))
router.GET(apiRootPath+"/page", apiHandler(apiPage))
router.GET(apiRootPath+"/page/:folder", apiHandler(apiPageFolder))
router.GET(apiRootPath+"/page/:folder/:page", apiHandler(apiPageFolderPage))
router.GET(apiRootPath+"/page/:folder/:page/content", apiHandler(apiPageFolderPageContent))
router.GET(apiRootPath+"/user", apiHandler(apiUser))
router.GET(apiRootPath+"/user/:name", apiHandler(apiUserName))
router.GET(apiRootPath+"/tag", apiHandler(apiTag))
router.GET(apiRootPath+"/tag/:name", apiHandler(apiTagName))
router.GET(apiRootPath+"/debug/profiler", apiHandler(apiDebugProfiler))
router.GET(apiRootPath+"/debug/profiler/:name", apiHandler(apiDebugProfilerName))
router.GET(apiRootPath+"/debug/database/reset", apiHandler(apiDebugDatabaseReset))
router.GET(apiRootPath+"/debug/memory/get", apiHandler(apiDebugMemoryGet))
router.GET(apiRootPath+"/debug/memory/gc", apiHandler(apiDebugMemoryGC))
}
func apiHandler(h httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
tStart := time.Now()
h(w, r, ps)
profiler.Add(profiler_API_PROCESS_REQUEST, time.Duration(time.Since(tStart).Microseconds()))
}
}
// API ROOT
func apiRoot(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
database.ExecuteStoredRoutine("test")
fmt.Fprintf(w, "API ROOT")
}
// API Auth
func apiAuth(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprintf(w, "API Auth")
}
func apiAuthLogin(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprintf(w, "API Auth Login")
}
func apiAuthLogout(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprintf(w, "API Auth Logout")
}
// API Page
func apiPage(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprintf(w, "API Page")
}
func apiPageFolder(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
fmt.Fprintf(w, "API Page Folder(%s)", params.ByName("page"))
}
func apiPageFolderPage(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
fmt.Fprintf(w, "API Page Folder(%s) Page(%s)", params.ByName("folder"), params.ByName("page"))
}
func apiPageFolderPageContent(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
fmt.Fprintf(w, "API Page Folder(%s) Page(%s) Content", params.ByName("folder"), params.ByName("page"))
}
// API User
func apiUser(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprintf(w, "API User")
}
func apiUserName(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
fmt.Fprintf(w, "API User Name(%s)", params.ByName("page"))
}
// API Tag
func apiTag(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprintf(w, "API Tag")
}
func apiTagName(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
fmt.Fprintf(w, "API Tag Name(%s)", params.ByName("page"))
}
// API Debug Profiler
func apiDebugProfiler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var entries []string
profiler.GetAll(&entries)
for i := range entries {
var entry string = entries[i]
fmt.Fprintf(w, "API Debug Profiler(%s)\n\n%s\n\n", entry, string(profiler.Get(entry).ToString()))
}
}
func apiDebugProfilerName(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
fmt.Fprintf(w, "API Debug Profiler(%s)\n%s", params.ByName("page"), string(profiler.Get(params.ByName("page")).ToString()))
}
// API Debug Database
func apiDebugDatabaseReset(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
setHeaderStream(w)
fmt.Fprintf(w, "Destroying Database...\n")
w.(http.Flusher).Flush()
database.DestroyDatabase()
fmt.Fprintf(w, "Init Database...\n")
w.(http.Flusher).Flush()
database.UpdateDatabaseStructure()
fmt.Fprintf(w, "Done")
}
// API Debug Memory Get
func apiDebugMemoryGet(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Fprintf(w, "Alloc = %s", humanize.IBytes(m.Alloc))
fmt.Fprintf(w, "\tTotalAlloc = %s", humanize.IBytes(m.TotalAlloc))
fmt.Fprintf(w, "\tSys = %s", humanize.IBytes(m.Sys))
fmt.Fprintf(w, "\tNumGC = %v\n", m.NumGC)
}
// API Debug Memory GarbageCollector
func apiDebugMemoryGC(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
runtime.GC()
w.Header().Set("Location", apiRootPath+"/debug/memory/get")
w.WriteHeader(http.StatusSeeOther)
}
// Utils
func setHeaderStream(w http.ResponseWriter) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Expose-Headers", "Content-Type")
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
}