Compare commits
10 Commits
v1.4.0
...
b475da66da
Author | SHA1 | Date | |
---|---|---|---|
b475da66da | |||
d0085ab2c3 | |||
58514f4c5f | |||
2a32a1b3ce | |||
6da7d408f9 | |||
e993bcf317 | |||
9b231a73d6 | |||
34acd0fa8d | |||
71d3bd77d0 | |||
1451abcca4 |
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@ -0,0 +1 @@
|
||||
* text=auto eol=lf
|
@ -15,6 +15,7 @@ fine with getting your hands dirty, but I plan on having it ready to go for more
|
||||
- CSRF protection
|
||||
- Minimal user login/registration + sessions
|
||||
- Config file handling
|
||||
- Scheduled tasks
|
||||
- Entire website compiles into a single binary (~10mb) (excluding env.json)
|
||||
- Minimal dependencies (just standard library, postgres driver, and experimental package for bcrypt)
|
||||
|
||||
|
4
go.mod
4
go.mod
@ -3,6 +3,6 @@ module GoWeb
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/lib/pq v1.10.7
|
||||
golang.org/x/crypto v0.7.0
|
||||
github.com/lib/pq v1.10.9
|
||||
golang.org/x/crypto v0.11.0
|
||||
)
|
||||
|
8
go.sum
8
go.sum
@ -1,4 +1,4 @@
|
||||
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
|
||||
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
|
||||
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
|
||||
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
|
||||
|
1
main.go
1
main.go
@ -78,6 +78,7 @@ func main() {
|
||||
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
|
||||
stop := make(chan struct{})
|
||||
go app.RunScheduledTasks(&appLoaded, 100, stop)
|
||||
|
||||
<-interrupt
|
||||
log.Println("Interrupt signal received. Shutting down server...")
|
||||
|
||||
|
@ -64,6 +64,18 @@ func CreateSession(app *app.App, w http.ResponseWriter, userId int64, remember b
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func GetSessionByAuthToken(app *app.App, authToken string) (Session, error) {
|
||||
session := Session{}
|
||||
|
||||
err := app.Db.QueryRow(selectSessionByAuthToken, authToken).Scan(&session.Id, &session.UserId, &session.AuthToken, &session.RememberMe, &session.CreatedAt)
|
||||
if err != nil {
|
||||
log.Println("Error getting session by auth token")
|
||||
return Session{}, err
|
||||
}
|
||||
|
||||
return session, nil
|
||||
}
|
||||
|
||||
// Generates a random 64-byte string
|
||||
func generateAuthToken(app *app.App) string {
|
||||
// Generate random bytes
|
||||
|
@ -23,7 +23,6 @@ const userColumns = "\"Id\", " + userColumnsNoId
|
||||
const userTable = "public.\"User\""
|
||||
|
||||
const (
|
||||
selectSessionIdByAuthToken = "SELECT \"Id\" FROM public.\"Session\" WHERE \"AuthToken\" = $1"
|
||||
selectUserById = "SELECT " + userColumns + " FROM " + userTable + " WHERE \"Id\" = $1"
|
||||
selectUserByUsername = "SELECT " + userColumns + " FROM " + userTable + " WHERE \"Username\" = $1"
|
||||
insertUser = "INSERT INTO " + userTable + " (" + userColumnsNoId + ") VALUES ($1, $2, $3, $4) RETURNING \"Id\""
|
||||
@ -37,16 +36,13 @@ func GetCurrentUser(app *app.App, r *http.Request) (User, error) {
|
||||
return User{}, err
|
||||
}
|
||||
|
||||
var userId int64
|
||||
|
||||
// Query row by AuthToken
|
||||
err = app.Db.QueryRow(selectSessionIdByAuthToken, cookie.Value).Scan(&userId)
|
||||
session, err := GetSessionByAuthToken(app, cookie.Value)
|
||||
if err != nil {
|
||||
log.Println("Error querying session row with session: " + cookie.Value)
|
||||
log.Println("Error getting session by auth token")
|
||||
return User{}, err
|
||||
}
|
||||
|
||||
return GetUserById(app, userId)
|
||||
return GetUserById(app, session.UserId)
|
||||
}
|
||||
|
||||
// GetUserById finds a User table row in the database by id and returns a struct representing this row
|
||||
|
57
restclient/client.go
Normal file
57
restclient/client.go
Normal file
@ -0,0 +1,57 @@
|
||||
package restclient
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// SendRequest sends an HTTP request to a URL and includes the specified headers and body.
|
||||
// A body can be nil for GET requests, a map[string]string for multipart/form-data requests,
|
||||
// or a struct for JSON requests
|
||||
func SendRequest(url string, method string, headers map[string]string, body interface{}) (http.Response, error) {
|
||||
var reqBody *bytes.Buffer
|
||||
var contentType string
|
||||
|
||||
switch v := body.(type) {
|
||||
case nil:
|
||||
reqBody = bytes.NewBuffer([]byte(""))
|
||||
case map[string]string:
|
||||
reqBody = &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(reqBody)
|
||||
for key, value := range v {
|
||||
writer.WriteField(key, value)
|
||||
}
|
||||
writer.Close()
|
||||
contentType = writer.FormDataContentType()
|
||||
default:
|
||||
jsonBody, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return http.Response{}, err
|
||||
}
|
||||
reqBody = bytes.NewBuffer(jsonBody)
|
||||
contentType = "application/json"
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, url, reqBody)
|
||||
if err != nil {
|
||||
return http.Response{}, err
|
||||
}
|
||||
|
||||
if contentType != "" {
|
||||
req.Header.Set("Content-Type", contentType)
|
||||
}
|
||||
|
||||
for key, value := range headers {
|
||||
req.Header.Add(key, value)
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return http.Response{}, err
|
||||
}
|
||||
|
||||
return *resp, nil
|
||||
}
|
Reference in New Issue
Block a user