Initial import: KeepNote service (dcntrlized-auth consumer)
This commit is contained in:
+73
@@ -0,0 +1,73 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.dcntrlized.org/Dcntrlized/KeepNote/db"
|
||||
"git.dcntrlized.org/Dcntrlized/KeepNote/handlers/actions"
|
||||
"git.dcntrlized.org/Dcntrlized/KeepNote/handlers/auth"
|
||||
"git.dcntrlized.org/Dcntrlized/KeepNote/handlers/manifest"
|
||||
"git.dcntrlized.org/Dcntrlized/KeepNote/logger"
|
||||
"git.dcntrlized.org/Dcntrlized/KeepNote/middleware"
|
||||
"git.dcntrlized.org/dcntrlized/dcntrlized-auth/authclient"
|
||||
"git.dcntrlized.org/dcntrlized/dcntrlized-auth/authproxy"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
db := db.NewDB()
|
||||
defer db.Close()
|
||||
|
||||
if err := authclient.Init(context.Background(), authproxy.Target()); err != nil {
|
||||
logger.Error("failed to initialize auth client: %v", err)
|
||||
}
|
||||
|
||||
logger.Info("Server started")
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("/_manifest", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
manifest.ManifestHandler(w, r)
|
||||
})
|
||||
|
||||
mux.HandleFunc("/auth/login", auth.LoginHandler())
|
||||
mux.HandleFunc("/auth/register", auth.RegisterHandler())
|
||||
mux.HandleFunc("/auth/logout", auth.LogoutHandler())
|
||||
mux.HandleFunc("/auth/refresh", auth.RefreshHandler())
|
||||
mux.HandleFunc("/auth/delete", auth.DeleteHandler())
|
||||
|
||||
mux.HandleFunc("/action/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if !strings.HasPrefix(r.URL.Path, "/action/") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
rest := strings.TrimPrefix(r.URL.Path, "/action/")
|
||||
params := map[string]string{"actionName": rest}
|
||||
switch r.Method {
|
||||
case http.MethodDelete:
|
||||
actions.ActionActionNameDelete(w, r, params)
|
||||
return
|
||||
case http.MethodGet:
|
||||
actions.ActionActionNameGet(w, r, params)
|
||||
return
|
||||
case http.MethodPost:
|
||||
actions.ActionActionNamePost(w, r, params)
|
||||
return
|
||||
case http.MethodPut:
|
||||
actions.ActionActionNamePut(w, r, params)
|
||||
return
|
||||
default:
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
handler := middleware.LoggingMiddleware(middleware.CorsMiddleware(mux))
|
||||
|
||||
http.ListenAndServe(":8080", handler)
|
||||
}
|
||||
Reference in New Issue
Block a user