basic api
This commit is contained in:
30
src/api.go
Normal file
30
src/api.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package main
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"encoding/json"
|
||||||
|
)
|
||||||
|
type accountApiResponse struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
DiscordUserId string `json:"discordUserId"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
}
|
||||||
|
const apiPassword string = "hezgf42gfgwfg"
|
||||||
|
func accountApi(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var accountKey string = r.FormValue("accountkey")
|
||||||
|
var password string = r.FormValue("password")
|
||||||
|
if password != secret.ApiToken {
|
||||||
|
http.Error(w, "Error 401 false password", 401)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var account accountApiResponse
|
||||||
|
var success bool
|
||||||
|
var usernameInter interface{}
|
||||||
|
usernameInter, success = sessions.GetStringKey(accountKey)
|
||||||
|
account.Username = usernameInter.(string)
|
||||||
|
if !success {
|
||||||
|
http.Error(w, "Error 400 invalid session", 400)
|
||||||
|
}
|
||||||
|
db.QueryRow("SELECT email,discordUserId FROM account WHERE username = ?", account.Username).Scan(&account.Email, &account.DiscordUserId)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(account)
|
||||||
|
}
|
||||||
@ -32,6 +32,7 @@ func login(w http.ResponseWriter, r *http.Request) {
|
|||||||
cookie := http.Cookie{
|
cookie := http.Cookie{
|
||||||
Name: sessionName,
|
Name: sessionName,
|
||||||
Value: key,
|
Value: key,
|
||||||
|
Domain: "redstoneunion.de",
|
||||||
Expires: time.Now().Add(sessionTimeout),
|
Expires: time.Now().Add(sessionTimeout),
|
||||||
HttpOnly: true,
|
HttpOnly: true,
|
||||||
Secure: true,
|
Secure: true,
|
||||||
|
|||||||
@ -30,6 +30,7 @@ type secrets_json struct {
|
|||||||
DiscordServerID string `json:"discordServerID"`
|
DiscordServerID string `json:"discordServerID"`
|
||||||
MoodleToken string `json:"moodleToken"`
|
MoodleToken string `json:"moodleToken"`
|
||||||
GiteaToken string `json:"giteaToken"`
|
GiteaToken string `json:"giteaToken"`
|
||||||
|
ApiToken string `json:"apiToken"`
|
||||||
}
|
}
|
||||||
type config_json struct {
|
type config_json struct {
|
||||||
CreateGiteaAccount bool `json:"createGiteaAccount"`
|
CreateGiteaAccount bool `json:"createGiteaAccount"`
|
||||||
@ -81,6 +82,7 @@ func main() {
|
|||||||
http.HandleFunc("/register", register)
|
http.HandleFunc("/register", register)
|
||||||
http.HandleFunc("/submit", submit)
|
http.HandleFunc("/submit", submit)
|
||||||
http.HandleFunc("/login", login)
|
http.HandleFunc("/login", login)
|
||||||
|
http.HandleFunc("/api/accountinfo", accountApi)
|
||||||
|
|
||||||
http.ListenAndServe(":" + fmt.Sprint(config.Port), nil)
|
http.ListenAndServe(":" + fmt.Sprint(config.Port), nil)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user