Skip to content

Commit

Permalink
refactor to use env vars everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
linomp committed Aug 10, 2024
1 parent 561f398 commit 55bb923
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
15 changes: 12 additions & 3 deletions src/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package main

import (
"fmt"
"log"
"net/http"
"os"

"github.com/joho/godotenv"
)

func main() {
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error occurred loading env variables: %s", err)
}

mux := http.NewServeMux()

mux.HandleFunc("GET /oauthdemo", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -27,7 +36,7 @@ func main() {
w.Write([]byte(html))
})

log.Println("Running on http://localhost:8001")
log.Println("Try oauth demo on http://localhost:8001/oauthdemo")
log.Fatal(http.ListenAndServe("0.0.0.0:8001", mux))
log.Printf("Running on http://%s:%s\n", os.Getenv("HOST"), os.Getenv("PORT"))
log.Printf("Try oauth demo on http://%s:%s/oauthdemo\n", os.Getenv("HOST"), os.Getenv("PORT"))
log.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%s", os.Getenv("PORT")), mux))
}
17 changes: 9 additions & 8 deletions src/oauthdemo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
var googleOauthConfig *oauth2.Config

type User struct {
ID string `json:"id"`
Email string `json:"email"`
VerifiedEmail bool `json:"verified_email"`
Picture string `json:"picture"`
Id string `json:"id"`
Email string `json:"email"`
IsEmailVerified bool `json:"verified_email"`
Picture string `json:"picture"`
}

func init() {
Expand All @@ -35,7 +35,7 @@ func init() {

// Initialize the OAuth2 config once during package initialization
googleOauthConfig = &oauth2.Config{
RedirectURL: "http://localhost:8001/google-oauth-callback",
RedirectURL: fmt.Sprintf("http://%s:%s/google-oauth-callback", os.Getenv("HOST"), os.Getenv("PORT")),
ClientID: os.Getenv("GOOGLE_CLIENT_ID"),
ClientSecret: os.Getenv("GOOGLE_CLIENT_SECRET"),
Scopes: []string{
Expand Down Expand Up @@ -135,12 +135,13 @@ func getAsHtml(userData []byte) (string, error) {
<div class="user-info">
<h1>Oauth2 Demo</h1>
<h2>Google sign-in succesful!</h2>
<div class="item">
<label>ID:</label>
<label>Email:</label>
<span>%s</span>
</div>
<div class="item">
<label>Email:</label>
<label>Google user ID:</label>
<span>%s</span>
</div>
<div class="item">
Expand All @@ -154,7 +155,7 @@ func getAsHtml(userData []byte) (string, error) {
</body>
</html>
`, user.ID, user.Email, user.VerifiedEmail, user.Picture)
`, user.Email, user.Id, user.IsEmailVerified, user.Picture)

return html, nil
}
13 changes: 13 additions & 0 deletions src/server_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ func generateServerStatusHTML(metrics ServerMetrics) string {
.secondary {
color: #9c83ff;
}
.secondary:hover {
text-decoration: underline;
}
</style>
<title>😈️ pointless-status 😈</title>
</head>
Expand All @@ -139,6 +142,16 @@ func generateServerStatusHTML(metrics ServerMetrics) string {
<div class=instance-card-inf__txt>Memory usage</div>
<div class=instance-card-inf__title>%s</div>
</div>
<div class=instance-card-inf__item>
<div class="instance-card-inf__title secondary">
<a href="https://github.com/linomp/pointless-backend" target="_blank">
<svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="40" height="40" viewBox="0,0,256,256">
<g fill="#9c83ff" fill-rule="nonzero" stroke="none" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" stroke-dasharray="" stroke-dashoffset="0" font-family="none" font-weight="none" font-size="none" text-anchor="none" style="mix-blend-mode: normal"><g transform="scale(8.53333,8.53333)"><path d="M15,3c-6.627,0 -12,5.373 -12,12c0,5.623 3.872,10.328 9.092,11.63c-0.056,-0.162 -0.092,-0.35 -0.092,-0.583v-2.051c-0.487,0 -1.303,0 -1.508,0c-0.821,0 -1.551,-0.353 -1.905,-1.009c-0.393,-0.729 -0.461,-1.844 -1.435,-2.526c-0.289,-0.227 -0.069,-0.486 0.264,-0.451c0.615,0.174 1.125,0.596 1.605,1.222c0.478,0.627 0.703,0.769 1.596,0.769c0.433,0 1.081,-0.025 1.691,-0.121c0.328,-0.833 0.895,-1.6 1.588,-1.962c-3.996,-0.411 -5.903,-2.399 -5.903,-5.098c0,-1.162 0.495,-2.286 1.336,-3.233c-0.276,-0.94 -0.623,-2.857 0.106,-3.587c1.798,0 2.885,1.166 3.146,1.481c0.896,-0.307 1.88,-0.481 2.914,-0.481c1.036,0 2.024,0.174 2.922,0.483c0.258,-0.313 1.346,-1.483 3.148,-1.483c0.732,0.731 0.381,2.656 0.102,3.594c0.836,0.945 1.328,2.066 1.328,3.226c0,2.697 -1.904,4.684 -5.894,5.097c1.098,0.573 1.899,2.183 1.899,3.396v2.734c0,0.104 -0.023,0.179 -0.035,0.268c4.676,-1.639 8.035,-6.079 8.035,-11.315c0,-6.627 -5.373,-12 -12,-12z"></path></g></g>
</svg>
</a>
</div>
<div class="instance-card-inf__txt secondary"><a href="/oauthdemo" target="_blank">/oauthdemo</a></div>
</div>
</div>
</div>
</div>
Expand Down

0 comments on commit 55bb923

Please sign in to comment.