Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Acme Web
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Geant DevOps
Acme Web
Commits
f3f86213
Unverified
Commit
f3f86213
authored
5 months ago
by
Max Adamo
Browse files
Options
Downloads
Patches
Plain Diff
feat: implement custom HTTP handler with 404 fallback and enhance routing logic
parent
53355cff
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
certinspector/inspector.go
+0
-1
0 additions, 1 deletion
certinspector/inspector.go
main.go
+68
-29
68 additions, 29 deletions
main.go
with
68 additions
and
30 deletions
certinspector/inspector.go
+
0
−
1
View file @
f3f86213
...
@@ -172,7 +172,6 @@ func ProcessCertificates(baseDir, provider string, verboseBool bool) ([]byte, er
...
@@ -172,7 +172,6 @@ func ProcessCertificates(baseDir, provider string, verboseBool bool) ([]byte, er
return
results
[
i
]
.
CertName
<
results
[
j
]
.
CertName
return
results
[
i
]
.
CertName
<
results
[
j
]
.
CertName
})
})
// Encode results directly into JSON
jsonData
,
err
:=
json
.
MarshalIndent
(
results
,
""
,
" "
)
jsonData
,
err
:=
json
.
MarshalIndent
(
results
,
""
,
" "
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to encode JSON: %w"
,
err
)
return
nil
,
fmt
.
Errorf
(
"failed to encode JSON: %w"
,
err
)
...
...
This diff is collapsed.
Click to expand it.
main.go
+
68
−
29
View file @
f3f86213
...
@@ -80,7 +80,6 @@ func renderPage(w http.ResponseWriter, req *http.Request) {
...
@@ -80,7 +80,6 @@ func renderPage(w http.ResponseWriter, req *http.Request) {
// trigger puppet
// trigger puppet
func
triggerPuppet
(
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
func
triggerPuppet
(
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
fmt
.
Printf
(
"triggerPuppet function
\n
"
)
// content-type currently not working
// content-type currently not working
cmd
:=
exec
.
Command
(
"/usr/bin/pkill"
,
"-f"
,
"/opt/puppetlabs/puppet/bin/puppet"
,
"--signal"
,
"SIGUSR1"
)
cmd
:=
exec
.
Command
(
"/usr/bin/pkill"
,
"-f"
,
"/opt/puppetlabs/puppet/bin/puppet"
,
"--signal"
,
"SIGUSR1"
)
authToken
:=
"BOFH"
authToken
:=
"BOFH"
...
@@ -111,11 +110,72 @@ func triggerPuppet(w http.ResponseWriter, req *http.Request) {
...
@@ -111,11 +110,72 @@ func triggerPuppet(w http.ResponseWriter, req *http.Request) {
// redirect to /by_name.html
// redirect to /by_name.html
func
redirect
(
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
func
redirect
(
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
fmt
.
Printf
(
"redirect function
\n
"
)
redirectURL
:=
filepath
.
Join
(
req
.
URL
.
Path
,
"/by_name.html"
)
redirectURL
:=
filepath
.
Join
(
req
.
URL
.
Path
,
"/by_name.html"
)
if
verboseBool
{
DebugLogger
.
Printf
(
"running redirect to: %v"
,
redirectURL
)
}
http
.
Redirect
(
w
,
req
,
redirectURL
,
http
.
StatusMovedPermanently
)
http
.
Redirect
(
w
,
req
,
redirectURL
,
http
.
StatusMovedPermanently
)
}
}
// Custom HTTP handler with 404 fallback
func
customHandler
(
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
path
:=
req
.
URL
.
Path
if
path
==
"/"
||
path
==
"/index.html"
||
path
==
"/index.htm"
{
//if verboseBool {
// DebugLogger.Printf("Serving file: %s", path)
//}
http
.
ServeFile
(
w
,
req
,
filepath
.
Join
(
webDir
,
"index.html"
))
return
}
if
path
==
"/favicon.ico"
{
http
.
ServeFile
(
w
,
req
,
filepath
.
Join
(
webDir
,
"static"
,
"anvil.ico"
))
}
if
path
==
"/ping"
{
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
w
.
WriteHeader
(
http
.
StatusOK
)
_
,
err
:=
w
.
Write
([]
byte
(
`{"status": "ok"}`
))
if
err
!=
nil
{
WarningLogger
.
Printf
(
"Failed to write ping response: %v"
,
err
)
}
return
}
// Check if the path matches any valid routes
for
_
,
apiElement
:=
range
apiURLs
{
if
path
==
apiElement
{
renderJSON
(
w
,
req
)
return
}
}
for
_
,
element
:=
range
baseURLs
{
if
path
==
element
{
redirect
(
w
,
req
)
return
}
}
for
_
,
otherElement
:=
range
otherURLs
{
if
path
==
otherElement
{
renderPage
(
w
,
req
)
return
}
}
if
path
==
"/puppet"
{
triggerPuppet
(
w
,
req
)
return
}
if
strings
.
HasPrefix
(
path
,
"/static/"
)
{
fs
:=
http
.
FileServer
(
http
.
Dir
(
filepath
.
Join
(
webDir
,
"static"
)))
http
.
StripPrefix
(
"/static/"
,
fs
)
.
ServeHTTP
(
w
,
req
)
return
}
// If no route matches, return 404
http
.
NotFound
(
w
,
req
)
}
func
main
()
{
func
main
()
{
progName
:=
filepath
.
Base
(
os
.
Args
[
0
])
progName
:=
filepath
.
Base
(
os
.
Args
[
0
])
...
@@ -173,13 +233,13 @@ Options:
...
@@ -173,13 +233,13 @@ Options:
baseURLs
=
append
(
baseURLs
,
"/"
+
provider
,
"/"
+
provider
+
"/"
)
baseURLs
=
append
(
baseURLs
,
"/"
+
provider
,
"/"
+
provider
+
"/"
)
}
}
for
_
,
provider
:=
range
acmeProviders
{
for
_
,
provider
:=
range
acmeProviders
{
otherURLs
=
append
(
otherURLs
,
"/"
+
provider
+
"/by_name.html"
,
"/"
+
provider
+
"/by_date.html"
)
otherURLs
=
append
(
otherURLs
,
"/"
+
provider
+
"/by_name.html"
,
"/"
+
provider
+
"/by_date.html"
,
"/"
+
provider
+
"/"
+
provider
+
".json"
,
"/"
+
provider
+
"/"
+
provider
+
"_expired.json"
,
)
}
}
for
_
,
provider
:=
range
acmeProviders
{
for
_
,
provider
:=
range
acmeProviders
{
apiURLs
=
append
(
apiURLs
=
append
(
apiURLs
,
"/api/"
+
provider
,
"/api/"
+
provider
+
"/"
)
apiURLs
,
"/api/"
+
provider
,
"/api/"
+
provider
+
"/"
,
"/api/"
+
provider
+
"/"
+
provider
+
".json"
,
"/api/"
+
provider
+
"/"
+
provider
+
"_expired.json"
,
)
}
}
if
verboseBool
{
if
verboseBool
{
DebugLogger
.
Printf
(
"Serving baseURLs: %v"
,
baseURLs
)
DebugLogger
.
Printf
(
"Serving baseURLs: %v"
,
baseURLs
)
...
@@ -187,28 +247,7 @@ Options:
...
@@ -187,28 +247,7 @@ Options:
DebugLogger
.
Printf
(
"Serving otherURLs: %v"
,
otherURLs
)
DebugLogger
.
Printf
(
"Serving otherURLs: %v"
,
otherURLs
)
}
}
puppetURL
:=
"/puppet"
http
.
HandleFunc
(
"/"
,
customHandler
)
fs
:=
http
.
FileServer
(
http
.
Dir
(
filepath
.
Join
(
webDir
,
"static"
)))
http
.
Handle
(
"/static/"
,
http
.
StripPrefix
(
"/static/"
,
fs
))
http
.
HandleFunc
(
puppetURL
,
triggerPuppet
)
http
.
HandleFunc
(
"/"
,
func
(
res
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
http
.
ServeFile
(
res
,
req
,
filepath
.
Join
(
webDir
,
"index.html"
))
})
for
_
,
apiElement
:=
range
apiURLs
{
http
.
HandleFunc
(
apiElement
,
renderJSON
)
}
for
_
,
element
:=
range
baseURLs
{
http
.
HandleFunc
(
element
,
redirect
)
}
for
_
,
otherElement
:=
range
otherURLs
{
http
.
HandleFunc
(
otherElement
,
renderPage
)
}
if
listenAddress
==
"any"
{
if
listenAddress
==
"any"
{
log
.
Fatal
(
http
.
ListenAndServe
(
fmt
.
Sprintf
(
":%v"
,
listenPort
),
nil
))
log
.
Fatal
(
http
.
ListenAndServe
(
fmt
.
Sprintf
(
":%v"
,
listenPort
),
nil
))
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment