Skip to content

Commit

Permalink
Redirect da http a https
Browse files Browse the repository at this point in the history
  • Loading branch information
FrostWalk committed Sep 14, 2024
1 parent 9f43c9f commit 02aceb9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 24 deletions.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ RUN CGO_ENABLED=0 go build -ldflags="-s -w" -trimpath -o inviter
FROM gcr.io/distroless/static

LABEL org.opencontainers.image.source=https://github.com/FrostWalk/GitHub-Inviter
LABEL org.opencontainers.image.description="linux/amd64"
LABEL org.opencontainers.image.licenses=MIT

COPY --from=buildenv /go/src/build/inviter /inviter
COPY --from=buildenv /go/src/build/static/ /static/
Expand All @@ -24,4 +26,5 @@ ENV GITHUB_GROUP_NAME=""
ENV INVITE_CODE=""
ENV TLS_CERT=""
ENV TLS_KEY=""
ENV PORT="8080"
ENV HTTP_PORT="80"
ENV HTTPS_PORT="443"
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ provided invitation code to get added to the organization's team.
- Simple web interface for user input
- Secure invitation process using a pre-defined invitation code
- Configurable for different GitHub organizations and teams
- Optional TLS support for secure connections
- Optional TLS support for secure connections, with automatic redirect

## Screenshot

![Screenshot](/assets/images/index.png)

## Configuration
Expand All @@ -24,7 +25,8 @@ The application is configured using environment variables. Here are the availabl
| `GITHUB_TOKEN` | GitHub personal access token with necessary permissions | Yes | - |
| `GITHUB_GROUP_NAME` | The name of the team in your organization | Yes | - |
| `INVITE_CODE` | The invitation code users need to provide | Yes | - |
| `PORT` | The port on which the application will run | No | 8080 |
| `HTTP_PORT` | The port on which the application will run | No | 80 |
| `HTTPS_PORT` | The port on which the application will run (https) | No | 443 |
| `TLS_CERT` | Path to the TLS certificate file | No | - |
| `TLS_KEY` | Path to the TLS key file | No | - |

Expand All @@ -42,17 +44,19 @@ services:
- GITHUB_TOKEN=your-github-token
- GITHUB_GROUP_NAME=your-team-name
- INVITE_CODE=your-invite-code
- PORT=8080
- HTTP_PORT=80
- HTTPS_PORT=443
# Uncomment the following lines if you want to use TLS
# - TLS_CERT=/path/to/your/cert.pem
# - TLS_KEY=/path/to/your/key.pem
ports:
- "8080:8080"
- "80:80"
# Uncomment the following lines if you want to use TLS
# - "443:443"
# volumes:
# - /path/to/your/cert.pem:/path/to/your/cert.pem:ro
# - /path/to/your/key.pem:/path/to/your/key.pem:ro
```
# - /path/to/your/cert.pem:/path/to/your/cert.pem:ro
# - /path/to/your/key.pem:/path/to/your/key.pem:ro
```

To run the application:

Expand Down
24 changes: 17 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ type AppConfig struct {
Token string //mandatory
GroupName string //mandatory
InviteCode []byte //mandatory
Port string //optional (default 8080)
HttpPort string //optional (default 80)
HttpsPort string //optional (default 443)
TlsCert string //optional
TlsKey string //optional
}
Expand Down Expand Up @@ -43,17 +44,22 @@ func Load() bool {
}

// Set the optional environment variables, using defaults if not set
port := strings.Trim(os.Getenv("PORT"), " ")
if len(port) == 0 {
port = "8080"
httpPort := strings.Trim(os.Getenv("HTTP_PORT"), " ")
if len(httpPort) == 0 {
httpPort = "80"
}
httpsPort := strings.Trim(os.Getenv("HTTPS_PORT"), " ")
if len(httpsPort) == 0 {
httpsPort = "443"
}

conf = AppConfig{
OrgName: orgName,
Token: token,
GroupName: strings.ToLower(groupName),
InviteCode: hash.CalculateHash(inviteCode),
Port: port,
HttpPort: httpPort,
HttpsPort: httpsPort,
TlsCert: strings.Trim(os.Getenv("TLS_CERT"), " "),
TlsKey: strings.Trim(os.Getenv("TLS_KEY"), " "),
}
Expand Down Expand Up @@ -88,8 +94,12 @@ func InviteCode() []byte {
return conf.InviteCode
}

func Port() string {
return conf.Port
func HttpPort() string {
return conf.HttpPort
}

func HttpsPort() string {
return conf.HttpsPort
}

func TlsCert() string {
Expand Down
10 changes: 6 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ services:
- GITHUB_TOKEN=your-github-token
- GITHUB_GROUP_NAME=your-team-name
- INVITE_CODE=your-invite-code
- PORT=8080
- HTTP_PORT=80
- HTTPS_PORT=443
# Uncomment the following lines if you want to use TLS
# - TLS_CERT=/path/to/your/cert.pem
# - TLS_KEY=/path/to/your/key.pem
ports:
- "8080:8080"
- "80:80"
# Uncomment the following lines if you want to use TLS
# - "443:443"
# volumes:
# - /path/to/your/cert.pem:/path/to/your/cert.pem:ro
# - /path/to/your/key.pem:/path/to/your/key.pem:ro
# - /path/to/your/cert.pem:/path/to/your/cert.pem:ro
# - /path/to/your/key.pem:/path/to/your/key.pem:ro
22 changes: 17 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"inviter/handlers"
"log"
"net/http"
"strings"
)

func main() {
Expand All @@ -29,11 +30,22 @@ func main() {
}

if tlsEnable {
fmt.Println("Server is running on https://127.0.0.1:" + config.Port())
log.Fatal(http.ListenAndServeTLS(fmt.Sprintf(":%s", config.Port()), config.TlsCert(), config.TlsKey(), nil))

go func() {
// Start HTTP server that redirects all traffic to HTTPS
log.Println("Starting HTTP to HTTPS redirect")
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", config.HttpPort()), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Redirect to HTTPS
index := strings.Index(r.Host, ":")
target := fmt.Sprintf("https://%s:%s%s", r.Host[:index], config.HttpsPort(), r.RequestURI)
http.Redirect(w, r, target, http.StatusMovedPermanently)
})))
}()

// Start HTTPS server
fmt.Println("Server is running on https://127.0.0.1:" + config.HttpsPort())
log.Fatal(http.ListenAndServeTLS(fmt.Sprintf(":%s", config.HttpsPort()), config.TlsCert(), config.TlsKey(), nil))
} else {
fmt.Println("Server is running on http://127.0.0.1:" + config.Port())
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", config.Port()), nil))
fmt.Println("Server is running on http://127.0.0.1:" + config.HttpPort())
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", config.HttpPort()), nil))
}
}

0 comments on commit 02aceb9

Please sign in to comment.