|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + "github.com/admin-golang/admin" |
| 10 | +) |
| 11 | + |
| 12 | +func main() { |
| 13 | + pages := admin.Pages{ |
| 14 | + admin.Page{ |
| 15 | + ID: "Dashboard", |
| 16 | + URL: "/dashboard", |
| 17 | + Type: admin.DashboardPage, |
| 18 | + }, |
| 19 | + admin.Page{ |
| 20 | + ID: "SignIn", |
| 21 | + URL: "/sign-in", |
| 22 | + Type: admin.SideFormPage, |
| 23 | + Form: admin.Form{ |
| 24 | + ID: "signIn", |
| 25 | + Fields: admin.Fields{ |
| 26 | + admin.Field{ |
| 27 | + ID: "email", |
| 28 | + Type: admin.InputText, |
| 29 | + Label: "Email", |
| 30 | + IsRequired: true, |
| 31 | + Value: "", |
| 32 | + }, |
| 33 | + admin.Field{ |
| 34 | + ID: "password", |
| 35 | + Type: admin.InputPassword, |
| 36 | + Label: "Password", |
| 37 | + IsRequired: true, |
| 38 | + Value: "", |
| 39 | + }, |
| 40 | + }, |
| 41 | + Submit: admin.Submit{ |
| 42 | + Label: "Sign In", |
| 43 | + URL: "/sign-in", |
| 44 | + Method: "POST", |
| 45 | + RedirectURL: "/dashboard", |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + admin := admin.New(&admin.Config{ |
| 52 | + UITheme: admin.MaterialUI, // admin.AntDesignUI, |
| 53 | + Endpoint: "localhost:8080/graphql", |
| 54 | + Pages: pages, |
| 55 | + }) |
| 56 | + |
| 57 | + http.Handle("/admin", admin) |
| 58 | + http.HandleFunc("/sign-in", signIn) |
| 59 | + |
| 60 | + log.Println("[admin-golang] running on port :8080, path: /admin") |
| 61 | + http.ListenAndServe(":8080", nil) |
| 62 | +} |
| 63 | + |
| 64 | +type signInForm struct { |
| 65 | + Email string `json:"email"` |
| 66 | + Password string `json:"password"` |
| 67 | +} |
| 68 | + |
| 69 | +func signIn(w http.ResponseWriter, r *http.Request) { |
| 70 | + log.Printf("[signIn]: %s %s", r.Method, r.URL.String()) |
| 71 | + |
| 72 | + rb, err := io.ReadAll(r.Body) |
| 73 | + if err != nil { |
| 74 | + log.Printf("failed to read request body: %v", err) |
| 75 | + http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) |
| 76 | + return |
| 77 | + } |
| 78 | + form := &signInForm{} |
| 79 | + |
| 80 | + if err := json.Unmarshal(rb, form); err != nil { |
| 81 | + log.Printf("failed to unmarshal request body: %v", err) |
| 82 | + http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + log.Printf("form: %+v", form) |
| 87 | + |
| 88 | + if form.Password == "testerror" { |
| 89 | + http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + resp := map[string]string{ |
| 94 | + "data": "ok", |
| 95 | + } |
| 96 | + |
| 97 | + b, err := json.Marshal(resp) |
| 98 | + if err != nil { |
| 99 | + log.Printf("failed to marshal json: %v", err) |
| 100 | + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + w.Write(b) |
| 105 | +} |
0 commit comments