Skip to content

Commit

Permalink
complete api
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Jun 29, 2024
1 parent f2dbcbf commit b7b6cc6
Show file tree
Hide file tree
Showing 13 changed files with 1,417 additions and 1,113 deletions.
35 changes: 35 additions & 0 deletions api/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* OpenBmclAPI (Golang Edition)
* Copyright (C) 2024 Kevin Z <zyxkad@gmail.com>
* All rights reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package api

import (
"encoding/json"
)

type ConfigHandler interface {
json.Marshaler
json.Unmarshaler
UnmarshalYAML(data []byte) error
MarshalJSONPath(path string) ([]byte, error)
UnmarshalJSONPath(path string, data []byte) error

Fingerprint() string
DoLockedAction(fingerprint string, callback func(ConfigHandler) error) error
}
45 changes: 45 additions & 0 deletions api/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* OpenBmclAPI (Golang Edition)
* Copyright (C) 2024 Kevin Z <zyxkad@gmail.com>
* All rights reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package api

import (
"net/http"
)

const (
RealAddrCtxKey = "handle.real.addr"
RealPathCtxKey = "handle.real.path"
AccessLogExtraCtxKey = "handle.access.extra"
)

func GetRequestRealAddr(req *http.Request) string {
addr, _ := req.Context().Value(RealAddrCtxKey).(string)
return addr
}

func GetRequestRealPath(req *http.Request) string {
return req.Context().Value(RealPathCtxKey).(string)
}

func SetAccessInfo(req *http.Request, key string, value any) {
if info, ok := req.Context().Value(AccessLogExtraCtxKey).(map[string]any); ok {
info[key] = value
}
}
89 changes: 89 additions & 0 deletions api/stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* OpenBmclAPI (Golang Edition)
* Copyright (C) 2024 Kevin Z <zyxkad@gmail.com>
* All rights reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package api

import (
"time"
)

type StatsManager interface {
GetStatus() StatusData
// if name is empty then gets the overall access data
GetAccessStat(name string) *AccessStatData
}

type StatusData struct {
StartAt time.Time `json:"startAt"`
Clusters []string `json:"clusters"`
Storages []string `json:"storages"`
}

type statInstData struct {
Hits int32 `json:"hits"`
Bytes int64 `json:"bytes"`
}

func (d *statInstData) update(o *statInstData) {
d.Hits += o.Hits
d.Bytes += o.Bytes
}

// statTime always save a UTC time
type statTime struct {
Hour int `json:"hour"`
Day int `json:"day"`
Month int `json:"month"`
Year int `json:"year"`
}

func makeStatTime(t time.Time) (st statTime) {
t = t.UTC()
st.Hour = t.Hour()
y, m, d := t.Date()
st.Day = d - 1
st.Month = (int)(m) - 1
st.Year = y
return
}

func (t statTime) IsLastDay() bool {
return time.Date(t.Year, (time.Month)(t.Month+1), t.Day+1+1, 0, 0, 0, 0, time.UTC).Day() == 1
}

type (
statDataHours = [24]statInstData
statDataDays = [31]statInstData
statDataMonths = [12]statInstData
)

type accessStatHistoryData struct {
Hours statDataHours `json:"hours"`
Days statDataDays `json:"days"`
Months statDataMonths `json:"months"`
}

type AccessStatData struct {
Date statTime `json:"date"`
accessStatHistoryData
Prev accessStatHistoryData `json:"prev"`
Years map[string]statInstData `json:"years"`

Accesses map[string]int `json:"accesses"`
}
6 changes: 6 additions & 0 deletions api/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package api
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"time"

Expand All @@ -30,6 +32,10 @@ import (
"github.com/LiterMC/go-openbmclapi/utils"
)

var (
ErrNotFound = errors.New("Item not found")
)

type SubscriptionManager interface {
GetWebPushKey() string

Expand Down
3 changes: 3 additions & 0 deletions api/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ import (
)

type TokenVerifier interface {
VerifyChallengeToken(clientId string, token string, action string) (err error)
VerifyAuthToken(clientId string, token string) (tokenId string, userId string, err error)
VerifyAPIToken(clientId string, token string, path string, query url.Values) (userId string, err error)
}

type TokenManager interface {
TokenVerifier
GenerateChallengeToken(clientId string, action string) (token string, err error)
GenerateAuthToken(clientId string, userId string) (token string, err error)
GenerateAPIToken(clientId string, userId string, path string, query map[string]string) (token string, err error)
InvalidToken(tokenId string) error
}
Loading

0 comments on commit b7b6cc6

Please sign in to comment.